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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/record_with_operator.rb', line 9
def self.included(base)
class << base
def reflections_with_operator
create_operator_associations
reflections_without_operator
end
alias_method_chain :reflections, :operator
def operator_associations_created?
@operator_associations_created
end
def create_operator_associations
return if operator_associations_created?
@operator_associations_created = true
if self.table_exists?
belongs_to :creator, {:foreign_key => "created_by", :class_name => RecordWithOperator.config[:user_class_name]}.merge(RecordWithOperator.config[:operator_association_options]) if column_names.include?('created_by')
belongs_to :updater, {:foreign_key => "updated_by", :class_name => RecordWithOperator.config[:user_class_name]}.merge(RecordWithOperator.config[:operator_association_options]) if column_names.include?('updated_by')
belongs_to :deleter, {:foreign_key => "deleted_by", :class_name => RecordWithOperator.config[:user_class_name]}.merge(RecordWithOperator.config[:operator_association_options]) if column_names.include?('deleted_by')
end
end
def has_many_with_operator(*args, &extension)
options = args.
if options[:extend]
options[:extend] = [options[:extend]] unless options[:extend].kind_of? Array
options[:extend] << AssociationWithOperator
else
options[:extend] = AssociationWithOperator
end
if options[:before_add]
options[:before_add] = [options[:before_add]] unless options[:before_add].kind_of? Array
options[:before_add] << :set_operator
else
options[:before_add] = :set_operator
end
args << options
has_many_without_operator(*args, &extension)
end
alias_method_chain :has_many, :operator
def find_with_for(*args)
options = args.
operator = options.delete(:for)
args << options
results = find_without_for(*args)
if operator
if results.kind_of? Array
results.each{|r| r.operator = operator}
elsif results
results.operator = operator
end
end
results
end
alias_method_chain :find, :for
def validate_find_options_with_for(options)
if options
options = options.dup
options.delete(:for)
end
validate_find_options_without_for(options)
end
alias_method_chain :validate_find_options, :for
private
def association_accessor_methods_with_operator(reflection, association_proxy_class)
association_accessor_methods_without_operator(reflection, association_proxy_class)
define_method("#{reflection.name}_with_operator") do |*params|
r = send("#{reflection.name}_without_operator", *params)
r.operator ||= self.operator if r && r.respond_to?(:operator=)
r
end
alias_method_chain "#{reflection.name}".to_sym, :operator
define_method("#{reflection.name}_with_operator=") do |new_value|
new_value.operator ||= self.operator if new_value.respond_to?(:operator=)
send("#{reflection.name}_without_operator=", new_value)
end
alias_method_chain "#{reflection.name}=".to_sym, :operator
end
alias_method_chain :association_accessor_methods, :operator
def association_constructor_method_with_operator(constructor, reflection, association_proxy_class)
association_constructor_method_without_operator(constructor, reflection, association_proxy_class)
define_method("#{constructor}_#{reflection.name}_with_operator") do |*params|
options = { :operator => self.operator }
params.empty? ? params[0] = options : params.first.merge!(options)
self.send("#{constructor}_#{reflection.name}_without_operator", *params)
end
alias_method_chain "#{constructor}_#{reflection.name}".to_sym, :operator
end
alias_method_chain :association_constructor_method, :operator
end
base.before_create :set_created_by
base.before_save :set_updated_by
base.before_destroy :set_deleted_by
end
|