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.



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

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
42
# 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
  define_validations model, reflection
  builder.define_extensions model
  reflection
end

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

Raises:

  • (ArgumentError)


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

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…



105
106
107
108
109
110
# File 'lib/active_record/associations/builder/association.rb', line 105

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



88
89
90
91
92
93
94
95
96
97
# File 'lib/active_record/associations/builder/association.rb', line 88

def self.define_callbacks(model, reflection)
  if dependent = reflection.options[:dependent]
    check_dependent_options(dependent)
    add_destroy_callbacks(model, reflection)
  end

  Association.extensions.each do |extension|
    extension.build model, reflection
  end
end

.define_readers(mixin, name) ⇒ Object



112
113
114
115
116
117
118
# File 'lib/active_record/associations/builder/association.rb', line 112

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

.define_validations(model, reflection) ⇒ Object



128
129
130
# File 'lib/active_record/associations/builder/association.rb', line 128

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

.define_writers(mixin, name) ⇒ Object



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

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)


132
133
134
# File 'lib/active_record/associations/builder/association.rb', line 132

def self.valid_dependent_options
  raise NotImplementedError
end

Instance Method Details

#build(model) ⇒ Object



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

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

#define_extensions(model) ⇒ Object



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

def define_extensions(model)
end

#macroObject

Raises:

  • (NotImplementedError)


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

def macro
  raise NotImplementedError
end

#valid_optionsObject



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

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

#validate_optionsObject



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

def validate_options
  options.assert_valid_keys(valid_options)
end