diff options
author | Bob Van Landuyt <bob@vanlanduyt.co> | 2017-09-07 19:08:56 +0200 |
---|---|---|
committer | Bob Van Landuyt <bob@vanlanduyt.co> | 2017-10-04 22:49:41 +0200 |
commit | 518216c0627cb6c4b3db62f10877b44d0e912ddb (patch) | |
tree | 68a4aa0a3301728097e73c6e09c48780a1b52757 /app/serializers | |
parent | 530cf2a2669ea1ee3c41d48a15919f875babefa4 (diff) | |
download | gitlab-ce-518216c0627cb6c4b3db62f10877b44d0e912ddb.tar.gz |
Merge group hierarchies when parents are shared
Diffstat (limited to 'app/serializers')
-rw-r--r-- | app/serializers/base_serializer.rb | 3 | ||||
-rw-r--r-- | app/serializers/group_child_serializer.rb | 37 |
2 files changed, 40 insertions, 0 deletions
diff --git a/app/serializers/base_serializer.rb b/app/serializers/base_serializer.rb index 4e6c15f673b..d78108480d3 100644 --- a/app/serializers/base_serializer.rb +++ b/app/serializers/base_serializer.rb @@ -1,5 +1,8 @@ class BaseSerializer + attr_reader :parameters + def initialize(parameters = {}) + @parameters = parameters @request = EntityRequest.new(parameters) end diff --git a/app/serializers/group_child_serializer.rb b/app/serializers/group_child_serializer.rb index fbf4a6783b9..ed84c3ae1d7 100644 --- a/app/serializers/group_child_serializer.rb +++ b/app/serializers/group_child_serializer.rb @@ -1,5 +1,42 @@ class GroupChildSerializer < BaseSerializer include WithPagination + attr_reader :hierarchy_root + entity GroupChildEntity + + def expand_hierarchy(hierarchy_root) + @hierarchy_root = hierarchy_root + self + end + + def represent(resource, opts = {}, entity_class = nil) + if hierarchy_root.present? + represent_hierarchies(resource, opts) + else + super(resource, opts) + end + end + + def represent_hierarchies(children, opts) + if children.is_a?(GroupHierarchy) + represent_hierarchy(children.hierarchy(hierarchy_root), opts) + else + children.map { |child| represent_hierarchy(child.hierarchy(hierarchy_root), opts) } + end + end + + def represent_hierarchy(hierarchy, opts) + serializer = self.class.new(parameters) + + result = if hierarchy.is_a?(Hash) + parent = hierarchy.keys.first + serializer.represent(parent, opts) + .merge(children: [serializer.represent_hierarchy(hierarchy[parent], opts)]) + else + serializer.represent(hierarchy, opts) + end + + result + end end |