2017-07-09 13:41:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2007-11-26 17:45:43 -05:00
|
|
|
class Ship < ActiveRecord::Base
|
|
|
|
self.record_timestamps = false
|
2009-01-31 20:44:30 -05:00
|
|
|
|
2009-11-06 17:53:33 -05:00
|
|
|
belongs_to :pirate
|
2016-08-06 13:37:57 -04:00
|
|
|
belongs_to :update_only_pirate, class_name: "Pirate"
|
2015-08-24 04:27:06 -04:00
|
|
|
belongs_to :developer, dependent: :destroy
|
2016-08-06 13:37:57 -04:00
|
|
|
has_many :parts, class_name: "ShipPart"
|
2015-01-28 11:36:42 -05:00
|
|
|
has_many :treasures
|
2009-01-31 20:44:30 -05:00
|
|
|
|
2016-08-06 13:37:57 -04:00
|
|
|
accepts_nested_attributes_for :parts, allow_destroy: true
|
|
|
|
accepts_nested_attributes_for :pirate, allow_destroy: true, reject_if: proc(&:empty?)
|
|
|
|
accepts_nested_attributes_for :update_only_pirate, update_only: true
|
2009-01-31 20:44:30 -05:00
|
|
|
|
|
|
|
validates_presence_of :name
|
2010-01-08 14:47:49 -05:00
|
|
|
|
|
|
|
attr_accessor :cancel_save_from_callback
|
2016-08-06 13:37:57 -04:00
|
|
|
before_save :cancel_save_callback_method, if: :cancel_save_from_callback
|
2010-01-08 14:47:49 -05:00
|
|
|
def cancel_save_callback_method
|
2014-12-15 01:10:15 -05:00
|
|
|
throw(:abort)
|
2010-01-08 14:47:49 -05:00
|
|
|
end
|
2009-02-08 08:23:35 -05:00
|
|
|
end
|
2014-05-01 02:38:15 -04:00
|
|
|
|
2015-07-18 10:28:24 -04:00
|
|
|
class ShipWithoutNestedAttributes < ActiveRecord::Base
|
|
|
|
self.table_name = "ships"
|
|
|
|
has_many :prisoners, inverse_of: :ship, foreign_key: :ship_id
|
2015-07-20 11:00:00 -04:00
|
|
|
has_many :parts, class_name: "ShipPart", foreign_key: :ship_id
|
2015-07-18 10:28:24 -04:00
|
|
|
|
2019-07-09 10:14:56 -04:00
|
|
|
validates :name, presence: true, if: -> { true }
|
|
|
|
validates :name, presence: true, if: -> { true }
|
2015-07-18 10:28:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class Prisoner < ActiveRecord::Base
|
|
|
|
belongs_to :ship, autosave: true, class_name: "ShipWithoutNestedAttributes", inverse_of: :prisoners
|
|
|
|
end
|
|
|
|
|
2014-05-01 02:38:15 -04:00
|
|
|
class FamousShip < ActiveRecord::Base
|
2016-08-06 12:26:20 -04:00
|
|
|
self.table_name = "ships"
|
2014-05-01 02:38:15 -04:00
|
|
|
belongs_to :famous_pirate
|
|
|
|
validates_presence_of :name, on: :conference
|
|
|
|
end
|