Skip to content

Commit

Permalink
port to oo; no cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mccalluc committed Jan 13, 2023
1 parent 743147c commit 53d279a
Showing 1 changed file with 103 additions and 93 deletions.
196 changes: 103 additions & 93 deletions app/models/work_activity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,6 @@ def created_by_user
User.find(created_by_user_id)
end

def self.unknown_user
"Unknown user outside the system"
end

def message_event_type?
MESSAGE_ACTIVITY_TYPES.include? activity_type
end
Expand Down Expand Up @@ -110,126 +106,140 @@ def event_type
end

def to_html
if changes_event_type?
metadata_changes_html
(if changes_event_type?
MetadataChanges
elsif file_changes_event_type?
file_changes_html
FileChanges
else
message_html
end
Message
end).new(self).to_html

end

private

def created_at_time
created_at.time
end
class Renderer
def initialize(work_activity)
@work_activity = work_activity
end

def event_timestamp
created_at_time.strftime("%B %d, %Y %H:%M")
end
UNKNOWN_USER = "Unknown user outside the system"

def event_timestamp_html
"#{event_timestamp} by " if system_event_type? || log_event_type?
end
def created_at_time
@work_activity.created_at.time
end

# This is working
def message_timestamp_html
"at #{event_timestamp}" if message_event_type?
end
def event_timestamp
created_at_time.strftime("%B %d, %Y %H:%M")
end

def title_html
<<-HTML
<span class="activity-history-title">
#{event_timestamp_html}
#{created_by_user_html}
#{message_timestamp_html}
</span>
HTML
end
def event_timestamp_html
"#{event_timestamp} by " if @work_activity.system_event_type? || @work_activity.log_event_type?
end

def event_html(children:)
title_html + "<span class='message-html'>#{children.chomp}</span>"
end
def message_timestamp_html
"at #{event_timestamp}" if @work_activity.message_event_type?
end

# Returns the message formatted to display _file_ changes that were logged as an activity
def file_changes_html
changes = JSON.parse(message)
changes_html = changes.map do |change|
icon = if change["action"] == "deleted"
'<i class="bi bi-file-earmark-minus-fill file-deleted-icon"></i>'
else
'<i class="bi bi-file-earmark-plus-fill file-added-icon"></i>'
end
"<tr><td>#{icon}</td><td>#{change['action']}</td> <td>#{change['filename']}</td>"
def title_html
<<-HTML
<span class="activity-history-title">
#{event_timestamp_html}
#{created_by_user_html}
#{message_timestamp_html}
</span>
HTML
end

children = "<p><b>Files updated:</b></p><table>#{changes_html.join}</table>"
event_html(children: children)
end
def event_html(children:)
title_html + "<span class='message-html'>#{children.chomp}</span>"
end

# Returns the message formatted to display _metadata_ changes that were logged as an activity
def metadata_changes_html
html = title_html
changes = JSON.parse(message)
def created_by_user_html
return UNKNOWN_USER unless @work_activity.created_by_user

changes.keys.each do |field|
change = changes[field]
mapped = change.map { |value| change_value_html(value) }
values = mapped.join
html += "<details class='message-html'><summary class='show-changes'>#{field}</summary>#{values}</details>"
@work_activity.created_by_user.display_name_safe
end

html
end
def created_at_html
return unless created_at

# rubocop:disable Metrics/MethodLength
def message_html
# convert user references to user links
text = message.gsub(USER_REFERENCE) do |at_uid|
uid = at_uid[1..-1]
user_info = self.class.unknown_user

if uid
user = User.find_by(uid: uid)
user_info = if user
user.display_name_safe
else
uid
end
end
created_at_time = created_at.time
created_at_time.strftime("%B %d, %Y %H:%M")
end

"<a class='message-user-link' title='#{user_info}' href='#{users_path}/#{uid}'>#{at_uid}</a>"
def change_value_html(value)
if value["action"] == "changed"
DiffTools::SimpleDiff.new(value["from"], value["to"]).to_html
else
"old change"
end
end
end

# allow ``` for code blocks (Kramdown only supports ~~~)
text = text.gsub("```", "~~~")
parsed_document = Kramdown::Document.new(text)
children = parsed_document.to_html
class MetadataChanges < Renderer
# Returns the message formatted to display _metadata_ changes that were logged as an activity
def to_html
html = title_html
changes = JSON.parse(@work_activity.message)

event_html(children: children)
changes.keys.each do |field|
change = changes[field]
mapped = change.map { |value| change_value_html(value) }
values = mapped.join
html += "<details class='message-html'><summary class='show-changes'>#{field}</summary>#{values}</details>"
end

html
end
end
# rubocop:enable Metrics/MethodLength

def created_by_user_html
return self.class.unknown_user unless created_by_user
class FileChanges < Renderer
# Returns the message formatted to display _file_ changes that were logged as an activity
def to_html
changes = JSON.parse(@work_activity.message)
changes_html = changes.map do |change|
icon = if change["action"] == "deleted"
'<i class="bi bi-file-earmark-minus-fill file-deleted-icon"></i>'
else
'<i class="bi bi-file-earmark-plus-fill file-added-icon"></i>'
end
"<tr><td>#{icon}</td><td>#{change['action']}</td> <td>#{change['filename']}</td>"
end

created_by_user.display_name_safe
children = "<p><b>Files updated:</b></p><table>#{changes_html.join}</table>"
event_html(children: children)
end
end

def created_at_html
return unless created_at
class Message < Renderer
# rubocop:disable Metrics/MethodLength
def to_html
# convert user references to user links
text = @work_activity.message.gsub(USER_REFERENCE) do |at_uid|
uid = at_uid[1..-1]
user_info = UNKNOWN_USER

if uid
user = User.find_by(uid: uid)
user_info = if user
user.display_name_safe
else
uid
end
end

"<a class='message-user-link' title='#{user_info}' href='#{@work_activity.users_path}/#{uid}'>#{at_uid}</a>"
end

created_at_time = created_at.time
created_at_time.strftime("%B %d, %Y %H:%M")
end
# allow ``` for code blocks (Kramdown only supports ~~~)
text = text.gsub("```", "~~~")
parsed_document = Kramdown::Document.new(text)
children = parsed_document.to_html

def change_value_html(value)
if value["action"] == "changed"
DiffTools::SimpleDiff.new(value["from"], value["to"]).to_html
else
"old change"
event_html(children: children)
end
# rubocop:enable Metrics/MethodLength
end
end
# rubocop:enable Metrics/ClassLength

0 comments on commit 53d279a

Please sign in to comment.