Module: ActiveRecord::AttributeMethods::Serialization::Behavior

Extended by:
ActiveSupport::Concern
Defined in:
activerecord/lib/active_record/attribute_methods/serialization.rb

Overview

This is only added to the model when serialize is called, which ensures we do not make things slower when serialization is not used.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Methods included from ActiveSupport::Concern

append_features, extended, included

Instance Method Details

#_field_changed?(attr, old, value) ⇒ Boolean

Returns:

  • (Boolean)

119
120
121
122
123
124
125
# File 'activerecord/lib/active_record/attribute_methods/serialization.rb', line 119

def _field_changed?(attr, old, value)
  if self.class.serialized_attributes.include?(attr)
    old != value
  else
    super
  end
end

#attributes_before_type_castObject


135
136
137
138
139
140
141
142
143
# File 'activerecord/lib/active_record/attribute_methods/serialization.rb', line 135

def attributes_before_type_cast
  super.dup.tap do |attributes|
    self.class.serialized_attributes.each_key do |key|
      if attributes.key?(key)
        attributes[key] = attributes[key].unserialized_value
      end
    end
  end
end

#read_attribute_before_type_cast(attr_name) ⇒ Object


127
128
129
130
131
132
133
# File 'activerecord/lib/active_record/attribute_methods/serialization.rb', line 127

def read_attribute_before_type_cast(attr_name)
  if self.class.serialized_attributes.include?(attr_name)
    super.unserialized_value
  else
    super
  end
end

#type_cast_attribute_for_write(column, value) ⇒ Object


111
112
113
114
115
116
117
# File 'activerecord/lib/active_record/attribute_methods/serialization.rb', line 111

def type_cast_attribute_for_write(column, value)
  if column && coder = self.class.serialized_attributes[column.name]
    Attribute.new(coder, value, :unserialized)
  else
    super
  end
end

#typecasted_attribute_value(name) ⇒ Object


145
146
147
148
149
150
151
# File 'activerecord/lib/active_record/attribute_methods/serialization.rb', line 145

def typecasted_attribute_value(name)
  if self.class.serialized_attributes.include?(name)
    @attributes[name].serialized_value
  else
    super
  end
end