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
words << "at"
end
words << event.project_name
words << event.resource_parent_name
words.join(" ")
end
......@@ -223,3 +223,5 @@ module EventsHelper
end
end
end
EventsHelper.prepend_if_ee('EE::EventsHelper')
......@@ -3,6 +3,8 @@
class Event < ApplicationRecord
include Sortable
include FromUnion
include Presentable
default_scope { reorder(nil) }
CREATED = 1
......@@ -135,6 +137,10 @@ class Event < ApplicationRecord
end
end
def present
super(presenter_class: ::EventPresenter)
end
# rubocop:disable Metrics/CyclomaticComplexity
# rubocop:disable Metrics/PerceivedComplexity
def visible_to_user?(user = nil)
......@@ -161,12 +167,8 @@ class Event < ApplicationRecord
# rubocop:enable Metrics/PerceivedComplexity
# rubocop:enable Metrics/CyclomaticComplexity
def project_name
if project
project.full_name
else
"(deleted project)"
end
def resource_parent
project || group
end
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)
event = event.present
xml.entry do
xml.id "tag:#{request.host},#{event.created_at.strftime("%Y-%m-%d")}:#{event.id}"
xml.link href: event_feed_url(event)
......
- event = event.present
- if event.visible_to_user?(current_user)
.event-item
.event-item-timestamp
......
......@@ -2,6 +2,5 @@
= event_preposition(event)
- if event.project
= link_to_project(event.project)
- else
= event.project_name
- elsif event.group
= link_to event.resource_parent_name, group_path(event.group)
......@@ -8,7 +8,7 @@
%span.event-type.d-inline-block.append-right-4{ class: event.action_name }
= event.action_name
%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
- unless event.milestone?
%span.event-target-title.append-right-4{ dir: "auto" }
......@@ -17,4 +17,4 @@
%span.event-type.d-inline-block.append-right-4{ class: event.action_name }
= 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 @@
- if event.project
= link_to_project(event.project)
- else
= event.project_name
= event.resource_parent_name
......@@ -27,7 +27,7 @@
- if event.project
= link_to_project(event.project)
- else
= event.project_name
= event.resource_parent_name
- else
made a private contribution
- else
......
......@@ -27,7 +27,7 @@ describe EventsHelper do
end
describe '#event_feed_url' do
let(:event) { create(:event) }
let(:event) { create(:event).present }
let(:project) { create(:project, :public, :repository) }
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