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

Constant Summary collapse

VALID_OPTIONS =

:nodoc:

[:class_name, :anonymous_class, :foreign_key, :validate]

Class Attribute Summary collapse

Class Method Summary collapse

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

Class Method Details

.add_destroy_callbacks(model, reflection) ⇒ Object



135
136
137
138
# File 'lib/active_record/associations/builder/association.rb', line 135

def self.add_destroy_callbacks(model, reflection)
  name = reflection.name
  model.before_destroy lambda { |o| o.association(name).handle_dependency }
end

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



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

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

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

.build_scope(scope, extension) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/active_record/associations/builder/association.rb', line 48

def self.build_scope(scope, extension)
  new_scope = scope

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

  if extension
    new_scope = wrap_scope new_scope, extension
  end

  new_scope
end

.check_dependent_options(dependent) ⇒ Object



129
130
131
132
133
# File 'lib/active_record/associations/builder/association.rb', line 129

def self.check_dependent_options(dependent)
  unless valid_dependent_options.include? dependent
    raise ArgumentError, "The :dependent option must be one of #{valid_dependent_options}, but is :#{dependent}"
  end
end

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

Raises:

  • (ArgumentError)


38
39
40
41
42
43
44
45
46
# File 'lib/active_record/associations/builder/association.rb', line 38

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

  validate_options(options)

  scope = build_scope(scope, extension)

  ActiveRecord::Reflection.create(macro, name, scope, 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…



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

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



81
82
83
84
85
86
87
88
89
90
# File 'lib/active_record/associations/builder/association.rb', line 81

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_extensions(model, name) ⇒ Object



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

def self.define_extensions(model, name)
end

.define_readers(mixin, name) ⇒ Object



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

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

.define_validations(model, reflection) ⇒ Object



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

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

.define_writers(mixin, name) ⇒ Object



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

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

.macroObject

Raises:

  • (NotImplementedError)


66
67
68
# File 'lib/active_record/associations/builder/association.rb', line 66

def self.macro
  raise NotImplementedError
end

.valid_dependent_optionsObject

Raises:

  • (NotImplementedError)


125
126
127
# File 'lib/active_record/associations/builder/association.rb', line 125

def self.valid_dependent_options
  raise NotImplementedError
end

.valid_options(options) ⇒ Object



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

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

.validate_options(options) ⇒ Object



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

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

.wrap_scope(scope, extension) ⇒ Object



62
63
64
# File 'lib/active_record/associations/builder/association.rb', line 62

def self.wrap_scope(scope, extension)
  scope
end