Module: QueryletRails::Controller::Queryable

Extended by:
ActiveSupport::Concern
Defined in:
lib/querylet_rails/controller/queryable.rb

Instance Method Summary collapse

Instance Method Details

#index_paramsObject



56
57
58
# File 'lib/querylet_rails/controller/queryable.rb', line 56

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



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/querylet_rails/controller/queryable.rb', line 31

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

  # Use the same query but to count all the records
  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



51
52
53
54
# File 'lib/querylet_rails/controller/queryable.rb', line 51

def render_paginated_json target, method, attrs
  json = render_paginated target, method, attrs
  render json: json
end

#set_pagination_headers(count_json) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/querylet_rails/controller/queryable.rb', line 6

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