Module: ActiveRecord::Acts::Comparable::InstanceMethods
- Defined in:
- lib/acts_as_comparable/acts_as_comparable.rb
Instance Method Summary collapse
-
#differences(other = nil, options = nil) ⇒ Object
Returns a hash of field to value mappings which signify the differences between this model object and the passed in
othermodel object given an optional set of options. -
#different?(other = nil, options = nil) ⇒ Boolean
Returns true if the passed in
othermodel is different then this model given the fields that are marked as comparable. -
#same?(other = nil, options = nil) ⇒ Boolean
Returns true if the passed in
othermodel is comparable to this model given the fields that are marked as comparable.
Instance Method Details
#differences(other = nil, options = nil) ⇒ Object
Returns a hash of field to value mappings which signify the differences between this model object and the passed in other model object given an optional set of options. Returns an empty hash if there are no comparable differences.
The value of the field to value mappings is an array of the values that differ.
Arguments
-
other - another ActiveRecord model to compare this model against
-
options - a hash of options. All options for ActiveRecord::Acts::Comparable::ClassMethods.acts_as_comparable apply.
Example
class Pet < ActiveRecord::Base ; end
pet1 = Pet.new :id=>1, :name=>"dog", :value=>"Tiny"
pet2 = Pet.new :id=>5, :name=>"cat", :value=>"Norm"
differences = pet1.differences(pet2)
# => {:value=>["Tiny", "Norm"], :name=>["dog", "cat"], :id=>[1, 5]}
145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/acts_as_comparable/acts_as_comparable.rb', line 145 def differences( other = nil, = nil ) other ||= self.new_record? ? self.class.new : self.class.find( self.id ) diffs = {} comparable_attributes_for( ).each_pair do |attr, action| unless self.send( attr ) == other.send( attr ) if action.is_a? Proc diffs[attr] = [ action.call( self ), action.call( other ) ] else diffs[attr] = [ self.send( attr ), other.send( attr ) ] end end end diffs.symbolize_keys end |
#different?(other = nil, options = nil) ⇒ Boolean
Returns true if the passed in other model is different then this model given the fields that are marked as comparable. Otherwise returns false.
Arguments
-
other - another ActiveRecord model to compare this model against
-
options - a hash of options. All options for ActiveRecord::Acts::Comparable::ClassMethods.acts_as_comparable apply.
101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/acts_as_comparable/acts_as_comparable.rb', line 101 def different?( other = nil, = nil ) other ||= self.new_record? ? self.class.new : self.class.find( self.id ) comparable_attributes_for( ).each_pair do |attr, action| if action.is_a? Proc return true unless action.call( self ) == action.call( other ) else return true unless self.send( attr ) == other.send( attr ) end end false end |
#same?(other = nil, options = nil) ⇒ Boolean
Returns true if the passed in other model is comparable to this model given the fields that are marked as comparable. Otherwise returns false.
Arguments
-
other - another ActiveRecord model to compare this model against
-
options - a hash of options. All options for ActiveRecord::Acts::Comparable::ClassMethods.acts_as_comparable apply.
121 122 123 |
# File 'lib/acts_as_comparable/acts_as_comparable.rb', line 121 def same?( other = nil, = nil ) not different?( other, ) end |