Class: ActiveRecord::Associations::Builder::CollectionAssociation
- Inherits:
-
Association
- Object
- Association
- ActiveRecord::Associations::Builder::CollectionAssociation
show all
- Defined in:
- lib/active_record/associations/builder/collection_association.rb
Overview
Constant Summary
collapse
- CALLBACKS =
[:before_add, :after_add, :before_remove, :after_remove]
Instance Attribute Summary collapse
Attributes inherited from Association
#model, #name, #options, #reflection, #scope
Instance Method Summary
collapse
Methods inherited from Association
build, #configure_dependency, #define_accessors, #macro, #mixin, #valid_dependent_options, #validate_options
Constructor Details
Returns a new instance of CollectionAssociation.
15
16
17
18
|
# File 'lib/active_record/associations/builder/collection_association.rb', line 15
def initialize(*args, &extension)
super(*args)
@block_extension = extension
end
|
Instance Attribute Details
#block_extension ⇒ Object
Returns the value of attribute block_extension.
13
14
15
|
# File 'lib/active_record/associations/builder/collection_association.rb', line 13
def block_extension
@block_extension
end
|
#extension_module ⇒ Object
Returns the value of attribute extension_module.
13
14
15
|
# File 'lib/active_record/associations/builder/collection_association.rb', line 13
def extension_module
@extension_module
end
|
Instance Method Details
#build ⇒ Object
20
21
22
23
24
25
26
|
# File 'lib/active_record/associations/builder/collection_association.rb', line 20
def build
show_deprecation_warnings
wrap_block_extension
reflection = super
CALLBACKS.each { |callback_name| define_callback(callback_name) }
reflection
end
|
#define_callback(callback_name) ⇒ Object
61
62
63
64
65
66
67
|
# File 'lib/active_record/associations/builder/collection_association.rb', line 61
def define_callback(callback_name)
full_callback_name = "#{callback_name}_for_#{name}"
model.class_attribute full_callback_name.to_sym unless model.method_defined?(full_callback_name)
model.send("#{full_callback_name}=", Array(options[callback_name.to_sym]))
end
|
#define_readers ⇒ Object
69
70
71
72
73
74
75
76
77
|
# File 'lib/active_record/associations/builder/collection_association.rb', line 69
def define_readers
super
mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
def #{name.to_s.singularize}_ids
association(:#{name}).ids_reader
end
CODE
end
|
#define_writers ⇒ Object
79
80
81
82
83
84
85
86
87
|
# File 'lib/active_record/associations/builder/collection_association.rb', line 79
def define_writers
super
mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
def #{name.to_s.singularize}_ids=(ids)
association(:#{name}).ids_writer(ids)
end
CODE
end
|
#extension_module_name ⇒ Object
57
58
59
|
# File 'lib/active_record/associations/builder/collection_association.rb', line 57
def extension_module_name
@extension_module_name ||= "#{model.name.demodulize}#{name.to_s.camelize}AssociationExtension"
end
|
#show_deprecation_warnings ⇒ Object
32
33
34
35
36
37
38
|
# File 'lib/active_record/associations/builder/collection_association.rb', line 32
def show_deprecation_warnings
[:finder_sql, :counter_sql].each do |name|
if options.include? name
ActiveSupport::Deprecation.warn("The :#{name} association option is deprecated. Please find an alternative (such as using scopes).")
end
end
end
|
#valid_options ⇒ Object
8
9
10
11
|
# File 'lib/active_record/associations/builder/collection_association.rb', line 8
def valid_options
super + [:table_name, :finder_sql, :counter_sql, :before_add,
:after_add, :before_remove, :after_remove, :extend]
end
|
#wrap_block_extension ⇒ Object
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/active_record/associations/builder/collection_association.rb', line 40
def wrap_block_extension
if block_extension
@extension_module = mod = Module.new(&block_extension)
silence_warnings do
model.parent.const_set(extension_module_name, mod)
end
prev_scope = @scope
if prev_scope
@scope = proc { |owner| instance_exec(owner, &prev_scope).extending(mod) }
else
@scope = proc { extending(mod) }
end
end
end
|
#writable? ⇒ Boolean
28
29
30
|
# File 'lib/active_record/associations/builder/collection_association.rb', line 28
def writable?
true
end
|