Class: ActiveRecord::Associations::Builder::CollectionAssociation
- Inherits:
-
Association
- Object
- Association
- ActiveRecord::Associations::Builder::CollectionAssociation
- Defined in:
- 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.
Attributes inherited from Association
Class Method Summary collapse
- .define_callback(model, callback_name, name, options) ⇒ Object
- .define_callbacks(model, reflection) ⇒ Object
-
.define_readers(mixin, name) ⇒ Object
Defines the setter and getter methods for the collection_singular_ids.
- .define_writers(mixin, name) ⇒ Object
Instance Method Summary collapse
- #define_extensions(model) ⇒ Object
-
#initialize(model, name, scope, options) ⇒ CollectionAssociation
constructor
A new instance of CollectionAssociation.
- #valid_options ⇒ Object
Methods inherited from Association
build, #build, create_builder, define_accessors, #macro, valid_dependent_options, #validate_options
Constructor Details
#initialize(model, name, scope, options) ⇒ CollectionAssociation
Returns a new instance of CollectionAssociation.
17 18 19 20 21 22 23 24 |
# File 'lib/active_record/associations/builder/collection_association.rb', line 17 def initialize(model, name, scope, ) super @mod = nil if block_given? @mod = Module.new(&Proc.new) @scope = wrap_scope @scope, @mod end end |
Instance Attribute Details
#block_extension ⇒ Object (readonly)
Returns the value of attribute block_extension.
15 16 17 |
# File 'lib/active_record/associations/builder/collection_association.rb', line 15 def block_extension @block_extension end |
Class Method Details
.define_callback(model, callback_name, name, options) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/active_record/associations/builder/collection_association.rb', line 42 def self.define_callback(model, callback_name, 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 unless model.method_defined?(full_callback_name) callbacks = Array([callback_name.to_sym]).map do |callback| case callback when Symbol ->(method, owner, record) { owner.send(callback, record) } when Proc ->(method, owner, record) { callback.call(owner, record) } else ->(method, owner, record) { callback.send(method, owner, record) } end end model.send "#{full_callback_name}=", callbacks end |
.define_callbacks(model, reflection) ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/active_record/associations/builder/collection_association.rb', line 26 def self.define_callbacks(model, reflection) super name = reflection.name = reflection. CALLBACKS.each { |callback_name| define_callback(model, callback_name, name, ) } end |
.define_readers(mixin, name) ⇒ Object
Defines the setter and getter methods for the collection_singular_ids.
61 62 63 64 65 66 67 68 69 |
# File 'lib/active_record/associations/builder/collection_association.rb', line 61 def self.define_readers(mixin, name) super mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1 def #{name.to_s.singularize}_ids association(:#{name}).ids_reader end CODE end |
.define_writers(mixin, name) ⇒ Object
71 72 73 74 75 76 77 78 79 |
# File 'lib/active_record/associations/builder/collection_association.rb', line 71 def self.define_writers(mixin, name) super mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1 def #{name.to_s.singularize}_ids=(ids) association(:#{name}).ids_writer(ids) end CODE end |
Instance Method Details
#define_extensions(model) ⇒ Object
35 36 37 38 39 40 |
# File 'lib/active_record/associations/builder/collection_association.rb', line 35 def define_extensions(model) if @mod extension_module_name = "#{model.name.demodulize}#{name.to_s.camelize}AssociationExtension" model.parent.const_set(extension_module_name, @mod) end end |
#valid_options ⇒ Object
10 11 12 13 |
# File 'lib/active_record/associations/builder/collection_association.rb', line 10 def super + [:table_name, :before_add, :after_add, :before_remove, :after_remove, :extend] end |