Class: RailsSimpleEventSourcing::Paginator
- Inherits:
-
Object
- Object
- RailsSimpleEventSourcing::Paginator
- Defined in:
- lib/rails_simple_event_sourcing/paginator.rb
Instance Attribute Summary collapse
-
#current_page ⇒ Object
readonly
Returns the value of attribute current_page.
-
#per_page ⇒ Object
readonly
Returns the value of attribute per_page.
-
#total_count ⇒ Object
readonly
Returns the value of attribute total_count.
-
#total_pages ⇒ Object
readonly
Returns the value of attribute total_pages.
Instance Method Summary collapse
-
#initialize(scope:, page:, per_page:) ⇒ Paginator
constructor
A new instance of Paginator.
- #records ⇒ Object
Constructor Details
#initialize(scope:, page:, per_page:) ⇒ Paginator
Returns a new instance of Paginator.
7 8 9 10 11 12 13 |
# File 'lib/rails_simple_event_sourcing/paginator.rb', line 7 def initialize(scope:, page:, per_page:) @scope = scope @per_page = per_page @total_count = scope.count @total_pages = [(@total_count.to_f / per_page).ceil, 1].max @current_page = (page.presence || 1).to_i.clamp(1, @total_pages) end |
Instance Attribute Details
#current_page ⇒ Object (readonly)
Returns the value of attribute current_page.
5 6 7 |
# File 'lib/rails_simple_event_sourcing/paginator.rb', line 5 def current_page @current_page end |
#per_page ⇒ Object (readonly)
Returns the value of attribute per_page.
5 6 7 |
# File 'lib/rails_simple_event_sourcing/paginator.rb', line 5 def per_page @per_page end |
#total_count ⇒ Object (readonly)
Returns the value of attribute total_count.
5 6 7 |
# File 'lib/rails_simple_event_sourcing/paginator.rb', line 5 def total_count @total_count end |
#total_pages ⇒ Object (readonly)
Returns the value of attribute total_pages.
5 6 7 |
# File 'lib/rails_simple_event_sourcing/paginator.rb', line 5 def total_pages @total_pages end |
Instance Method Details
#records ⇒ Object
15 16 17 |
# File 'lib/rails_simple_event_sourcing/paginator.rb', line 15 def records @scope.offset((@current_page - 1) * @per_page).limit(@per_page) end |