module Flame::Pagination::ForController
Mixin for controllers
Constants
- DEFAULT_PER_PAGE
- PER_PAGE_OPTIONS
Attributes
pages_enum[R]
Protected Instance Methods
execute(action)
click to toggle source
Calls superclass method
# File lib/flame/pagination/for_controller.rb, line 26 def execute(action) @action = action @previous_saved_per_page = saved_per_page per_page_change_if_different super end
Private Instance Methods
current_page()
click to toggle source
# File lib/flame/pagination/for_controller.rb, line 49 def current_page @current_page ||= (params[:page] || 1).to_i end
current_per_page()
click to toggle source
# File lib/flame/pagination/for_controller.rb, line 53 def current_per_page return @current_per_page if defined? @current_per_page per_page_index = PER_PAGE_OPTIONS.index( params.fetch(:per_page, saved_per_page).to_i ) @current_per_page = per_page_index ? PER_PAGE_OPTIONS[per_page_index] : DEFAULT_PER_PAGE end
halt_redirect_to_page(page, action = @action)
click to toggle source
# File lib/flame/pagination/for_controller.rb, line 72 def halt_redirect_to_page(page, action = @action) halt redirect action, **params_with_page(page, current_per_page) end
page_after_per_page_change()
click to toggle source
# File lib/flame/pagination/for_controller.rb, line 76 def page_after_per_page_change previous_per_page = @previous_saved_per_page || DEFAULT_PER_PAGE Rational((current_page - 1) * previous_per_page + 1, current_per_page).ceil end
paginate!(form, near_edge: 0, arround_current: 2)
click to toggle source
# File lib/flame/pagination/for_controller.rb, line 40 def paginate!(form, near_edge: 0, arround_current: 2) available_current_page, @pages_count = form.paginate! current_page, per_page: current_per_page halt_redirect_to_page available_current_page if available_current_page != current_page @pages_enum = Pages.new(current_page, @pages_count, near_edge, arround_current).to_enum end
params_with_page(page, per_page)
click to toggle source
# File lib/flame/pagination/for_controller.rb, line 64 def params_with_page(page, per_page) params.merge(page: page, per_page: per_page) end
per_page_change_if_different()
click to toggle source
# File lib/flame/pagination/for_controller.rb, line 81 def per_page_change_if_different return if @previous_saved_per_page == current_per_page authenticated.session&.update( options: Sequel.pg_jsonb_op(:options).concat( self.class.records_key_for_session => current_per_page ) ) return unless authenticated.account return unless current_page > 1 halt_redirect_to_page page_after_per_page_change, @action end
saved_per_page()
click to toggle source
# File lib/flame/pagination/for_controller.rb, line 68 def saved_per_page authenticated.session&.options&.[](self.class.records_key_for_session) end