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



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

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]
  }

  base.class_eval do

    belongs_to :voter, :polymorphic => true
    has_many :votes, :class_name => "ActsAsVotable::Vote", :as => :voter 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_votesObject



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

def find_down_votes
  find_votes :vote_flag => false
end

#find_down_votes_for_class(klass) ⇒ Object



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

def find_down_votes_for_class klass
  find_votes_for_class klass, :vote_flag => false
end

#find_up_votesObject



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

def find_up_votes
  find_votes :vote_flag => true
end

#find_up_votes_for_class(klass) ⇒ Object



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

def find_up_votes_for_class klass
  find_votes_for_class klass, :vote_flag => true
end

#find_votes(extra_conditions = {}) ⇒ Object



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

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

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



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

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

#unvote_for(model) ⇒ Object



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

def unvote_for model
  model.unvote :voter => self
end

#vote(args) ⇒ Object

voting



33
34
35
# File 'lib/acts_as_votable/voter.rb', line 33

def vote args
  args[:votable].vote args.merge({:voter => self})
end

#vote_down_for(model) ⇒ Object



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

def vote_down_for model
  vote :votable => model, :vote => false
end

#vote_up_for(model = nil) ⇒ Object



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

def vote_up_for model=nil
  vote :votable => model, :vote => true
end

#voted_as_when_voting_on(votable) ⇒ Object Also known as: voted_as_when_voted_for



68
69
70
71
72
# File 'lib/acts_as_votable/voter.rb', line 68

def voted_as_when_voting_on votable
  votes = find_votes(:votable_id => votable.id, :votable_type => votable.class.base_class.name)
  return nil if votes.size == 0
  return votes.first.vote_flag
end

#voted_down_on?(votable) ⇒ Boolean Also known as: voted_down_for?

Returns:

  • (Boolean)


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

def voted_down_on? votable
  votes = find_votes(:votable_id => votable.id, :votable_type => votable.class.base_class.name, :vote_flag => false)
  votes.size > 0
end

#voted_on?(votable) ⇒ Boolean Also known as: voted_for?

results

Returns:

  • (Boolean)


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

def voted_on? votable
  votes = find_votes(:votable_id => votable.id, :votable_type => votable.class.base_class.name)
  votes.size > 0
end

#voted_up_on?(votable) ⇒ Boolean Also known as: voted_up_for?

Returns:

  • (Boolean)


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

def voted_up_on? votable
  votes = find_votes(:votable_id => votable.id, :votable_type => votable.class.base_class.name, :vote_flag => true)
  votes.size > 0
end