collection_association.rb 2.5 KB
Newer Older
1 2
# This class is inherited by the has_many and has_many_and_belongs_to_many association classes 

3 4
require 'active_record/associations'

5 6
module ActiveRecord::Associations::Builder
  class CollectionAssociation < Association #:nodoc:
7

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

10
    def valid_options
11
      super + [:table_name, :before_add,
J
Jon Leighton 已提交
12
               :after_add, :before_remove, :after_remove, :extend]
13
    end
14

15
    attr_reader :block_extension
16

17
    def initialize(name, scope, options)
18
      super
19 20 21 22 23
      @mod = nil
      if block_given?
        @mod = Module.new(&Proc.new)
        @scope = wrap_scope @scope, @mod
      end
24 25
    end

26 27 28 29 30
    def define_callbacks(model, reflection)
      super
      CALLBACKS.each { |callback_name| define_callback(model, callback_name) }
    end

31
    def define_extensions(model)
32
      if @mod
33
        extension_module_name = "#{model.name.demodulize}#{name.to_s.camelize}AssociationExtension"
34
        model.parent.const_set(extension_module_name, @mod)
35
      end
J
Jon Leighton 已提交
36
    end
37

38
    def define_callback(model, callback_name)
J
Jon Leighton 已提交
39
      full_callback_name = "#{callback_name}_for_#{name}"
40

J
Jon Leighton 已提交
41
      # TODO : why do i need method_defined? I think its because of the inheritance chain
A
Aaron Patterson 已提交
42
      model.class_attribute full_callback_name unless model.method_defined?(full_callback_name)
43 44 45 46 47 48 49
      callbacks = Array(options[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
A
Aaron Patterson 已提交
50
          ->(method, owner, record) { callback.send(method, owner, record) }
51 52 53
        end
      end
      model.send "#{full_callback_name}=", callbacks
J
Jon Leighton 已提交
54
    end
55

56 57
    # Defines the setter and getter methods for the collection_singular_ids.

58
    def define_readers(mixin)
J
Jon Leighton 已提交
59 60
      super

61 62 63 64 65
      mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
        def #{name.to_s.singularize}_ids
          association(:#{name}).ids_reader
        end
      CODE
J
Jon Leighton 已提交
66
    end
67

68
    def define_writers(mixin)
J
Jon Leighton 已提交
69
      super
70

71 72 73 74 75
      mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
        def #{name.to_s.singularize}_ids=(ids)
          association(:#{name}).ids_writer(ids)
        end
      CODE
J
Jon Leighton 已提交
76
    end
77 78 79 80 81 82 83 84 85 86 87 88

    private

    def wrap_scope(scope, mod)
      prev_scope = scope

      if prev_scope
        proc { |owner| instance_exec(owner, &prev_scope).extending(mod) }
      else
        proc { extending(mod) }
      end
    end
89 90
  end
end