mirror of
https://github.com/Growstuff/growstuff.git
synced 2026-03-27 19:23:59 -04:00
53 lines
1.6 KiB
Ruby
53 lines
1.6 KiB
Ruby
class User < ActiveRecord::Base
|
|
extend FriendlyId
|
|
friendly_id :username, use: :slugged
|
|
|
|
has_many :updates
|
|
has_many :gardens
|
|
|
|
# Include default devise modules. Others available are:
|
|
# :token_authenticatable, :confirmable,
|
|
# :lockable, :timeoutable and :omniauthable
|
|
devise :database_authenticatable, :registerable,
|
|
:recoverable, :rememberable, :trackable, :validatable,
|
|
:confirmable, :lockable, :timeoutable
|
|
|
|
# Setup accessible (or protected) attributes for your model
|
|
attr_accessible :username, :email, :password, :password_confirmation,
|
|
:remember_me, :login, :tos_agreement
|
|
# attr_accessible :title, :body
|
|
|
|
# Virtual attribute for authenticating by either username or email
|
|
# This is in addition to a real persisted field like 'username'
|
|
attr_accessor :login
|
|
|
|
# Requires acceptance of the Terms of Service
|
|
validates_acceptance_of :tos_agreement, :allow_nil => false,
|
|
:accept => true
|
|
|
|
# Give each new user a default garden
|
|
after_create {|user| Garden.new(:name => "Garden", :user_id => user.id).save }
|
|
|
|
# allow login via either username or email address
|
|
def self.find_first_by_auth_conditions(warden_conditions)
|
|
conditions = warden_conditions.dup
|
|
if login = conditions.delete(:login)
|
|
where(conditions).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first
|
|
else
|
|
where(conditions).first
|
|
end
|
|
end
|
|
|
|
def self.find_confirmed(params)
|
|
find(params, :conditions => 'confirmed_at IS NOT NULL')
|
|
end
|
|
|
|
def self.confirmed
|
|
where('confirmed_at IS NOT NULL')
|
|
end
|
|
|
|
def to_s
|
|
return username
|
|
end
|
|
end
|