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, #offset, #size, #to_a

Constructor Details

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

Returns a new instance of RailsPaginator.



100
101
102
103
104
105
106
107
# File 'lib/ludy/paginator.rb', line 100

def initialize model_class, 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
  })
end

Instance Attribute Details

#model_classObject (readonly)

the model class that you passed in this paginator



99
100
101
# File 'lib/ludy/paginator.rb', line 99

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.



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

def page page; super page.to_i; end