Module: ActsAsVotable::Cacheable

Included in:
Votable
Defined in:
lib/acts_as_votable/cacheable.rb

Instance Method Summary collapse

Instance Method Details

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



129
130
131
132
133
# File 'lib/acts_as_votable/cacheable.rb', line 129

def count_votes_down(skip_cache = false, vote_scope = nil)
  from_cache(skip_cache, :cached_votes_down, vote_scope) do
    get_down_votes(vote_scope: vote_scope).count
  end
end

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



135
136
137
138
139
140
141
# File 'lib/acts_as_votable/cacheable.rb', line 135

def count_votes_score(skip_cache = false, vote_scope = nil)
  from_cache(skip_cache, :cached_votes_score, vote_scope) do
    ups = count_votes_up(true, vote_scope)
    downs = count_votes_down(true, vote_scope)
    ups - downs
  end
end

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

counting



117
118
119
120
121
# File 'lib/acts_as_votable/cacheable.rb', line 117

def count_votes_total(skip_cache = false, vote_scope = nil)
  from_cache(skip_cache, :cached_votes_total, vote_scope) do
    find_votes_for(scope_or_empty_hash(vote_scope)).count
  end
end

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



123
124
125
126
127
# File 'lib/acts_as_votable/cacheable.rb', line 123

def count_votes_up(skip_cache = false, vote_scope = nil)
  from_cache(skip_cache, :cached_votes_up, vote_scope) do
    get_up_votes(vote_scope: vote_scope).count
  end
end

#scope_cache_field(field, vote_scope) ⇒ 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
35
36
37
38
# File 'lib/acts_as_votable/cacheable.rb', line 5

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

#update_cached_votes(vote_scope = nil) ⇒ Object



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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/acts_as_votable/cacheable.rb', line 40

def update_cached_votes(vote_scope = nil)
  updates = {}

  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_total=)
    updates[:cached_votes_total] = (
      (updates[:cached_votes_up] || count_votes_up(true)) +
      (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_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_votes_total=, vote_scope)
      updates[scope_cache_field :cached_votes_total, 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_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.send(acts_as_votable_options[:cacheable_strategy], updates) if updates.size > 0
end

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



157
158
159
160
161
162
163
164
165
166
# File 'lib/acts_as_votable/cacheable.rb', line 157

def weighted_average(skip_cache = false, vote_scope = nil)
  from_cache(skip_cache, :cached_weighted_average, vote_scope) do
    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
end

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



149
150
151
152
153
154
155
# File 'lib/acts_as_votable/cacheable.rb', line 149

def weighted_score(skip_cache = false, vote_scope = nil)
  from_cache(skip_cache, :cached_weighted_score, vote_scope) do
    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
end

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



143
144
145
146
147
# File 'lib/acts_as_votable/cacheable.rb', line 143

def weighted_total(skip_cache = false, vote_scope = nil)
  from_cache(skip_cache, :cached_weighted_total, vote_scope) do
    find_votes_for(scope_or_empty_hash(vote_scope)).sum(:vote_weight)
  end
end