Class: ActiveRecord::Associations::Builder::CollectionAssociation
- Inherits:
-
Association
- Object
- Association
- ActiveRecord::Associations::Builder::CollectionAssociation
- Defined in:
- activerecord/lib/active_record/associations/builder/collection_association.rb
Overview
:nodoc:
Direct Known Subclasses
Constant Summary collapse
- CALLBACKS =
[:before_add, :after_add, :before_remove, :after_remove]
Instance Attribute Summary collapse
-
#block_extension ⇒ Object
readonly
Returns the value of attribute block_extension.
-
#extension_module ⇒ Object
readonly
Returns the value of attribute extension_module.
Attributes inherited from Association
#model, #name, #options, #scope
Instance Method Summary collapse
- #build ⇒ Object
- #define_callback(callback_name) ⇒ Object
-
#define_readers ⇒ Object
Defines the setter and getter methods for the collection_singular_ids.
- #define_writers ⇒ Object
- #extension_module_name ⇒ Object
-
#initialize(*args, &extension) ⇒ CollectionAssociation
constructor
A new instance of CollectionAssociation.
- #valid_options ⇒ Object
- #wrap_block_extension ⇒ Object
- #writable? ⇒ Boolean
Methods inherited from Association
build, #configure_dependency, #define_accessors, #macro, #mixin, #valid_dependent_options, #validate_options
Constructor Details
#initialize(*args, &extension) ⇒ CollectionAssociation
Returns a new instance of CollectionAssociation.
17 18 19 20 |
# File 'activerecord/lib/active_record/associations/builder/collection_association.rb', line 17 def initialize(*args, &extension) super(*args) @block_extension = extension end |
Instance Attribute Details
#block_extension ⇒ Object (readonly)
Returns the value of attribute block_extension
15 16 17 |
# File 'activerecord/lib/active_record/associations/builder/collection_association.rb', line 15 def block_extension @block_extension end |
#extension_module ⇒ Object (readonly)
Returns the value of attribute extension_module
15 16 17 |
# File 'activerecord/lib/active_record/associations/builder/collection_association.rb', line 15 def extension_module @extension_module end |
Instance Method Details
#build ⇒ Object
22 23 24 25 26 27 |
# File 'activerecord/lib/active_record/associations/builder/collection_association.rb', line 22 def build wrap_block_extension reflection = super CALLBACKS.each { |callback_name| define_callback(callback_name) } reflection end |
#define_callback(callback_name) ⇒ Object
54 55 56 57 58 59 60 |
# File 'activerecord/lib/active_record/associations/builder/collection_association.rb', line 54 def define_callback(callback_name) full_callback_name = "#{callback_name}_for_#{name}" # TODO : why do i need method_defined? I think its because of the inheritance chain model.class_attribute full_callback_name.to_sym unless model.method_defined?(full_callback_name) model.send("#{full_callback_name}=", Array([callback_name.to_sym])) end |
#define_readers ⇒ Object
Defines the setter and getter methods for the collection_singular_ids.
64 65 66 67 68 69 70 71 72 |
# File 'activerecord/lib/active_record/associations/builder/collection_association.rb', line 64 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
74 75 76 77 78 79 80 81 82 |
# File 'activerecord/lib/active_record/associations/builder/collection_association.rb', line 74 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
50 51 52 |
# File 'activerecord/lib/active_record/associations/builder/collection_association.rb', line 50 def extension_module_name @extension_module_name ||= "#{model.name.demodulize}#{name.to_s.camelize}AssociationExtension" end |
#valid_options ⇒ Object
10 11 12 13 |
# File 'activerecord/lib/active_record/associations/builder/collection_association.rb', line 10 def super + [:table_name, :before_add, :after_add, :before_remove, :after_remove, :extend] end |
#wrap_block_extension ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'activerecord/lib/active_record/associations/builder/collection_association.rb', line 33 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
29 30 31 |
# File 'activerecord/lib/active_record/associations/builder/collection_association.rb', line 29 def writable? true end |