Module: Kaminari::ActiveRecordRelationMethods

Defined in:
lib/kaminari/activerecord/active_record_relation_methods.rb

Overview

Active Record specific page scope methods implementations

Instance Method Summary collapse

Instance Method Details

#entry_name(options = {}) ⇒ Object

Used for page_entry_info



7
8
9
10
# File 'lib/kaminari/activerecord/active_record_relation_methods.rb', line 7

def entry_name(options = {})
  default = options[:count] == 1 ? model_name.human : model_name.human.pluralize
  model_name.human(options.reverse_merge(default: default))
end

#resetObject

:nodoc:



12
13
14
15
# File 'lib/kaminari/activerecord/active_record_relation_methods.rb', line 12

def reset #:nodoc:
  @total_count = nil
  super
end

#total_count(column_name = :all, _options = nil) ⇒ Object

:nodoc:



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
# File 'lib/kaminari/activerecord/active_record_relation_methods.rb', line 17

def total_count(column_name = :all, _options = nil) #:nodoc:
  return @total_count if defined?(@total_count) && @total_count

  # There are some cases that total count can be deduced from loaded records
  if loaded?
    # Total count has to be 0 if loaded records are 0
    return @total_count = 0 if (current_page == 1) && @records.empty?
    # Total count is calculable at the last page
    return @total_count = (current_page - 1) * limit_value + @records.length if @records.any? && (@records.length < limit_value)
  end

  # #count overrides the #select which could include generated columns referenced in #order, so skip #order here, where it's irrelevant to the result anyway
  c = except(:offset, :limit, :order)
  # Remove includes only if they are irrelevant
  c = c.except(:includes) unless references_eager_loaded_tables?

  c = c.limit(max_pages * limit_value) if max_pages && max_pages.respond_to?(:*)

  # .group returns an OrderedHash that responds to #count
  c = c.count(column_name)
  @total_count = if c.is_a?(Hash) || c.is_a?(ActiveSupport::OrderedHash)
                   c.count
                 elsif c.respond_to? :count
                   c.count(column_name)
                 else
                   c
                 end
end

#without_countObject

Turn this Relation to a “without count mode” Relation. Note that the “without count mode” is supposed to be performant but has a feature limitation.

Pro: paginates without casting an extra SELECT COUNT query
Con: unable to know the total number of records/pages


50
51
52
# File 'lib/kaminari/activerecord/active_record_relation_methods.rb', line 50

def without_count
  extend ::Kaminari::PaginatableWithoutCount
end