diff options
author | Bob Van Landuyt <bob@vanlanduyt.co> | 2017-10-05 13:12:59 +0200 |
---|---|---|
committer | Bob Van Landuyt <bob@vanlanduyt.co> | 2017-10-05 13:12:59 +0200 |
commit | 082c28cce697682b2e13e35eef3ce9f3ad95aa63 (patch) | |
tree | 8e39d1fae3c7317969dd7aa8ab3ad20517e46136 | |
parent | 23af161b79e45dec0d11e23b77032f9b7f5c12cf (diff) | |
download | gitlab-ce-082c28cce697682b2e13e35eef3ce9f3ad95aa63.tar.gz |
Use `expose` with blocks instead of defining methods
-rw-r--r-- | app/serializers/group_child_entity.rb | 53 |
1 files changed, 26 insertions, 27 deletions
diff --git a/app/serializers/group_child_entity.rb b/app/serializers/group_child_entity.rb index b602a17156f..2f24ca9c4db 100644 --- a/app/serializers/group_child_entity.rb +++ b/app/serializers/group_child_entity.rb @@ -2,44 +2,40 @@ class GroupChildEntity < Grape::Entity include ActionView::Helpers::NumberHelper include RequestAwareEntity - expose :id, :name, :description, :visibility, :full_name, :relative_path, - :created_at, :updated_at, :can_edit, :type, :avatar_url, :permission, :edit_path + expose :id, :name, :description, :visibility, :full_name, + :created_at, :updated_at, :avatar_url - def project? - object.is_a?(Project) - end - - def type - object.class.name.downcase + expose :type do |instance| + instance.class.name.downcase end - def can_edit + expose :can_edit do |instance| return false unless request.respond_to?(:current_user) if project? - can?(request.current_user, :admin_project, object) + can?(request.current_user, :admin_project, instance) else - can?(request.current_user, :admin_group, object) + can?(request.current_user, :admin_group, instance) end end - def edit_path + expose :edit_path do |instance| if project? - edit_project_path(object) + edit_project_path(instance) else - edit_group_path(object) + edit_group_path(instance) end end - def relative_path + expose :relative_path do |instance| if project? - project_path(object) + project_path(instance) else - group_path(object) + group_path(instance) end end - def permission + expose :permission do |instance| membership&.human_access end @@ -48,15 +44,14 @@ class GroupChildEntity < Grape::Entity if: lambda { |_instance, _options| project? } # Group only attributes - expose :children_count, :leave_path, :parent_id, :number_projects_with_delimiter, - :number_users_with_delimiter, :project_count, :subgroup_count, :can_leave, + expose :children_count, :parent_id, :project_count, :subgroup_count, unless: lambda { |_instance, _options| project? } - def leave_path - leave_group_members_path(object) + expose :leave_path, unless: lambda { |_instance, _options| project? } do |instance| + leave_group_members_path(instance) end - def can_leave + expose :can_leave, unless: lambda { |_instance, _options| project? } do |instance| if membership can?(request.current_user, :destroy_group_member, membership) else @@ -64,12 +59,12 @@ class GroupChildEntity < Grape::Entity end end - def number_projects_with_delimiter - number_with_delimiter(object.project_count) + expose :number_projects_with_delimiter, unless: lambda { |_instance, _options| project? } do |instance| + number_with_delimiter(instance.project_count) end - def number_users_with_delimiter - number_with_delimiter(object.member_count) + expose :number_users_with_delimiter, unless: lambda { |_instance, _options| project? } do |instance| + number_with_delimiter(instance.member_count) end private @@ -79,4 +74,8 @@ class GroupChildEntity < Grape::Entity @membership ||= request.current_user.membership_for_object(object) end + + def project? + object.is_a?(Project) + end end |