module ActiveRecord::Diff

Constants

VERSION

Public Class Methods

included(base) click to toggle source
# File lib/active_record/diff.rb, line 11
def self.included(base)
  base.class_attribute :diff_attrs
  base.extend ClassMethod
end

Public Instance Methods

diff(other_record = nil) click to toggle source
# File lib/active_record/diff.rb, line 20
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
    attrs = self.class.diff_attrs

    if attrs.nil?
      attrs = self.class.content_columns.map { |column| column.name.to_sym }
    elsif attrs.length == 1 && Hash === attrs.first
      columns = self.class.content_columns.map { |column| column.name.to_sym }

      attrs = columns + (attrs.first[:include] || []) - (attrs.first[:exclude] || [])
    end

    diff_each(attrs) do |attr_name|
      [attr_name, old_record.send(attr_name), new_record.send(attr_name)]
    end
  end
end
diff?(record = nil) click to toggle source
# File lib/active_record/diff.rb, line 16
def diff?(record = nil)
  not diff(record).empty?
end
diff_each(enum) { |attr_name| ... } click to toggle source
# File lib/active_record/diff.rb, line 48
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