Commit 66acda99 authored by Stan Hu's avatar Stan Hu

Merge branch 'refactoring-entities-file-16' into 'master'

Separate service entities into own class files

See merge request gitlab-org/gitlab!24936
parents a9025781 e2b53b5f
---
title: Separate service entities into own class files
merge_request: 24936
author: Rajendra Kadam
type: added
...@@ -163,78 +163,6 @@ module API ...@@ -163,78 +163,6 @@ module API
end end
end end
class GlobalNotificationSetting < NotificationSetting
expose :notification_email do |notification_setting, options|
notification_setting.user.notification_email
end
end
class ProjectServiceBasic < Grape::Entity
expose :id, :title
expose :slug do |service|
service.to_param.dasherize
end
expose :created_at, :updated_at, :active
expose :commit_events, :push_events, :issues_events, :confidential_issues_events
expose :merge_requests_events, :tag_push_events, :note_events
expose :confidential_note_events, :pipeline_events, :wiki_page_events
expose :job_events, :comment_on_event_enabled
end
class ProjectService < ProjectServiceBasic
# Expose serialized properties
expose :properties do |service, options|
# TODO: Simplify as part of https://gitlab.com/gitlab-org/gitlab/issues/29404
if service.data_fields_present?
service.data_fields.as_json.slice(*service.api_field_names)
else
service.properties.slice(*service.api_field_names)
end
end
end
class ProjectWithAccess < Project
expose :permissions do
expose :project_access, using: Entities::ProjectAccess do |project, options|
if options[:project_members]
options[:project_members].find { |member| member.source_id == project.id }
else
project.project_member(options[:current_user])
end
end
expose :group_access, using: Entities::GroupAccess do |project, options|
if project.group
if options[:group_members]
options[:group_members].find { |member| member.source_id == project.namespace_id }
else
project.group.highest_group_member(options[:current_user])
end
end
end
end
# rubocop: disable CodeReuse/ActiveRecord
def self.preload_relation(projects_relation, options = {})
relation = super(projects_relation, options)
project_ids = relation.select('projects.id')
namespace_ids = relation.select(:namespace_id)
options[:project_members] = options[:current_user]
.project_members
.where(source_id: project_ids)
.preload(:source, user: [notification_settings: :source])
options[:group_members] = options[:current_user]
.group_members
.where(source_id: namespace_ids)
.preload(:source, user: [notification_settings: :source])
relation
end
# rubocop: enable CodeReuse/ActiveRecord
end
class LabelBasic < Grape::Entity class LabelBasic < Grape::Entity
expose :id, :name, :color, :description, :description_html, :text_color expose :id, :name, :color, :description, :description_html, :text_color
end end
......
# frozen_string_literal: true
module API
module Entities
class GlobalNotificationSetting < Entities::NotificationSetting
expose :notification_email do |notification_setting, options|
notification_setting.user.notification_email
end
end
end
end
# frozen_string_literal: true
module API
module Entities
class ProjectService < Entities::ProjectServiceBasic
# Expose serialized properties
expose :properties do |service, options|
# TODO: Simplify as part of https://gitlab.com/gitlab-org/gitlab/issues/29404
if service.data_fields_present?
service.data_fields.as_json.slice(*service.api_field_names)
else
service.properties.slice(*service.api_field_names)
end
end
end
end
end
# frozen_string_literal: true
module API
module Entities
class ProjectServiceBasic < Grape::Entity
expose :id, :title
expose :slug do |service|
service.to_param.dasherize
end
expose :created_at, :updated_at, :active
expose :commit_events, :push_events, :issues_events, :confidential_issues_events
expose :merge_requests_events, :tag_push_events, :note_events
expose :confidential_note_events, :pipeline_events, :wiki_page_events
expose :job_events, :comment_on_event_enabled
end
end
end
# frozen_string_literal: true
module API
module Entities
class ProjectWithAccess < Project
expose :permissions do
expose :project_access, using: Entities::ProjectAccess do |project, options|
if options[:project_members]
options[:project_members].find { |member| member.source_id == project.id }
else
project.project_member(options[:current_user])
end
end
expose :group_access, using: Entities::GroupAccess do |project, options|
if project.group
if options[:group_members]
options[:group_members].find { |member| member.source_id == project.namespace_id }
else
project.group.highest_group_member(options[:current_user])
end
end
end
end
# rubocop: disable CodeReuse/ActiveRecord
def self.preload_relation(projects_relation, options = {})
relation = super(projects_relation, options)
project_ids = relation.select('projects.id')
namespace_ids = relation.select(:namespace_id)
options[:project_members] = options[:current_user]
.project_members
.where(source_id: project_ids)
.preload(:source, user: [notification_settings: :source])
options[:group_members] = options[:current_user]
.group_members
.where(source_id: namespace_ids)
.preload(:source, user: [notification_settings: :source])
relation
end
# rubocop: enable CodeReuse/ActiveRecord
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment