Class: Leaflet::Collection
- Inherits:
-
Array
- Object
- Array
- Leaflet::Collection
- Defined in:
- lib/leaflet/collection.rb,
lib/leaflet/decoration.rb
Constant Summary collapse
- PartialCollectionCannotBePaginated =
Class.new(StandardError)
Instance Attribute Summary collapse
-
#success ⇒ Object
Returns the value of attribute success.
Instance Method Summary collapse
- #as_json(options = {}) ⇒ Object
- #current_page ⇒ Object
-
#decorate ⇒ Object
Basic support for decorating collections just like draper does.
- #first_page? ⇒ Boolean
-
#initialize(*args) ⇒ Collection
constructor
–––––––––––––– Initialization ––––––––––––––.
- #inspect ⇒ Object
- #last_page? ⇒ Boolean
- #next_page ⇒ Object
-
#offset ⇒ Object
(also: #offset_value)
–––––––––––––––––––– Public Class Getters ––––––––––––––––––––.
- #paginate(options = {}) ⇒ Object
- #per_page ⇒ Object (also: #limit_value)
- #previous_page ⇒ Object
-
#success? ⇒ Boolean
––––––––––––––––––––––– Public Instance Getters –––––––––––––––––––––––.
- #total_entries ⇒ Object (also: #total_count)
- #total_pages ⇒ Object
Constructor Details
#initialize(*args) ⇒ Collection
––––––––––––––Initialization ––––––––––––––
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/leaflet/collection.rb', line 15 def initialize(*args) @success = true if args.size == 1 # An Array was passed in. "Convert" it to a Collection. replace args.shift elsif args.size == 2 # Someone is building a custom Paginator, let's fetch the data. = args.pop records = Array(args.shift) replace records @current_page = [:page] @per_page = [:per_page] @total_entries = [:total] || self.size else raise ArgumentError end end |
Instance Attribute Details
#success ⇒ Object
Returns the value of attribute success.
9 10 11 |
# File 'lib/leaflet/collection.rb', line 9 def success @success end |
Instance Method Details
#as_json(options = {}) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/leaflet/collection.rb', line 106 def as_json( = {}) { total_entries: total_entries, total_count: total_count, per_page: per_page, limit_value: limit_value, current_page: current_page, offset: offset, offset_value: offset_value, total_pages: total_pages, records: self.to_a } end |
#current_page ⇒ Object
62 63 64 65 66 |
# File 'lib/leaflet/collection.rb', line 62 def current_page Positify.it(max: total_pages) { @current_page ||= 1 } end |
#decorate ⇒ Object
Basic support for decorating collections just like draper does. Could be improved with more options using something like this: github.com/drapergem/draper/blob/4b933ef39d252ecfe93c573a072633be545c49fb/lib/draper/collection_decorator.rb Also a #decorated? would be nice. But the following works in 99% of the cases as is.
9 10 11 12 13 |
# File 'lib/leaflet/decoration.rb', line 9 def decorate result = dup result.map!(&:decorate) result end |
#first_page? ⇒ Boolean
98 99 100 |
# File 'lib/leaflet/collection.rb', line 98 def first_page? current_page == 1 end |
#inspect ⇒ Object
83 84 85 86 87 88 |
# File 'lib/leaflet/collection.rb', line 83 def inspect result = ['#<Collection with'] result << (complete? ? "#{self.size} records" : "#{self.size}/#{total_entries} records") result << "on page #{current_page}/#{total_pages} (#{per_page} per page): #{self.to_a}>" result.join(' ') end |
#last_page? ⇒ Boolean
102 103 104 |
# File 'lib/leaflet/collection.rb', line 102 def last_page? current_page == total_pages end |
#next_page ⇒ Object
94 95 96 |
# File 'lib/leaflet/collection.rb', line 94 def next_page current_page < total_pages ? (current_page + 1) : nil end |
#offset ⇒ Object Also known as: offset_value
––––––––––––––––––––Public Class Getters ––––––––––––––––––––
72 73 74 |
# File 'lib/leaflet/collection.rb', line 72 def offset (current_page - 1) * per_page end |
#paginate(options = {}) ⇒ Object
36 37 38 39 40 |
# File 'lib/leaflet/collection.rb', line 36 def paginate( = {}) raise PartialCollectionCannotBePaginated unless complete? preliminary = self.class.new [], .merge!(total: self.size) result = self.class.new self[preliminary.offset, preliminary.per_page].to_a, end |
#per_page ⇒ Object Also known as: limit_value
55 56 57 58 59 |
# File 'lib/leaflet/collection.rb', line 55 def per_page Positify.it(max: Leaflet.config.max_per_page) { @per_page ||= Leaflet.config.default_per_page } end |
#previous_page ⇒ Object
90 91 92 |
# File 'lib/leaflet/collection.rb', line 90 def previous_page current_page > 1 ? (current_page - 1) : nil end |
#success? ⇒ Boolean
–––––––––––––––––––––––Public Instance Getters –––––––––––––––––––––––
46 47 48 |
# File 'lib/leaflet/collection.rb', line 46 def success? !!success end |
#total_entries ⇒ Object Also known as: total_count
50 51 52 |
# File 'lib/leaflet/collection.rb', line 50 def total_entries (@total_entries ||= self.size).to_i.abs end |
#total_pages ⇒ Object
77 78 79 80 81 |
# File 'lib/leaflet/collection.rb', line 77 def total_pages Positify.it { total_entries.zero? ? 1 : (total_entries / per_page.to_f).ceil } end |