Class: DuckRecord::Associations::Builder::SingularAssociation

Inherits:
Association
  • Object
show all
Defined in:
lib/duck_record/associations/builder/singular_association.rb

Overview

:nodoc:

Direct Known Subclasses

HasOne

Constant Summary

Constants inherited from Association

Association::VALID_OPTIONS

Class Method Summary collapse

Methods inherited from Association

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

Class Method Details

.define_accessors(model, reflection) ⇒ Object

:nodoc:



5
6
7
8
9
10
11
# File 'lib/duck_record/associations/builder/singular_association.rb', line 5

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



14
15
16
17
18
19
20
# File 'lib/duck_record/associations/builder/singular_association.rb', line 14

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