Sha256: 99e8a7e205e982bdf8e51d3fb91b84b090ca66038dc8b28f38bae5d89423721c

Contents?: true

Size: 1.46 KB

Versions: 20

Compression:

Stored size: 1.46 KB

Contents

require_dependency "renalware/pd"
require_dependency "renalware/success"
require_dependency "renalware/failure"

module Renalware
  module PD
    class ReviseRegime
      def initialize(regime)
        @regime = regime
      end

      def call(by:, params:)
        Regime.transaction do
          regime.assign_attributes(params)
          return ::Renalware::Success.new(regime) unless regime.anything_changed?
          unless regime.valid?
            return ::Renalware::Failure.new(regime.with_bag_destruction_marks_removed)
          end

          new_regime = revise_regime(by: by)
          ::Renalware::Success.new(new_regime)
        end
      end

      private

      attr_reader :regime

      def revise_regime(by:)
        new_regime = duplicate_original_regime
        terminate_original_regime(by: by, new_regime: new_regime)
        new_regime
      end

      def duplicate_original_regime
        new_regime = regime.deep_dup
        new_regime.save!
        new_regime
      end

      def terminate_original_regime(by:, new_regime:)
        regime.deep_restore_attributes

        if new_regime.start_date >= regime.start_date
          # Without this condition a validation error could result; the down
          # side of this condition is that the end_date of the original regime.
          # could be stuck at Pending.
          regime.end_date ||= new_regime.start_date
        end
        regime.terminate(by: by)
        regime.save!
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
renalware-core-2.0.8 app/models/renalware/pd/revise_regime.rb
renalware-core-2.0.7 app/models/renalware/pd/revise_regime.rb
renalware-core-2.0.5 app/models/renalware/pd/revise_regime.rb
renalware-core-2.0.4 app/models/renalware/pd/revise_regime.rb
renalware-core-2.0.3 app/models/renalware/pd/revise_regime.rb
renalware-core-2.0.2 app/models/renalware/pd/revise_regime.rb
renalware-core-2.0.1 app/models/renalware/pd/revise_regime.rb
renalware-core-2.0.0 app/models/renalware/pd/revise_regime.rb
renalware-core-2.0.0.pre.rc13 app/models/renalware/pd/revise_regime.rb
renalware-core-2.0.0.pre.rc11 app/models/renalware/pd/revise_regime.rb
renalware-core-2.0.0.pre.rc10 app/models/renalware/pd/revise_regime.rb
renalware-core-2.0.0.pre.rc9 app/models/renalware/pd/revise_regime.rb
renalware-core-2.0.0.pre.rc8 app/models/renalware/pd/revise_regime.rb
renalware-core-2.0.0.pre.rc7 app/models/renalware/pd/revise_regime.rb
renalware-core-2.0.0.pre.rc6 app/models/renalware/pd/revise_regime.rb
renalware-core-2.0.0.pre.rc5 app/models/renalware/pd/revise_regime.rb
renalware-core-2.0.0.pre.rc4 app/models/renalware/pd/revise_regime.rb
renalware-core-2.0.0.pre.rc3 app/models/renalware/pd/revise_regime.rb
renalware-core-2.0.0.pre.rc1 app/models/renalware/pd/revise_regime.rb
renalware-core-2.0.0.pre.beta12 app/models/renalware/pd/revise_regime.rb