Skip to content

Commit

Permalink
fixed paging metadata, closes #248, see #160
Browse files Browse the repository at this point in the history
  • Loading branch information
cofiem committed Jan 15, 2016
1 parent e984ccf commit 9e15230
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
5 changes: 3 additions & 2 deletions lib/modules/api/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def build(status_symbol = :ok, data = nil, opts = {})
result[:meta][:paging][:total] = opts[:total]
end

# max page
# paging: max page
if !opts[:total].blank? && !opts[:items].blank?
max_page = (opts[:total].to_f / opts[:items].to_f).ceil
opts[:max_page] = max_page
Expand Down Expand Up @@ -270,11 +270,12 @@ def add_paging_and_sorting(new_query, filter_settings, filter_query)

# add paging
new_query = filter_query.query_paging(new_query)
items = filter_query.is_paging_disabled? ? total : filter_query.paging[:items]

# update options
opts.merge!(
page: filter_query.paging[:page],
items: filter_query.paging[:items],
items: items,
total: total
)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/filter/parse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def parse_paging(params, default_page, default_items, max_items)
# POST body
page = params[:paging][:page] if page.blank? && !params[:paging].blank?
items = params[:paging][:items] if items.blank? && !params[:paging].blank?
disable_paging = params[:paging][:disable_paging] if disable_paging.blank? && !params[:paging].blank?
disable_paging = params[:paging][:disable_paging] if !params[:paging].blank? && disable_paging.blank?

# page and items are mutually exclusive with disable_paging
fail CustomErrors::UnprocessableEntityError, 'Page and items are mutually exclusive with disable_paging' if (!page.blank? || !items.blank?) && !disable_paging.blank?
Expand Down
47 changes: 34 additions & 13 deletions spec/acceptance/projects_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
get '/projects' do
let(:authentication_token) { writer_token }
standard_request_options(:get, 'LIST (as confirmed_user)', :ok, {
expected_json_path: 'data/0/name',
data_item_count: 1
})
expected_json_path: 'data/0/name',
data_item_count: 1
})
end

get '/projects' do
Expand Down Expand Up @@ -210,21 +210,42 @@
}
let(:authentication_token) { reader_token }
standard_request_options(:post, 'FILTER (as reader)', :ok, {
expected_json_path: ['data/0/name', 'meta/projection/include'],
data_item_count: 1,
regex_match: /"site_ids"\:\[[0-9]+\]/,
response_body_content: "\"site_ids\":[",
invalid_content: "\"site_ids\":[{\"id\":"
})
expected_json_path: ['data/0/name', 'meta/projection/include'],
data_item_count: 1,
regex_match: /"site_ids"\:\[[0-9]+\]/,
response_body_content: "\"site_ids\":[",
invalid_content: "\"site_ids\":[{\"id\":"
})
end

get '/projects/filter?direction=desc&filter_name=a&filter_partial_match=partial_match_text&items=35&order_by=createdAt&page=1' do
let(:authentication_token) { reader_token }
standard_request_options(:get, 'BASIC FILTER (as reader with filtering, sorting, paging)', :ok, {
expected_json_path: 'meta/paging/current',
data_item_count: 0,
response_body_content: '/projects/filter?direction=desc\u0026filter_name=a\u0026filter_partial_match=partial_match_text\u0026items=35\u0026order_by=createdAt\u0026page=1'
})
expected_json_path: 'meta/paging/current',
data_item_count: 0,
response_body_content: '/projects/filter?direction=desc\u0026filter_name=a\u0026filter_partial_match=partial_match_text\u0026items=35\u0026order_by=createdAt\u0026page=1'
})
end

get '/projects/filter?disable_paging=true' do
let(:authentication_token) { writer_token }
let!(:more_projects) {
# default items per page is 25
29.times do
FactoryGirl.create(:project, creator: @write_permission.user)
end
}

standard_request_options(:get, 'BASIC FILTER (as reader with filtering, paging disabled)', :ok, {
expected_json_path: 'meta/paging/current',
data_item_count: 30,
response_body_content: [
'{"meta":{"status":200,"message":"OK","sorting":{"order_by":"name","direction":"desc"},',
'"paging":{"page":1,"items":30,"total":30,"max_page":1,',
'"current":"http://localhost:3000/projects/filter?direction=desc\u0026disable_paging=true\u0026items=30\u0026order_by=name\u0026page=1",',
'"previous":null,"next":null}}'
]
})
end

end

0 comments on commit 9e15230

Please sign in to comment.