Module: ActiveRecord::ActsHasMany::ClassMethods

Defined in:
lib/acts_has_many/active_record/acts_has_many.rb

Instance Method Summary collapse

Instance Method Details

#acts_has_many(*opt, &block) ⇒ Object



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.extract_options!
  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

#acts_has_many_for(*relations) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/acts_has_many/active_record/acts_has_many.rb', line 45

def acts_has_many_for *relations
  class_eval "    def self.dependent_relations\n      \#{relations}\n    end\n\n    attr_accessor :tmp_garbage\n\n    include ActiveRecord::ActsHasMany::Parent\n\n    after_save :clear_garbage\n  EOV\nend\n", __FILE__ , __LINE__ + 1