<commit_before># Copyright (C) 2007, 2008, 2009, 2010 The Collaborative Software Foundation
#
# This file is part of TriSano.
#
# TriSano is free software: you can redistribute it and/or modify it under the
# terms of the GNU Affero General Public License as published by the
# Free Software Foundation, either version 3 of the License,
# or (at your option) any later version.
#
# TriSano is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with TriSano. If not, see http://www.gnu.org/licenses/agpl-3.0.txt.

module Trisano
  module TrisanoPerinatalHepB
    module Models
      module HumanEvent
        hook! "HumanEvent"
        reloadable!

        class << self
          def included(base)
            base.has_one :expected_delivery_facility,
              :foreign_key => "event_id",
              :order => 'created_at ASC',
              :dependent => :destroy

            base.has_one :actual_delivery_facility,
              :foreign_key => "event_id",
              :order => 'created_at ASC',
              :dependent => :destroy

            base.has_one :health_care_provider,
              :foreign_key => "event_id",
              :order => 'created_at ASC',
              :dependent => :destroy

            base.belongs_to :state_manager, {
              :class_name => 'User'
            }

            base.accepts_nested_attributes_for :expected_delivery_facility, {
              :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } },
              :allow_destroy => true }

            base.accepts_nested_attributes_for :actual_delivery_facility, {
              :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } },
              :allow_destroy => true }

            base.accepts_nested_attributes_for :health_care_provider, {
              :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } },
              :allow_destroy => true }

            base.class_eval do
              def validate_with_p_hep_b_validate
                validate_without_p_hep_b_validate

                disease = try(:disease).try(:disease)
                if self.is_a?(::ContactEvent) &&
                    !disease.nil? &&
                    ::DiseaseSpecificValidation.diseases_ids_for_key(:treatment_date_required).include?(disease.id)

                  self.interested_party.treatments.each do |pt|
                    unless pt.treatment.id.blank?
                      pt.errors.add_on_blank(:treatment_date)
                      self.errors.add_to_base("#{I18n.t(:treatment_date)} #{I18n.t(:blank, :scope => 'activerecord.errors.messages')}") if pt.treatment_date.blank?
                    end
                  end

                end
              end
            end

            base.alias_method_chain :validate, :p_hep_b_validate
          end
        end
<commit_msg>Resolving an issue with validations when a disease is changed on a contact event with existing treatments.<commit_after># Copyright (C) 2007, 2008, 2009, 2010 The Collaborative Software Foundation
#
# This file is part of TriSano.
#
# TriSano is free software: you can redistribute it and/or modify it under the
# terms of the GNU Affero General Public License as published by the
# Free Software Foundation, either version 3 of the License,
# or (at your option) any later version.
#
# TriSano is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with TriSano. If not, see http://www.gnu.org/licenses/agpl-3.0.txt.

module Trisano
  module TrisanoPerinatalHepB
    module Models
      module HumanEvent
        hook! "HumanEvent"
        reloadable!

        class << self
          def included(base)
            base.has_one :expected_delivery_facility,
              :foreign_key => "event_id",
              :order => 'created_at ASC',
              :dependent => :destroy

            base.has_one :actual_delivery_facility,
              :foreign_key => "event_id",
              :order => 'created_at ASC',
              :dependent => :destroy

            base.has_one :health_care_provider,
              :foreign_key => "event_id",
              :order => 'created_at ASC',
              :dependent => :destroy

            base.belongs_to :state_manager, {
              :class_name => 'User'
            }

            base.accepts_nested_attributes_for :expected_delivery_facility, {
              :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } },
              :allow_destroy => true }

            base.accepts_nested_attributes_for :actual_delivery_facility, {
              :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } },
              :allow_destroy => true }

            base.accepts_nested_attributes_for :health_care_provider, {
              :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } },
              :allow_destroy => true }

            base.class_eval do
              def validate_with_p_hep_b_validate
                validate_without_p_hep_b_validate

                disease = try(:disease).try(:disease)
                if self.is_a?(::ContactEvent) &&
                    !disease.nil? &&
                    ::DiseaseSpecificValidation.diseases_ids_for_key(:treatment_date_required).include?(disease.id)

                  self.interested_party.treatments.each do |pt|
                    unless pt.treatment.try(:id).blank?
                      pt.errors.add_on_blank(:treatment_date)
                      self.errors.add_to_base("#{I18n.t(:treatment_date)} #{I18n.t(:blank, :scope => 'activerecord.errors.messages')}") if pt.treatment_date.blank?
                    end
                  end

                end
              end
            end

            base.alias_method_chain :validate, :p_hep_b_validate
          end
        end

{'fraction_non_alphanumeric': 0.09011303932494825, 'fraction_numerical': 0.007323674574112402, 'mean_word_length': 2.0436046511627906, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6652107', 'n_tokens_mistral': 1845, 'n_tokens_neox': 1700, 'n_words': 472}