module DeltaChanges::Extension

Public Class Methods

included(base) click to toggle source
# File lib/delta_changes.rb, line 5
def self.included(base)
  base.extend(ClassMethods)
  base.cattr_accessor :delta_changes_options
  base.attribute_method_suffix '_delta_changed?', '_delta_change', '_delta_was', '_delta_will_change!'
  if ::ActiveRecord.version < Gem::Version.new('5.2')
    base.send(:prepend, InstanceMethodsLegacy)
  else
    base.send(:prepend, InstanceMethods)
  end
end

Public Instance Methods

delta_changes() click to toggle source
# File lib/delta_changes.rb, line 92
def delta_changes
  delta_changed_attributes.keys.inject({}) { |h, attr| h[attr] = attribute_delta_change(attr); h }
end
delta_changes?() click to toggle source
# File lib/delta_changes.rb, line 96
def delta_changes?
  delta_changed_attributes.keys.present?
end
reset_delta_changes!() click to toggle source
# File lib/delta_changes.rb, line 102
def reset_delta_changes!
  delta_changed_attributes.clear
end

Private Instance Methods

attribute_delta_change(attr) click to toggle source

Handle *_delta_change for method_missing.

# File lib/delta_changes.rb, line 119
def attribute_delta_change(attr)
  [delta_changed_attributes[attr], __send__(attr)] if attribute_delta_changed?(attr)
end
attribute_delta_changed?(attr) click to toggle source

Handle *_delta_changed? for method_missing.

# File lib/delta_changes.rb, line 114
def attribute_delta_changed?(attr)
  delta_changed_attributes.include?(attr)
end
attribute_delta_was(attr) click to toggle source

Handle *_delta_was for method_missing.

# File lib/delta_changes.rb, line 124
def attribute_delta_was(attr)
  attribute_delta_changed?(attr) ? delta_changed_attributes[attr] : __send__(attr)
end
attribute_delta_will_change!(attr, options = {}) click to toggle source

Handle *_will_change! for method_missing.

# File lib/delta_changes.rb, line 129
def attribute_delta_will_change!(attr, options = {})
  attribute_value = if self.class.delta_changes_options[:attributes].include?(attr)
    value = send(attr)
    value.duplicable? ? value.clone : value
  else
    options[:from] || (respond_to?(:clone_attribute_value) ? clone_attribute_value(:read_attribute, attr) : read_attribute(attr).dup)
  end
  delta_changed_attributes[attr] = attribute_value
end
delta_changed_attributes() click to toggle source

Map of change attr => original value.

# File lib/delta_changes.rb, line 109
def delta_changed_attributes
  @delta_changed_attributes ||= {}
end
delta_changes_field_changed?(attr, old, value) click to toggle source
# File lib/delta_changes.rb, line 139
def delta_changes_field_changed?(attr, old, value)
  return true if !old.present? && value.present?

  self[attr] != old
end