association.rb 3.3 KB
Newer Older
V
Vijay Dev 已提交
1 2 3 4
# This is the parent Association class which defines the variables
# used by all associations.
#
# The hierarchy is defined as follows:
5
#  Association
6
#    - SingularAssociation
V
Vijay Dev 已提交
7 8
#      - BelongsToAssociation
#      - HasOneAssociation
9
#    - CollectionAssociation
V
Vijay Dev 已提交
10
#      - HasManyAssociation
11

12 13
module ActiveRecord::Associations::Builder
  class Association #:nodoc:
14
    class << self
15
      attr_accessor :extensions
16
    end
17
    self.extensions = []
18

19
    VALID_OPTIONS = [:class_name, :class, :foreign_key, :validate]
20

21
    attr_reader :name, :scope, :options
22

23
    def self.build(model, name, scope, options, &block)
A
Aaron Patterson 已提交
24
      builder = create_builder model, name, scope, options, &block
25
      reflection = builder.build(model)
26
      builder.define_accessors model, reflection
27
      define_callbacks model, reflection
28
      builder.define_extensions model
29
      reflection
30
    end
31 32 33 34 35 36 37 38 39 40 41

    def self.create_builder(model, name, scope, options, &block)
      raise ArgumentError, "association names must be a Symbol" unless name.kind_of?(Symbol)

      if scope.is_a?(Hash)
        options = scope
        scope   = nil
      end

      new(name, scope, options, &block)
    end
42

43
    def initialize(name, scope, options)
44
      @name    = name
45 46
      @scope   = scope
      @options = options
J
Jon Leighton 已提交
47

48 49
      validate_options

50 51
      if scope && scope.arity == 0
        @scope = proc { instance_exec(&scope) }
J
Jon Leighton 已提交
52
      end
53 54
    end

55
    def build(model)
56
      ActiveRecord::Reflection.create(macro, name, scope, options, model)
57 58 59 60 61 62 63
    end

    def macro
      raise NotImplementedError
    end

    def valid_options
64
      VALID_OPTIONS + Association.extensions.flat_map(&:valid_options)
65 66
    end

J
Jon Leighton 已提交
67 68 69
    def validate_options
      options.assert_valid_keys(valid_options)
    end
70

71 72 73
    def define_extensions(model)
    end

74 75
    def self.define_callbacks(model, reflection)
      add_before_destroy_callbacks(model, reflection) if reflection.options[:dependent]
76
      Association.extensions.each do |extension|
77
        extension.build model, reflection
78 79 80
      end
    end

81 82 83 84
    # Defines the setter and getter methods for the association
    # class Post < ActiveRecord::Base
    #   has_many :comments
    # end
85
    #
86
    # Post.first.comments and Post.first.comments= methods are defined by this method...
87
    def define_accessors(model, reflection)
88
      mixin = model.generated_feature_methods
89
      name = reflection.name
90
      self.class.define_readers(mixin, name)
91
      self.class.define_writers(mixin, name)
J
Jon Leighton 已提交
92
    end
93

94
    def self.define_readers(mixin, name)
95 96 97 98 99
      mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
        def #{name}(*args)
          association(:#{name}).reader(*args)
        end
      CODE
J
Jon Leighton 已提交
100
    end
101

102
    def self.define_writers(mixin, name)
103 104 105 106 107
      mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
        def #{name}=(value)
          association(:#{name}).writer(value)
        end
      CODE
J
Jon Leighton 已提交
108
    end
109

110
    def self.valid_dependent_options
111 112 113 114 115
      raise NotImplementedError
    end

    private

116 117 118
    def self.add_before_destroy_callbacks(model, reflection)
      unless valid_dependent_options.include? reflection.options[:dependent]
        raise ArgumentError, "The :dependent option must be one of #{valid_dependent_options}, but is :#{reflection.options[:dependent]}"
119
      end
120

121
      name = reflection.name
122
      model.before_destroy lambda { |o| o.association(name).handle_dependency }
J
Jon Leighton 已提交
123
    end
124
  end
125
end