Commit faa0ec7c634bdf394b67366e8f90fb4c5a40542d

Authored by Nihad Abbasov
1 parent fc74627f

cleanup User model

Showing 2 changed files with 7 additions and 15 deletions   Show diff stats
app/models/user.rb
1 1 class User < ActiveRecord::Base
2   - # Include default devise modules. Others available are:
3   - # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
4 2 devise :database_authenticatable, :token_authenticatable,
5 3 :recoverable, :rememberable, :trackable, :validatable, :omniauthable
6 4  
7   - # Setup accessible (or protected) attributes for your model
8 5 attr_accessible :email, :password, :password_confirmation, :remember_me, :bio,
9 6 :name, :projects_limit, :skype, :linkedin, :twitter, :dark_scheme, :theme_id
10 7  
... ... @@ -46,7 +43,7 @@ class User &lt; ActiveRecord::Base
46 43  
47 44 validates :bio, :length => { :within => 0..255 }
48 45  
49   - before_create :ensure_authentication_token
  46 + before_save :ensure_authentication_token
50 47 alias_attribute :private_token, :authentication_token
51 48  
52 49 scope :not_in_project, lambda { |project| where("id not in (:ids)", :ids => project.users.map(&:id) ) }
... ... @@ -76,7 +73,6 @@ class User &lt; ActiveRecord::Base
76 73 admin
77 74 end
78 75  
79   -
80 76 def require_ssh_key?
81 77 keys.count == 0
82 78 end
... ... @@ -89,12 +85,8 @@ class User &lt; ActiveRecord::Base
89 85 projects.first
90 86 end
91 87  
92   - def self.generate_random_password
93   - (0...8).map{ ('a'..'z').to_a[rand(26)] }.join
94   - end
95   -
96 88 def first_name
97   - name.split(" ").first unless name.blank?
  89 + name.split.first unless name.blank?
98 90 end
99 91  
100 92 def self.find_for_ldap_auth(omniauth_info)
... ... @@ -104,8 +96,9 @@ class User &lt; ActiveRecord::Base
104 96 if @user = User.find_by_email(email)
105 97 @user
106 98 else
107   - password = generate_random_password
108   - @user = User.create(:name => name,
  99 + password = Devise.friendly_token[0, 8].downcase
  100 + @user = User.create(
  101 + :name => name,
109 102 :email => email,
110 103 :password => password,
111 104 :password_confirmation => password
... ... @@ -133,7 +126,7 @@ class User &lt; ActiveRecord::Base
133 126 end
134 127  
135 128 def projects_limit_percent
136   - return 100 if projects_limit.zero?
  129 + return 100 if projects_limit.zero?
137 130 (my_own_projects.count.to_f / projects_limit) * 100
138 131 end
139 132 end
... ... @@ -163,4 +156,3 @@ end
163 156 # authentication_token :string(255)
164 157 # dark_scheme :boolean default(FALSE), not null
165 158 #
166   -
... ...
spec/models/user_spec.rb
... ... @@ -14,6 +14,7 @@ describe User do
14 14 it { should respond_to(:is_admin?) }
15 15 it { should respond_to(:identifier) }
16 16 it { should respond_to(:name) }
  17 + it { should respond_to(:private_token) }
17 18 end
18 19  
19 20 it "should return valid identifier" do
... ... @@ -67,4 +68,3 @@ end
67 68 # authentication_token :string(255)
68 69 # dark_scheme :boolean default(FALSE), not null
69 70 #
70   -
... ...