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



173
174
175
176
177
178
179
# File 'lib/mongoid-haystack/search.rb', line 173

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



169
170
171
# File 'lib/mongoid-haystack/search.rb', line 169

def _paginated
  @_paginated ||= Map.new
end

#paginate(*args, &block) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/mongoid-haystack/search.rb', line 136

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