Class: DuckRecord::Associations::Builder::EmbedsOne

Inherits:
EmbedsAssociation show all
Defined in:
lib/duck_record/associations/builder/embeds_one.rb

Overview

:nodoc:

Constant Summary

Constants inherited from EmbedsAssociation

DuckRecord::Associations::Builder::EmbedsAssociation::VALID_OPTIONS

Class Method Summary collapse

Methods inherited from EmbedsAssociation

build, create_reflection, define_callbacks, define_extensions, define_readers, define_writers, valid_options, validate_options

Class Method Details

.define_accessors(model, reflection) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/duck_record/associations/builder/embeds_one.rb', line 17

def self.define_accessors(model, reflection)
  super
  mixin = model.generated_association_methods
  name = reflection.name

  define_constructors(mixin, name) if reflection.constructable?
end

.define_constructors(mixin, name) ⇒ Object

Defines the (build|create)_association methods for belongs_to or has_one association



26
27
28
29
30
31
32
# File 'lib/duck_record/associations/builder/embeds_one.rb', line 26

def self.define_constructors(mixin, name)
  mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
    def build_#{name}(*args, &block)
      association(:#{name}).build(*args, &block)
    end
  CODE
end

.define_validations(model, reflection) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/duck_record/associations/builder/embeds_one.rb', line 9

def self.define_validations(model, reflection)
  super

  if reflection.options[:required]
    model.validates_presence_of reflection.name, message: :required
  end
end

.macroObject

:nodoc:



5
6
7
# File 'lib/duck_record/associations/builder/embeds_one.rb', line 5

def self.macro
  :embeds_one
end