Module: RailsSimpleEventSourcing::EventsHelper

Defined in:
app/helpers/rails_simple_event_sourcing/events_helper.rb

Instance Method Summary collapse

Instance Method Details

#pagination_window(current_page, total_pages, window: 2) ⇒ Object

rubocop:disable Metrics/MethodLength



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/helpers/rails_simple_event_sourcing/events_helper.rb', line 5

def pagination_window(current_page, total_pages, window: 2) # rubocop:disable Metrics/MethodLength
  return [1] if total_pages <= 1

  pages = []
  left = [current_page - window, 1].max
  right = [current_page + window, total_pages].min

  if left > 1
    pages << 1
    pages << :gap if left > 2
  end

  (left..right).each { |p| pages << p }

  if right < total_pages
    pages << :gap if right < total_pages - 1
    pages << total_pages
  end

  pages
end