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
|
# File 'lib/acts_has_many/active_record/acts_has_many.rb', line 6
def acts_has_many *opt, &block
options = {compare: :title, through: false}
options.update opt.
options.assert_valid_keys :compare, :through
options[:relations] = opt
if options[:relations].blank?
options[:relations] = self.reflect_on_all_associations(:has_many).map(&:name)
end
dependent_relations = []
options[:relations].each do |relation|
if reflect_on_association relation.to_sym
dependent_relations << relation.to_s.tableize
else
raise ArgumentError, "No association found for name `#{relation}'. Has it been defined yet?"
end
end
if block_given?
self.class.send :define_method, :condition, block
else
self.class.send :define_method, :condition do |data|
where options[:compare] => data[options[:compare]]
end
end
class_eval " def self.dependent_relations\n \#{dependent_relations}\n end\n\n include ActiveRecord::ActsHasMany::Child\n \#{\"extend ActiveRecord::ActsHasMany::ChildThrough\" if options[:through]}\n\n \#{\"validates :\#{options[:compare]}, uniqueness: true\" unless block_given?}\n EOV\nend\n", __FILE__ , __LINE__ + 1
|