Module: Ramaze::Helper::Pager

Defined in:
lib/ramaze/helper/pager.rb

Overview

Pager related helper methods.

Instance Method Summary collapse

Instance Method Details

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

Helper method that generates a collection of items and the associated pager object.

Example

items = [ ‘item1’, ‘item2’, … ] entries, pager = paginate(items, :limit => 10)

<ul> <?r for entry in entries ?>

<li>#{entry.to_link}</li>

<?r end ?> </ul> #Ramaze::Helper::Pager.pagerpager.navigation

Og Example

entries, pager = paginate(Article, :where => ‘title LIKE..’, :limit => 10)

or

entries, pager = paginate(article.comments, :limit => 10)



337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/ramaze/helper/pager.rb', line 337

def paginate(items, options = {})
  limit = options.delete(:limit) || Pager.trait[:limit]
  pager_key = options.delete(:pager_key) || Pager.trait[:key]

  case items
  when Array
    pager = Pager.new(request, limit, items.size, pager_key)
    items = items.slice(pager.offset, pager.limit[:limit])
    return items, pager
  end

  if defined?(Og) && items.is_a?(Og::Collection)
    pager = Pager.new(request, limit, items.count, pager_key)
    options.update(pager.limit)
    items = items.reload(options)
    return items, pager
  elsif defined?(Og::EntityMixin) && items.is_a?(Og::EntityMixin) ||
        defined?(Og::Mixin) && items.is_a?(Og::Mixin) # Og <= 0.41
    pager = Pager.new(request, limit, items.count(options), pager_key)
    options.update(pager.limit)
    items = items.all(options)
    return items, pager
  end

  raise "No suitable pagination method for #{items.inspect}"
end