Sha256: 64685b3263b283e468a990205d508ab5f5016c0bb9f71953f96cefb717e787ad

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 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

      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 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

          if inheritance_column && attribute_types.key?(inheritance_column)
            klass = find_sti_class(attributes[inheritance_column])
          end

          attributes.each_key { |k, v| attributes.delete(k) unless klass.attribute_types.key?(k) }

          super(klass, attributes, column_types, &block)
        end

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

Version data entries

1 entries across 1 versions & 1 rubygems

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