Class: ActiveData::Model::Associations::PersistenceAdapters::ActiveRecord

Inherits:
Base
  • Object
show all
Defined in:
lib/active_data/model/associations/persistence_adapters/active_record.rb,
lib/active_data/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 ActiveData::Model::Associations::PersistenceAdapters::Base

Instance Method Details

#build(attributes) ⇒ Object



24
25
26
# File 'lib/active_data/model/associations/persistence_adapters/active_record.rb', line 24

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

#identify(object) ⇒ Object



46
47
48
# File 'lib/active_data/model/associations/persistence_adapters/active_record.rb', line 46

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

#persist(object, raise_error: false) ⇒ Object



28
29
30
# File 'lib/active_data/model/associations/persistence_adapters/active_record.rb', line 28

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

#primary_keyObject



50
51
52
# File 'lib/active_data/model/associations/persistence_adapters/active_record.rb', line 50

def primary_key
  @primary_key ||= :id
end

#primary_key_typeObject



54
55
56
57
# File 'lib/active_data/model/associations/persistence_adapters/active_record.rb', line 54

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

#referenced_proxy(association) ⇒ Object



59
60
61
# File 'lib/active_data/model/associations/persistence_adapters/active_record.rb', line 59

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

#scope(owner, source) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/active_data/model/associations/persistence_adapters/active_record.rb', line 32

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