2018-09-07 09:39:20 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-03-31 11:07:53 -04:00
|
|
|
class LabelNote < SyntheticNote
|
2018-09-07 09:39:20 -04:00
|
|
|
attr_accessor :resource_parent
|
|
|
|
attr_reader :events
|
|
|
|
|
|
|
|
def self.from_events(events, resource: nil, resource_parent: nil)
|
|
|
|
resource ||= events.first.issuable
|
|
|
|
|
2020-03-31 11:07:53 -04:00
|
|
|
attrs = note_attributes('label', events.first, resource, resource_parent).merge(events: events)
|
2018-09-07 09:39:20 -04:00
|
|
|
|
|
|
|
LabelNote.new(attrs)
|
|
|
|
end
|
|
|
|
|
|
|
|
def events=(events)
|
|
|
|
@events = events
|
|
|
|
|
|
|
|
update_outdated_markdown
|
|
|
|
end
|
|
|
|
|
|
|
|
def cached_html_up_to_date?(markdown_field)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def note_html
|
|
|
|
@note_html ||= "<p dir=\"auto\">#{note_text(html: true)}</p>"
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def update_outdated_markdown
|
|
|
|
events.each do |event|
|
|
|
|
if event.outdated_markdown?
|
|
|
|
event.refresh_invalid_reference
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def note_text(html: false)
|
2019-06-28 13:09:31 -04:00
|
|
|
added = labels_str(label_refs_by_action('add', html), prefix: 'added', suffix: added_suffix)
|
|
|
|
removed = labels_str(label_refs_by_action('remove', html), prefix: removed_prefix)
|
2018-09-07 09:39:20 -04:00
|
|
|
|
|
|
|
[added, removed].compact.join(' and ')
|
|
|
|
end
|
|
|
|
|
2019-06-28 13:09:31 -04:00
|
|
|
def removed_prefix
|
|
|
|
'removed'
|
|
|
|
end
|
|
|
|
|
|
|
|
def added_suffix
|
|
|
|
''
|
|
|
|
end
|
|
|
|
|
2018-09-07 09:39:20 -04:00
|
|
|
# returns string containing added/removed labels including
|
|
|
|
# count of deleted labels:
|
|
|
|
#
|
|
|
|
# added ~1 ~2 + 1 deleted label
|
|
|
|
# added 3 deleted labels
|
|
|
|
# added ~1 ~2 labels
|
2019-06-28 13:09:31 -04:00
|
|
|
def labels_str(label_refs, prefix: '', suffix: '')
|
2018-09-07 09:39:20 -04:00
|
|
|
existing_refs = label_refs.select { |ref| ref.present? }.sort
|
|
|
|
refs_str = existing_refs.empty? ? nil : existing_refs.join(' ')
|
|
|
|
|
|
|
|
deleted = label_refs.count - existing_refs.count
|
|
|
|
deleted_str = deleted == 0 ? nil : "#{deleted} deleted"
|
|
|
|
|
2019-02-08 07:19:53 -05:00
|
|
|
return unless refs_str || deleted_str
|
2018-09-07 09:39:20 -04:00
|
|
|
|
|
|
|
label_list_str = [refs_str, deleted_str].compact.join(' + ')
|
2019-06-28 13:09:31 -04:00
|
|
|
suffix += ' label'.pluralize(deleted > 0 ? deleted : existing_refs.count)
|
2018-09-07 09:39:20 -04:00
|
|
|
|
2019-06-28 13:09:31 -04:00
|
|
|
"#{prefix} #{label_list_str} #{suffix.squish}"
|
2018-09-07 09:39:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def label_refs_by_action(action, html)
|
|
|
|
field = html ? :reference_html : :reference
|
|
|
|
|
|
|
|
events.select { |e| e.action == action }.map(&field)
|
|
|
|
end
|
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
LabelNote.prepend_mod_with('LabelNote')
|