2010-10-18 14:58:45 -07:00
|
|
|
class User < ActiveRecord::Base
|
|
|
|
DefaultAuthServerId = 1
|
|
|
|
|
2010-11-06 08:52:58 -07:00
|
|
|
has_many :contributions
|
|
|
|
|
2010-11-06 09:15:10 -07:00
|
|
|
scope :top_contributors, order('points DESC').where(arel_table[:points].gt(0))
|
2010-11-05 15:45:05 -07:00
|
|
|
|
2010-11-06 15:08:42 -07:00
|
|
|
def contribute!(contributables)
|
|
|
|
new_contributions = []
|
|
|
|
contributables.each do |contributable|
|
|
|
|
if contributable.new_record?
|
|
|
|
contribution = Contribution.new(:contributed => contributable,
|
|
|
|
:user => self)
|
|
|
|
new_contributions << contribution
|
|
|
|
self.points += contribution.point_value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
self.contributions += new_contributions
|
|
|
|
save!
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
end
|