Class: Granite::Form::Model::Associations::PersistenceAdapters::ActiveRecord

Inherits:
Base
  • Object
show all
Defined in:
lib/granite/form/model/associations/persistence_adapters/active_record.rb,
lib/granite/form/model/associations/persistence_adapters/active_record/referenced_proxy.rb

Defined Under Namespace

Classes: ReferencedProxy

Constant Summary collapse

TYPES =
{
  integer: Integer,
  float: Float,
  decimal: BigDecimal,
  datetime: Time,
  timestamp: Time,
  time: Time,
  date: Date,
  text: String,
  string: String,
  binary: String,
  boolean: Boolean,
  enum: String
}.freeze

Instance Attribute Summary

Attributes inherited from Base

#data_source, #scope_proc

Instance Method Summary collapse

Methods inherited from Base

#find_all, #find_one, #initialize

Constructor Details

This class inherits a constructor from Granite::Form::Model::Associations::PersistenceAdapters::Base

Instance Method Details

#build(attributes) ⇒ Object



26
27
28
# File 'lib/granite/form/model/associations/persistence_adapters/active_record.rb', line 26

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

#identify(object) ⇒ Object



44
45
46
# File 'lib/granite/form/model/associations/persistence_adapters/active_record.rb', line 44

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

#primary_keyObject



48
49
50
# File 'lib/granite/form/model/associations/persistence_adapters/active_record.rb', line 48

def primary_key
  @primary_key ||= :id
end

#primary_key_typeObject



52
53
54
55
# File 'lib/granite/form/model/associations/persistence_adapters/active_record.rb', line 52

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

#referenced_proxy(association) ⇒ Object



57
58
59
# File 'lib/granite/form/model/associations/persistence_adapters/active_record.rb', line 57

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

#scope(owner, source) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/granite/form/model/associations/persistence_adapters/active_record.rb', line 30

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