Module: ActiveRecord::AttributeMethods::Serialization::Behavior
- Extended by:
- ActiveSupport::Concern
- Defined in:
- 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
- #_field_changed?(attr, old, value) ⇒ Boolean
- #attributes_before_type_cast ⇒ Object
- #attributes_for_coder ⇒ Object
- #keys_for_partial_write ⇒ Object
- #raw_type_cast_attribute_for_write(column, value) ⇒ Object
- #read_attribute_before_type_cast(attr_name) ⇒ Object
- #should_record_timestamps? ⇒ Boolean
- #type_cast_attribute_for_write(column, value) ⇒ Object
- #typecasted_attribute_value(name) ⇒ Object
Instance Method Details
#_field_changed?(attr, old, value) ⇒ Boolean
158 159 160 161 162 163 164 |
# File 'lib/active_record/attribute_methods/serialization.rb', line 158 def _field_changed?(attr, old, value) if self.class.serialized_attributes.include?(attr) old != value else super end end |
#attributes_before_type_cast ⇒ Object
174 175 176 177 178 179 180 181 182 |
# File 'lib/active_record/attribute_methods/serialization.rb', line 174 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 |
#attributes_for_coder ⇒ Object
192 193 194 195 196 197 198 199 200 |
# File 'lib/active_record/attribute_methods/serialization.rb', line 192 def attributes_for_coder attribute_names.each_with_object({}) do |name, attrs| attrs[name] = if self.class.serialized_attributes.include?(name) @attributes[name].serialized_value else read_attribute(name) end end end |
#keys_for_partial_write ⇒ Object
138 139 140 |
# File 'lib/active_record/attribute_methods/serialization.rb', line 138 def keys_for_partial_write super | (attributes.keys & self.class.serialized_attributes.keys) end |
#raw_type_cast_attribute_for_write(column, value) ⇒ Object
150 151 152 153 154 155 156 |
# File 'lib/active_record/attribute_methods/serialization.rb', line 150 def raw_type_cast_attribute_for_write(column, value) if column && coder = self.class.serialized_attributes[column.name] Attribute.new(coder, value, :serialized) else super end end |
#read_attribute_before_type_cast(attr_name) ⇒ Object
166 167 168 169 170 171 172 |
# File 'lib/active_record/attribute_methods/serialization.rb', line 166 def read_attribute_before_type_cast(attr_name) if self.class.serialized_attributes.include?(attr_name) super.unserialized_value else super end end |
#should_record_timestamps? ⇒ Boolean
134 135 136 |
# File 'lib/active_record/attribute_methods/serialization.rb', line 134 def super || (self. && (attributes.keys & self.class.serialized_attributes.keys).present?) end |
#type_cast_attribute_for_write(column, value) ⇒ Object
142 143 144 145 146 147 148 |
# File 'lib/active_record/attribute_methods/serialization.rb', line 142 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
184 185 186 187 188 189 190 |
# File 'lib/active_record/attribute_methods/serialization.rb', line 184 def typecasted_attribute_value(name) if self.class.serialized_attributes.include?(name) @attributes[name].serialized_value else super end end |