Class: Granite::Form::Model::Attributes::ReferenceOne
- Inherits:
-
Base
- Object
- Base
- Granite::Form::Model::Attributes::ReferenceOne
show all
- Defined in:
- lib/granite/form/model/attributes/reference_one.rb
Instance Attribute Summary
Attributes inherited from Base
#type_definition
Instance Method Summary
collapse
Methods inherited from Base
#came_from_default?, #came_from_user?, #initialize, #inspect_attribute, #pollute, #query, #readonly?, #reset, #value_present?, #write_value
Instance Method Details
#read ⇒ Object
17
18
19
20
21
22
23
|
# File 'lib/granite/form/model/attributes/reference_one.rb', line 17
def read
if association.target
association.identify
else
type_casted_value
end
end
|
#read_before_type_cast ⇒ Object
31
32
33
|
# File 'lib/granite/form/model/attributes/reference_one.rb', line 31
def read_before_type_cast
@value_cache
end
|
#type_casted_value ⇒ Object
25
26
27
28
29
|
# File 'lib/granite/form/model/attributes/reference_one.rb', line 25
def type_casted_value
variable_cache(:value) do
type_definition.prepare(read_before_type_cast)
end
end
|
#write(value) ⇒ Object
6
7
8
9
10
11
12
13
14
15
|
# File 'lib/granite/form/model/attributes/reference_one.rb', line 6
def write(value)
pollute do
previous = type_casted_value
result = write_value value
changed = (!value.nil? && type_casted_value.nil?) || type_casted_value != previous
association.reset if changed
result
end
end
|