Class: DuckRecord::Associations::Builder::Association

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

Overview

:nodoc:

Direct Known Subclasses

CollectionAssociation, SingularAssociation

Constant Summary collapse

VALID_OPTIONS =

:nodoc:

[:class_name, :anonymous_class, :validate]

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.extensionsObject

Returns the value of attribute extensions.



15
16
17
# File 'lib/duck_record/associations/builder/association.rb', line 15

def extensions
  @extensions
end

Class Method Details

.build(model, name, options, &block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/duck_record/associations/builder/association.rb', line 21

def self.build(model, name, options, &block)
  if model.dangerous_attribute_method?(name)
    raise ArgumentError, "You tried to define an association named #{name} on the model #{model.name}, but " \
                         "this will conflict with a method #{name} already defined by Active Record. " \
                         "Please choose a different association name."
  end

  extension = define_extensions model, name, &block
  reflection = create_reflection model, name, options, extension
  define_accessors model, reflection
  define_callbacks model, reflection
  define_validations model, reflection
  reflection
end

.create_reflection(model, name, options, extension = nil) ⇒ Object

Raises:

  • (ArgumentError)


36
37
38
39
40
41
42
# File 'lib/duck_record/associations/builder/association.rb', line 36

def self.create_reflection(model, name, options, extension = nil)
  raise ArgumentError, "association names must be a Symbol" unless name.kind_of?(Symbol)

  validate_options(options)

  DuckRecord::Reflection.create(macro, name, options, model)
end

.define_accessors(model, reflection) ⇒ Object

Defines the setter and getter methods for the association class Post < ActiveRecord::Base

has_many :comments

end

Post.first.comments and Post.first.comments= methods are defined by this method…



71
72
73
74
75
76
# File 'lib/duck_record/associations/builder/association.rb', line 71

def self.define_accessors(model, reflection)
  mixin = model.generated_association_methods
  name = reflection.name
  define_readers(mixin, name)
  define_writers(mixin, name)
end

.define_callbacks(model, reflection) ⇒ Object



59
60
61
62
63
# File 'lib/duck_record/associations/builder/association.rb', line 59

def self.define_callbacks(model, reflection)
  Association.extensions.each do |extension|
    extension.build model, reflection
  end
end

.define_extensions(model, name) ⇒ Object



56
57
# File 'lib/duck_record/associations/builder/association.rb', line 56

def self.define_extensions(model, name)
end

.define_readers(mixin, name) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/duck_record/associations/builder/association.rb', line 78

def self.define_readers(mixin, name)
  mixin.class_eval "    def \#{name}(*args)\n      association(:\#{name}).reader(*args)\n    end\n  CODE\nend\n", __FILE__, __LINE__ + 1

.define_validations(model, reflection) ⇒ Object



94
95
96
# File 'lib/duck_record/associations/builder/association.rb', line 94

def self.define_validations(model, reflection)
  # noop
end

.define_writers(mixin, name) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/duck_record/associations/builder/association.rb', line 86

def self.define_writers(mixin, name)
  mixin.class_eval "    def \#{name}=(value)\n      association(:\#{name}).writer(value)\n    end\n  CODE\nend\n", __FILE__, __LINE__ + 1

.macroObject

Raises:

  • (NotImplementedError)


44
45
46
# File 'lib/duck_record/associations/builder/association.rb', line 44

def self.macro
  raise NotImplementedError
end

.valid_options(options) ⇒ Object



48
49
50
# File 'lib/duck_record/associations/builder/association.rb', line 48

def self.valid_options(options)
  VALID_OPTIONS + Association.extensions.flat_map(&:valid_options)
end

.validate_options(options) ⇒ Object



52
53
54
# File 'lib/duck_record/associations/builder/association.rb', line 52

def self.validate_options(options)
  options.assert_valid_keys(valid_options(options))
end