Class: Granite::Form::Model::Attributes::ReferenceOne

Inherits:
Base
  • Object
show all
Defined in:
lib/granite/form/model/attributes/reference_one.rb

Direct Known Subclasses

ReferenceMany

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

Constructor Details

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

Instance Method Details

#readObject



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_castObject



31
32
33
# File 'lib/granite/form/model/attributes/reference_one.rb', line 31

def read_before_type_cast
  @value_cache
end

#type_casted_valueObject



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