gitlab-org--gitlab-foss/app/serializers/environment_serializer.rb

56 lines
1.3 KiB
Ruby
Raw Normal View History

2016-10-28 08:32:07 -04:00
class EnvironmentSerializer < BaseSerializer
Item = Struct.new(:name, :size, :latest)
2016-10-28 08:32:07 -04:00
entity EnvironmentEntity
def within_folders
tap { @itemize = true }
end
def with_pagination(request, response)
tap { @paginator = Gitlab::Serializer::Pagination.new(request, response) }
end
def itemized?
@itemize
end
def paginated?
@paginator.present?
end
def represent(resource, opts = {})
if itemized?
itemize(resource).map do |item|
{ name: item.name,
size: item.size,
latest: super(item.latest, opts) }
end
else
resource = @paginator.paginate(resource) if paginated?
super(resource, opts)
end
end
private
def itemize(resource)
items = resource.order('folder ASC')
.group('COALESCE(environment_type, name)')
.select('COALESCE(environment_type, name) AS folder',
'COUNT(*) AS size', 'MAX(id) AS last_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?
environments = resource.where(id: items.map(&:last_id)).index_by(&:id)
items.map do |item|
Item.new(item.folder, item.size, environments[item.last_id])
end
end
2016-10-28 08:32:07 -04:00
end