Sha256: 04bc4b8f21ecaedf39ae1760fe89bc74aa499919faf629ca4ead1fbd020b097c

Contents?: true

Size: 1.6 KB

Versions: 9

Compression:

Stored size: 1.6 KB

Contents

require 'active_data/model/associations/persistence_adapters/active_record/referenced_proxy'

module ActiveData
  module Model
    module Associations
      module PersistenceAdapters
        class ActiveRecord < Base
          TYPES = {
            integer: Integer,
            float: Float,
            decimal: BigDecimal,
            datetime: Time,
            timestamp: Time,
            time: Time,
            date: Date,
            text: String,
            string: String,
            binary: String,
            boolean: Boolean
          }.freeze

          alias_method :data_type, :data_source

          def build(attributes)
            data_source.new(attributes)
          end

          def persist(object, raise_error: false)
            raise_error ? object.save! : object.save
          end

          def scope(owner, source)
            scope = data_source.unscoped

            if scope_proc
              scope = if scope_proc.arity.zero?
                scope.instance_exec(&scope_proc)
              else
                scope.instance_exec(owner, &scope_proc)
              end
            end

            scope.where(primary_key => source)
          end

          def identify(object)
            object[primary_key] if object
          end

          def primary_key
            @primary_key ||= :id
          end

          def primary_key_type
            column = data_source.columns_hash[primary_key.to_s]
            TYPES[column.type]
          end

          def referenced_proxy(association)
            ReferencedProxy.new(association)
          end
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
active_data-1.2.0 lib/active_data/model/associations/persistence_adapters/active_record.rb
active_data-1.1.7 lib/active_data/model/associations/persistence_adapters/active_record.rb
active_data-1.1.6 lib/active_data/model/associations/persistence_adapters/active_record.rb
active_data-1.1.5 lib/active_data/model/associations/persistence_adapters/active_record.rb
active_data-1.1.4 lib/active_data/model/associations/persistence_adapters/active_record.rb
active_data-1.1.3 lib/active_data/model/associations/persistence_adapters/active_record.rb
active_data-1.1.2 lib/active_data/model/associations/persistence_adapters/active_record.rb
active_data-1.1.1 lib/active_data/model/associations/persistence_adapters/active_record.rb
active_data-1.1.0 lib/active_data/model/associations/persistence_adapters/active_record.rb