2019-05-09 23:10:02 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-23 21:19:08 +08:00
|
|
|
class ProtocolProtocolKeyword < ApplicationRecord
|
2016-07-21 19:11:15 +08:00
|
|
|
after_create :increment_protocols_count
|
|
|
|
after_destroy :decrement_protocols_count
|
|
|
|
|
|
|
|
validates :protocol, presence: true
|
|
|
|
validates :protocol_keyword, presence: true
|
|
|
|
|
2019-05-09 23:10:02 +08:00
|
|
|
belongs_to :protocol, inverse_of: :protocol_protocol_keywords
|
|
|
|
belongs_to :protocol_keyword, inverse_of: :protocol_protocol_keywords
|
2016-07-21 19:11:15 +08:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def increment_protocols_count
|
|
|
|
self.protocol_keyword.increment!(:nr_of_protocols)
|
|
|
|
end
|
|
|
|
|
|
|
|
def decrement_protocols_count
|
|
|
|
self.protocol_keyword.decrement!(:nr_of_protocols)
|
|
|
|
if self.protocol_keyword.nr_of_protocols == 0 then
|
|
|
|
self.protocol_keyword.destroy
|
|
|
|
end
|
|
|
|
end
|
2017-06-23 21:19:08 +08:00
|
|
|
end
|