forked from rubymanor/vestibule
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.rb
57 lines (47 loc) · 1.86 KB
/
user.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class User < ActiveRecord::Base
has_many :proposals, :foreign_key => :proposer_id
has_many :suggestions, :foreign_key => :author_id
has_many :proposals_of_interest, :through => :suggestions, :source => :proposal, :uniq => true
scope :with_signup_reasons, where("signup_reason IS NOT NULL")
scope :without_signup_reasons, where(:signup_reason => nil)
scope :by_contribution, order("contribution_score DESC")
before_save :update_contribution_score
TWITTER_USERS_PER_REQUEST = 100
def proposals_you_should_look_at
Proposal.without_suggestions_from(self).not_proposed_by(self)
end
def proposals_that_have_changed
proposals_of_interest.not_proposed_by(self).select { |p| p.updated_at > p.suggestions.by(self).maximum(:updated_at) }
end
def self.create_with_omniauth(auth)
create! do |user|
user.name = auth["user_info"]["name"]
user.twitter_uid = auth["uid"]
user.twitter_nickname = auth["user_info"]["nickname"]
user.twitter_image = auth["user_info"]["image"]
end
end
def self.update_twitter_images
all.map { |user| user.twitter_uid.to_i }.each_slice(TWITTER_USERS_PER_REQUEST) do |uids|
Twitter.users(uids).each do |twitter_user|
if (user = find_by_twitter_uid(twitter_user.id.to_s)).present? && (twitter_image = twitter_user.profile_image_url).present?
user.update_attributes :twitter_image => twitter_image
end
end
end
end
def to_param
twitter_nickname
end
REASON_WEIGHT = 5
PROPOSAL_WEIGHT = 10
INTERESTING_PROPOSALS_WEIGHT = 5
SUGGESTION_WEIGHT = 2
def update_contribution_score
self.contribution_score =
(proposals.count * PROPOSAL_WEIGHT) +
(proposals_of_interest.count * INTERESTING_PROPOSALS_WEIGHT) +
(suggestions.count * SUGGESTION_WEIGHT)
self.contribution_score += REASON_WEIGHT if self.signup_reason.present?
end
end