Sha256: b6127bfaffdf434e97b9e9616befdf5fe6edc3b1f5c8491492647d5ea0364342

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 KB

Contents

module Undo
  module Serializer
    class ActiveModel
      VERSION = "0.0.2"

      def initialize(model_serializer = nil)
        @model_serializer = model_serializer
      end

      def serialize(object)
        model_serializer(object).as_json
      end

      def deserialize(hash)
        hash.each do |object_class, data|
          next unless data.is_a? Hash
          data.stringify_keys!

          object_class = object_class.to_s.camelize.constantize
          object = object_class.where(id: data.fetch("id")).first_or_initialize

          data.each do |field, value|
            next if "id" == field && object.persisted?
            _, association = field.to_s.split("___")
            if association
              deserialize_association(association, value)
            else
              object.send "#{field}=", value # not public_send!
            end
          end

          object.save!
          return object
        end
      end

      private
      def model_serializer(object)
        @model_serializer ||= object.active_model_serializer
        @model_serializer.new object
      end

      def deserialize_association(association, values)
        Array.wrap(values).each do |value|
          deserialize(association.singularize => value)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
undo-serializer-active_model-0.0.2 lib/undo/serializer/active_model.rb