lib/acts_as_diffable.rb
# Helper for processing a collection of associated objects,
# such as has_many (or habtm) association.
Helper for processing a collection of associated objects,
such as has_many (or habtm) association.
Helper for processing a collection of associated objects such as has_many ( or habtm ) association .
def plural_association_diff(left_parent, right_parent, association, key_pattern)
          key_pattern = Array(key_pattern)
          association = association.to_s 
          left_association_set = Array(left_parent.instance_eval(association))
          right_associaton_set = Array(right_parent.instance_eval(association))
          key_sets = (
            left_association_set.collect{|i| key_pattern.collect{|k| i.send(k) } } +
            right_associaton_set.collect{|i| key_pattern.collect{|k| i.send(k) } } ).uniq
          diff_set = {}
          key_sets.each do |key_set|
            conditions = {}
            key_pattern.each_with_index{|k, i| conditions[k] = key_set[i] }
            left_instance = left_association_set.find{|i|
              conditions.collect{|cf,cv| i.send(cf) == cv}.all?
            }
            right_instance = right_associaton_set.find{|i|
              conditions.collect{|cf,cv| i.send(cf) == cv}.all?
            }
            diff_set[keyify(key_set)] = attributes_diff(left_instance, right_instance, association_ids(left_parent) )
          end
          remove_unchanged_entries diff_set
        end
def plural_association_diff ( left_parent , right_parent , association , key_pattern ) key_pattern = Array ( key_pattern ) association = association . to_s left_association_set = Array ( left_parent . instance_eval ( association ) ) right_associaton_set = Array ( right_parent . instance_eval ( association ) ) key_sets = ( left_association_set . collect { | i | key_pattern . collect { | k | i . send ( k ) } } + right_associaton_set . collect { | i | key_pattern . collect { | k | i . send ( k ) } } ) . uniq diff_set = { } key_sets . each do | key_set | conditions = { } key_pattern . each_with_index { | k , i | conditions [ k ] = key_set [ i ] } left_instance = left_association_set . find { | i | conditions . collect { | cf , cv | i . send ( cf ) == cv } . all? } right_instance = right_associaton_set . find { | i | conditions . collect { | cf , cv | i . send ( cf ) == cv } . all? } diff_set [ keyify ( key_set ) ] = attributes_diff ( left_instance , right_instance , association_ids ( left_parent ) ) end remove_unchanged_entries diff_set end
Helper for processing a collection of associated objects,
such as has_many (or habtm) association.
Helper for processing a collection of associated objects such as has_many ( or habtm ) association .
#instance_eval doesn't like symbols.  What'ev. # construct a set of values (key_set) from the attributes defined in key_pattern # for each key_set, compare instances in each collection # clean up unchanged pairs
left_parent
<not_specific>
right_parent
<not_specific>
association
<not_specific>
key_pattern
<not_specific>