Commit a9e54bee authored by Patrick Derichs's avatar Patrick Derichs

Extract SyntheticNote base class

Update existing synthetic note classes as well.
parent 7ee61f74
# frozen_string_literal: true # frozen_string_literal: true
class LabelNote < Note class LabelNote < SyntheticNote
attr_accessor :resource_parent attr_accessor :resource_parent
attr_reader :events attr_reader :events
def self.from_events(events, resource: nil, resource_parent: nil) def self.from_events(events, resource: nil, resource_parent: nil)
resource ||= events.first.issuable resource ||= events.first.issuable
attrs = { attrs = note_attributes('label', events.first, resource, resource_parent).merge(events: events)
system: true,
author: events.first.user,
created_at: events.first.created_at,
discussion_id: events.first.discussion_id,
noteable: resource,
system_note_metadata: SystemNoteMetadata.new(action: 'label'),
events: events,
resource_parent: resource_parent
}
if resource_parent.is_a?(Project)
attrs[:project_id] = resource_parent.id
end
LabelNote.new(attrs) LabelNote.new(attrs)
end end
...@@ -35,22 +22,10 @@ class LabelNote < Note ...@@ -35,22 +22,10 @@ class LabelNote < Note
true true
end end
def note
@note ||= note_text
end
def note_html def note_html
@note_html ||= "<p dir=\"auto\">#{note_text(html: true)}</p>" @note_html ||= "<p dir=\"auto\">#{note_text(html: true)}</p>"
end end
def project
resource_parent if resource_parent.is_a?(Project)
end
def group
resource_parent if resource_parent.is_a?(Group)
end
private private
def update_outdated_markdown def update_outdated_markdown
......
# frozen_string_literal: true # frozen_string_literal: true
class MilestoneNote < ::Note class MilestoneNote < SyntheticNote
attr_accessor :resource_parent, :event, :milestone attr_accessor :milestone
def self.from_event(event, resource: nil, resource_parent: nil) def self.from_event(event, resource: nil, resource_parent: nil)
resource ||= event.resource attrs = note_attributes('milestone', event, resource, resource_parent).merge(milestone: event.milestone)
attrs = {
system: true,
author: event.user,
created_at: event.created_at,
noteable: resource,
milestone: event.milestone,
discussion_id: event.discussion_id,
event: event,
system_note_metadata: ::SystemNoteMetadata.new(action: 'milestone'),
resource_parent: resource_parent
}
if resource_parent.is_a?(Project)
attrs[:project_id] = resource_parent.id
end
MilestoneNote.new(attrs) MilestoneNote.new(attrs)
end end
def note
@note ||= note_text
end
def note_html def note_html
@note_html ||= Banzai::Renderer.cacheless_render_field(self, :note, { group: group, project: project }) @note_html ||= Banzai::Renderer.cacheless_render_field(self, :note, { group: group, project: project })
end end
def project
resource_parent if resource_parent.is_a?(Project)
end
def group
resource_parent if resource_parent.is_a?(Group)
end
private private
def note_text(html: false) def note_text(html: false)
......
# frozen_string_literal: true
class SyntheticNote < Note
attr_accessor :resource_parent, :event
self.abstract_class = true
def self.note_attributes(action, event, resource, resource_parent)
resource ||= event.resource
attrs = {
system: true,
author: event.user,
created_at: event.created_at,
discussion_id: event.discussion_id,
noteable: resource,
event: event,
system_note_metadata: ::SystemNoteMetadata.new(action: action),
resource_parent: resource_parent
}
if resource_parent.is_a?(Project)
attrs[:project_id] = resource_parent.id
end
attrs
end
def project
resource_parent if resource_parent.is_a?(Project)
end
def group
resource_parent if resource_parent.is_a?(Group)
end
def note
@note ||= note_text
end
def note_html
raise NotImplementedError
end
private
def note_text(html: false)
raise NotImplementedError
end
end
# frozen_string_literal: true # frozen_string_literal: true
class WeightNote < ::Note class WeightNote < ::SyntheticNote
attr_accessor :resource_parent, :event attr_accessor :resource_parent, :event
def self.from_event(event, resource: nil, resource_parent: nil) def self.from_event(event, resource: nil, resource_parent: nil)
resource ||= event.issue attrs = note_attributes('weight', event, resource, resource_parent)
attrs = {
system: true,
author: event.user,
created_at: event.created_at,
noteable: resource,
event: event,
discussion_id: event.discussion_id,
system_note_metadata: ::SystemNoteMetadata.new(action: 'weight'),
resource_parent: resource_parent
}
if resource_parent.is_a?(Project)
attrs[:project_id] = resource_parent.id
end
WeightNote.new(attrs) WeightNote.new(attrs)
end end
def note
@note ||= note_text
end
def note_html def note_html
@note_html ||= "<p dir=\"auto\">#{note_text(html: true)}</p>" @note_html ||= "<p dir=\"auto\">#{note_text(html: true)}</p>"
end end
def project
resource_parent if resource_parent.is_a?(Project)
end
def group
resource_parent if resource_parent.is_a?(Group)
end
private private
def note_text(html: false) def note_text(html: false)
......
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