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

Inherits:
ReferencesAny
  • Object
show all
Includes:
Singular
Defined in:
lib/granite/form/model/associations/reflections/references_one.rb

Constant Summary

Constants inherited from Base

Base::READ, Base::WRITE

Instance Attribute Summary

Attributes inherited from Base

#name, #options, #parent_reflection

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Singular

#collection?

Methods inherited from ReferencesAny

#embedded?, #initialize, #inspect, #klass, #persistence_adapter, persistence_adapter, #read_source, #write_source

Methods inherited from Base

association_class, #belongs_to?, #build_association, #collection?, #default, generate_methods, #initialize, #klass, #macro, #read_source, #write_source

Constructor Details

This class inherits a constructor from Granite::Form::Model::Associations::Reflections::ReferencesAny

Class Method Details

.build(target, generated_methods, name, *args, &block) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/granite/form/model/associations/reflections/references_one.rb', line 12

def self.build(target, generated_methods, name, *args, &block)
  reflection = super

  target.add_attribute(
    Granite::Form::Model::Attributes::Reflections::ReferenceOne,
    reflection.reference_key,
    type: reflection.persistence_adapter.primary_key_type,
    association: name
  )

  reflection
end

Instance Method Details

#reference_keyObject



25
26
27
28
# File 'lib/granite/form/model/associations/reflections/references_one.rb', line 25

def reference_key
  @reference_key ||= options[:reference_key].presence.try(:to_sym) ||
                     :"#{name}_#{primary_key}"
end