Module: VoteFu::Concerns::Voteable::ClassMethods
- Defined in:
- lib/vote_fu/concerns/voteable.rb
Instance Method Summary collapse
-
#by_vote_count(direction = :desc) ⇒ Object
Order by vote count.
-
#by_votes(direction = :desc) ⇒ Object
Order by total vote value (sum of all vote values).
-
#trending(since: 24.hours.ago) ⇒ Object
Trending items (most votes in time period).
-
#with_positive_score ⇒ Object
Items with positive net votes.
-
#with_votes ⇒ Object
Items with any votes.
-
#without_votes ⇒ Object
Items without any votes.
Instance Method Details
#by_vote_count(direction = :desc) ⇒ Object
Order by vote count
66 67 68 69 70 |
# File 'lib/vote_fu/concerns/voteable.rb', line 66 def by_vote_count(direction = :desc) left_joins(:received_votes) .group(:id) .order(Arel.sql("COUNT(vote_fu_votes.id) #{direction.to_s.upcase}")) end |
#by_votes(direction = :desc) ⇒ Object
Order by total vote value (sum of all vote values)
59 60 61 62 63 |
# File 'lib/vote_fu/concerns/voteable.rb', line 59 def by_votes(direction = :desc) left_joins(:received_votes) .group(:id) .order(Arel.sql("COALESCE(SUM(vote_fu_votes.value), 0) #{direction.to_s.upcase}")) end |
#trending(since: 24.hours.ago) ⇒ Object
Trending items (most votes in time period)
91 92 93 94 95 96 |
# File 'lib/vote_fu/concerns/voteable.rb', line 91 def trending(since: 24.hours.ago) joins(:received_votes) .where(vote_fu_votes: { created_at: since.. }) .group(:id) .order(Arel.sql("COUNT(vote_fu_votes.id) DESC")) end |
#with_positive_score ⇒ Object
Items with positive net votes
73 74 75 76 77 |
# File 'lib/vote_fu/concerns/voteable.rb', line 73 def with_positive_score left_joins(:received_votes) .group(:id) .having("COALESCE(SUM(vote_fu_votes.value), 0) > 0") end |
#with_votes ⇒ Object
Items with any votes
80 81 82 |
# File 'lib/vote_fu/concerns/voteable.rb', line 80 def with_votes joins(:received_votes).distinct end |
#without_votes ⇒ Object
Items without any votes
85 86 87 88 |
# File 'lib/vote_fu/concerns/voteable.rb', line 85 def without_votes left_joins(:received_votes) .where(vote_fu_votes: { id: nil }) end |