Commit f4945879 authored by Jan Provaznik's avatar Jan Provaznik Committed by Rémy Coutable

Add event presenter

This presenter will be used in an upcoming MR which adds
rendering of epic events on group activity page.
parent 6d486fde
...@@ -103,7 +103,7 @@ module EventsHelper ...@@ -103,7 +103,7 @@ module EventsHelper
words << "at" words << "at"
end end
words << event.project_name words << event.resource_parent_name
words.join(" ") words.join(" ")
end end
...@@ -223,3 +223,5 @@ module EventsHelper ...@@ -223,3 +223,5 @@ module EventsHelper
end end
end end
end end
EventsHelper.prepend_if_ee('EE::EventsHelper')
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
class Event < ApplicationRecord class Event < ApplicationRecord
include Sortable include Sortable
include FromUnion include FromUnion
include Presentable
default_scope { reorder(nil) } default_scope { reorder(nil) }
CREATED = 1 CREATED = 1
...@@ -135,6 +137,10 @@ class Event < ApplicationRecord ...@@ -135,6 +137,10 @@ class Event < ApplicationRecord
end end
end end
def present
super(presenter_class: ::EventPresenter)
end
# rubocop:disable Metrics/CyclomaticComplexity # rubocop:disable Metrics/CyclomaticComplexity
# rubocop:disable Metrics/PerceivedComplexity # rubocop:disable Metrics/PerceivedComplexity
def visible_to_user?(user = nil) def visible_to_user?(user = nil)
...@@ -161,12 +167,8 @@ class Event < ApplicationRecord ...@@ -161,12 +167,8 @@ class Event < ApplicationRecord
# rubocop:enable Metrics/PerceivedComplexity # rubocop:enable Metrics/PerceivedComplexity
# rubocop:enable Metrics/CyclomaticComplexity # rubocop:enable Metrics/CyclomaticComplexity
def project_name def resource_parent
if project project || group
project.full_name
else
"(deleted project)"
end
end end
def target_title def target_title
......
# frozen_string_literal: true
class EventPresenter < Gitlab::View::Presenter::Delegated
presents :event
def resource_parent_name
resource_parent&.full_name || ''
end
def target_link_options
case resource_parent
when Group
[event.group, event.target]
when Project
[event.project.namespace.becomes(Namespace), event.project, event.target]
else
''
end
end
end
return unless event.visible_to_user?(current_user) return unless event.visible_to_user?(current_user)
event = event.present
xml.entry do xml.entry do
xml.id "tag:#{request.host},#{event.created_at.strftime("%Y-%m-%d")}:#{event.id}" xml.id "tag:#{request.host},#{event.created_at.strftime("%Y-%m-%d")}:#{event.id}"
xml.link href: event_feed_url(event) xml.link href: event_feed_url(event)
......
- event = event.present
- if event.visible_to_user?(current_user) - if event.visible_to_user?(current_user)
.event-item .event-item
.event-item-timestamp .event-item-timestamp
......
...@@ -2,6 +2,5 @@ ...@@ -2,6 +2,5 @@
= event_preposition(event) = event_preposition(event)
- if event.project - if event.project
= link_to_project(event.project) = link_to_project(event.project)
- else - elsif event.group
= event.project_name = link_to event.resource_parent_name, group_path(event.group)
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
%span.event-type.d-inline-block.append-right-4{ class: event.action_name } %span.event-type.d-inline-block.append-right-4{ class: event.action_name }
= event.action_name = event.action_name
%span.event-target-type.append-right-4= event.target_type.titleize.downcase %span.event-target-type.append-right-4= event.target_type.titleize.downcase
= link_to [event.project.namespace.becomes(Namespace), event.project, event.target], class: 'has-tooltip event-target-link append-right-4', title: event.target_title do = link_to event.target_link_options, class: 'has-tooltip event-target-link append-right-4', title: event.target_title do
= event.target.reference_link_text = event.target.reference_link_text
- unless event.milestone? - unless event.milestone?
%span.event-target-title.append-right-4{ dir: "auto" } %span.event-target-title.append-right-4{ dir: "auto" }
...@@ -17,4 +17,4 @@ ...@@ -17,4 +17,4 @@
%span.event-type.d-inline-block.append-right-4{ class: event.action_name } %span.event-type.d-inline-block.append-right-4{ class: event.action_name }
= event_action_name(event) = event_action_name(event)
= render "events/event_scope", event: event = render "events/event_scope", event: event if event.resource_parent.present?
...@@ -10,4 +10,4 @@ ...@@ -10,4 +10,4 @@
- if event.project - if event.project
= link_to_project(event.project) = link_to_project(event.project)
- else - else
= event.project_name = event.resource_parent_name
...@@ -27,7 +27,7 @@ ...@@ -27,7 +27,7 @@
- if event.project - if event.project
= link_to_project(event.project) = link_to_project(event.project)
- else - else
= event.project_name = event.resource_parent_name
- else - else
made a private contribution made a private contribution
- else - else
......
...@@ -27,7 +27,7 @@ describe EventsHelper do ...@@ -27,7 +27,7 @@ describe EventsHelper do
end end
describe '#event_feed_url' do describe '#event_feed_url' do
let(:event) { create(:event) } let(:event) { create(:event).present }
let(:project) { create(:project, :public, :repository) } let(:project) { create(:project, :public, :repository) }
context 'issue' do context 'issue' do
......
# frozen_string_literal: true
require 'spec_helper'
describe EventPresenter do
include Gitlab::Routing.url_helpers
set(:group) { create(:group) }
set(:project) { create(:project, group: group) }
set(:target) { create(:milestone, project: project) }
set(:group_event) { create(:event, :created, project: nil, group: group, target: target) }
set(:project_event) { create(:event, :created, project: project, target: target) }
describe '#resource_parent_name' do
context 'with group event' do
subject { group_event.present.resource_parent_name }
it { is_expected.to eq(group.full_name) }
end
context 'with project label' do
subject { project_event.present.resource_parent_name }
it { is_expected.to eq(project.full_name) }
end
end
describe '#target_link_options' do
context 'with group event' do
subject { group_event.present.target_link_options }
it { is_expected.to eq([group, target]) }
end
context 'with project label' do
subject { project_event.present.target_link_options }
it { is_expected.to eq([group.becomes(Namespace), project, target]) }
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