-
Notifications
You must be signed in to change notification settings - Fork 21.6k
/
association.rb
55 lines (44 loc) · 1.26 KB
/
association.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
module ActiveRecord::Associations::Builder
class Association #:nodoc:
class_attribute :valid_options
self.valid_options = [:class_name, :foreign_key, :select, :conditions, :include, :extend, :readonly, :validate]
# Set by subclasses
class_attribute :macro
attr_reader :model, :name, :options, :reflection
def self.build(model, name, options)
new(model, name, options).build
end
def initialize(model, name, options)
@model, @name, @options = model, name, options
end
def mixin
@model.generated_feature_methods
end
def build
validate_options
reflection = model.create_reflection(self.class.macro, name, options, model)
define_accessors
reflection
end
private
def validate_options
options.assert_valid_keys(self.class.valid_options)
end
def define_accessors
define_readers
define_writers
end
def define_readers
name = self.name
mixin.redefine_method(name) do |*params|
association(name).reader(*params)
end
end
def define_writers
name = self.name
mixin.redefine_method("#{name}=") do |value|
association(name).writer(value)
end
end
end
end