Module: IronTrail::ChangeModelConcern

Extended by:
ActiveSupport::Concern
Defined in:
lib/iron_trail/change_model_concern.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#compute_changesetObject

This mimics the method with the same name available in the papertrail gem. It is an extended rec_delta, where attributes values are properly deserialized as rails’ ActiveRecord would do.

For instance, timestamps are serialized as strings in JSON, so rec_delta would return strings for timestamps. Using this method, it’d return a proper timestamp deserialized from the string.

This method doesn’t do caching and always computes the full thing. It’s up to the user to perform caching if wanted.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/iron_trail/change_model_concern.rb', line 36

def compute_changeset
  return nil unless update_operation?

  klass = rec_class

  HashWithIndifferentAccess.new.tap do |changes|
    rec_delta.each do |col_name, in_delta|
      type_class = klass.type_for_attribute(col_name)
      out_delta = in_delta.map { |val| type_class.deserialize(val) }

      changes[col_name] = out_delta
    end
  end
end

#delete_operation?Boolean

Returns:

  • (Boolean)


9
# File 'lib/iron_trail/change_model_concern.rb', line 9

def delete_operation? = (operation == 'd')

#insert_operation?Boolean

Returns:

  • (Boolean)


7
# File 'lib/iron_trail/change_model_concern.rb', line 7

def insert_operation? = (operation == 'i')

#rec_classObject

We don’t store the class name of the object, but we do store the rec_table. This method infers the class name from the rec_table and also the “type” attribute in the stored object in case it’s a rails STI class.

It returns the class instance. Raises an error in case the class couldn’t be inferred.



21
22
23
24
# File 'lib/iron_trail/change_model_concern.rb', line 21

def rec_class
  source_attributes = (delete_operation? ? rec_old : rec_new)
  Reifier.model_from_table_name(rec_table, source_attributes.fetch('type', nil))
end

#reifyObject



11
12
13
# File 'lib/iron_trail/change_model_concern.rb', line 11

def reify
  Reifier.reify(self)
end

#update_operation?Boolean

Returns:

  • (Boolean)


8
# File 'lib/iron_trail/change_model_concern.rb', line 8

def update_operation? = (operation == 'u')