association.rb 3.1 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, :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

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

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

    def macro
      raise NotImplementedError
    end

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

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

69 70 71
    def define_extensions(model)
    end

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

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

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

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

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

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

    private

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

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