2007-10-26 01:56:46 -04:00
|
|
|
class Pirate < ActiveRecord::Base
|
2009-09-12 08:55:34 -04:00
|
|
|
belongs_to :parrot, :validate => true
|
|
|
|
belongs_to :non_validated_parrot, :class_name => 'Parrot'
|
2010-06-12 06:55:31 -04:00
|
|
|
has_and_belongs_to_many :parrots, :validate => true, :order => 'parrots.id ASC'
|
2009-09-12 08:55:34 -04:00
|
|
|
has_and_belongs_to_many :non_validated_parrots, :class_name => 'Parrot'
|
2009-03-12 11:24:37 -04:00
|
|
|
has_and_belongs_to_many :parrots_with_method_callbacks, :class_name => "Parrot",
|
|
|
|
:before_add => :log_before_add,
|
|
|
|
:after_add => :log_after_add,
|
|
|
|
:before_remove => :log_before_remove,
|
|
|
|
:after_remove => :log_after_remove
|
|
|
|
has_and_belongs_to_many :parrots_with_proc_callbacks, :class_name => "Parrot",
|
|
|
|
:before_add => proc {|p,pa| p.ship_log << "before_adding_proc_parrot_#{pa.id || '<new>'}"},
|
|
|
|
:after_add => proc {|p,pa| p.ship_log << "after_adding_proc_parrot_#{pa.id || '<new>'}"},
|
|
|
|
:before_remove => proc {|p,pa| p.ship_log << "before_removing_proc_parrot_#{pa.id}"},
|
|
|
|
:after_remove => proc {|p,pa| p.ship_log << "after_removing_proc_parrot_#{pa.id}"}
|
2008-02-01 22:27:31 -05:00
|
|
|
|
2009-03-12 11:24:37 -04:00
|
|
|
has_many :treasures, :as => :looter
|
2008-02-01 22:27:31 -05:00
|
|
|
has_many :treasure_estimates, :through => :treasures, :source => :price_estimates
|
2008-05-12 18:46:57 -04:00
|
|
|
|
2009-01-31 20:44:30 -05:00
|
|
|
# These both have :autosave enabled because accepts_nested_attributes_for is used on them.
|
2009-11-06 17:53:33 -05:00
|
|
|
has_one :ship
|
2009-12-28 15:08:20 -05:00
|
|
|
has_one :update_only_ship, :class_name => 'Ship'
|
2009-09-12 08:55:34 -04:00
|
|
|
has_one :non_validated_ship, :class_name => 'Ship'
|
2010-06-12 06:55:31 -04:00
|
|
|
has_many :birds, :order => 'birds.id ASC'
|
2009-03-12 11:24:37 -04:00
|
|
|
has_many :birds_with_method_callbacks, :class_name => "Bird",
|
|
|
|
:before_add => :log_before_add,
|
|
|
|
:after_add => :log_after_add,
|
|
|
|
:before_remove => :log_before_remove,
|
|
|
|
:after_remove => :log_after_remove
|
|
|
|
has_many :birds_with_proc_callbacks, :class_name => "Bird",
|
|
|
|
:before_add => proc {|p,b| p.ship_log << "before_adding_proc_bird_#{b.id || '<new>'}"},
|
|
|
|
:after_add => proc {|p,b| p.ship_log << "after_adding_proc_bird_#{b.id || '<new>'}"},
|
|
|
|
:before_remove => proc {|p,b| p.ship_log << "before_removing_proc_bird_#{b.id}"},
|
|
|
|
:after_remove => proc {|p,b| p.ship_log << "after_removing_proc_bird_#{b.id}"}
|
2009-05-09 23:02:00 -04:00
|
|
|
has_many :birds_with_reject_all_blank, :class_name => "Bird"
|
2009-01-31 20:44:30 -05:00
|
|
|
|
|
|
|
accepts_nested_attributes_for :parrots, :birds, :allow_destroy => true, :reject_if => proc { |attributes| attributes.empty? }
|
2009-02-08 08:23:35 -05:00
|
|
|
accepts_nested_attributes_for :ship, :allow_destroy => true, :reject_if => proc { |attributes| attributes.empty? }
|
2009-12-28 15:08:20 -05:00
|
|
|
accepts_nested_attributes_for :update_only_ship, :update_only => true
|
2009-03-12 11:24:37 -04:00
|
|
|
accepts_nested_attributes_for :parrots_with_method_callbacks, :parrots_with_proc_callbacks,
|
|
|
|
:birds_with_method_callbacks, :birds_with_proc_callbacks, :allow_destroy => true
|
2009-05-09 23:02:00 -04:00
|
|
|
accepts_nested_attributes_for :birds_with_reject_all_blank, :reject_if => :all_blank
|
2009-01-31 20:44:30 -05:00
|
|
|
|
2008-05-12 18:46:57 -04:00
|
|
|
validates_presence_of :catchphrase
|
2009-03-12 11:24:37 -04:00
|
|
|
|
|
|
|
def ship_log
|
|
|
|
@ship_log ||= []
|
|
|
|
end
|
|
|
|
|
2009-10-07 18:50:30 -04:00
|
|
|
def reject_empty_ships_on_create(attributes)
|
2010-11-07 09:05:18 -05:00
|
|
|
attributes.delete('_reject_me_if_new').present? && !persisted?
|
2009-10-07 18:50:30 -04:00
|
|
|
end
|
|
|
|
|
2010-01-08 14:47:49 -05:00
|
|
|
attr_accessor :cancel_save_from_callback
|
|
|
|
before_save :cancel_save_callback_method, :if => :cancel_save_from_callback
|
|
|
|
def cancel_save_callback_method
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2009-03-12 11:24:37 -04:00
|
|
|
private
|
|
|
|
def log_before_add(record)
|
|
|
|
log(record, "before_adding_method")
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_after_add(record)
|
|
|
|
log(record, "after_adding_method")
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_before_remove(record)
|
|
|
|
log(record, "before_removing_method")
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_after_remove(record)
|
|
|
|
log(record, "after_removing_method")
|
|
|
|
end
|
|
|
|
|
|
|
|
def log(record, callback)
|
|
|
|
ship_log << "#{callback}_#{record.class.name.downcase}_#{record.id || '<new>'}"
|
|
|
|
end
|
2007-10-26 01:56:46 -04:00
|
|
|
end
|