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



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'app/models/work_activity.rb', line 184

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



212
213
214
215
216
217
218
# File 'app/models/work_activity.rb', line 212

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