class Mobility::Plugins::ActiveModel::Dirty::MobilityMutationTracker

@note Seriously, I really don't want to reproduce all of

ActiveModel::Dirty here, but having fought with upstream changes
many many times I finally decided it's more future-proof to just
re-implement the stuff we need here, to avoid weird breakage.

Although this is somewhat ugly, at least it's explicit and since
it's self-defined (rather than hooking into fickle private methods
in Rails), it won't break all of a sudden. We just need to ensure
that specs are up-to-date with the latest weird dirty method
pattern Rails has decided to support.

Constants

OPTION_NOT_GIVEN

Attributes

current_changes[R]
model[R]
previous_changes[R]

Public Class Methods

new(model) click to toggle source
# File lib/mobility/plugins/active_model/dirty.rb, line 236
def initialize(model)
  @model = model
  @current_changes = {}.with_indifferent_access
  @previous_changes = {}.with_indifferent_access
end

Public Instance Methods

attribute_before_last_save(attr_name)
attribute_change(attr_name) click to toggle source
# File lib/mobility/plugins/active_model/dirty.rb, line 271
def attribute_change(attr_name)
  if attribute_changed?(attr_name)
    [attribute_was(attr_name), fetch_value(attr_name)]
  end
end
Also aliased as: attribute_change_to_be_saved
attribute_change_to_be_saved(attr_name)
Alias for: attribute_change
attribute_changed?(attr_name, from: OPTION_NOT_GIVEN, to: OPTION_NOT_GIVEN) click to toggle source
# File lib/mobility/plugins/active_model/dirty.rb, line 289
def attribute_changed?(attr_name, from: OPTION_NOT_GIVEN, to: OPTION_NOT_GIVEN)
  current_changes.include?(attr_name) &&
    (OPTION_NOT_GIVEN == from || attribute_was(attr_name) == from) &&
    (OPTION_NOT_GIVEN == to || fetch_value(attr_name) == to)
end
attribute_in_database(attr_name)
Alias for: attribute_was
attribute_previous_change(attr_name) click to toggle source
# File lib/mobility/plugins/active_model/dirty.rb, line 277
def attribute_previous_change(attr_name)
  previous_changes[attr_name]
end
Also aliased as: saved_change_to_attribute
attribute_previously_changed?(attr_name) click to toggle source
# File lib/mobility/plugins/active_model/dirty.rb, line 295
def attribute_previously_changed?(attr_name)
  previous_changes.include?(attr_name)
end
Also aliased as: saved_change_to_attribute?
attribute_previously_was(attr_name) click to toggle source
# File lib/mobility/plugins/active_model/dirty.rb, line 281
def attribute_previously_was(attr_name)
  if attribute_previously_changed?(attr_name)
    # Calling +first+ here fetches the value before change from the
    # hash.
    previous_changes[attr_name].first
  end
end
Also aliased as: attribute_before_last_save
attribute_was(attr_name) click to toggle source
# File lib/mobility/plugins/active_model/dirty.rb, line 299
def attribute_was(attr_name)
  if attribute_changed?(attr_name)
    current_changes[attr_name]
  else
    fetch_value(attr_name)
  end
end
Also aliased as: attribute_in_database
attribute_will_change!(attr_name) click to toggle source
# File lib/mobility/plugins/active_model/dirty.rb, line 307
def attribute_will_change!(attr_name)
  current_changes[attr_name] = fetch_value(attr_name) unless current_changes.include?(attr_name)
end
changed() click to toggle source
# File lib/mobility/plugins/active_model/dirty.rb, line 247
def changed
  attr_names.select { |attr_name| attribute_changed?(attr_name) }
end
changed?() click to toggle source
# File lib/mobility/plugins/active_model/dirty.rb, line 267
def changed?
  attr_names.any? { |attr| attribute_changed?(attr) }
end
changed_attributes() click to toggle source
# File lib/mobility/plugins/active_model/dirty.rb, line 251
def changed_attributes
  attr_names.each_with_object({}.with_indifferent_access) do |attr_name, result|
    if attribute_changed?(attr_name)
      result[attr_name] = attribute_was(attr_name)
    end
  end
end
changes() click to toggle source
# File lib/mobility/plugins/active_model/dirty.rb, line 259
def changes
  attr_names.each_with_object({}.with_indifferent_access) do |attr_name, result|
    if change = attribute_change(attr_name)
      result.merge!(attr_name => change)
    end
  end
end
finalize_changes() click to toggle source
# File lib/mobility/plugins/active_model/dirty.rb, line 242
def finalize_changes
  @previous_changes = changes
  @current_changes = {}.with_indifferent_access
end
restore_attribute!(attr_name) click to toggle source
# File lib/mobility/plugins/active_model/dirty.rb, line 311
def restore_attribute!(attr_name)
  current_changes.delete(attr_name)
end
saved_change_to_attribute(attr_name)
saved_change_to_attribute?(attr_name)

These are for ActiveRecord, but we'll define them here.

will_save_change_to_attribute?(attr_name, from: OPTION_NOT_GIVEN, to: OPTION_NOT_GIVEN)
Alias for: attribute_changed?

Private Instance Methods

attr_names() click to toggle source
# File lib/mobility/plugins/active_model/dirty.rb, line 326
def attr_names
  current_changes.keys
end
fetch_value(attr_name) click to toggle source
# File lib/mobility/plugins/active_model/dirty.rb, line 330
def fetch_value(attr_name)
  model.__send__(attr_name)
end