Class: PaperTrail::AttributeSerializers::CastAttributeSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/paper_trail/attribute_serializers/cast_attribute_serializer.rb,
lib/paper_trail/attribute_serializers/cast_attribute_serializer.rb,
lib/paper_trail/attribute_serializers/cast_attribute_serializer.rb

Overview

This implementation uses AR 4.2’s ‘type_cast_for_database`. For versions of AR < 4.2 we provide an implementation of `type_cast_for_database` in our shim attribute type classes, `NoOpAttribute` and `SerializedAttribute`.

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ CastAttributeSerializer

Returns a new instance of CastAttributeSerializer.



15
16
17
# File 'lib/paper_trail/attribute_serializers/cast_attribute_serializer.rb', line 15

def initialize(klass)
  @klass = klass
end

Instance Method Details

#deserialize(attr, val) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/paper_trail/attribute_serializers/cast_attribute_serializer.rb', line 42

def deserialize(attr, val)
  if defined_enums[attr] && val.is_a?(::String)
    # Because PT 4 used to save the string version of enums to `object_changes`
    val
  else
    AttributeSerializerFactory.for(@klass, attr).deserialize(val)
  end
end

#serialize(attr, val) ⇒ Object



38
39
40
# File 'lib/paper_trail/attribute_serializers/cast_attribute_serializer.rb', line 38

def serialize(attr, val)
  AttributeSerializerFactory.for(@klass, attr).serialize(val)
end