Sha256: 44a9ec99c9ee879a67101c31b7c289079ac0b1d2f2c43de4a4375d4e6a268919

Contents?: true

Size: 1007 Bytes

Versions: 1

Compression:

Stored size: 1007 Bytes

Contents

module Granite
  module Form
    module Model
      module Attributes
        class ReferenceOne < Base
          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

          def read
            if association.target
              association.identify
            else
              type_casted_value
            end
          end

          def type_casted_value
            variable_cache(:value) do
              type_definition.prepare(read_before_type_cast)
            end
          end

          def read_before_type_cast
            @value_cache
          end

          private

          def association
            @association ||= owner.association(reflection.association)
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
granite-form-0.6.0 lib/granite/form/model/attributes/reference_one.rb