Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/will_paginate/array.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#member_class ⇒ Object
Returns the value of attribute member_class.
-
#origin ⇒ Object
Returns the value of attribute origin.
-
#origin_attribute ⇒ Object
Returns the value of attribute origin_attribute.
Instance Method Summary collapse
-
#paginate(options = {}) ⇒ Object
Paginates a static array (extracting a subset of it).
-
#paginate_with_hobo_metadata(*args, &block) ⇒ Object
Hobo Extension.
-
#to_url_path ⇒ Object
Hobo Extension.
-
#typed_id ⇒ Object
Hobo Extension.
Instance Attribute Details
#member_class ⇒ Object
Returns the value of attribute member_class.
34 35 36 |
# File 'lib/will_paginate/array.rb', line 34 def member_class @member_class end |
#origin ⇒ Object
Returns the value of attribute origin.
34 35 36 |
# File 'lib/will_paginate/array.rb', line 34 def origin @origin end |
#origin_attribute ⇒ Object
Returns the value of attribute origin_attribute.
34 35 36 |
# File 'lib/will_paginate/array.rb', line 34 def origin_attribute @origin_attribute end |
Instance Method Details
#paginate(options = {}) ⇒ Object
Paginates a static array (extracting a subset of it). The result is a WillPaginate::Collection instance, which is an array with a few more properties about its paginated state.
Parameters:
-
:page- current page, defaults to 1 -
:per_page- limit of items per page, defaults to 30 -
:total_entries- total number of items in the array, defaults toarray.length(obviously)
Example:
arr = ['a', 'b', 'c', 'd', 'e']
paged = arr.paginate(:per_page => 2) #-> ['a', 'b']
paged.total_entries #-> 5
arr.paginate(:page => 2, :per_page => 2) #-> ['c', 'd']
arr.paginate(:page => 3, :per_page => 2) #-> ['e']
This method was originally suggested by Desi McAdam and later proved to be the most useful method of will_paginate library.
24 25 26 27 28 29 30 31 32 |
# File 'lib/will_paginate/array.rb', line 24 def paginate( = {}) page = [:page] || 1 per_page = [:per_page] || WillPaginate.per_page total = [:total_entries] || self.length WillPaginate::Collection.create(page, per_page, total) do |pager| pager.replace self[pager.offset, pager.per_page].to_a end end |
#paginate_with_hobo_metadata(*args, &block) ⇒ Object
Hobo Extension
48 49 50 51 52 53 54 |
# File 'lib/will_paginate/array.rb', line 48 def (*args, &block) collection = (*args, &block) collection.member_class = member_class collection.origin = try.proxy_owner collection.origin_attribute = try.proxy_association._?.reflection._?.name collection end |
#to_url_path ⇒ Object
Hobo Extension
37 38 39 40 |
# File 'lib/will_paginate/array.rb', line 37 def to_url_path base_path = origin.try.to_url_path "#{base_path}/#{origin_attribute}" unless base_path.blank? end |
#typed_id ⇒ Object
Hobo Extension
43 44 45 |
# File 'lib/will_paginate/array.rb', line 43 def typed_id origin and origin_id = origin.try.typed_id and "#{origin_id}:#{origin_attribute}" end |