Module: ActiveCollection::Pagination
- Defined in:
- lib/active_collection/pagination.rb
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- PER_PAGE =
30
Instance Attribute Summary collapse
-
#per_page ⇒ Object
Defaults to the model class’ per_page.
Class Method Summary collapse
Instance Method Summary collapse
- #as_data_hash ⇒ Object
- #current_page ⇒ Object
-
#last_page? ⇒ Boolean
true if the collection is the last page.
-
#next_page ⇒ Object
current_page + 1 or nil if there is no next page.
-
#next_page_collection ⇒ Object
New Collection for current_page + 1 or nil.
-
#offset ⇒ Object
Current offset of the paginated collection.
-
#out_of_bounds? ⇒ Boolean
Helper method that is true when someone tries to fetch a page with a larger number than the last page.
-
#page(pg, per = self.per_page) ⇒ Object
Create a new collection for the page specified.
-
#page!(pg, per = self.per_page) ⇒ Object
Force this collection to a page specified.
-
#paginate ⇒ Object
return a paginated collection if it isn’t already paginated.
-
#paginate! ⇒ Object
forces pagination of self, raising if already loaded.
-
#paginated? ⇒ Boolean
if the collection has a page parameter.
-
#previous_page ⇒ Object
current_page - 1 or nil if there is no previous page.
-
#previous_page_collection ⇒ Object
New Collection for current_page - 1 or nil.
-
#size_with_pagination ⇒ Object
Loads total entries and calculates the size from that.
- #to_xml(options = {}) ⇒ Object
-
#total_entries_with_pagination ⇒ Object
Always returns the total count regardless of pagination.
-
#total_pages ⇒ Object
Total number of pages.
Instance Attribute Details
#per_page ⇒ Object
Defaults to the model class’ per_page.
26 27 28 |
# File 'lib/active_collection/pagination.rb', line 26 def per_page @per_page ||= params[:per_page] || (model_class.respond_to?(:per_page) && model_class.per_page) || self.class.per_page end |
Class Method Details
.included(mod) ⇒ Object
5 6 7 8 9 10 11 12 13 |
# File 'lib/active_collection/pagination.rb', line 5 def self.included(mod) mod.extend ClassMethods mod.class_eval do alias_method_chain :total_entries, :pagination alias_method_chain :size, :pagination find_scope :pagination_options end end |
Instance Method Details
#as_data_hash ⇒ Object
156 157 158 159 160 161 162 163 164 165 |
# File 'lib/active_collection/pagination.rb', line 156 def as_data_hash data_hash = { "collection" => collection.as_json } if paginated? data_hash["total_entries"] = total_entries data_hash["page"] = current_page data_hash["per_page"] = per_page data_hash["total_pages"] = total_pages end data_hash end |
#current_page ⇒ Object
21 22 23 |
# File 'lib/active_collection/pagination.rb', line 21 def current_page @current_page ||= params.has_key?(:page) ? (params[:page] || 1).to_i : nil end |
#last_page? ⇒ Boolean
true if the collection is the last page.
may load total_entries if not already loaded.
100 101 102 |
# File 'lib/active_collection/pagination.rb', line 100 def last_page? !out_of_bounds? && next_page.nil? end |
#next_page ⇒ Object
current_page + 1 or nil if there is no next page.
loads total_entries if not already loaded.
93 94 95 |
# File 'lib/active_collection/pagination.rb', line 93 def next_page current_page < total_pages ? (current_page + 1) : nil end |
#next_page_collection ⇒ Object
New Collection for current_page + 1 or nil
loads total_entries if not already loaded.
112 113 114 |
# File 'lib/active_collection/pagination.rb', line 112 def next_page_collection next_page ? page(next_page, per_page) : nil end |
#offset ⇒ Object
Current offset of the paginated collection. If we’re on the first page, it is always 0. If we’re on the 2nd page and there are 30 entries per page, the offset is 30. This property is useful if you want to render ordinals side by side with records in the view: simply start with offset + 1.
loads total_entries if not already loaded.
81 82 83 |
# File 'lib/active_collection/pagination.rb', line 81 def offset (current_page - 1) * per_page end |
#out_of_bounds? ⇒ Boolean
Helper method that is true when someone tries to fetch a page with a larger number than the last page. Can be used in combination with flashes and redirecting.
loads total_entries if not already loaded.
71 72 73 |
# File 'lib/active_collection/pagination.rb', line 71 def out_of_bounds? current_page > total_pages end |
#page(pg, per = self.per_page) ⇒ Object
Create a new collection for the page specified
Optionally accepts a per page parameter which will override the default per_page for the new collection (without changing the current collection).
50 51 52 53 54 |
# File 'lib/active_collection/pagination.rb', line 50 def page(pg, per = self.per_page) new_collection = self.class.new(params.merge(:page => pg)) new_collection.per_page = per new_collection end |
#page!(pg, per = self.per_page) ⇒ Object
Force this collection to a page specified
Optionally accepts a per page parameter which will override the per_page for this collection.
60 61 62 63 64 |
# File 'lib/active_collection/pagination.rb', line 60 def page!(pg, per = self.per_page) raise_if_loaded @per_page = per @current_page = pg end |
#paginate ⇒ Object
return a paginated collection if it isn’t already paginated. returns self if already paginated.
139 140 141 |
# File 'lib/active_collection/pagination.rb', line 139 def paginate paginated?? self : page(current_page || 1) end |
#paginate! ⇒ Object
forces pagination of self, raising if already loaded. returns current_page if the collection is now paginated returns nil if already paginated
146 147 148 149 |
# File 'lib/active_collection/pagination.rb', line 146 def paginate! raise_if_loaded current_page ? nil : @current_page = 1 end |
#paginated? ⇒ Boolean
if the collection has a page parameter
152 153 154 |
# File 'lib/active_collection/pagination.rb', line 152 def paginated? current_page && current_page > 0 end |
#previous_page ⇒ Object
current_page - 1 or nil if there is no previous page.
86 87 88 |
# File 'lib/active_collection/pagination.rb', line 86 def previous_page current_page > 1 ? (current_page - 1) : nil end |
#previous_page_collection ⇒ Object
New Collection for current_page - 1 or nil.
105 106 107 |
# File 'lib/active_collection/pagination.rb', line 105 def previous_page_collection previous_page ? page(previous_page, per_page) : nil end |
#size_with_pagination ⇒ Object
Loads total entries and calculates the size from that.
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/active_collection/pagination.rb', line 32 def size_with_pagination if paginated? if out_of_bounds? 0 elsif last_page? size_without_pagination % per_page else per_page end else size_without_pagination end end |
#to_xml(options = {}) ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/active_collection/pagination.rb', line 167 def to_xml( = {}) collect [:indent] ||= 2 xml = [:builder] ||= Builder::XmlMarkup.new(:indent => [:indent]) xml.instruct! unless [:skip_instruct] xml.tag!(table_name) do if paginated? xml.total_entries(total_entries, :type => "integer") xml.page(current_page, :type => "integer") xml.per_page(per_page, :type => "integer") xml.total_pages(total_pages, :type => "integer") end xml.collection(:type => "array") do collection.each do |item| item.to_xml(:indent => [:indent], :builder => xml, :skip_instruct => true) end end end end |
#total_entries_with_pagination ⇒ Object
Always returns the total count regardless of pagination.
Attempts to save a count query if collection is loaded and is the last page.
119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/active_collection/pagination.rb', line 119 def total_entries_with_pagination @total_entries ||= if paginated? if loaded? and length < per_page and (current_page == 1 or length > 0) offset + length else total_entries_without_pagination end else total_entries_without_pagination end end |
#total_pages ⇒ Object
Total number of pages.
133 134 135 |
# File 'lib/active_collection/pagination.rb', line 133 def total_pages @total_pages ||= (total_entries / per_page.to_f).ceil end |