Class: Paginate::Base
- Inherits:
-
Object
- Object
- Paginate::Base
- Defined in:
- lib/paginate/base.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
-
#scope ⇒ Object
Returns the value of attribute scope.
Instance Method Summary collapse
- #collection_size ⇒ Object
-
#initialize(scope, options = {}) ⇒ Base
constructor
A new instance of Base.
- #limit ⇒ Object
- #next_page? ⇒ Boolean
- #offset ⇒ Object
- #page ⇒ Object
- #previous_page? ⇒ Boolean
- #to_options ⇒ Object
- #to_scope ⇒ Object
Constructor Details
#initialize(scope, options = {}) ⇒ Base
Returns a new instance of Base.
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/paginate/base.rb', line 6 def initialize(scope, = {}) @scope = scope if .kind_of?(Hash) @options = else @options = {page: .to_i} end @options.reverse_merge!(Paginate.configuration.to_hash) end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
3 4 5 |
# File 'lib/paginate/base.rb', line 3 def @options end |
#scope ⇒ Object
Returns the value of attribute scope.
4 5 6 |
# File 'lib/paginate/base.rb', line 4 def scope @scope end |
Instance Method Details
#collection_size ⇒ Object
18 19 20 |
# File 'lib/paginate/base.rb', line 18 def collection_size @collection_size ||= [:collection].size end |
#limit ⇒ Object
38 39 40 |
# File 'lib/paginate/base.rb', line 38 def limit [[:size], 10].compact.first.to_i + 1 end |
#next_page? ⇒ Boolean
22 23 24 |
# File 'lib/paginate/base.rb', line 22 def next_page? collection_size > [:size] end |
#offset ⇒ Object
34 35 36 |
# File 'lib/paginate/base.rb', line 34 def offset (page - 1) * (limit - 1) end |
#page ⇒ Object
30 31 32 |
# File 'lib/paginate/base.rb', line 30 def page [1, .fetch(:page, 1).to_i].max end |
#previous_page? ⇒ Boolean
26 27 28 |
# File 'lib/paginate/base.rb', line 26 def previous_page? page > 1 end |
#to_options ⇒ Object
42 43 44 |
# File 'lib/paginate/base.rb', line 42 def {limit: limit, offset: offset} end |
#to_scope ⇒ Object
46 47 48 49 50 |
# File 'lib/paginate/base.rb', line 46 def to_scope scope .limit(limit) .offset(offset) end |