Class: ActiveRecord::Associations::Builder::Association

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

Overview

:nodoc:

Direct Known Subclasses

CollectionAssociation, SingularAssociation

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, name, scope, options) ⇒ Association

Returns a new instance of Association.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/active_record/associations/builder/association.rb', line 49

def initialize(model, name, scope, options)
  # TODO: Move this to create_builder as soon we drop support to activerecord-deprecated_finders.
  if scope.is_a?(Hash)
    options = scope
    scope   = nil
  end

  # TODO: Remove this model argument as soon we drop support to activerecord-deprecated_finders.
  @name    = name
  @scope   = scope
  @options = options

  validate_options

  if scope && scope.arity == 0
    @scope = proc { instance_exec(&scope) }
  end
end

Class Attribute Details

.extensionsObject

Returns the value of attribute extensions.



17
18
19
# File 'lib/active_record/associations/builder/association.rb', line 17

def extensions
  @extensions
end

.valid_optionsObject

TODO: This class accessor is needed to make activerecord-deprecated_finders work. We can move it to a constant in 5.0.



20
21
22
# File 'lib/active_record/associations/builder/association.rb', line 20

def valid_options
  @valid_options
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



26
27
28
# File 'lib/active_record/associations/builder/association.rb', line 26

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



26
27
28
# File 'lib/active_record/associations/builder/association.rb', line 26

def options
  @options
end

#scopeObject (readonly)

Returns the value of attribute scope.



26
27
28
# File 'lib/active_record/associations/builder/association.rb', line 26

def scope
  @scope
end

Class Method Details

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



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/active_record/associations/builder/association.rb', line 28

def self.build(model, name, scope, 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

  builder = create_builder model, name, scope, options, &block
  reflection = builder.build(model)
  define_accessors model, reflection
  define_callbacks model, reflection
  builder.define_extensions model
  reflection
end

.create_builder(model, name, scope, options, &block) ⇒ Object

Raises:

  • (ArgumentError)


43
44
45
46
47
# File 'lib/active_record/associations/builder/association.rb', line 43

def self.create_builder(model, name, scope, options, &block)
  raise ArgumentError, "association names must be a Symbol" unless name.kind_of?(Symbol)

  new(model, name, scope, options, &block)
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…



100
101
102
103
104
105
# File 'lib/active_record/associations/builder/association.rb', line 100

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



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

def self.define_callbacks(model, reflection)
  add_before_destroy_callbacks(model, reflection) if reflection.options[:dependent]
  Association.extensions.each do |extension|
    extension.build model, reflection
  end
end

.define_readers(mixin, name) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/active_record/associations/builder/association.rb', line 107

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

.define_writers(mixin, name) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/active_record/associations/builder/association.rb', line 115

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

.valid_dependent_optionsObject

Raises:

  • (NotImplementedError)


123
124
125
# File 'lib/active_record/associations/builder/association.rb', line 123

def self.valid_dependent_options
  raise NotImplementedError
end

Instance Method Details

#build(model) ⇒ Object



68
69
70
# File 'lib/active_record/associations/builder/association.rb', line 68

def build(model)
  ActiveRecord::Reflection.create(macro, name, scope, options, model)
end

#define_extensions(model) ⇒ Object



84
85
# File 'lib/active_record/associations/builder/association.rb', line 84

def define_extensions(model)
end

#macroObject

Raises:

  • (NotImplementedError)


72
73
74
# File 'lib/active_record/associations/builder/association.rb', line 72

def macro
  raise NotImplementedError
end

#valid_optionsObject



76
77
78
# File 'lib/active_record/associations/builder/association.rb', line 76

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

#validate_optionsObject



80
81
82
# File 'lib/active_record/associations/builder/association.rb', line 80

def validate_options
  options.assert_valid_keys(valid_options)
end