Class: User

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/user.rb

Constant Summary collapse

@@per_page =
5

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#passwordObject

Virtual attribute for the unencrypted password


6
7
8
# File 'app/models/user.rb', line 6

def password
  @password
end

Class Method Details

.authenticate(login, pass) ⇒ Object


121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/models/user.rb', line 121

def self.authenticate(, pass)
  return nil if .blank?
  candidate = where("login = ?", ).first
  return nil if candidate.nil?

  if Tracks::Config.auth_schemes.include?('database')
    return candidate if candidate.auth_type == 'database' and
      candidate.password_matches? pass
  end

  return nil
end

.find_adminObject


138
139
140
# File 'app/models/user.rb', line 138

def self.find_admin
  where(:is_admin => true).first
end

.no_users_yet?Boolean

Returns:

  • (Boolean)

134
135
136
# File 'app/models/user.rb', line 134

def self.no_users_yet?
  count == 0
end

Instance Method Details

#change_password(pass, pass_confirm) ⇒ Object


157
158
159
160
161
# File 'app/models/user.rb', line 157

def change_password(pass,pass_confirm)
  self.password = pass
  self.password_confirmation = pass_confirm
  save!
end

#create_hash(s) ⇒ Object


192
193
194
# File 'app/models/user.rb', line 192

def create_hash(s)
  BCrypt::Password.create(s)
end

#crypt_passwordObject (protected)


198
199
200
201
# File 'app/models/user.rb', line 198

def crypt_password
  return if password.blank?
  write_attribute("crypted_password", self.create_hash(password)) if password == password_confirmation
end

#dateObject


163
164
165
# File 'app/models/user.rb', line 163

def date
  Date.current
end

#delete_taggingsObject (protected)


213
214
215
216
217
# File 'app/models/user.rb', line 213

def delete_taggings
  ids = todos.pluck(:id)
  taggings = Tagging.where(taggable_id: ids).pluck(:id)
  Tagging.where(id: taggings).delete_all
end

#destroy_dependenciesObject (protected)


207
208
209
210
211
# File 'app/models/user.rb', line 207

def destroy_dependencies
  ids = todos.pluck(:id)
  pred_deps = Dependency.where(predecessor_id: ids).destroy_all
  succ_deps = Dependency.where(predecessor_id: ids).destroy_all
end

#display_nameObject


146
147
148
149
150
151
152
153
154
155
# File 'app/models/user.rb', line 146

def display_name
  if first_name.blank? && last_name.blank?
    return 
  elsif first_name.blank?
    return last_name
  elsif last_name.blank?
    return first_name
  end
  "#{first_name} #{last_name}"
end

#forget_meObject


182
183
184
185
186
# File 'app/models/user.rb', line 182

def forget_me
  self.remember_token_expires_at = nil
  self.remember_token            = nil
  save
end

#generate_tokenObject


167
168
169
# File 'app/models/user.rb', line 167

def generate_token
  self.token = Digest::SHA1.hexdigest "#{Time.now.to_i}#{rand}"
end

#password_matches?(pass) ⇒ Boolean

Returns:

  • (Boolean)

188
189
190
# File 'app/models/user.rb', line 188

def password_matches?(pass)
  BCrypt::Password.new(crypted_password) == pass
end

#password_required?Boolean (protected)

Returns:

  • (Boolean)

203
204
205
# File 'app/models/user.rb', line 203

def password_required?
  auth_type == 'database' && crypted_password.blank? || password.present?
end

#remember_meObject

These create and unset the fields required for remembering users between browser closes


176
177
178
179
180
# File 'app/models/user.rb', line 176

def remember_me
  self.remember_token_expires_at = 2.weeks.from_now.utc
  self.remember_token ||= Digest::SHA1.hexdigest("#{}--#{remember_token_expires_at}")
  save
end

#remember_token?Boolean

Returns:

  • (Boolean)

171
172
173
# File 'app/models/user.rb', line 171

def remember_token?
  remember_token_expires_at && Time.now.utc < remember_token_expires_at
end

#to_paramObject


142
143
144
# File 'app/models/user.rb', line 142

def to_param
  
end

#validate_auth_typeObject


113
114
115
116
117
# File 'app/models/user.rb', line 113

def validate_auth_type
  unless Tracks::Config.auth_schemes.include?(auth_type)
    errors.add("auth_type", "not a valid authentication type (#{auth_type})")
  end
end