Module: Cms::Behaviors::Pagination::ClassMethods

Defined in:
lib/cms/behaviors/pagination.rb

Instance Method Summary collapse

Instance Method Details

#paginate(*args, &block) ⇒ Object

This is the main paginating finder.

Special parameters for paginating finders

  • :page – REQUIRED, but defaults to 1 if false or nil

  • :per_page – defaults to CurrentModel.per_page (which is 30 if not overridden)

  • :total_entries – use only if you manually count total entries

  • :count – additional options that are passed on to count

  • :finder – name of the ActiveRecord finder used (default: “find”)

All other options (conditions, order, …) are forwarded to find and count calls.



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/cms/behaviors/pagination.rb', line 131

def paginate(*args, &block)
  options = args.pop
  page, per_page, total_entries = parse_pagination_options(options)

  finder = (options[:finder] || 'find').to_s
  if finder == 'find'
    # an array of IDs may have been given:
    total_entries ||= (Array === args.first and args.first.size)
    # :all is implicit
    args.unshift(:all) if args.empty?
  end

  Collection.create(page, per_page, total_entries) do |pager|
    count_options = options.except :page, :per_page, :total_entries, :finder
    find_options = count_options.except(:count).update(:offset => pager.offset, :limit => pager.per_page) 

    args << find_options
    # @options_from_last_find = nil
    pager.replace send(finder, *args, &block)

    # magic counting for user convenience:
    pager.total_entries = count_for_pagination(count_options, args, finder) unless pager.total_entries
  end
end