Handles custom fields as well

pull/312/head
Hagen Schink 11 years ago
parent 0a5071b0a6
commit ecaa922bf9
  1. 29
      app/models/journal.rb
  2. 66
      app/models/journal_manager.rb

@ -129,33 +129,30 @@ class Journal < ActiveRecord::Base
end
end
@changes.merge!(get_attachment_changes predecessor)
@changes.merge!(get_association_changes predecessor, "attachable", "attachments", :attachment_id, :filename)
@changes.merge!(get_association_changes predecessor, "customizable", "custom_fields", :custom_field_id, :value)
end
@changes
end
def get_attachment_changes(predecessor)
def get_association_changes(predecessor, journal_association, association, key, value)
changes = {}
journal_assoc_name = "#{journal_association}_journals".to_sym
if predecessor.nil?
attachable_journals.each_with_object(changes) {|a, h| h["attachments_#{a.attachment_id}".to_sym] = [nil, a.filename] }
send(journal_assoc_name).each_with_object(changes) {|a, h| h["#{association}_#{a.send(key)}".to_sym] = [nil, a.send(value)] }
else
all_attachable_journal_ids = attachable_journals.map(&:attachment_id) | predecessor.attachable_journals.map(&:attachment_id)
current = send(journal_assoc_name).map(&:attributes)
predecessor_attachable_journals = predecessor.send(journal_assoc_name).map(&:attributes)
all_attachable_journals = all_attachable_journal_ids.each_with_object({}) { |i, h| h[i] = [predecessor.attachable_journals.find_by_attachment_id(i), attachable_journals.find_by_attachment_id(i)] }
merged_journals = JournalManager.merge_reference_journals_by_id current,
predecessor_attachable_journals,
key.to_s
# find new attachments
all_attachable_journals.select {|_, v| v[0].nil? and not v[1].nil?}
.each_with_object(changes) { |k,h| h["attachments_#{k[0]}".to_sym] = [nil, k[1][1].filename] }
# find removed attachments
all_attachable_journals.select {|_, v| not v[0].nil? and v[1].nil?}
.each_with_object(changes) { |k,h| h["attachments_#{k[0]}".to_sym] = [k[1][0].filename, nil] }
# find changed attachments
all_attachable_journals.select {|_, v| not v[0].nil? and not v[1].nil? and v[0].filename != v[1].filename}
.each_with_object(changes) { |k,h| h["attachments_#{k[0]}".to_sym] = [k[1][0].filename, k[1][1].filename] }
changes.merge! JournalManager.added_references(merged_journals, association, value.to_s)
changes.merge! JournalManager.removed_references(merged_journals, association, value.to_s)
changes.merge! JournalManager.changed_references(merged_journals, association, value.to_s)
end
changes

@ -16,16 +16,66 @@ class JournalManager
not obj.nil? and obj.respond_to? :journals
end
def self.attributes_changed?(journaled)
def self.changed?(journaled)
if journaled.journals.count > 0
current = journaled.attributes
predecessor = journaled.journals.last.data.journaled_attributes
changed = attributes_changed? journaled
changed ||= association_changed? journaled, "attachable", :attachments, :id, :attachment_id, :filename
changed ||= association_changed? journaled, "customizable", :custom_values, :custom_field_id, :custom_field_id, :value
changed
else
true
end
end
def self.attributes_changed?(journaled)
current = journaled.attributes
predecessor = journaled.journals.last.data.journaled_attributes
return predecessor.map{|k,v| current[k.to_s] != v}
.inject(false) { |r, c| r || c }
end
def self.association_changed?(journaled, journal_association, association, id, key, value)
if journaled.respond_to? association
journal_assoc_name = "#{journal_association}_journals".to_sym
changes = {}
current = journaled.send(association).map {|a| { key.to_s => a.send(id), value.to_s => a.send(value)} }
predecessor = journaled.journals.last.send(journal_assoc_name).map(&:attributes)
merged_journals = JournalManager.merge_reference_journals_by_id current, predecessor, key.to_s
return predecessor.map{|k,v| current[k.to_s] != v}
.inject(false) { |r, c| r || c }
changes.merge! JournalManager.added_references(merged_journals, association.to_s, value.to_s)
changes.merge! JournalManager.removed_references(merged_journals, association.to_s, value.to_s)
changes.merge! JournalManager.changed_references(merged_journals, association.to_s, value.to_s)
not changes.empty?
else
false
end
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] }
all_attachable_journal_ids.each_with_object({}) { |i, h| h[i] = [predecessor.detect { |j| j[key] == i },
current.detect { |j| j[key] == i }] }
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]}".to_sym] = [nil, k[1][1][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]}".to_sym] = [k[1][0][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]}".to_sym] = [k[1][0][value], k[1][1][value]] }
true
end
def self.recreate_initial_journal(type, journal, changed_data)
@ -136,8 +186,8 @@ class JournalManager
end
def self.create_custom_field_data(journaled, journal)
journaled.custom_values.each do |c|
journal.customizable_journals.build journal: journal, custom_field: c, value: c.value
journaled.custom_values.each do |cv|
journal.customizable_journals.build journal: journal, custom_field: cv.custom_field, value: cv.value
end
end
end

Loading…
Cancel
Save