Class: WorkActivity::MetadataChanges

Inherits:
Renderer
  • Object
show all
Defined in:
app/models/work_activity.rb

Constant Summary

Constants inherited from Renderer

Renderer::DATE_FORMAT, Renderer::DATE_TIME_FORMAT, Renderer::SORTABLE_DATE_TIME_FORMAT, Renderer::UNKNOWN_USER

Instance Method Summary collapse

Methods inherited from Renderer

#created_by_user_html, #created_sortable_html, #created_updated_html, #initialize, #title_html, #to_html

Constructor Details

This class inherits a constructor from WorkActivity::Renderer

Instance Method Details

#body_htmlObject

Returns the message formatted to display metadata changes that were logged as an activity



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'app/models/work_activity.rb', line 158

def body_html
  message_json = JSON.parse(@work_activity.message)

  # Messages should consistently be Arrays of Hashes, but this might require a migration from legacy field records
  messages = if message_json.is_a?(Array)
               message_json
             else
               Array.wrap(message_json)
             end

  elements = messages.map do |message|
    markup = if message.is_a?(Hash)
               message.keys.map do |field|
                 mapped = message[field].map { |value| change_value_html(value) }
                 "<details class='message-html'><summary class='show-changes'>#{field&.titleize}</summary>#{mapped.join}</details>"
               end
             else
               # For handling cases where WorkActivity#message only contains Strings, or Arrays of Strings
               [
                 "<details class='message-html'><summary class='show-changes'></summary>#{message}</details>"
               ]
             end
    markup.join
  end

  elements.flatten.join
end

#change_value_html(value) ⇒ Object



186
187
188
189
190
191
192
# File 'app/models/work_activity.rb', line 186

def change_value_html(value)
  if value["action"] == "changed"
    DiffTools::SimpleDiff.new(value["from"], value["to"]).to_html
  else
    "old change"
  end
end