Module: ActiveRecord::Diff
- Defined in:
- lib/generators/somatics/install/templates/lib/active_record/diff.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.included(base) ⇒ Object
23 24 25 |
# File 'lib/generators/somatics/install/templates/lib/active_record/diff.rb', line 23 def self.included(base) base.extend ClassMethods end |
Instance Method Details
#diff(other_record = nil) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/generators/somatics/install/templates/lib/active_record/diff.rb', line 31 def diff(other_record = nil) if other_record.nil? old_record, new_record = self.class.find(id), self else old_record, new_record = self, other_record end if new_record.is_a?(Hash) diff_each(new_record) do |(attr_name, hash_value)| [attr_name, old_record.send(attr_name), hash_value] end else diff_each(self.class.diff_attrs) do |attr_name| [attr_name, old_record.send(attr_name), new_record.send(attr_name)] end end end |
#diff?(record = nil) ⇒ Boolean
27 28 29 |
# File 'lib/generators/somatics/install/templates/lib/active_record/diff.rb', line 27 def diff?(record = nil) not diff(record).empty? end |
#diff_each(enum) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/generators/somatics/install/templates/lib/active_record/diff.rb', line 49 def diff_each(enum) enum.inject({}) do |diff_hash, attr_name| attr_name, old_value, new_value = *yield(attr_name) unless old_value === new_value diff_hash[attr_name.to_sym] = [old_value, new_value] end diff_hash end end |