2016-10-28 08:32:07 -04:00
|
|
|
class EnvironmentSerializer < BaseSerializer
|
2017-02-07 07:11:15 -05:00
|
|
|
Item = Struct.new(:name, :size, :latest)
|
2017-02-02 07:10:31 -05:00
|
|
|
|
2016-10-28 08:32:07 -04:00
|
|
|
entity EnvironmentEntity
|
2017-02-02 07:10:31 -05:00
|
|
|
|
2017-02-03 06:03:57 -05:00
|
|
|
def within_folders
|
2017-02-02 07:10:31 -05:00
|
|
|
tap { @itemize = true }
|
|
|
|
end
|
|
|
|
|
2017-02-02 08:37:11 -05:00
|
|
|
def with_pagination(request, response)
|
2017-02-06 08:47:56 -05:00
|
|
|
tap { @paginator = Gitlab::Serializer::Pagination.new(request, response) }
|
2017-02-02 08:37:11 -05:00
|
|
|
end
|
|
|
|
|
2017-02-02 07:10:31 -05:00
|
|
|
def itemized?
|
|
|
|
@itemize
|
|
|
|
end
|
|
|
|
|
2017-02-02 08:37:11 -05:00
|
|
|
def paginated?
|
2017-02-07 07:13:33 -05:00
|
|
|
@paginator.present?
|
2017-02-02 08:37:11 -05:00
|
|
|
end
|
|
|
|
|
2017-02-02 07:10:31 -05:00
|
|
|
def represent(resource, opts = {})
|
2017-02-02 08:37:11 -05:00
|
|
|
resource = @paginator.paginate(resource) if paginated?
|
2017-02-02 07:10:31 -05:00
|
|
|
|
|
|
|
if itemized?
|
|
|
|
itemize(resource).map do |item|
|
|
|
|
{ name: item.name,
|
|
|
|
size: item.size,
|
|
|
|
latest: super(item.latest, opts) }
|
|
|
|
end
|
|
|
|
else
|
|
|
|
super(resource, opts)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def itemize(resource)
|
|
|
|
items = resource.group(:item_name).order('item_name ASC')
|
|
|
|
.pluck('COALESCE(environment_type, name) AS item_name',
|
|
|
|
'COUNT(*) AS environments_count',
|
|
|
|
'MAX(id) AS last_environment_id')
|
|
|
|
|
2017-02-07 07:11:15 -05:00
|
|
|
environments = resource.where(id: items.map(&:last)).index_by(&:id)
|
2017-02-02 07:10:31 -05:00
|
|
|
|
2017-02-07 07:11:15 -05:00
|
|
|
items.map do |name, size, id|
|
|
|
|
Item.new(name, size, environments[id])
|
2017-02-02 07:10:31 -05:00
|
|
|
end
|
|
|
|
end
|
2016-10-28 08:32:07 -04:00
|
|
|
end
|