Sha256: 9985a68e825046d286a6a3cbb3eaf67cb78dec2590793943caafa4c45ef15ca5

Contents?: true

Size: 1.72 KB

Versions: 3

Compression:

Stored size: 1.72 KB

Contents

require 'granite/form/model/associations/persistence_adapters/active_record/referenced_proxy'

module Granite
  module Form
    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
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
granite-form-0.2.0 lib/granite/form/model/associations/persistence_adapters/active_record.rb
granite-form-0.1.1 lib/granite/form/model/associations/persistence_adapters/active_record.rb
granite-form-0.1.0 lib/granite/form/model/associations/persistence_adapters/active_record.rb