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



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

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

#identify(object) ⇒ Object



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

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

#primary_keyObject



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

def primary_key
  @primary_key ||= :id
end

#primary_key_typeObject



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

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

#referenced_proxy(association) ⇒ Object



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

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

#scope(owner, source) ⇒ Object



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

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