Merged updates from trunk into stable branch
[feedcatcher.git] / vendor / rails / activerecord / test / models / pirate.rb
1 class Pirate < ActiveRecord::Base
2 belongs_to :parrot
3 has_and_belongs_to_many :parrots
4 has_and_belongs_to_many :parrots_with_method_callbacks, :class_name => "Parrot",
5 :before_add => :log_before_add,
6 :after_add => :log_after_add,
7 :before_remove => :log_before_remove,
8 :after_remove => :log_after_remove
9 has_and_belongs_to_many :parrots_with_proc_callbacks, :class_name => "Parrot",
10 :before_add => proc {|p,pa| p.ship_log << "before_adding_proc_parrot_#{pa.id || '<new>'}"},
11 :after_add => proc {|p,pa| p.ship_log << "after_adding_proc_parrot_#{pa.id || '<new>'}"},
12 :before_remove => proc {|p,pa| p.ship_log << "before_removing_proc_parrot_#{pa.id}"},
13 :after_remove => proc {|p,pa| p.ship_log << "after_removing_proc_parrot_#{pa.id}"}
14
15 has_many :treasures, :as => :looter
16 has_many :treasure_estimates, :through => :treasures, :source => :price_estimates
17
18 # These both have :autosave enabled because accepts_nested_attributes_for is used on them.
19 has_one :ship
20 has_many :birds
21 has_many :birds_with_method_callbacks, :class_name => "Bird",
22 :before_add => :log_before_add,
23 :after_add => :log_after_add,
24 :before_remove => :log_before_remove,
25 :after_remove => :log_after_remove
26 has_many :birds_with_proc_callbacks, :class_name => "Bird",
27 :before_add => proc {|p,b| p.ship_log << "before_adding_proc_bird_#{b.id || '<new>'}"},
28 :after_add => proc {|p,b| p.ship_log << "after_adding_proc_bird_#{b.id || '<new>'}"},
29 :before_remove => proc {|p,b| p.ship_log << "before_removing_proc_bird_#{b.id}"},
30 :after_remove => proc {|p,b| p.ship_log << "after_removing_proc_bird_#{b.id}"}
31
32 accepts_nested_attributes_for :parrots, :birds, :allow_destroy => true, :reject_if => proc { |attributes| attributes.empty? }
33 accepts_nested_attributes_for :ship, :allow_destroy => true, :reject_if => proc { |attributes| attributes.empty? }
34 accepts_nested_attributes_for :parrots_with_method_callbacks, :parrots_with_proc_callbacks,
35 :birds_with_method_callbacks, :birds_with_proc_callbacks, :allow_destroy => true
36
37 validates_presence_of :catchphrase
38
39 def ship_log
40 @ship_log ||= []
41 end
42
43 private
44 def log_before_add(record)
45 log(record, "before_adding_method")
46 end
47
48 def log_after_add(record)
49 log(record, "after_adding_method")
50 end
51
52 def log_before_remove(record)
53 log(record, "before_removing_method")
54 end
55
56 def log_after_remove(record)
57 log(record, "after_removing_method")
58 end
59
60 def log(record, callback)
61 ship_log << "#{callback}_#{record.class.name.downcase}_#{record.id || '<new>'}"
62 end
63 end