Class: ActiveRecord::Associations::Builder::Association
- Inherits:
-
Object
- Object
- ActiveRecord::Associations::Builder::Association
show all
- Defined in:
- lib/active_record/associations/builder/association.rb
Overview
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.
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/active_record/associations/builder/association.rb', line 15
def initialize(model, name, scope, options)
raise ArgumentError, "association names must be a Symbol" unless name.kind_of?(Symbol)
@model = model
@name = name
if scope.is_a?(Hash)
@scope = nil
@options = scope
else
@scope = scope
@options = options
end
if @scope && @scope.arity == 0
prev_scope = @scope
@scope = proc { instance_exec(&prev_scope) }
end
end
|
Class Attribute Details
.valid_options ⇒ Object
Returns the value of attribute valid_options.
4
5
6
|
# File 'lib/active_record/associations/builder/association.rb', line 4
def valid_options
@valid_options
end
|
Instance Attribute Details
#model ⇒ Object
Returns the value of attribute model.
9
10
11
|
# File 'lib/active_record/associations/builder/association.rb', line 9
def model
@model
end
|
#name ⇒ Object
Returns the value of attribute name.
9
10
11
|
# File 'lib/active_record/associations/builder/association.rb', line 9
def name
@name
end
|
#options ⇒ Object
Returns the value of attribute options.
9
10
11
|
# File 'lib/active_record/associations/builder/association.rb', line 9
def options
@options
end
|
#reflection ⇒ Object
Returns the value of attribute reflection.
9
10
11
|
# File 'lib/active_record/associations/builder/association.rb', line 9
def reflection
@reflection
end
|
#scope ⇒ Object
Returns the value of attribute scope.
9
10
11
|
# File 'lib/active_record/associations/builder/association.rb', line 9
def scope
@scope
end
|
Class Method Details
.build(*args, &block) ⇒ Object
11
12
13
|
# File 'lib/active_record/associations/builder/association.rb', line 11
def self.build(*args, &block)
new(*args, &block).build
end
|
Instance Method Details
#build ⇒ Object
41
42
43
44
45
46
47
48
|
# File 'lib/active_record/associations/builder/association.rb', line 41
def build
validate_options
define_accessors
configure_dependency if options[:dependent]
@reflection = model.create_reflection(macro, name, scope, options, model)
super @reflection
end
|
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/active_record/associations/builder/association.rb', line 83
def configure_dependency
unless valid_dependent_options.include? options[:dependent]
raise ArgumentError, "The :dependent option must be one of #{valid_dependent_options}, but is :#{options[:dependent]}"
end
if options[:dependent] == :restrict
ActiveSupport::Deprecation.warn(
"The :restrict option is deprecated. Please use :restrict_with_exception instead, which " \
"provides the same functionality."
)
end
mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
def #{macro}_dependent_for_#{name}
association(:#{name}).handle_dependency
end
CODE
model.before_destroy "#{macro}_dependent_for_#{name}"
end
|
#define_accessors ⇒ Object
62
63
64
65
|
# File 'lib/active_record/associations/builder/association.rb', line 62
def define_accessors
define_readers
define_writers
end
|
#define_readers ⇒ Object
67
68
69
70
71
72
73
|
# File 'lib/active_record/associations/builder/association.rb', line 67
def define_readers
mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
def #{name}(*args)
association(:#{name}).reader(*args)
end
CODE
end
|
#define_writers ⇒ Object
75
76
77
78
79
80
81
|
# File 'lib/active_record/associations/builder/association.rb', line 75
def define_writers
mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
def #{name}=(value)
association(:#{name}).writer(value)
end
CODE
end
|
#macro ⇒ Object
50
51
52
|
# File 'lib/active_record/associations/builder/association.rb', line 50
def macro
raise NotImplementedError
end
|
#mixin ⇒ Object
35
36
37
|
# File 'lib/active_record/associations/builder/association.rb', line 35
def mixin
@model.generated_feature_methods
end
|
#valid_dependent_options ⇒ Object
104
105
106
|
# File 'lib/active_record/associations/builder/association.rb', line 104
def valid_dependent_options
raise NotImplementedError
end
|
#valid_options ⇒ Object
54
55
56
|
# File 'lib/active_record/associations/builder/association.rb', line 54
def valid_options
Association.valid_options
end
|
#validate_options ⇒ Object
58
59
60
|
# File 'lib/active_record/associations/builder/association.rb', line 58
def validate_options
options.assert_valid_keys(valid_options)
end
|