Module: WillPaginate::ActiveRecord::Pagination

Defined in:
lib/will_paginate/active_record.rb

Instance Method Summary collapse

Instance Method Details

#page(num) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/will_paginate/active_record.rb', line 159

def page(num)
  rel = if ::ActiveRecord::Relation === self
    self
  elsif !defined?(::ActiveRecord::Scoping) or ::ActiveRecord::Scoping::ClassMethods.method_defined? :with_scope
    # Active Record 3
    scoped
  else
    # Active Record 4
    all
  end

  rel = rel.extending(RelationMethods)
  pagenum = ::WillPaginate::PageNumber(num.nil? ? 1 : num)
  per_page = rel.limit_value || self.per_page
  rel = rel.offset(pagenum.to_offset(per_page).to_i)
  rel = rel.limit(per_page) unless rel.limit_value
  rel.current_page = pagenum
  rel
end

#paginate(options) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/will_paginate/active_record.rb', line 143

def paginate(options)
  options  = options.dup
  pagenum  = options.fetch(:page) { raise ArgumentError, ":page parameter required" }
  options.delete(:page)
  per_page = options.delete(:per_page) || self.per_page
  total    = options.delete(:total_entries)

  if options.any?
    raise ArgumentError, "unsupported parameters: %p" % options.keys
  end

  rel = limit(per_page.to_i).page(pagenum)
  rel.total_entries = total.to_i          unless total.blank?
  rel
end