1 class Pirate
< ActiveRecord
::Base
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}"}
15 has_many
:treasures, :as => :looter
16 has_many
:treasure_estimates, :through => :treasures, :source => :price_estimates
18 # These both have :autosave enabled because accepts_nested_attributes_for is used on them.
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}"}
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
37 validates_presence_of
:catchphrase
44 def log_before_add(record
)
45 log(record
, "before_adding_method")
48 def log_after_add(record
)
49 log(record
, "after_adding_method")
52 def log_before_remove(record
)
53 log(record
, "before_removing_method")
56 def log_after_remove(record
)
57 log(record
, "after_removing_method")
60 def log(record
, callback
)
61 ship_log
<< "#{callback}_#{record.class.name.downcase}_#{record.id || '<new>'}"