Class: Granite::Form::Model::Associations::Reflections::EmbedsOne

Inherits:
EmbedsAny
  • Object
show all
Includes:
Singular
Defined in:
lib/granite/form/model/associations/reflections/embeds_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

Methods included from Singular

#collection?

Methods inherited from EmbedsAny

#embedded?, #inspect, #klass

Methods inherited from Base

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

Constructor Details

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

Class Method Details

.build(target, generated_methods, name, options = {}, &block) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/granite/form/model/associations/reflections/embeds_one.rb', line 9

def self.build(target, generated_methods, name, options = {}, &block)
  if target < Granite::Form::Model::Attributes
    target.add_attribute(Granite::Form::Model::Attributes::Reflections::Base, name,
                         type: Object)
  end
  options[:validate] = true unless options.key?(:validate)
  super
end

.generate_methods(name, target) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/granite/form/model/associations/reflections/embeds_one.rb', line 18

def self.generate_methods(name, target)
  super

  target.class_eval <<-RUBY, __FILE__, __LINE__ + 1
    def build_#{name} attributes = {}
      association(:#{name}).build(attributes)
    end
  RUBY
end