2017-07-09 17:41:28 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2007-10-26 05:56:46 +00:00
|
|
|
class Pirate < ActiveRecord::Base
|
2016-08-06 17:37:57 +00:00
|
|
|
belongs_to :parrot, validate: true
|
|
|
|
belongs_to :non_validated_parrot, class_name: "Parrot"
|
|
|
|
has_and_belongs_to_many :parrots, -> { order("parrots.id ASC") }, validate: true
|
|
|
|
has_and_belongs_to_many :non_validated_parrots, class_name: "Parrot"
|
|
|
|
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",
|
2016-10-29 03:05:58 +00:00
|
|
|
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}" }
|
2014-02-03 13:48:59 +00:00
|
|
|
has_and_belongs_to_many :autosaved_parrots, class_name: "Parrot", autosave: true
|
2008-02-02 03:27:31 +00:00
|
|
|
|
2018-07-27 06:50:45 +00:00
|
|
|
module PostTreasuresExtension
|
|
|
|
def build(attributes = {})
|
|
|
|
super({ name: "from extension" }.merge(attributes))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
has_many :treasures, as: :looter, extend: PostTreasuresExtension
|
2016-08-06 17:37:57 +00:00
|
|
|
has_many :treasure_estimates, through: :treasures, source: :price_estimates
|
2008-05-12 22:46:57 +00:00
|
|
|
|
2009-11-06 22:53:33 +00:00
|
|
|
has_one :ship
|
2016-08-06 17:37:57 +00:00
|
|
|
has_one :update_only_ship, class_name: "Ship"
|
|
|
|
has_one :non_validated_ship, class_name: "Ship"
|
2016-08-06 16:26:20 +00:00
|
|
|
has_many :birds, -> { order("birds.id ASC") }
|
2016-08-06 17:37:57 +00: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",
|
2016-10-29 03:05:58 +00:00
|
|
|
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}" }
|
2016-08-06 17:37:57 +00:00
|
|
|
has_many :birds_with_reject_all_blank, class_name: "Bird"
|
2009-02-01 01:44:30 +00:00
|
|
|
|
2016-08-06 17:37:57 +00:00
|
|
|
has_one :foo_bulb, -> { where name: "foo" }, foreign_key: :car_id, class_name: "Bulb"
|
2011-01-23 21:29:36 +00:00
|
|
|
|
2016-08-06 17:37:57 +00:00
|
|
|
accepts_nested_attributes_for :parrots, :birds, allow_destroy: true, reject_if: proc(&:empty?)
|
|
|
|
accepts_nested_attributes_for :ship, allow_destroy: true, reject_if: proc(&:empty?)
|
|
|
|
accepts_nested_attributes_for :update_only_ship, update_only: true
|
2009-03-12 15:24:37 +00:00
|
|
|
accepts_nested_attributes_for :parrots_with_method_callbacks, :parrots_with_proc_callbacks,
|
2016-08-06 17:37:57 +00:00
|
|
|
:birds_with_method_callbacks, :birds_with_proc_callbacks, allow_destroy: true
|
|
|
|
accepts_nested_attributes_for :birds_with_reject_all_blank, reject_if: :all_blank
|
2009-02-01 01:44:30 +00:00
|
|
|
|
2008-05-12 22:46:57 +00:00
|
|
|
validates_presence_of :catchphrase
|
2009-03-12 15:24:37 +00:00
|
|
|
|
|
|
|
def ship_log
|
|
|
|
@ship_log ||= []
|
|
|
|
end
|
|
|
|
|
2009-10-07 22:50:30 +00:00
|
|
|
def reject_empty_ships_on_create(attributes)
|
2016-08-06 16:26:20 +00:00
|
|
|
attributes.delete("_reject_me_if_new").present? && !persisted?
|
2009-10-07 22:50:30 +00:00
|
|
|
end
|
|
|
|
|
2012-08-26 00:02:05 +00:00
|
|
|
attr_accessor :cancel_save_from_callback, :parrots_limit
|
2016-08-06 17:37:57 +00:00
|
|
|
before_save :cancel_save_callback_method, if: :cancel_save_from_callback
|
2010-01-08 19:47:49 +00:00
|
|
|
def cancel_save_callback_method
|
2014-12-15 06:10:15 +00:00
|
|
|
throw(:abort)
|
2010-01-08 19:47:49 +00:00
|
|
|
end
|
|
|
|
|
2009-03-12 15:24:37 +00: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 05:56:46 +00:00
|
|
|
end
|
2011-01-07 19:12:37 +00:00
|
|
|
|
|
|
|
class DestructivePirate < Pirate
|
2016-08-06 17:37:57 +00:00
|
|
|
has_one :dependent_ship, class_name: "Ship", foreign_key: :pirate_id, dependent: :destroy
|
2011-01-07 19:12:37 +00:00
|
|
|
end
|
2014-05-01 06:38:15 +00:00
|
|
|
|
|
|
|
class FamousPirate < ActiveRecord::Base
|
2016-08-06 16:26:20 +00:00
|
|
|
self.table_name = "pirates"
|
2014-05-01 06:38:15 +00:00
|
|
|
has_many :famous_ships
|
|
|
|
validates_presence_of :catchphrase, on: :conference
|
2015-02-17 20:28:06 +00:00
|
|
|
end
|
2019-03-14 19:57:14 +00:00
|
|
|
|
|
|
|
class SpacePirate < ActiveRecord::Base
|
|
|
|
self.table_name = "pirates"
|
|
|
|
|
|
|
|
belongs_to :parrot
|
|
|
|
belongs_to :parrot_with_annotation, -> { annotate("that tells jokes") }, class_name: :Parrot, foreign_key: :parrot_id
|
|
|
|
has_and_belongs_to_many :parrots, foreign_key: :pirate_id
|
|
|
|
has_and_belongs_to_many :parrots_with_annotation, -> { annotate("that are very colorful") }, class_name: :Parrot, foreign_key: :pirate_id
|
|
|
|
has_one :ship, foreign_key: :pirate_id
|
|
|
|
has_one :ship_with_annotation, -> { annotate("that is a rocket") }, class_name: :Ship, foreign_key: :pirate_id
|
|
|
|
has_many :birds, foreign_key: :pirate_id
|
|
|
|
has_many :birds_with_annotation, -> { annotate("that are also parrots") }, class_name: :Bird, foreign_key: :pirate_id
|
|
|
|
has_many :treasures, as: :looter
|
|
|
|
has_many :treasure_estimates, through: :treasures, source: :price_estimates
|
|
|
|
has_many :treasure_estimates_with_annotation, -> { annotate("yarrr") }, through: :treasures, source: :price_estimates
|
|
|
|
end
|