Skip to content

Commit

Permalink
refactor total_count memoization code
Browse files Browse the repository at this point in the history
@Empact "Would be nicer to use the "@total_count ||= begin" convention, IMO." https://github.com/amatsuda/kaminari/pull/138#issuecomment-1692686
  • Loading branch information
amatsuda committed Dec 11, 2011
1 parent 27cf6e8 commit 0a36c17
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions lib/kaminari/models/active_record_relation_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ def count #:nodoc:

def total_count #:nodoc:
# #count overrides the #select which could include generated columns referenced in #order, so skip #order here, where it's irrelevant to the result anyway
return @total_count if @total_count
c = except(:offset, :limit, :order)
# a workaround for 3.1.beta1 bug. see: https://github.com/rails/rails/issues/406
c = c.reorder nil
# Remove includes only if they are irrelevant
c = c.except(:includes) unless references_eager_loaded_tables?
# .group returns an OrderdHash that responds to #count
c = c.count
@total_count = c.respond_to?(:count) ? c.count : c
@total_count ||= begin
c = except(:offset, :limit, :order)
# a workaround for 3.1.beta1 bug. see: https://github.com/rails/rails/issues/406
c = c.reorder nil
# Remove includes only if they are irrelevant
c = c.except(:includes) unless references_eager_loaded_tables?
# .group returns an OrderdHash that responds to #count
c = c.count
@total_count = c.respond_to?(:count) ? c.count : c
end
end
end
end
Expand Down

0 comments on commit 0a36c17

Please sign in to comment.