Class: Granite::Form::Model::Associations::ReferencesOne

Inherits:
ReferencesAny show all
Defined in:
lib/granite/form/model/associations/references_one.rb

Instance Attribute Summary

Attributes inherited from Base

#owner, #reflection

Instance Method Summary collapse

Methods inherited from ReferencesAny

#scope

Methods inherited from Base

#callback, #evar_loaded?, #initialize, #inspect, #loaded!, #loaded?, #reload, #reset, #target, #transaction

Constructor Details

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

Instance Method Details

#defaultObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/granite/form/model/associations/references_one.rb', line 16

def default
  return if evar_loaded?

  default = reflection.default(owner)

  return unless default

  case default
  when reflection.persistence_adapter.data_type
    default
  when Hash
    build_object(default)
  else
    reflection.persistence_adapter.find_one(owner, default)
  end
end

#identifyObject



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

def identify
  reflection.persistence_adapter.identify(target)
end

#load_targetObject



11
12
13
14
# File 'lib/granite/form/model/associations/references_one.rb', line 11

def load_target
  source = read_source
  source ? reflection.persistence_adapter.find_one(owner, source) : default
end

#reader(force_reload = false) ⇒ Object



33
34
35
36
# File 'lib/granite/form/model/associations/references_one.rb', line 33

def reader(force_reload = false)
  reset if force_reload
  target
end

#replace(object) ⇒ Object Also known as: writer



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/granite/form/model/associations/references_one.rb', line 38

def replace(object)
  raise_type_mismatch(object) unless object.nil? || matches_type?(object)

  transaction do
    attribute.pollute do
      self.target = object
      write_source identify
    end
  end

  target
end

#target=(object) ⇒ Object



6
7
8
9
# File 'lib/granite/form/model/associations/references_one.rb', line 6

def target=(object)
  loaded!
  @target = object
end