Module: Kaminari::PaginatableWithoutCount

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

Overview

A module that makes AR::Relation paginatable without having to cast another SELECT COUNT query

Defined Under Namespace

Modules: LimitValueSetter

Instance Method Summary collapse

Instance Method Details

#last_page?Boolean

The page wouldn’t be the last page if there’s “limit + 1” record

Returns:

  • (Boolean)


107
108
109
# File 'lib/kaminari/activerecord/active_record_relation_methods.rb', line 107

def last_page?
  !out_of_range? && !@_has_next
end

#loadObject

Overwrite AR::Relation#load to actually load one more record to judge if the page has next page then store the result in @_has_next ivar



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/kaminari/activerecord/active_record_relation_methods.rb', line 88

def load
  if loaded? || limit_value.nil?
    super
  else
    set_limit_value limit_value + 1
    super
    set_limit_value limit_value - 1

    if @records.any?
      @records = @records.dup if (frozen = @records.frozen?)
      @_has_next = !!@records.delete_at(limit_value)
      @records.freeze if frozen
    end

    self
  end
end

#out_of_range?Boolean

Empty relation needs no pagination

Returns:

  • (Boolean)


112
113
114
115
# File 'lib/kaminari/activerecord/active_record_relation_methods.rb', line 112

def out_of_range?
  load unless loaded?
  @records.empty?
end

#total_countObject

Force to raise an exception if #total_count is called explicitly.



118
119
120
121
# File 'lib/kaminari/activerecord/active_record_relation_methods.rb', line 118

def total_count
  raise "This scope is marked as a non-count paginable scope and can't be used in combination " \
        "with `#paginate' or `#page_entries_info'. Use #link_to_next_page or #link_to_previous_page instead."
end