2010-10-18 14:58:45 -07:00
|
|
|
class User < ActiveRecord::Base
|
|
|
|
DefaultAuthServerId = 1
|
2010-12-11 06:37:39 -08:00
|
|
|
PreviewTopContributorsCount = 3
|
2011-07-12 16:37:16 -07:00
|
|
|
|
|
|
|
has_many :closet_hangers
|
2010-11-06 08:52:58 -07:00
|
|
|
has_many :contributions
|
2010-11-11 10:43:22 -08:00
|
|
|
has_many :outfits
|
2011-07-12 16:37:16 -07:00
|
|
|
|
2010-11-06 09:15:10 -07:00
|
|
|
scope :top_contributors, order('points DESC').where(arel_table[:points].gt(0))
|
2011-07-12 16:37:16 -07:00
|
|
|
|
2011-01-26 10:40:15 -08:00
|
|
|
devise :rememberable
|
2011-07-12 16:37:16 -07:00
|
|
|
|
2010-11-06 16:07:15 -07:00
|
|
|
def contribute!(pet)
|
2010-11-06 15:08:42 -07:00
|
|
|
new_contributions = []
|
2010-11-06 16:07:15 -07:00
|
|
|
new_points = 0
|
|
|
|
pet.contributables.each do |contributable|
|
2010-11-06 15:08:42 -07:00
|
|
|
if contributable.new_record?
|
|
|
|
contribution = Contribution.new(:contributed => contributable,
|
|
|
|
:user => self)
|
|
|
|
new_contributions << contribution
|
2010-11-06 16:07:15 -07:00
|
|
|
new_points += contribution.point_value
|
2010-11-06 15:08:42 -07:00
|
|
|
end
|
|
|
|
end
|
2010-11-06 16:07:15 -07:00
|
|
|
self.points += new_points
|
|
|
|
Pet.transaction do
|
|
|
|
pet.save!
|
2011-02-22 15:45:53 -08:00
|
|
|
new_contributions.each do |contribution|
|
|
|
|
begin
|
|
|
|
contribution.save!
|
|
|
|
rescue ActiveRecord::RecordNotSaved => e
|
|
|
|
raise ActiveRecord::RecordNotSaved, "#{e.message}, #{contribution.inspect}, #{contribution.valid?.inspect}, #{contribution.errors.inspect}"
|
|
|
|
end
|
|
|
|
end
|
2011-02-23 13:01:43 -08:00
|
|
|
begin
|
|
|
|
save!
|
|
|
|
rescue ActiveRecord::RecordNotSaved => e
|
|
|
|
raise ActiveRecord::RecordNotSaved, "#{e.message}, #{self.inspect}, #{self.valid?.inspect}, #{self.errors.inspect}"
|
|
|
|
end
|
2010-11-06 16:07:15 -07:00
|
|
|
end
|
|
|
|
new_points
|
2010-11-06 15:08:42 -07:00
|
|
|
end
|
2011-07-12 16:37:16 -07:00
|
|
|
|
2010-10-18 14:58:45 -07:00
|
|
|
def self.find_or_create_from_remote_auth_data(user_data)
|
|
|
|
user = find_or_initialize_by_remote_id_and_auth_server_id(
|
|
|
|
user_data['id'],
|
|
|
|
DefaultAuthServerId
|
|
|
|
)
|
|
|
|
if user.new_record?
|
|
|
|
user.name = user_data['name']
|
|
|
|
user.save
|
|
|
|
end
|
|
|
|
user
|
|
|
|
end
|
2011-07-12 16:37:16 -07:00
|
|
|
|
2010-12-11 06:37:39 -08:00
|
|
|
def self.points_required_to_pass_top_contributor(offset)
|
2011-02-07 05:46:15 -08:00
|
|
|
user = User.top_contributors.select(:points).limit(1).offset(offset).first
|
|
|
|
user ? user.points : 0
|
2010-12-11 06:37:39 -08:00
|
|
|
end
|
2010-10-18 14:58:45 -07:00
|
|
|
end
|
2011-07-12 16:37:16 -07:00
|
|
|
|