Module: ActsAsVotable::Voter

Defined in:
lib/acts_as_votable/voter.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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
# File 'lib/acts_as_votable/voter.rb', line 5

def self.included(base)
  # allow user to define these
  aliases = {
    vote_up_for: [:likes, :upvotes, :up_votes],
    vote_down_for: [:dislikes, :downvotes, :down_votes],
    unvote_for: [:unlike, :undislike],
    voted_on?: [:voted_for?],
    voted_up_on?: [:voted_up_for?, :liked?],
    voted_down_on?: [:voted_down_for?, :disliked?],
    voted_as_when_voting_on: [:voted_as_when_voted_on, :voted_as_when_voting_for, :voted_as_when_voted_for],
    find_up_voted_items: [:find_liked_items],
    find_down_voted_items: [:find_disliked_items]
  }

  base.class_eval do

    has_many :votes, class_name: "ActsAsVotable::Vote", as: :voter, dependent: :delete_all do
      def votables
        includes(:votable).map(&:votable)
      end
    end

    aliases.each do |method, links|
      links.each do |new_method|
        alias_method(new_method, method)
      end
    end

  end
end

Instance Method Details

#find_down_voted_items(extra_conditions = {}) ⇒ Object



117
118
119
# File 'lib/acts_as_votable/voter.rb', line 117

def find_down_voted_items(extra_conditions = {})
  find_voted_items extra_conditions.merge(vote_flag: false)
end

#find_down_votes(args = {}) ⇒ Object



87
88
89
# File 'lib/acts_as_votable/voter.rb', line 87

def find_down_votes(args = {})
  find_votes vote_flag: false, vote_scope: args[:vote_scope]
end

#find_down_votes_for_class(klass, args = {}) ⇒ Object



99
100
101
# File 'lib/acts_as_votable/voter.rb', line 99

def find_down_votes_for_class(klass, args = {})
  find_votes_for_class klass, vote_flag: false, vote_scope: args[:vote_scope]
end

#find_up_voted_items(extra_conditions = {}) ⇒ Object



113
114
115
# File 'lib/acts_as_votable/voter.rb', line 113

def find_up_voted_items(extra_conditions = {})
  find_voted_items extra_conditions.merge(vote_flag: true)
end

#find_up_votes(args = {}) ⇒ Object



83
84
85
# File 'lib/acts_as_votable/voter.rb', line 83

def find_up_votes(args = {})
  find_votes vote_flag: true, vote_scope: args[:vote_scope]
end

#find_up_votes_for_class(klass, args = {}) ⇒ Object



95
96
97
# File 'lib/acts_as_votable/voter.rb', line 95

def find_up_votes_for_class(klass, args = {})
  find_votes_for_class klass, vote_flag: true, vote_scope: args[:vote_scope]
end

#find_voted_items(extra_conditions = {}) ⇒ Object



108
109
110
111
# File 'lib/acts_as_votable/voter.rb', line 108

def find_voted_items(extra_conditions = {})
  options = extra_conditions.merge voter_id: id, voter_type: self.class.base_class.name
  include_objects.where(options).collect(&:votable)
end

#find_votes(extra_conditions = {}) ⇒ Object



79
80
81
# File 'lib/acts_as_votable/voter.rb', line 79

def find_votes(extra_conditions = {})
  votes.where(extra_conditions)
end

#find_votes_for_class(klass, extra_conditions = {}) ⇒ Object



91
92
93
# File 'lib/acts_as_votable/voter.rb', line 91

def find_votes_for_class(klass, extra_conditions = {})
  find_votes extra_conditions.merge(votable_type: klass.name)
end

#get_down_voted(klass) ⇒ Object



129
130
131
# File 'lib/acts_as_votable/voter.rb', line 129

def get_down_voted(klass)
  klass.joins(:votes_for).merge find_down_votes
end

#get_up_voted(klass) ⇒ Object



125
126
127
# File 'lib/acts_as_votable/voter.rb', line 125

def get_up_voted(klass)
  klass.joins(:votes_for).merge find_up_votes
end

#get_voted(klass, extra_conditions = {}) ⇒ Object



121
122
123
# File 'lib/acts_as_votable/voter.rb', line 121

def get_voted(klass, extra_conditions = {})
  klass.joins(:votes_for).merge find_votes(extra_conditions)
end

#include_objectsObject

Including polymporphic relations for eager loading



104
105
106
# File 'lib/acts_as_votable/voter.rb', line 104

def include_objects
  ActsAsVotable::Vote.includes(:votable)
end

#unvote_for(model, args = {}) ⇒ Object



49
50
51
# File 'lib/acts_as_votable/voter.rb', line 49

def unvote_for(model, args = {})
  model.unvote voter: self, vote_scope: args[:vote_scope]
end

#vote(args) ⇒ Object

voting



37
38
39
# File 'lib/acts_as_votable/voter.rb', line 37

def vote(args)
  args[:votable].vote_by args.merge(voter: self)
end

#vote_down_for(model = nil, args = {}) ⇒ Object



45
46
47
# File 'lib/acts_as_votable/voter.rb', line 45

def vote_down_for(model = nil, args = {})
  vote votable: model, vote_scope: args[:vote_scope], vote: false
end

#vote_up_for(model = nil, args = {}) ⇒ Object



41
42
43
# File 'lib/acts_as_votable/voter.rb', line 41

def vote_up_for(model = nil, args = {})
  vote votable: model, vote_scope: args[:vote_scope], vote: true
end

#voted_as_when_voting_on(votable, args = {}) ⇒ Object



72
73
74
75
76
77
# File 'lib/acts_as_votable/voter.rb', line 72

def voted_as_when_voting_on(votable, args = {})
  vote = find_votes(votable_id: votable.id, votable_type: votable.class.base_class.name,
                     vote_scope: args[:vote_scope]).select(:vote_flag).last
  return nil unless vote
  return vote.vote_flag
end

#voted_down_on?(votable, args = {}) ⇒ Boolean



66
67
68
69
70
# File 'lib/acts_as_votable/voter.rb', line 66

def voted_down_on?(votable, args = {})
  votes = find_votes(votable_id: votable.id, votable_type: votable.class.base_class.name,
                     vote_scope: args[:vote_scope], vote_flag: false)
  votes.exists?
end

#voted_on?(votable, args = {}) ⇒ Boolean

results



54
55
56
57
58
# File 'lib/acts_as_votable/voter.rb', line 54

def voted_on?(votable, args = {})
  votes = find_votes(votable_id: votable.id, votable_type: votable.class.base_class.name,
                     vote_scope: args[:vote_scope])
  votes.exists?
end

#voted_up_on?(votable, args = {}) ⇒ Boolean



60
61
62
63
64
# File 'lib/acts_as_votable/voter.rb', line 60

def voted_up_on?(votable, args = {})
  votes = find_votes(votable_id: votable.id, votable_type: votable.class.base_class.name,
                     vote_scope: args[:vote_scope], vote_flag: true)
  votes.exists?
end