Fix pagination headers in grouped environments API
This commit is contained in:
parent
482e7ff012
commit
d0d94e4f10
|
@ -20,8 +20,6 @@ class EnvironmentSerializer < BaseSerializer
|
||||||
end
|
end
|
||||||
|
|
||||||
def represent(resource, opts = {})
|
def represent(resource, opts = {})
|
||||||
resource = @paginator.paginate(resource) if paginated?
|
|
||||||
|
|
||||||
if itemized?
|
if itemized?
|
||||||
itemize(resource).map do |item|
|
itemize(resource).map do |item|
|
||||||
{ name: item.name,
|
{ name: item.name,
|
||||||
|
@ -29,6 +27,8 @@ class EnvironmentSerializer < BaseSerializer
|
||||||
latest: super(item.latest, opts) }
|
latest: super(item.latest, opts) }
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
|
resource = @paginator.paginate(resource) if paginated?
|
||||||
|
|
||||||
super(resource, opts)
|
super(resource, opts)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -36,15 +36,20 @@ class EnvironmentSerializer < BaseSerializer
|
||||||
private
|
private
|
||||||
|
|
||||||
def itemize(resource)
|
def itemize(resource)
|
||||||
items = resource.group(:item_name).order('item_name ASC')
|
items = resource.order('folder_name ASC')
|
||||||
.pluck('COALESCE(environment_type, name) AS item_name',
|
.group('COALESCE(environment_type, name)')
|
||||||
'COUNT(*) AS environments_count',
|
.select('COALESCE(environment_type, name) AS folder_name',
|
||||||
'MAX(id) AS last_environment_id')
|
'COUNT(*) AS size', 'MAX(id) AS last_id')
|
||||||
|
|
||||||
environments = resource.where(id: items.map(&:last)).index_by(&:id)
|
# It makes a difference when you call `paginate` method, because
|
||||||
|
# although `page` is effective at the end, it calls counting methods
|
||||||
|
# immediately.
|
||||||
|
items = @paginator.paginate(items) if paginated?
|
||||||
|
|
||||||
items.map do |name, size, id|
|
environments = resource.where(id: items.map(&:last_id)).index_by(&:id)
|
||||||
Item.new(name, size, environments[id])
|
|
||||||
|
items.map do |item|
|
||||||
|
Item.new(item.folder_name, item.size, environments[item.last_id])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -181,6 +181,17 @@ describe EnvironmentSerializer do
|
||||||
expect(subject.first[:name]).to eq 'production'
|
expect(subject.first[:name]).to eq 'production'
|
||||||
expect(subject.second[:name]).to eq 'staging'
|
expect(subject.second[:name]).to eq 'staging'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'appends correct total page count header' do
|
||||||
|
expect(subject).not_to be_empty
|
||||||
|
expect(response).to have_received(:[]=).with('X-Total', '3')
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'appends correct page count headers' do
|
||||||
|
expect(subject).not_to be_empty
|
||||||
|
expect(response).to have_received(:[]=).with('X-Total-Pages', '2')
|
||||||
|
expect(response).to have_received(:[]=).with('X-Per-Page', '2')
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue