try to improve readability of JournalManager

- using useful names
- using rubys awesome syntax to avoid nested list accesses (e.g. k[0][1])
pull/3275/head
Jan Sandbrink 9 years ago
parent 3e82f24402
commit d8cfa271cd
  1. 38
      app/models/journal_manager.rb
  2. 8
      lib/plugins/acts_as_journalized/lib/journal_changes.rb

@ -85,31 +85,45 @@ class JournalManager
# This would lead to false change information, otherwise.
# We need to be careful though, because we want to accept false (and false.blank? == true)
def self.remove_empty_associations(associations, value)
associations.reject { |h| h.has_key?(value) && h[value].blank? && h[value] != false }
associations.reject { |association|
association.has_key?(value) &&
association[value].blank? &&
association[value] != false
}
end
def self.merge_reference_journals_by_id(current, predecessor, key)
all_attachable_journal_ids = current.map { |j| j[key] } | predecessor.map { |j| j[key] }
def self.merge_reference_journals_by_id(new_journals, old_journals, id_key)
all_associated_journal_ids = new_journals.map { |j| j[id_key] } |
old_journals.map { |j| j[id_key] }
all_attachable_journal_ids.each_with_object({}) { |i, h|
h[i] = [predecessor.detect { |j| j[key] == i },
current.detect { |j| j[key] == i }]
all_associated_journal_ids.each_with_object({}) { |id, result|
result[id] = [old_journals.detect { |j| j[id_key] == id },
new_journals.detect { |j| j[id_key] == id }]
}
end
def self.added_references(merged_references, key, value)
merged_references.select { |_, v| v[0].nil? and not v[1].nil? }
.each_with_object({}) { |k, h| h["#{key}_#{k[0]}"] = [nil, k[1][1][value]] }
merged_references.select { |_, (old_attributes, new_attributes)|
old_attributes.nil? && !new_attributes.nil?
}.each_with_object({}) { |(id, (_, new_attributes)), result|
result["#{key}_#{id}"] = [nil, new_attributes[value]]
}
end
def self.removed_references(merged_references, key, value)
merged_references.select { |_, v| not v[0].nil? and v[1].nil? }
.each_with_object({}) { |k, h| h["#{key}_#{k[0]}"] = [k[1][0][value], nil] }
merged_references.select { |_, (old_attributes, new_attributes)|
!old_attributes.nil? && new_attributes.nil?
}.each_with_object({}) { |(id, (old_attributes, _)), result|
result["#{key}_#{id}"] = [old_attributes[value], nil]
}
end
def self.changed_references(merged_references, key, value)
merged_references.select { |_, v| not v[0].nil? and not v[1].nil? and v[0][value] != v[1][value] }
.each_with_object({}) { |k, h| h["#{key}_#{k[0]}"] = [k[1][0][value], k[1][1][value]] }
merged_references.select { |_, (old_attributes, new_attributes)|
!old_attributes.nil? && !new_attributes.nil? && old_attributes[value] != new_attributes[value]
}.each_with_object({}) { |(id, (old_attributes, new_attributes)), result|
result["#{key}_#{id}"] = [old_attributes[value], new_attributes[value]]
}
end
def self.recreate_initial_journal(type, journal, changed_data)

@ -69,11 +69,11 @@ module JournalChanges
h[changed_attribute] = [nil, new_value]
}
else
new_attributes = send(journal_assoc_name).map(&:attributes)
old_attributes = predecessor.send(journal_assoc_name).map(&:attributes)
new_journals = send(journal_assoc_name).map(&:attributes)
old_journals = predecessor.send(journal_assoc_name).map(&:attributes)
merged_journals = JournalManager.merge_reference_journals_by_id new_attributes,
old_attributes,
merged_journals = JournalManager.merge_reference_journals_by_id new_journals,
old_journals,
key.to_s
changes.merge! JournalManager.added_references(merged_journals, association, value.to_s)

Loading…
Cancel
Save