Module: Votes::ActiveRecordExt::VoteableInstanceMethods

Defined in:
lib/votes/active_record_ext.rb

Instance Method Summary collapse

Instance Method Details

#recalculate_ratingObject



47
48
49
# File 'lib/votes/active_record_ext.rb', line 47

def recalculate_rating
  self.update_attribute(:rating, self.base_rating + self.votes.map(&:points).sum)
end

#update_rating(vote) ⇒ Object



43
44
45
# File 'lib/votes/active_record_ext.rb', line 43

def update_rating(vote)
  self.update_attribute(:rating, self.rating + vote.points)
end

#vote_by_current_visitor(current_user, session) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/votes/active_record_ext.rb', line 27

def vote_by_current_visitor current_user, session
  if current_user
    vote_by_user(current_user)
  else
    vote_by_session_key(session)
  end
end

#vote_by_session_key(session_key) ⇒ Object



39
40
41
# File 'lib/votes/active_record_ext.rb', line 39

def vote_by_session_key session_key
  self.votes.find_by_session_key(session_key)
end

#vote_by_user(user) ⇒ Object



35
36
37
# File 'lib/votes/active_record_ext.rb', line 35

def vote_by_user user
  self.votes.find_by_user_id(user.id)
end