Class: Houston::ActiveRecordSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/houston/boot/active_record_serializer.rb

Instance Method Summary collapse

Instance Method Details

#applies_to?(object) ⇒ Boolean

Returns:

  • (Boolean)


4
5
6
# File 'lib/houston/boot/active_record_serializer.rb', line 4

def applies_to?(object)
  object.is_a?(ActiveRecord::Base)
end

#pack(record) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/houston/boot/active_record_serializer.rb', line 8

def pack(record)
  model = record.class
  normal_attributes = record.attributes.each_with_object({}) do |(attribute, value), attributes|
    column = model.column_for_attribute(attribute)

    # Don't serialize the attribute if the column was deleted
    next if column.is_a?(ActiveRecord::ConnectionAdapters::NullColumn)

    attributes[attribute] = model.connection.type_cast_from_column(column, value)
  end
  { "class" => model.name, "attributes" => normal_attributes }
end

#unpack(object) ⇒ Object



21
22
23
24
# File 'lib/houston/boot/active_record_serializer.rb', line 21

def unpack(object)
  klass, attributes = object.values_at("class", "attributes")
  klass.constantize.instantiate(attributes)
end