Module: Redmine::Pagination

Included in:
ApplicationController
Defined in:
lib/redmine/pagination.rb

Defined Under Namespace

Modules: Helper Classes: Paginator

Instance Method Summary collapse

Instance Method Details

#deprecated_paginate(arg, finder_options, options = {}) ⇒ Object


132
133
134
135
136
137
# File 'lib/redmine/pagination.rb', line 132

def deprecated_paginate(arg, finder_options, options={})
  ActiveSupport::Deprecation.warn "#paginate with a Symbol and/or find options is depreceted and will be removed. Use a scope instead."
  klass = arg.is_a?(Symbol) ? arg.to_s.classify.constantize : arg
  scope = klass.scoped(finder_options)
  paginate(scope, options)
end

#paginate(scope, options = {}) ⇒ Object

Paginates the given scope or model. Returns a Paginator instance and the collection of objects for the current page.

Options:

:parameter     name of the page parameter

Examples:

@user_pages, @users = paginate User.where(:status => 1)

113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/redmine/pagination.rb', line 113

def paginate(scope, options={})
  options = options.dup
  finder_options = options.extract!(
    :conditions,
    :order,
    :joins,
    :include,
    :select
  )
  if scope.is_a?(Symbol) || finder_options.values.compact.any?
    return deprecated_paginate(scope, finder_options, options)
  end

  paginator = paginator(scope.count, options)
  collection = scope.limit(paginator.per_page).offset(paginator.offset).to_a

  return paginator, collection
end

#paginator(item_count, options = {}) ⇒ Object


139
140
141
142
143
144
145
146
# File 'lib/redmine/pagination.rb', line 139

def paginator(item_count, options={})
  options.assert_valid_keys :parameter, :per_page

  page_param = options[:parameter] || :page
  page = (params[page_param] || 1).to_i
  per_page = options[:per_page] || per_page_option
  Paginator.new(item_count, per_page, page, page_param)
end