association.rb 3.0 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 11
#      - HasManyAssociation
#      - HasAndBelongsToManyAssociation
12

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

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

22
    attr_reader :name, :scope, :options
23

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

27 28 29 30 31
      if scope.is_a?(Hash)
        options = scope
        scope   = nil
      end

32 33
      builder = new(name, scope, options, &block)
      reflection = builder.build(model)
34
      builder.define_accessors model
35
      builder.define_callbacks model, reflection
36
      builder.define_extensions model
37
      reflection
38 39
    end

40
    def initialize(name, scope, options)
41
      @name    = name
42 43
      @scope   = scope
      @options = options
J
Jon Leighton 已提交
44

45 46
      validate_options

47 48
      if scope && scope.arity == 0
        @scope = proc { instance_exec(&scope) }
J
Jon Leighton 已提交
49
      end
50 51
    end

52
    def build(model)
53
      ActiveRecord::Reflection.create(macro, name, scope, options, model)
54 55 56 57 58 59 60
    end

    def macro
      raise NotImplementedError
    end

    def valid_options
61
      VALID_OPTIONS + Association.extensions.flat_map(&:valid_options)
62 63
    end

J
Jon Leighton 已提交
64 65 66
    def validate_options
      options.assert_valid_keys(valid_options)
    end
67

68 69 70
    def define_extensions(model)
    end

71
    def define_callbacks(model, reflection)
72
      add_before_destroy_callbacks(model, name) if options[:dependent]
73
      Association.extensions.each do |extension|
74
        extension.build model, reflection
75 76 77
      end
    end

78 79 80 81
    # Defines the setter and getter methods for the association
    # class Post < ActiveRecord::Base
    #   has_many :comments
    # end
82
    #
83
    # Post.first.comments and Post.first.comments= methods are defined by this method...
84

85 86
    def define_accessors(model)
      mixin = model.generated_feature_methods
87 88
      define_readers(mixin)
      define_writers(mixin)
J
Jon Leighton 已提交
89
    end
90

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

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

107 108 109 110 111 112 113
    def valid_dependent_options
      raise NotImplementedError
    end

    private

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

118
      model.before_destroy lambda { |o| o.association(name).handle_dependency }
J
Jon Leighton 已提交
119
    end
120
  end
121
end