Class: Greedo::Paginator
- Inherits:
-
Object
- Object
- Greedo::Paginator
- Defined in:
- lib/greedo/paginator.rb
Instance Attribute Summary collapse
-
#order ⇒ Object
readonly
Returns the value of attribute order.
-
#order_by ⇒ Object
readonly
Returns the value of attribute order_by.
-
#page ⇒ Object
readonly
Returns the value of attribute page.
-
#per_page ⇒ Object
readonly
Returns the value of attribute per_page.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(scope, page:, per_page:, order: nil, order_by: nil) ⇒ Paginator
constructor
A new instance of Paginator.
- #records ⇒ Object
- #show? ⇒ Boolean
Constructor Details
#initialize(scope, page:, per_page:, order: nil, order_by: nil) ⇒ Paginator
Returns a new instance of Paginator.
17 18 19 20 21 22 23 |
# File 'lib/greedo/paginator.rb', line 17 def initialize(scope, page:, per_page:, order: nil, order_by: nil) @scope = scope @page = page @per_page = per_page @order = order @order_by = order_by end |
Instance Attribute Details
#order ⇒ Object (readonly)
Returns the value of attribute order.
6 7 8 |
# File 'lib/greedo/paginator.rb', line 6 def order @order end |
#order_by ⇒ Object (readonly)
Returns the value of attribute order_by.
6 7 8 |
# File 'lib/greedo/paginator.rb', line 6 def order_by @order_by end |
#page ⇒ Object (readonly)
Returns the value of attribute page.
6 7 8 |
# File 'lib/greedo/paginator.rb', line 6 def page @page end |
#per_page ⇒ Object (readonly)
Returns the value of attribute per_page.
6 7 8 |
# File 'lib/greedo/paginator.rb', line 6 def per_page @per_page end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
6 7 8 |
# File 'lib/greedo/paginator.rb', line 6 def scope @scope end |
Class Method Details
.build(scope, page: 1, per_page: 20, order_by: nil) ⇒ Object
8 9 10 11 12 13 14 15 |
# File 'lib/greedo/paginator.rb', line 8 def self.build(scope, page: 1, per_page: 20, order_by: nil) return ArrayPaginator.new(scope, order_by.order, order_by.sort) if scope.is_a?(Array) Paginator.new(scope, page: page, per_page: per_page, order: order_by.order, order_by: order_by.sort) end |
Instance Method Details
#records ⇒ Object
25 26 27 28 29 |
# File 'lib/greedo/paginator.rb', line 25 def records paginated = scope.paginate(page: page, per_page: per_page) order_by_fields = [order_by].flatten.map { |field| "#{field} #{order}" }.join(", ") order_by ? paginated.reorder(order_by_fields) : paginated end |
#show? ⇒ Boolean
31 32 33 |
# File 'lib/greedo/paginator.rb', line 31 def show? scope.length > per_page end |