Module: MonsterQueries::ActionController
- Defined in:
- lib/monster_queries/action_controller.rb
Instance Method Summary collapse
- #index_params ⇒ Object
-
#render_paginated(target, method, attrs) ⇒ Object
AB - We should phase out {sort} in favour of {order} {by} for more finetune control.
- #render_paginated_json(target, method, attrs) ⇒ Object
- #set_pagination_headers(count_json) ⇒ Object
Instance Method Details
#index_params ⇒ Object
50 51 52 |
# File 'lib/monster_queries/action_controller.rb', line 50 def index_params params.permit :page, :search, :sort end |
#render_paginated(target, method, attrs) ⇒ Object
AB - We should phase out {sort} in favour of {order} {by} for more finetune control
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/monster_queries/action_controller.rb', line 28 def render_paginated target, method, attrs if attrs.key?(:sort) name,dir = attrs[:sort].split ',' v = [name] v.push dir.upcase if dir v = v.join ' ' attrs[:sort] = v attrs[:order] = name attrs[:by] = (dir || 'ASC').upcase end attrs[:count] = true count_json = target.send method,attrs set_pagination_headers count_json attrs.delete(:count) target.send method, attrs end |
#render_paginated_json(target, method, attrs) ⇒ Object
45 46 47 48 |
# File 'lib/monster_queries/action_controller.rb', line 45 def render_paginated_json target, method, attrs json = render_paginated target, method, attrs render json: json end |
#set_pagination_headers(count_json) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/monster_queries/action_controller.rb', line 3 def set_pagination_headers count_json json = JSON.parse(count_json) json = json.first if json.is_a?(Array) page = json['page'] total = json['total_entries'] per_page = json['per_page'] || 20 pages = (total.to_f / per_page).ceil end_entry = page * per_page end_entry = total if end_entry > total headers["X-Pagination"] = { page: page, total: total, total_pages: pages, first_page: page == 1, last_page: page >= pages, previous_page: page - 1, next_page: page + 1, out_of_bounds: page < 1 || page > pages, first_entry: (page - 1) * per_page + 1, end_entry: end_entry }.to_json end |