Module: YeshouaCrm::ActsAsVotable::Votable

Includes:
Helpers::Words
Defined in:
lib/yeshoua_crm/acts_as_votable/rcrm_acts_as_votable.rb,
lib/yeshoua_crm/acts_as_votable/votable.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::Words

#votable_words

Instance Attribute Details

#vote_registeredObject

Returns the value of attribute vote_registered.



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

def vote_registered
  @vote_registered
end

Class Method Details

.included(base) ⇒ Object



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
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/yeshoua_crm/acts_as_votable/votable.rb', line 8

def self.included(base)
  # allow the user to define these himself
  aliases = {

    :vote_up => [
      :up_by, :upvote_by, :like_by, :liked_by,
      :up_from, :upvote_from, :upvote_by, :like_from, :liked_from, :vote_from
    ],

    :vote_down => [
      :down_by, :downvote_by, :dislike_by, :disliked_by,
      :down_from, :downvote_from, :downvote_by, :dislike_by, :disliked_by
    ],

    :get_up_votes => [
      :get_true_votes, :get_ups, :get_upvotes, :get_likes, :get_positives, :get_for_votes,
    ],

    :get_down_votes => [
      :get_false_votes, :get_downs, :get_downvotes, :get_dislikes, :get_negatives
    ],
    :unvote_by => [
      :unvote_up, :unvote_down, :unliked_by, :undisliked_by
    ]
  }

  base.class_eval do
    has_many :votes_for, :class_name => 'YeshouaCrm::ActsAsVotable::Vote', :as => :votable, :dependent => :destroy do
      def voters
        includes(:voter).map(&:voter)
      end
    end

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

Instance Method Details

#count_votes_down(skip_cache = false, vote_scope = nil) ⇒ Object



272
273
274
275
276
277
# File 'lib/yeshoua_crm/acts_as_votable/votable.rb', line 272

def count_votes_down(skip_cache = false, vote_scope = nil)
  if !skip_cache && self.respond_to?(scope_cache_field :cached_votes_down, vote_scope)
    return self.send(scope_cache_field :cached_votes_down, vote_scope)
  end
  get_down_votes(:vote_scope => vote_scope).count
end

#count_votes_total(skip_cache = false, vote_scope = nil) ⇒ Object

counting



258
259
260
261
262
263
# File 'lib/yeshoua_crm/acts_as_votable/votable.rb', line 258

def count_votes_total(skip_cache = false, vote_scope = nil)
  if !skip_cache && self.respond_to?(scope_cache_field :cached_votes_total, vote_scope)
    return self.send(scope_cache_field :cached_votes_total, vote_scope)
  end
  find_votes_for(scope_or_empty_hash(vote_scope)).count
end

#count_votes_up(skip_cache = false, vote_scope = nil) ⇒ Object



265
266
267
268
269
270
# File 'lib/yeshoua_crm/acts_as_votable/votable.rb', line 265

def count_votes_up(skip_cache = false, vote_scope = nil)
  if !skip_cache && self.respond_to?(scope_cache_field :cached_votes_up, vote_scope)
    return self.send(scope_cache_field :cached_votes_up, vote_scope)
  end
  get_up_votes(:vote_scope => vote_scope).count
end

#create_index(table_name, column_name) ⇒ Object



21
22
23
24
25
# File 'lib/yeshoua_crm/acts_as_votable/rcrm_acts_as_votable.rb', line 21

def create_index(table_name, column_name)
  return if self.connection.index_exists?(table_name, column_name)

  self.connection.add_index table_name, column_name
end

#create_votable_table(options = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/yeshoua_crm/acts_as_votable/rcrm_acts_as_votable.rb', line 27

def create_votable_table(options = {})
  votes_name_table = options[:votes] || :votes

  if !self.connection.table_exists?(votes_name_table)
    self.connection.create_table(votes_name_table) do |t|
      t.references :votable, :polymorphic => true
      t.references :voter, :polymorphic => true

      t.column :vote_flag, :boolean
      t.column :vote_scope, :string
      t.column :vote_weight, :integer
      t.column :vote_ip, :string

      t.timestamps
    end
  else #if table exists - check existence of separate columns
    fields = {
      :votable_id => :integer,
      :votable_type => :string,
      :voter_id => :integer,
      :voter_type => :string,
      :vote_flag => :boolean,
      :vote_scope => :string,
      :vote_weight => :integer,
      :vote_ip => :string
    }
    fields.each do |name, type|
      if !self.connection.column_exists?(votes_name_table, name)
        self.connection.add_column(votes_name_table, name, type)
      end
    end

  end

  if self.parent::VERSION::MAJOR < 4
    create_index votes_name_table, [:votable_id, :votable_type, :vote_ip]
    create_index votes_name_table, [:voter_id, :voter_type, :vote_ip]
  end

  create_index votes_name_table, [:voter_id, :voter_type, :vote_scope]
  create_index votes_name_table, [:votable_id, :votable_type, :vote_scope]
  create_index votes_name_table, [:voter_type, :vote_scope, :vote_ip]
  create_index votes_name_table, [:votable_type, :vote_scope, :vote_ip]
end

#default_conditionsObject



55
56
57
58
59
60
# File 'lib/yeshoua_crm/acts_as_votable/votable.rb', line 55

def default_conditions
  {
    :votable_id => self.id,
    :votable_type => self.class.base_class.name.to_s
  }
end

#drop_votable_table(options = {}) ⇒ Object



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

def drop_votable_table(options = {})
  votes_name_table = options[:votes] || :votes
  if self.connection.table_exists?(votes_name_table)
    self.connection.drop_table votes_name_table
  end
end

#find_votes_for(extra_conditions = {}) ⇒ Object

results



243
244
245
# File 'lib/yeshoua_crm/acts_as_votable/votable.rb', line 243

def find_votes_for(extra_conditions = {})
  votes_for.where(extra_conditions)
end

#get_down_votes(options = {}) ⇒ Object



252
253
254
255
# File 'lib/yeshoua_crm/acts_as_votable/votable.rb', line 252

def get_down_votes(options = {})
  vote_scope_hash = scope_or_empty_hash(options[:vote_scope])
  find_votes_for({ :vote_flag => false }.merge(vote_scope_hash))
end

#get_up_votes(options = {}) ⇒ Object



247
248
249
250
# File 'lib/yeshoua_crm/acts_as_votable/votable.rb', line 247

def get_up_votes(options = {})
  vote_scope_hash = scope_or_empty_hash(options[:vote_scope])
  find_votes_for({:vote_flag => true}.merge(vote_scope_hash))
end

#rcrm_acts_as_votableObject



10
11
12
13
14
15
16
17
18
19
# File 'lib/yeshoua_crm/acts_as_votable/rcrm_acts_as_votable.rb', line 10

def rcrm_acts_as_votable
  require 'yeshoua_crm/acts_as_votable/votable'
  include YeshouaCrm::ActsAsVotable::Votable

  class_eval do
    def self.votable?
      true
    end
  end
end

#scope_cache_field(field, vote_scope) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/yeshoua_crm/acts_as_votable/votable.rb', line 132

def scope_cache_field(field, vote_scope)
  return field if vote_scope.nil?

  case field
  when :cached_votes_total=
    "cached_scoped_#{vote_scope}_votes_total="
  when :cached_votes_total
    "cached_scoped_#{vote_scope}_votes_total"
  when :cached_votes_up=
    "cached_scoped_#{vote_scope}_votes_up="
  when :cached_votes_up
    "cached_scoped_#{vote_scope}_votes_up"
  when :cached_votes_down=
    "cached_scoped_#{vote_scope}_votes_down="
  when :cached_votes_down
    "cached_scoped_#{vote_scope}_votes_down"
  when :cached_votes_score=
    "cached_scoped_#{vote_scope}_votes_score="
  when :cached_votes_score
    "cached_scoped_#{vote_scope}_votes_score"
  when :cached_weighted_total
    "cached_weighted_#{vote_scope}_total"
  when :cached_weighted_total=
    "cached_weighted_#{vote_scope}_total="
  when :cached_weighted_score
    "cached_weighted_#{vote_scope}_score"
  when :cached_weighted_score=
    "cached_weighted_#{vote_scope}_score="
  when :cached_weighted_average
    "cached_weighted_#{vote_scope}_average"
  when :cached_weighted_average=
    "cached_weighted_#{vote_scope}_average="
  end
end

#unvote(args = {}) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/yeshoua_crm/acts_as_votable/votable.rb', line 101

def unvote(args = {})
  return false if (!args[:vote_by_ip] && args[:voter].nil?) || (args[:vote_by_ip] && args[:vote_ip].nil?)
  vote_conditions = { :vote_scope => args[:vote_scope], :voter_type => args[:voter].class.base_class.name }
  vote_conditions.merge!(args[:vote_by_ip] ? { :vote_ip => args[:vote_ip] } : { :voter_id => args[:voter].id})
  votes_for = find_votes_for(vote_conditions)

  return true if votes_for.empty?
  votes_for.each(&:destroy)
  update_cached_votes args[:vote_scope]
  self.vote_registered = false if votes_for.count == 0
  true
end

#unvote_by(voter, options = {}) ⇒ Object



126
127
128
129
130
# File 'lib/yeshoua_crm/acts_as_votable/votable.rb', line 126

def unvote_by(voter, options = {})
  # Does not need vote_weight since the votes_for are anyway getting destroyed
  self.unvote :voter => voter, :vote_scope => options[:vote_scope], :vote_ip => options[:vote_ip],
              :vote_by_ip => options[:vote_by_ip]
end

#update_cached_votes(vote_scope = nil) ⇒ Object

caching



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/yeshoua_crm/acts_as_votable/votable.rb', line 168

def update_cached_votes(vote_scope = nil)
  updates = {}

  if self.respond_to?(:cached_votes_total=)
    updates[:cached_votes_total] = count_votes_total(true)
  end

  if self.respond_to?(:cached_votes_up=)
    updates[:cached_votes_up] = count_votes_up(true)
  end

  if self.respond_to?(:cached_votes_down=)
    updates[:cached_votes_down] = count_votes_down(true)
  end

  if self.respond_to?(:cached_votes_score=)
    updates[:cached_votes_score] = (
      (updates[:cached_votes_up] || count_votes_up(true)) -
      (updates[:cached_votes_down] || count_votes_down(true))
    )
  end

  if self.respond_to?(:cached_weighted_total=)
    updates[:cached_weighted_total] = weighted_total(true)
  end

  if self.respond_to?(:cached_weighted_score=)
    updates[:cached_weighted_score] = weighted_score(true)
  end

  if self.respond_to?(:cached_weighted_average=)
    updates[:cached_weighted_average] = weighted_average(true)
  end

  if vote_scope
    if self.respond_to?(scope_cache_field :cached_votes_total=, vote_scope)
      updates[scope_cache_field :cached_votes_total, vote_scope] = count_votes_total(true, vote_scope)
    end

    if self.respond_to?(scope_cache_field :cached_votes_up=, vote_scope)
      updates[scope_cache_field :cached_votes_up, vote_scope] = count_votes_up(true, vote_scope)
    end

    if self.respond_to?(scope_cache_field :cached_votes_down=, vote_scope)
      updates[scope_cache_field :cached_votes_down, vote_scope] = count_votes_down(true, vote_scope)
    end

    if self.respond_to?(scope_cache_field :cached_weighted_total=, vote_scope)
      updates[scope_cache_field :cached_weighted_total, vote_scope] = weighted_total(true, vote_scope)
    end

    if self.respond_to?(scope_cache_field :cached_weighted_score=, vote_scope)
      updates[scope_cache_field :cached_weighted_score, vote_scope] = weighted_score(true, vote_scope)
    end

    if self.respond_to?(scope_cache_field :cached_votes_score=, vote_scope)
      updates[scope_cache_field :cached_votes_score, vote_scope] = (
        (updates[scope_cache_field :cached_votes_up, vote_scope] || count_votes_up(true, vote_scope)) -
        (updates[scope_cache_field :cached_votes_down, vote_scope] || count_votes_down(true, vote_scope))
      )
    end

    if self.respond_to?(scope_cache_field :cached_weighted_average=, vote_scope)
      updates[scope_cache_field :cached_weighted_average, vote_scope] = weighted_average(true, vote_scope)
    end
  end
  self.record_timestamps = false
  if (::ActiveRecord::VERSION::MAJOR == 3) && (::ActiveRecord::VERSION::MINOR != 0)
    self.update_attributes(updates, :without_protection => true) if !updates.empty?
  else
    self.update_attributes(updates) if !updates.empty?
  end
end

#votable?Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/yeshoua_crm/acts_as_votable/rcrm_acts_as_votable.rb', line 6

def votable?
  false
end

#vote_by(args = {}) ⇒ Object

voting



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/yeshoua_crm/acts_as_votable/votable.rb', line 63

def vote_by(args = {})
  options = {
    :vote => true,
    :vote_scope => nil
  }.merge(args)

  self.vote_registered = false

  return false if options[:voter].nil?

  # find the vote
  vote_conditions = { :vote_scope => options[:vote_scope], :voter_type => options[:voter].class.base_class.name }
  vote_conditions.merge!(options[:vote_by_ip] ? { :vote_ip => options[:vote_ip] } : { :voter_id => options[:voter].id} )
  votes_for = find_votes_for(vote_conditions)

  if votes_for.count == 0 || options[:duplicate]
    # this voter has never voted
    vote_params = { :votable => self, :voter => options[:voter], :vote_scope => options[:vote_scope] }
    vote_params[:vote_ip] = options[:vote_ip] if options[:vote_ip]
    vote = YeshouaCrm::ActsAsVotable::Vote.new(vote_params)
  else
    # this voter is potentially changing his vote
    vote = votes_for.last
  end

  vote.vote_flag = votable_words.meaning_of(options[:vote])

  # Allowing for a vote_weight to be associated with every vote. Could change with every voter object
  vote.vote_weight = (options[:vote_weight].to_i if options[:vote_weight].present?) || 1

  return false if vote.invalid?

  self.vote_registered = vote.changed?
  vote.save!(validate: false)
  update_cached_votes(options[:vote_scope])
  vote
end

#vote_down(voter, options = {}) ⇒ Object



120
121
122
123
124
# File 'lib/yeshoua_crm/acts_as_votable/votable.rb', line 120

def vote_down(voter, options={})
  self.vote_by :voter => voter, :vote => false,
               :vote_scope => options[:vote_scope], :vote_weight => options[:vote_weight], :vote_ip => options[:vote_ip],
               :vote_by_ip => options[:vote_by_ip]
end

#vote_registered?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/yeshoua_crm/acts_as_votable/votable.rb', line 51

def vote_registered?
  self.vote_registered
end

#vote_up(voter, options = {}) ⇒ Object



114
115
116
117
118
# File 'lib/yeshoua_crm/acts_as_votable/votable.rb', line 114

def vote_up(voter, options={})
  self.vote_by :voter => voter, :vote => true,
               :vote_scope => options[:vote_scope], :vote_weight => options[:vote_weight], :vote_ip => options[:vote_ip],
               :vote_by_ip => options[:vote_by_ip]
end

#voted_on_by?(voter) ⇒ Boolean

voters

Returns:

  • (Boolean)


311
312
313
314
# File 'lib/yeshoua_crm/acts_as_votable/votable.rb', line 311

def voted_on_by?(voter)
  votes = find_votes_for :voter_id => voter.id, :voter_type => voter.class.base_class.name
  votes.count > 0
end

#weighted_average(skip_cache = false, vote_scope = nil) ⇒ Object



297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/yeshoua_crm/acts_as_votable/votable.rb', line 297

def weighted_average(skip_cache = false, vote_scope = nil)
  if !skip_cache && self.respond_to?(scope_cache_field :cached_weighted_average, vote_scope)
    return self.send(scope_cache_field :cached_weighted_average, vote_scope)
  end

  count = count_votes_total(skip_cache, vote_scope).to_i
  if count > 0
    weighted_score(skip_cache, vote_scope).to_f / count
  else
    0.0
  end
end

#weighted_score(skip_cache = false, vote_scope = nil) ⇒ Object



288
289
290
291
292
293
294
295
# File 'lib/yeshoua_crm/acts_as_votable/votable.rb', line 288

def weighted_score(skip_cache = false, vote_scope = nil)
  if !skip_cache && self.respond_to?(scope_cache_field :cached_weighted_score, vote_scope)
    return self.send(scope_cache_field :cached_weighted_score, vote_scope)
  end
  ups = get_up_votes(:vote_scope => vote_scope).sum(:vote_weight)
  downs = get_down_votes(:vote_scope => vote_scope).sum(:vote_weight)
  ups - downs
end

#weighted_total(skip_cache = false, vote_scope = nil) ⇒ Object



279
280
281
282
283
284
285
286
# File 'lib/yeshoua_crm/acts_as_votable/votable.rb', line 279

def weighted_total(skip_cache = false, vote_scope = nil)
  if !skip_cache && self.respond_to?(scope_cache_field :cached_weighted_total, vote_scope)
    return self.send(scope_cache_field :cached_weighted_total, vote_scope)
  end
  ups = get_up_votes(:vote_scope => vote_scope).sum(:vote_weight)
  downs = get_down_votes(:vote_scope => vote_scope).sum(:vote_weight)
  ups + downs
end