Class: Ludy::RailsPaginator

Inherits:
Paginator show all
Defined in:
lib/ludy/paginator.rb

Overview

rails paginator was provided for convenience, it wraps Paginator for you, and you can just pass the model class to it. you don’t have to care about the fetcher and counter. additionally, you can pass other options to rails paginator, they would be used in find options. e.g.,

RailsPaginator.new Model, :order => 'created_at DESC'

would invoke Model.find :all, :offset => ?, :limit => ?, order => ‘created_at DESC’

Instance Attribute Summary collapse

Attributes inherited from Paginator

#counter, #fetcher, #per_page

Instance Method Summary collapse

Methods inherited from Paginator

#==, #count, #each, null, #offset, #size, #to_a

Constructor Details

#initialize(model_class, per_page = 20, opts = {}) ⇒ RailsPaginator

Returns a new instance of RailsPaginator.



106
107
108
109
110
111
112
113
# File 'lib/ludy/paginator.rb', line 106

def initialize model_class, per_page = 20, opts = {}
  @model_class = model_class
  super(lambda{ |offset, per_page|
    @model_class.find :all, opts.merge(:offset => offset, :limit => per_page)
  }, lambda{
    @model_class.count opts
  }, 20)
end

Instance Attribute Details

#model_classObject (readonly)

the model class that you passed in this paginator



105
106
107
# File 'lib/ludy/paginator.rb', line 105

def model_class
  @model_class
end

Instance Method Details

#page(page) ⇒ Object Also known as: []

it simply call super(page.to_i), so RailsPaginator also eat string.



115
# File 'lib/ludy/paginator.rb', line 115

def page page; super page.to_i; end