module Draftsman::AttributesSerialization

Constants

DESERIALIZE
NO_OP_ATTRIBUTE

Public Instance Methods

alter_attributes_for_draftsman(serializer, attributes) click to toggle source
# File lib/draftsman/attributes_serialization.rb, line 59
def alter_attributes_for_draftsman(serializer, attributes)
  # Don't serialize before values before inserting into columns of type
  # `JSON` on `PostgreSQL` databases.
  return attributes if self.draft_class.object_col_is_json?

  attributes.each do |key, value|
    attributes[key] = type_for_attribute(key).send(serializer, value)
  end
end
alter_draft_attribute_changes(serializer, changes) click to toggle source
# File lib/draftsman/attributes_serialization.rb, line 78
def alter_draft_attribute_changes(serializer, changes)
  # Don't serialize before values before inserting into columns of type
  # `JSON` on `PostgreSQL` databases.
  return changes if self.draft_class.object_changes_col_is_json?

  changes.clone.each do |key, change|
    type = type_for_attribute(key)
    changes[key] = Array(change).map { |value| type.send(serializer, value) }
  end
end
serialize_attributes_for_draftsman(attributes) click to toggle source

Used for `Version#object` attribute.

# File lib/draftsman/attributes_serialization.rb, line 51
def serialize_attributes_for_draftsman(attributes)
  alter_attributes_for_draftsman(SERIALIZE, attributes)
end
serialize_draft_attribute_changes(changes) click to toggle source

Used for Version#object_changes attribute.

# File lib/draftsman/attributes_serialization.rb, line 70
def serialize_draft_attribute_changes(changes)
  alter_draft_attribute_changes(SERIALIZE, changes)
end
serialized_attribute_types() click to toggle source
# File lib/draftsman/attributes_serialization.rb, line 42
def serialized_attribute_types
  @attribute_types ||= Hash[serialized_attributes.map do |attr_name, coder|
    [attr_name, SerializedAttribute.new(coder)]
  end]
end
type_for_attribute(attr_name) click to toggle source

Backport Rails 4.2 and later's `type_for_attribute` to build on a common interface.

# File lib/draftsman/attributes_serialization.rb, line 38
def type_for_attribute(attr_name)
  serialized_attribute_types[attr_name.to_s] || NO_OP_ATTRIBUTE
end
unserialize_attributes_for_draftsman(attributes) click to toggle source
# File lib/draftsman/attributes_serialization.rb, line 55
def unserialize_attributes_for_draftsman(attributes)
  alter_attributes_for_draftsman(DESERIALIZE, attributes)
end
unserialize_draft_attribute_changes(changes) click to toggle source
# File lib/draftsman/attributes_serialization.rb, line 74
def unserialize_draft_attribute_changes(changes)
  alter_draft_attribute_changes(DESERIALIZE, changes)
end