Module: SnFoil::Controller::Concerns::IndexControllerConcern

Extended by:
ActiveSupport::Concern
Included in:
Base
Defined in:
lib/sn_foil/controller/concerns/index_controller_concern.rb

Instance Method Summary collapse

Instance Method Details

#index(**options) ⇒ Object



16
17
18
19
20
# File 'lib/sn_foil/controller/concerns/index_controller_concern.rb', line 16

def index(**options)
  options = setup_index(**options)
  results = process_index(**options)
  render_index(results, **options)
end

#meta(results, **options) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/sn_foil/controller/concerns/index_controller_concern.rb', line 52

def meta(results, **options)
  results = paginate(results, **options)
  total_pages = results.respond_to?(:total_pages) ? results.total_pages : nil
  total_count = results.respond_to?(:total_count) ? results.total_count : nil

  {
    page: page(**options),
    pages: total_pages,
    total: total_count,
    per: per_page(**options)
  }
end

#page(**options) ⇒ Object



41
42
43
# File 'lib/sn_foil/controller/concerns/index_controller_concern.rb', line 41

def page(**options)
  (options.dig(:params, :page) || 1).to_i
end

#paginate(results, **options) ⇒ Object



34
35
36
37
38
39
# File 'lib/sn_foil/controller/concerns/index_controller_concern.rb', line 34

def paginate(results, **options)
  return results unless results.respond_to?(:page)

  results.page(page(**options))
         .per(per_page(**options))
end

#per_page(**options) ⇒ Object



45
46
47
48
49
50
# File 'lib/sn_foil/controller/concerns/index_controller_concern.rb', line 45

def per_page(**options)
  per_page_param = (options.dig(:params, :per_page) || 10).to_i
  return 1000 if per_page_param.zero? || per_page_param > 1000

  per_page_param
end

#process_index(**options) ⇒ Object



26
27
28
# File 'lib/sn_foil/controller/concerns/index_controller_concern.rb', line 26

def process_index(**options)
  current_context(**options).index(options)
end

#render_index(results, **options) ⇒ Object



30
31
32
# File 'lib/sn_foil/controller/concerns/index_controller_concern.rb', line 30

def render_index(results, **options)
  render paginate(results, **options), meta: meta(results, options)
end

#setup_index(**options) ⇒ Object



22
23
24
# File 'lib/sn_foil/controller/concerns/index_controller_concern.rb', line 22

def setup_index(**options)
  setup_options(**options)
end