Skip to content

Commit

Permalink
Optimize db queries in several places
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima committed Nov 17, 2020
1 parent 5673c1d commit 2177f9e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
26 changes: 15 additions & 11 deletions lib/acts_as_votable/cacheable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ def scope_cache_field(field, vote_scope)
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
Expand All @@ -52,6 +48,13 @@ def update_cached_votes(vote_scope = nil)
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)) -
Expand All @@ -72,10 +75,6 @@ def update_cached_votes(vote_scope = nil)
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
Expand All @@ -84,6 +83,13 @@ def update_cached_votes(vote_scope = nil)
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
Expand Down Expand Up @@ -136,9 +142,7 @@ def count_votes_score(skip_cache = false, vote_scope = nil)

def weighted_total(skip_cache = false, vote_scope = nil)
from_cache(skip_cache, :cached_weighted_total, 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
find_votes_for(scope_or_empty_hash(vote_scope)).sum(:vote_weight)
end
end

Expand Down
11 changes: 5 additions & 6 deletions lib/acts_as_votable/votable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def self.included(base)
}

base.class_eval do
has_many :votes_for, class_name: "ActsAsVotable::Vote", as: :votable, dependent: :destroy do
has_many :votes_for, class_name: "ActsAsVotable::Vote", as: :votable, dependent: :delete_all do
def voters
includes(:voter).map(&:voter)
end
Expand Down Expand Up @@ -74,7 +74,7 @@ def vote_by(args = {})
# find the vote
votes = find_votes_by(options[:voter], options[:vote_scope])

if votes.empty? || options[:duplicate]
if options[:duplicate] || !votes.exists?
# this voter has never voted
vote = ActsAsVotable::Vote.new(
votable: self,
Expand Down Expand Up @@ -109,12 +109,11 @@ def unvote(args = {})
return false if args[:voter].nil?
votes = find_votes_by(args[:voter], args[:vote_scope])

return true if votes.empty?
ActiveRecord::Base.transaction do
votes.each(&:destroy)
update_cached_votes args[:vote_scope]
deleted_count = votes.delete_all
update_cached_votes(args[:vote_scope]) if deleted_count > 0
end
self.vote_registered = false if votes_for.empty?
self.vote_registered = false
return true
end

Expand Down
2 changes: 1 addition & 1 deletion lib/acts_as_votable/voter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def self.included(base)

base.class_eval do

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

0 comments on commit 2177f9e

Please sign in to comment.