Module: YeshouaCrm::ActsAsVotable::Voter

Defined in:
lib/yeshoua_crm/acts_as_votable/voter.rb,
lib/yeshoua_crm/acts_as_votable/rcrm_acts_as_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
31
# File 'lib/yeshoua_crm/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],
    :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 => 'YeshouaCrm::ActsAsVotable::Vote', :as => :voter, :dependent => :destroy 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



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

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

#find_down_votes(args = {}) ⇒ Object



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

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

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



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

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



110
111
112
# File 'lib/yeshoua_crm/acts_as_votable/voter.rb', line 110

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

#find_up_votes(args = {}) ⇒ Object



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

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

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



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

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



105
106
107
108
# File 'lib/yeshoua_crm/acts_as_votable/voter.rb', line 105

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



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

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

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



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

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

#get_down_voted(klass) ⇒ Object



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

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

#get_up_voted(klass) ⇒ Object



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

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

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



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

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

#include_objectsObject

Including polymporphic relations for eager loading



101
102
103
# File 'lib/yeshoua_crm/acts_as_votable/voter.rb', line 101

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

#rcrm_acts_as_voter(*args) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/yeshoua_crm/acts_as_votable/rcrm_acts_as_voter.rb', line 8

def rcrm_acts_as_voter(*args)
  require 'yeshoua_crm/acts_as_votable/voter'
  include YeshouaCrm::ActsAsVotable::Voter

  class_eval do
    def self.voter?
      true
    end
  end
end

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



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

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

#vote(args) ⇒ Object

voting



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

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

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



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

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



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

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



69
70
71
72
73
74
# File 'lib/yeshoua_crm/acts_as_votable/voter.rb', line 69

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
  vote.vote_flag
end

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

Returns:

  • (Boolean)


63
64
65
66
67
# File 'lib/yeshoua_crm/acts_as_votable/voter.rb', line 63

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.empty?
end

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

results

Returns:

  • (Boolean)


51
52
53
54
55
# File 'lib/yeshoua_crm/acts_as_votable/voter.rb', line 51

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.empty?
end

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

Returns:

  • (Boolean)


57
58
59
60
61
# File 'lib/yeshoua_crm/acts_as_votable/voter.rb', line 57

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.empty?
end

#voter?Boolean

Returns:

  • (Boolean)


4
5
6
# File 'lib/yeshoua_crm/acts_as_votable/rcrm_acts_as_voter.rb', line 4

def voter?
  false
end