diff options
Diffstat (limited to 'app/serializers')
-rw-r--r-- | app/serializers/entity_date_helper.rb | 11 | ||||
-rw-r--r-- | app/serializers/issuable_sidebar_basic_entity.rb | 106 | ||||
-rw-r--r-- | app/serializers/issuable_sidebar_entity.rb | 120 | ||||
-rw-r--r-- | app/serializers/issuable_sidebar_extras_entity.rb | 14 | ||||
-rw-r--r-- | app/serializers/issue_serializer.rb | 8 | ||||
-rw-r--r-- | app/serializers/issue_sidebar_basic_entity.rb | 6 | ||||
-rw-r--r-- | app/serializers/issue_sidebar_entity.rb | 12 | ||||
-rw-r--r-- | app/serializers/issue_sidebar_extras_entity.rb | 5 | ||||
-rw-r--r-- | app/serializers/merge_request_serializer.rb | 8 | ||||
-rw-r--r-- | app/serializers/merge_request_sidebar_basic_entity.rb | 11 | ||||
-rw-r--r-- | app/serializers/merge_request_sidebar_entity.rb | 11 |
11 files changed, 154 insertions, 158 deletions
diff --git a/app/serializers/entity_date_helper.rb b/app/serializers/entity_date_helper.rb index 87bba87a0ed..f515abe5917 100644 --- a/app/serializers/entity_date_helper.rb +++ b/app/serializers/entity_date_helper.rb @@ -44,13 +44,10 @@ module EntityDateHelper # It returns "Upcoming" for upcoming entities # If due date is provided, it returns "# days|weeks|months remaining|ago" # If start date is provided and elapsed, with no due date, it returns "# days elapsed" - def remaining_days_in_words(entity) - start_date = entity.try(:start_date) || entity.try(:[], :start_date) - due_date = entity.try(:due_date) || entity.try(:[], :due_date) - - if due_date && due_date.past? + def remaining_days_in_words(due_date, start_date = nil) + if due_date&.past? content_tag(:strong, 'Past due') - elsif start_date && start_date.future? + elsif start_date&.future? content_tag(:strong, 'Upcoming') elsif due_date is_upcoming = (due_date - Date.today).to_i > 0 @@ -66,7 +63,7 @@ module EntityDateHelper remaining_or_ago = is_upcoming ? _("remaining") : _("ago") "#{content} #{remaining_or_ago}".html_safe - elsif start_date && start_date.past? + elsif start_date&.past? days = (Date.today - start_date).to_i "#{content_tag(:strong, days)} #{'day'.pluralize(days)} elapsed".html_safe end diff --git a/app/serializers/issuable_sidebar_basic_entity.rb b/app/serializers/issuable_sidebar_basic_entity.rb new file mode 100644 index 00000000000..61de3c93337 --- /dev/null +++ b/app/serializers/issuable_sidebar_basic_entity.rb @@ -0,0 +1,106 @@ +# frozen_string_literal: true + +class IssuableSidebarBasicEntity < Grape::Entity + include RequestAwareEntity + + expose :id + expose :type do |issuable| + issuable.to_ability_name + end + expose :author_id + expose :project_id do |issuable| + issuable.project.id + end + expose :discussion_locked + expose :reference do |issuable| + issuable.to_reference(issuable.project, full: true) + end + + expose :milestone, using: ::API::Entities::Milestone + expose :labels, using: LabelEntity + + expose :current_user, if: lambda { |_issuable| current_user } do + expose :current_user, merge: true, using: API::Entities::UserBasic + + expose :todo, using: IssuableSidebarTodoEntity do |issuable| + current_user.pending_todo_for(issuable) + end + + expose :can_edit do |issuable| + can?(current_user, :"admin_#{issuable.to_ability_name}", issuable.project) + end + + expose :can_move do |issuable| + issuable.can_move?(current_user) + end + + expose :can_admin_label do |issuable| + can?(current_user, :admin_label, issuable.project) + end + end + + expose :issuable_json_path do |issuable| + if issuable.is_a?(MergeRequest) + project_merge_request_path(issuable.project, issuable.iid, :json) + else + project_issue_path(issuable.project, issuable.iid, :json) + end + end + + expose :namespace_path do |issuable| + issuable.project.namespace.full_path + end + + expose :project_path do |issuable| + issuable.project.path + end + + expose :project_full_path do |issuable| + issuable.project.full_path + end + + expose :project_issuables_path do |issuable| + project = issuable.project + namespace = project.namespace + + if issuable.is_a?(MergeRequest) + namespace_project_merge_requests_path(namespace, project) + else + namespace_project_issues_path(namespace, project) + end + end + + expose :create_todo_path do |issuable| + project_todos_path(issuable.project) + end + + expose :project_milestones_path do |issuable| + project_milestones_path(issuable.project, :json) + end + + expose :project_labels_path do |issuable| + project_labels_path(issuable.project, :json, include_ancestor_groups: true) + end + + expose :toggle_subscription_path do |issuable| + toggle_subscription_path(issuable) + end + + expose :move_issue_path do |issuable| + move_namespace_project_issue_path( + namespace_id: issuable.project.namespace.to_param, + project_id: issuable.project, + id: issuable + ) + end + + expose :projects_autocomplete_path do |issuable| + autocomplete_projects_path(project_id: issuable.project.id) + end + + private + + def current_user + request.current_user + end +end diff --git a/app/serializers/issuable_sidebar_entity.rb b/app/serializers/issuable_sidebar_entity.rb deleted file mode 100644 index 86210db4ef3..00000000000 --- a/app/serializers/issuable_sidebar_entity.rb +++ /dev/null @@ -1,120 +0,0 @@ -# frozen_string_literal: true - -class IssuableSidebarEntity < Grape::Entity - include RequestAwareEntity - - with_options if: { include_basic: true } do - expose :id - expose :type do |issuable| - issuable.to_ability_name - end - expose :author_id - expose :project_id do |issuable| - issuable.project.id - end - expose :discussion_locked - expose :reference do |issuable| - issuable.to_reference(issuable.project, full: true) - end - - expose :current_user, using: UserEntity do |issuable| - request.current_user - end - - # Relationships - expose :todo, using: IssuableSidebarTodoEntity do |issuable| - request.current_user.pending_todo_for(issuable) if request.current_user - end - expose :milestone, using: ::API::Entities::Milestone - expose :labels, using: LabelEntity - - # Permissions - expose :signed_in do |issuable| - request.current_user.present? - end - - expose :can_edit do |issuable| - can?(request.current_user, :"admin_#{issuable.to_ability_name}", issuable.project) - end - - expose :can_move do |issuable| - request.current_user && issuable.can_move?(request.current_user) - end - - expose :can_admin_label do |issuable| - can?(request.current_user, :admin_label, issuable.project) - end - - # Paths - expose :issuable_json_path do |issuable| - if issuable.is_a?(MergeRequest) - project_merge_request_path(issuable.project, issuable.iid, :json) - else - project_issue_path(issuable.project, issuable.iid, :json) - end - end - - expose :namespace_path do |issuable| - issuable.project.namespace.full_path - end - - expose :project_path do |issuable| - issuable.project.path - end - - expose :project_full_path do |issuable| - issuable.project.full_path - end - - expose :project_issuables_path do |issuable| - project = issuable.project - namespace = project.namespace - - if issuable.is_a?(MergeRequest) - namespace_project_merge_requests_path(namespace, project) - else - namespace_project_issues_path(namespace, project) - end - end - - expose :create_todo_path do |issuable| - project_todos_path(issuable.project) - end - - expose :project_milestones_path do |issuable| - project_milestones_path(issuable.project, :json) - end - - expose :project_labels_path do |issuable| - project_labels_path(issuable.project, :json, include_ancestor_groups: true) - end - - expose :toggle_subscription_path do |issuable| - toggle_subscription_path(issuable) - end - - expose :move_issue_path do |issuable| - move_namespace_project_issue_path( - namespace_id: issuable.project.namespace.to_param, - project_id: issuable.project, - id: issuable - ) - end - - expose :projects_autocomplete_path do |issuable| - autocomplete_projects_path(project_id: issuable.project.id) - end - end - - with_options if: { include_extras: true } do - include TimeTrackableEntity - - expose :participants, using: ::API::Entities::UserBasic do |issuable| - issuable.participants(request.current_user) - end - - expose :subscribed do |issuable| - issuable.subscribed?(request.current_user, issuable.project) - end - end -end diff --git a/app/serializers/issuable_sidebar_extras_entity.rb b/app/serializers/issuable_sidebar_extras_entity.rb new file mode 100644 index 00000000000..d60253564e1 --- /dev/null +++ b/app/serializers/issuable_sidebar_extras_entity.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +class IssuableSidebarExtrasEntity < Grape::Entity + include RequestAwareEntity + include TimeTrackableEntity + + expose :participants, using: ::API::Entities::UserBasic do |issuable| + issuable.participants(request.current_user) + end + + expose :subscribed do |issuable| + issuable.subscribed?(request.current_user, issuable.project) + end +end diff --git a/app/serializers/issue_serializer.rb b/app/serializers/issue_serializer.rb index 93625ff3afc..0fa76f098cd 100644 --- a/app/serializers/issue_serializer.rb +++ b/app/serializers/issue_serializer.rb @@ -7,10 +7,10 @@ class IssueSerializer < BaseSerializer def represent(issue, opts = {}) entity = case opts[:serializer] - when 'sidebar', 'sidebar_extras' - opts[:include_basic] = (opts[:serializer] == 'sidebar') - opts[:include_extras] = (opts[:serializer] == 'sidebar_extras') - IssueSidebarEntity + when 'sidebar' + IssueSidebarBasicEntity + when 'sidebar_extras' + IssueSidebarExtrasEntity when 'board' IssueBoardEntity else diff --git a/app/serializers/issue_sidebar_basic_entity.rb b/app/serializers/issue_sidebar_basic_entity.rb new file mode 100644 index 00000000000..723875809ec --- /dev/null +++ b/app/serializers/issue_sidebar_basic_entity.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +class IssueSidebarBasicEntity < IssuableSidebarBasicEntity + expose :due_date + expose :confidential +end diff --git a/app/serializers/issue_sidebar_entity.rb b/app/serializers/issue_sidebar_entity.rb deleted file mode 100644 index d50e19ad046..00000000000 --- a/app/serializers/issue_sidebar_entity.rb +++ /dev/null @@ -1,12 +0,0 @@ -# frozen_string_literal: true - -class IssueSidebarEntity < IssuableSidebarEntity - with_options if: { include_basic: true } do - expose :due_date - expose :confidential - end - - with_options if: { include_extras: true } do - expose :assignees, using: API::Entities::UserBasic - end -end diff --git a/app/serializers/issue_sidebar_extras_entity.rb b/app/serializers/issue_sidebar_extras_entity.rb new file mode 100644 index 00000000000..7b6e860140b --- /dev/null +++ b/app/serializers/issue_sidebar_extras_entity.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +class IssueSidebarExtrasEntity < IssuableSidebarExtrasEntity + expose :assignees, using: API::Entities::UserBasic +end diff --git a/app/serializers/merge_request_serializer.rb b/app/serializers/merge_request_serializer.rb index 4d236273d49..4cf84336aa4 100644 --- a/app/serializers/merge_request_serializer.rb +++ b/app/serializers/merge_request_serializer.rb @@ -7,10 +7,10 @@ class MergeRequestSerializer < BaseSerializer def represent(merge_request, opts = {}) entity = case opts[:serializer] - when 'sidebar', 'sidebar_extras' - opts[:include_basic] = (opts[:serializer] == 'sidebar') - opts[:include_extras] = (opts[:serializer] == 'sidebar_extras') - MergeRequestSidebarEntity + when 'sidebar' + MergeRequestSidebarBasicEntity + when 'sidebar_extras' + IssuableSidebarExtrasEntity when 'basic' MergeRequestBasicEntity else diff --git a/app/serializers/merge_request_sidebar_basic_entity.rb b/app/serializers/merge_request_sidebar_basic_entity.rb new file mode 100644 index 00000000000..0ae7298a7c1 --- /dev/null +++ b/app/serializers/merge_request_sidebar_basic_entity.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +class MergeRequestSidebarBasicEntity < IssuableSidebarBasicEntity + expose :assignee, if: lambda { |issuable| issuable.assignee } do + expose :assignee, merge: true, using: API::Entities::UserBasic + + expose :can_merge do |issuable| + issuable.can_be_merged_by?(issuable.assignee) + end + end +end diff --git a/app/serializers/merge_request_sidebar_entity.rb b/app/serializers/merge_request_sidebar_entity.rb deleted file mode 100644 index a861f2ea73b..00000000000 --- a/app/serializers/merge_request_sidebar_entity.rb +++ /dev/null @@ -1,11 +0,0 @@ -# frozen_string_literal: true - -class MergeRequestSidebarEntity < IssuableSidebarEntity - with_options if: { include_basic: true } do - expose :assignee, using: API::Entities::UserBasic - - expose :can_merge do |issuable| - issuable.can_be_merged_by?(issuable.assignee) if issuable.assignee - end - end -end |