Sha256: e9f438555ac776643a179ef48c0cfa5b691a7f688c79461f5592cbe717187f8b

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 KB

Contents

module Superstore
  module Persistence
    extend ActiveSupport::Concern

    module ClassMethods
      def find_by_id(id)
        find_by(id: id)
      end

      def _insert_record(attributes)
        id = attributes.fetch(primary_key)

        adapter.insert table_name, id, serialize_attributes(attributes)
      end

      def _update_record(attributes, constraints)
        id = constraints.fetch(primary_key)

        adapter.update table_name, id, serialize_attributes(attributes)
      end

      if Rails.version >= '6.0'
        def instantiate_instance_of(klass, attributes, column_types = {}, &block)
          if attributes[superstore_column].is_a?(String)
            attributes = JSON.parse(attributes[superstore_column]).merge('id' => attributes['id'])
          end

          super(klass, attributes, column_types, &block)
        end
        private :instantiate_instance_of
      else
        def instantiate(attributes, column_types = {}, &block)
          if attributes[superstore_column].is_a?(String)
            attributes = JSON.parse(attributes[superstore_column]).merge('id' => attributes['id'])
          end

          super(attributes, column_types, &block)
        end
      end

      def serialize_attributes(attributes)
        serialized = {}
        attributes.each do |attr_name, value|
          next if attr_name == primary_key
          serialized[attr_name] = attribute_types[attr_name].serialize(value)
        end
        serialized
      end

      private

        def adapter
          @adapter ||= Superstore::Adapters::JsonbAdapter.new
        end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
superstore-2.5.0 lib/superstore/persistence.rb