Module: LedgerSync::ResourceAttribute::DirtyMixin

Defined in:
lib/ledger_sync/resource_attribute/dirty_mixin.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



7
8
9
10
# File 'lib/ledger_sync/resource_attribute/dirty_mixin.rb', line 7

def self.included(base)
  base.include(ActiveModel::Dirty)
  base.extend(ClassMethods)
end

Instance Method Details

#changes_to_hObject

Change the dirty change set of => [“Bill”, “Bob”] to current values of attributes that have changed: => “Bob”



38
39
40
# File 'lib/ledger_sync/resource_attribute/dirty_mixin.rb', line 38

def changes_to_h
  changes.transform_values(&:last)
end

#dirty_attributes_to_hObject



42
43
44
45
46
47
48
49
# File 'lib/ledger_sync/resource_attribute/dirty_mixin.rb', line 42

def dirty_attributes_to_h
  Hash[self.class.dirty_attributes.keys.map do |k|
    [
      k,
      public_send(k)
    ]
  end]
end

#saveObject

Normally you would just call ‘changes_applied`, but because we define an `@attributes` instance variable, the `ActiveModel::Dirty` mixin assumes it is a list of attributes in their format (spoiler: it isn’t). So this is copying the code from ‘changes_applied` and setting `@mutations_from_database` to the value it would receive if `@attributes` did not exist.



57
58
59
60
61
62
63
# File 'lib/ledger_sync/resource_attribute/dirty_mixin.rb', line 57

def save
  # changes_applied # Code reproduced below.
  @mutations_before_last_save = mutations_from_database
  # forget_attribute_assignments # skipped as our attributes do not implement this method
  @mutations_from_database = ActiveModel::ForcedMutationTracker.new(self) # Manually set to expected value
  resource_attributes.references_many.map(&:save)
end

#save!Object



65
66
67
# File 'lib/ledger_sync/resource_attribute/dirty_mixin.rb', line 65

def save!
  save
end