“master”上不存在“spring-boot/getting-help.html”
pirate.rb 3.7 KB
Newer Older
1 2
# frozen_string_literal: true

3
class Pirate < ActiveRecord::Base
4 5 6 7 8 9 10 11 12 13
  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",
14 15 16 17
    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}" }
18
  has_and_belongs_to_many :autosaved_parrots, class_name: "Parrot", autosave: true
19

20 21
  has_many :treasures, as: :looter
  has_many :treasure_estimates, through: :treasures, source: :price_estimates
22

23
  has_one :ship
24 25
  has_one :update_only_ship, class_name: "Ship"
  has_one :non_validated_ship, class_name: "Ship"
26
  has_many :birds, -> { order("birds.id ASC") }
27 28 29 30 31 32
  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",
33 34 35 36
    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}" }
37
  has_many :birds_with_reject_all_blank, class_name: "Bird"
38

39
  has_one :foo_bulb, -> { where name: "foo" }, foreign_key: :car_id, class_name: "Bulb"
40

41 42 43
  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
44
  accepts_nested_attributes_for :parrots_with_method_callbacks, :parrots_with_proc_callbacks,
45 46
    :birds_with_method_callbacks, :birds_with_proc_callbacks, allow_destroy: true
  accepts_nested_attributes_for :birds_with_reject_all_blank, reject_if: :all_blank
47

48
  validates_presence_of :catchphrase
49 50 51 52 53

  def ship_log
    @ship_log ||= []
  end

54
  def reject_empty_ships_on_create(attributes)
55
    attributes.delete("_reject_me_if_new").present? && !persisted?
56 57
  end

58
  attr_accessor :cancel_save_from_callback, :parrots_limit
59
  before_save :cancel_save_callback_method, if: :cancel_save_from_callback
60
  def cancel_save_callback_method
61
    throw(:abort)
62 63
  end

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
  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
84
end
85 86

class DestructivePirate < Pirate
87
  has_one :dependent_ship, class_name: "Ship", foreign_key: :pirate_id, dependent: :destroy
88
end
89 90

class FamousPirate < ActiveRecord::Base
91
  self.table_name = "pirates"
92
  has_many :famous_ships, inverse_of: :famous_pirate, foreign_key: :pirate_id
93
  validates_presence_of :catchphrase, on: :conference
94
end