Sha256: b7d36c0c05a2fd8d474d69b564757d17d4a02a3f3526e0a5948224a86133da31

Contents?: true

Size: 1.59 KB

Versions: 3

Compression:

Stored size: 1.59 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 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.5.0 lib/granite/form/model/associations/persistence_adapters/active_record.rb
granite-form-0.4.0 lib/granite/form/model/associations/persistence_adapters/active_record.rb
granite-form-0.3.0 lib/granite/form/model/associations/persistence_adapters/active_record.rb