diff options
author | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2017-02-02 13:10:31 +0100 |
---|---|---|
committer | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2017-02-02 13:10:31 +0100 |
commit | ac06070147f23909dfb5d3468a17a29e6b0cd447 (patch) | |
tree | e8cf19ff01cfa4c35b8a8237b7ca90894d264a43 /app/serializers | |
parent | 40a824357c700280f3d2f8e2cda2fabc65af7f69 (diff) | |
download | gitlab-ce-ac06070147f23909dfb5d3468a17a29e6b0cd447.tar.gz |
Use serializer to group environments into folders
Diffstat (limited to 'app/serializers')
-rw-r--r-- | app/serializers/environment_serializer.rb | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/app/serializers/environment_serializer.rb b/app/serializers/environment_serializer.rb index 91955542f25..8624392e07f 100644 --- a/app/serializers/environment_serializer.rb +++ b/app/serializers/environment_serializer.rb @@ -1,3 +1,42 @@ class EnvironmentSerializer < BaseSerializer + Struct.new('Item', :name, :size, :id, :latest) + entity EnvironmentEntity + + def with_folders + tap { @itemize = true } + end + + def itemized? + @itemize + end + + def represent(resource, opts = {}) + # resource = paginate(resource) if paginated? + + 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') + + environments = resource.where(id: items.map(&:last)) + + items.zip(environments).map do |item| + Struct::Item.new(*item.flatten) + end + end end |