Class: Decidim::Log::DiffChangesetCalculator
- Inherits:
-
Object
- Object
- Decidim::Log::DiffChangesetCalculator
- Defined in:
- app/services/decidim/log/diff_changeset_calculator.rb
Overview
This class takes a changeset from a ‘PaperTrail::Version` and a field mapping and cleans the changeset so it can be easily rendered in the log section. It is intended to be used by the `Decidim::Log::BasePresenter` class, which handles most of the work to render a log.
Example:
version = resource.versions.last
fields_mapping = { updated_at: :date, title: :string }
i18n_labels_scope = "activemodel.attributes.my_resource"
DiffChangesetCalculator
.new(version.changeset, fields_mapping, i18n_labels_scope)
.changeset
Instance Method Summary collapse
-
#changeset ⇒ Object
Calculates the changeset that should be rendered, from the ‘original_changeset` and the `fields_mapping` values.
-
#initialize(original_changeset, fields_mapping, i18n_labels_scope) ⇒ DiffChangesetCalculator
constructor
original_changeset - a ‘changeset` from a PaperTrail::Version instance fields_mapping - a Hash mapping attribute names and a type to render them i18n_labels_scope - a String representing the I18n scope where the attribute labels can be found.
Constructor Details
#initialize(original_changeset, fields_mapping, i18n_labels_scope) ⇒ DiffChangesetCalculator
original_changeset - a ‘changeset` from a PaperTrail::Version instance fields_mapping - a Hash mapping attribute names and a type to render them i18n_labels_scope - a String representing the I18n scope where the attribute
labels can be found
25 26 27 28 29 |
# File 'app/services/decidim/log/diff_changeset_calculator.rb', line 25 def initialize(original_changeset, fields_mapping, i18n_labels_scope) @original_changeset = original_changeset @fields_mapping = fields_mapping @i18n_labels_scope = i18n_labels_scope end |
Instance Method Details
#changeset ⇒ Object
Calculates the changeset that should be rendered, from the ‘original_changeset` and the `fields_mapping` values.
Returns an Array of Hashes.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'app/services/decidim/log/diff_changeset_calculator.rb', line 35 def changeset original_changeset.inject([]) do |diff, (attribute, values)| attribute = attribute.to_sym type = :default type = fields_mapping[attribute] unless fields_mapping.nil? if type.blank? || values[0] == values[1] diff else diff.concat(calculate_changeset(attribute, values, type)) end end.compact end |