Module: Mongoid::Haystack::Pagination

Included in:
Denormalization::Results
Defined in:
lib/mongoid-haystack/search.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



185
186
187
188
189
190
191
# File 'lib/mongoid-haystack/search.rb', line 185

def method_missing(method, *args, &block)
  if respond_to?(:_paginated) and _paginated.has_key?(method) and args.empty? and block.nil?
    _paginated[method]
  else
    super
  end
end

Instance Method Details

#_paginatedObject



181
182
183
# File 'lib/mongoid-haystack/search.rb', line 181

def _paginated
  @_paginated ||= Map.new
end

#paginate(*args, &block) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/mongoid-haystack/search.rb', line 148

def paginate(*args, &block)
  list = self
  options = Map.options_for!(args)

  page = Integer(args.shift || options[:page] || 1)
  size = Integer(args.shift || options[:size] || 42)

  count =
    if list.is_a?(Array)
      list.size
    else
      list.count
    end

  limit = size
  skip = (page - 1 ) * size
  
  result =
    if list.is_a?(Array)
      list.slice(skip, limit)
    else
      list.skip(skip).limit(limit)
    end

  result._paginated.update(
    :total_pages  => (count / size.to_f).ceil,
    :num_pages    => (count / size.to_f).ceil,
    :current_page => page
  )

  result
end