Sha256: 4f903dfdc7187f34b825ed14bb1d305cc81d0ee20f6f6ddb8a6538afae043e1e

Contents?: true

Size: 858 Bytes

Versions: 5

Compression:

Stored size: 858 Bytes

Contents

module Houston
  class ActiveRecordSerializer

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

    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

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

  end
end

Houston.add_serializer Houston::ActiveRecordSerializer.new

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
houston-core-0.8.3 lib/houston/boot/active_record_serializer.rb
houston-core-0.8.2 lib/houston/boot/active_record_serializer.rb
houston-core-0.8.1 lib/houston/boot/active_record_serializer.rb
houston-core-0.8.0 lib/houston/boot/active_record_serializer.rb
houston-core-0.8.0.pre2 lib/houston/boot/active_record_serializer.rb