Class: Locomotive::Steam::Models::Pager
- Inherits:
-
Object
- Object
- Locomotive::Steam::Models::Pager
- Defined in:
- lib/locomotive/steam/models/pager.rb
Constant Summary collapse
- DEFAULT_PER_PAGE =
10.freeze
Instance Attribute Summary collapse
-
#collection ⇒ Object
readonly
Returns the value of attribute collection.
-
#current_page ⇒ Object
readonly
Returns the value of attribute current_page.
-
#per_page ⇒ Object
readonly
Returns the value of attribute per_page.
-
#total_entries ⇒ Object
readonly
Returns the value of attribute total_entries.
-
#total_pages ⇒ Object
readonly
Returns the value of attribute total_pages.
Instance Method Summary collapse
-
#initialize(source, page, per_page) ⇒ Pager
constructor
A new instance of Pager.
- #next_page ⇒ Object
- #previous_page ⇒ Object
- #to_liquid ⇒ Object
Constructor Details
#initialize(source, page, per_page) ⇒ Pager
Returns a new instance of Pager.
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/locomotive/steam/models/pager.rb', line 10 def initialize(source, page, per_page) @current_page, @per_page = page || 1, per_page || DEFAULT_PER_PAGE @current_page = 1 if @current_page < 1 @total_entries = source.count @total_pages = (@total_entries.to_f / @per_page).ceil index = (@current_page - 1) * @per_page offset = (index + @per_page - 1) >= @total_entries ? @total_entries : (index + @per_page - 1) @collection = paginate(source, index, offset) end |
Instance Attribute Details
#collection ⇒ Object (readonly)
Returns the value of attribute collection.
8 9 10 |
# File 'lib/locomotive/steam/models/pager.rb', line 8 def collection @collection end |
#current_page ⇒ Object (readonly)
Returns the value of attribute current_page.
8 9 10 |
# File 'lib/locomotive/steam/models/pager.rb', line 8 def current_page @current_page end |
#per_page ⇒ Object (readonly)
Returns the value of attribute per_page.
8 9 10 |
# File 'lib/locomotive/steam/models/pager.rb', line 8 def per_page @per_page end |
#total_entries ⇒ Object (readonly)
Returns the value of attribute total_entries.
8 9 10 |
# File 'lib/locomotive/steam/models/pager.rb', line 8 def total_entries @total_entries end |
#total_pages ⇒ Object (readonly)
Returns the value of attribute total_pages.
8 9 10 |
# File 'lib/locomotive/steam/models/pager.rb', line 8 def total_pages @total_pages end |
Instance Method Details
#next_page ⇒ Object
27 28 29 |
# File 'lib/locomotive/steam/models/pager.rb', line 27 def next_page current_page >= total_pages ? nil : current_page + 1 end |
#previous_page ⇒ Object
23 24 25 |
# File 'lib/locomotive/steam/models/pager.rb', line 23 def previous_page current_page <= 1 ? nil : current_page - 1 end |
#to_liquid ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/locomotive/steam/models/pager.rb', line 31 def to_liquid { collection: collection, current_page: current_page, per_page: per_page, previous_page: previous_page, next_page: next_page, total_entries: total_entries, total_pages: total_pages } end |