Class: ActiveRecord::Associations::Builder::CollectionAssociation

Inherits:
Association show all
Defined in:
activerecord/lib/active_record/associations/builder/collection_association.rb

Overview

:nodoc:

Direct Known Subclasses

HasAndBelongsToMany, HasMany

Constant Summary collapse

CALLBACKS =
[:before_add, :after_add, :before_remove, :after_remove]

Instance Attribute Summary collapse

Attributes inherited from Association

#model, #name, #options, #scope

Instance Method Summary collapse

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_extensionObject (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_moduleObject (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

#buildObject



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(options[callback_name.to_sym]))
end

#define_readersObject

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_writersObject



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_nameObject



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_optionsObject



10
11
12
13
# File 'activerecord/lib/active_record/associations/builder/collection_association.rb', line 10

def valid_options
  super + [:table_name, :before_add,
           :after_add, :before_remove, :after_remove, :extend]
end

#wrap_block_extensionObject



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

Returns:

  • (Boolean)


29
30
31
# File 'activerecord/lib/active_record/associations/builder/collection_association.rb', line 29

def writable?
  true
end