Refactor event title generation for more consistent messages.

Example: "User joined project Namespace / Project" rather than "User joined project at Namespace / Project"
This commit is contained in:
Douwe Maan 2015-02-13 12:00:12 +01:00
parent 07d05d2df7
commit 522efa43fe
5 changed files with 85 additions and 58 deletions

View file

@ -10,11 +10,15 @@ module EventsHelper
end end
def event_action_name(event) def event_action_name(event)
target = if event.target_type target = if event.target_type
event.target_type.titleize.downcase if event.note?
else event.note_target_type
'project' else
end event.target_type.titleize.downcase
end
else
'project'
end
[event.action_name, target].join(" ") [event.action_name, target].join(" ")
end end
@ -42,21 +46,30 @@ module EventsHelper
end end
def event_feed_title(event) def event_feed_title(event)
if event.issue? words = []
"#{event.author_name} #{event.action_name} issue ##{event.target_iid}: #{event.issue_title} at #{event.project_name}" words << event.author_name
elsif event.merge_request? words << event_action_name(event)
"#{event.author_name} #{event.action_name} MR ##{event.target_iid}: #{event.merge_request_title} at #{event.project_name}"
elsif event.push? if event.push?
"#{event.author_name} #{event.push_action_name} #{event.ref_type} #{event.ref_name} at #{event.project_name}" words << event.ref_type
elsif event.membership_changed? words << event.ref_name
"#{event.author_name} #{event.action_name} #{event.project_name}" words << "at"
elsif event.note? && event.note_commit? elsif event.commented?
"#{event.author_name} commented on #{event.note_target_type} #{event.note_short_commit_id} at #{event.project_name}" if event.note_commit?
elsif event.note? words << event.note_short_commit_id
"#{event.author_name} commented on #{event.note_target_type} ##{truncate event.note_target_iid} at #{event.project_name}" else
else words << "##{truncate event.note_target_iid}"
"" end
words << "at"
elsif event.target
words << "##{event.target_iid}:"
words << event.target.title if event.target.respond_to?(:title)
words << "at"
end end
words << event.project_name
words.join(" ")
end end
def event_feed_url(event) def event_feed_url(event)
@ -96,8 +109,6 @@ module EventsHelper
render "events/event_push", event: event render "events/event_push", event: event
elsif event.merge_request? elsif event.merge_request?
render "events/event_merge_request", merge_request: event.merge_request render "events/event_merge_request", merge_request: event.merge_request
elsif event.push?
render "events/event_push", event: event
elsif event.note? elsif event.note?
render "events/event_note", note: event.note render "events/event_note", note: event.note
end end

View file

@ -75,25 +75,43 @@ class Event < ActiveRecord::Base
end end
def target_title def target_title
if target && target.respond_to?(:title) target.title if target && target.respond_to?(:title)
target.title end
end
def created?
action == CREATED
end end
def push? def push?
action == self.class::PUSHED && valid_push? action == PUSHED && valid_push?
end end
def merged? def merged?
action == self.class::MERGED action == MERGED
end end
def closed? def closed?
action == self.class::CLOSED action == CLOSED
end end
def reopened? def reopened?
action == self.class::REOPENED action == REOPENED
end
def joined?
action == JOINED
end
def left?
action == LEFT
end
def commented?
action == COMMENTED
end
def membership_changed?
joined? || left?
end end
def milestone? def milestone?
@ -112,32 +130,32 @@ class Event < ActiveRecord::Base
target_type == "MergeRequest" target_type == "MergeRequest"
end end
def joined? def milestone
action == JOINED target if milestone?
end
def left?
action == LEFT
end
def membership_changed?
joined? || left?
end end
def issue def issue
target if target_type == "Issue" target if issue?
end end
def merge_request def merge_request
target if target_type == "MergeRequest" target if merge_request?
end end
def note def note
target if target_type == "Note" target if note?
end end
def action_name def action_name
if closed? if push?
if new_ref?
"pushed new"
elsif rm_ref?
"deleted"
else
"pushed to"
end
elsif closed?
"closed" "closed"
elsif merged? elsif merged?
"accepted" "accepted"
@ -145,6 +163,8 @@ class Event < ActiveRecord::Base
'joined' 'joined'
elsif left? elsif left?
'left' 'left'
elsif commented?
"commented on"
else else
"opened" "opened"
end end
@ -213,16 +233,6 @@ class Event < ActiveRecord::Base
tag? ? "tag" : "branch" tag? ? "tag" : "branch"
end end
def push_action_name
if new_ref?
"pushed new"
elsif rm_ref?
"deleted"
else
"pushed to"
end
end
def push_with_commits? def push_with_commits?
md_ref? && commits.any? && commit_from && commit_to md_ref? && commits.any? && commit_from && commit_to
end end

View file

@ -1,15 +1,17 @@
.event-title .event-title
%span.author_name= link_to_author event %span.author_name= link_to_author event
%span.event_label{class: event.action_name}= event_action_name(event) %span.event_label{class: event.action_name}
= event_action_name(event)
- if event.target - if event.target
%strong= link_to "##{event.target_iid}", [event.project, event.target] %strong= link_to "##{event.target_iid}", [event.project, event.target]
- else at
%strong= gfm event.target_title
at
- if event.project - if event.project
= link_to_project event.project = link_to_project event.project
- else - else
= event.project_name = event.project_name
- if event.target.respond_to?(:title) - if event.target.respond_to?(:title)
.event-body .event-body
.event-note .event-note

View file

@ -1,6 +1,10 @@
.event-title .event-title
%span.author_name= link_to_author event %span.author_name= link_to_author event
%span.event_label commented on #{event_note_title_html(event)} at %span.event_label
= event.action_name
= event_note_title_html(event)
at
- if event.project - if event.project
= link_to_project event.project = link_to_project event.project
- else - else

View file

@ -1,6 +1,6 @@
.event-title .event-title
%span.author_name= link_to_author event %span.author_name= link_to_author event
%span.event_label.pushed #{event.push_action_name} #{event.ref_type} %span.event_label.pushed #{event.action_name} #{event.ref_type}
- if event.rm_ref? - if event.rm_ref?
%strong= event.ref_name %strong= event.ref_name
- else - else