Module: ShopifyAPI::Kaminari
- Defined in:
- lib/shopify-kaminari.rb
Overview
add Kaminari pagination support to ShopifyAPI
Constant Summary collapse
- @@per =
25
Class Method Summary collapse
-
.per ⇒ Integer
get limit of rows per page.
-
.per=(value) ⇒ Integer
set limit of rows to return per page.
Instance Method Summary collapse
-
#paginate(options = {}) ⇒ Array
paginates a ShopifyAPI::Base resource.
Class Method Details
.per ⇒ Integer
get limit of rows per page
15 16 17 |
# File 'lib/shopify-kaminari.rb', line 15 def per @@per end |
.per=(value) ⇒ Integer
set limit of rows to return per page
25 26 27 |
# File 'lib/shopify-kaminari.rb', line 25 def per=(value) @@per = value end |
Instance Method Details
#paginate(options = {}) ⇒ Array
paginates a ShopifyAPI::Base resource
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/shopify-kaminari.rb', line 38 def paginate( = {}) = {:page => } if .class == Fixnum = {} if .nil? # create params for pagination params = { :page => [:page] || 1, :limit => [:per] || 25 } # remove pagination params from options .delete :page .delete :per # merge any params from options params = params.merge [:params] if .include? :params # remove params from options .delete :params # create arguments to pass to ActiveResource find args = {:params => params}.merge # run the query result = self.find :all, args # add instance methods to result Array to support paginating with Kaminari result.instance_eval <<-EVAL def current_page; #{args[:params][:page]}; end def total_pages; #{self.count / args[:params][:limit] + 1}; end def limit_value; #{args[:params][:limit]}; end EVAL result end |