Class: Granite::Form::Model::Associations::EmbedsOne
- Inherits:
-
EmbedsAny
- Object
- Base
- EmbedsAny
- Granite::Form::Model::Associations::EmbedsOne
show all
- Defined in:
- lib/granite/form/model/associations/embeds_one.rb
Instance Attribute Summary
Attributes inherited from Base
#owner, #reflection
Instance Method Summary
collapse
Methods inherited from Base
#callback, #evar_loaded?, #initialize, #inspect, #loaded!, #loaded?, #reload, #reset, #target, #transaction
Instance Method Details
#build(attributes = {}) ⇒ Object
6
7
8
|
# File 'lib/granite/form/model/associations/embeds_one.rb', line 6
def build(attributes = {})
self.target = build_object(attributes)
end
|
#clear ⇒ Object
47
48
49
50
51
|
# File 'lib/granite/form/model/associations/embeds_one.rb', line 47
def clear
target
@target = nil
true
end
|
#default ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/granite/form/model/associations/embeds_one.rb', line 25
def default
return if evar_loaded?
default = reflection.default(owner)
return unless default
object = if default.is_a?(reflection.klass)
default
else
reflection.klass.with_sanitize(false) do
build_object(default)
end
end
object.send(:clear_changes_information) if reflection.klass.dirty?
object
end
|
#load_target ⇒ Object
20
21
22
23
|
# File 'lib/granite/form/model/associations/embeds_one.rb', line 20
def load_target
source = read_source
source ? reflection.klass.instantiate(source) : default
end
|
#reader(force_reload = false) ⇒ Object
53
54
55
56
|
# File 'lib/granite/form/model/associations/embeds_one.rb', line 53
def reader(force_reload = false)
reload if force_reload
target
end
|
#replace(object) ⇒ Object
Also known as:
writer
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/granite/form/model/associations/embeds_one.rb', line 58
def replace(object)
if object
raise AssociationTypeMismatch.new(reflection.klass, object.class) unless object.is_a?(reflection.klass)
transaction do
clear
self.target = object
end
else
clear
end
target
end
|
#sync ⇒ Object
43
44
45
|
# File 'lib/granite/form/model/associations/embeds_one.rb', line 43
def sync
write_source(model_data(target))
end
|
#target=(object) ⇒ Object
10
11
12
13
14
15
16
17
18
|
# File 'lib/granite/form/model/associations/embeds_one.rb', line 10
def target=(object)
if object
callback(:before_add, object)
setup_performers! object
end
loaded!
@target = object
callback(:after_add, object) if object
end
|