Sha256: b1c2530ace51e0b2f9a27911e713abca7f1a424f428da1fe73d527f5aeedc878

Contents?: true

Size: 1.26 KB

Versions: 106

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

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

module Renalware
  module PD
    class CreateRegime
      def initialize(patient:)
        @patient = patient
      end

      def call(by:, params:)
        regime = patient.pd_regimes.new(params)
        if regime.valid? && save_regime(regime, by)
          return ::Renalware::Success.new(regime)
        else
          return ::Renalware::Failure.new(regime)
        end
      end

      private

      attr_reader :patient

      def save_regime(new_regime, by)
        Regime.transaction do
          if update_old_regime_end_date?(current_regime, new_regime)
            current_regime.end_date ||= new_regime.start_date
          end
          current_regime.terminate(by: by).save && new_regime.save
        end
      end

      def update_old_regime_end_date?(old_regime, new_regime)
        # This odd .to_time.to_i stuff is to get around a stack overflow recursion
        # error when running rspec unit tests!
        new_regime.start_date.to_time.to_i >= old_regime.start_date.to_time.to_i
      end

      def current_regime
        @current_regime ||= patient.pd_regimes.current || NullObject.instance
      end
    end
  end
end

Version data entries

106 entries across 106 versions & 1 rubygems

Version Path
renalware-core-2.0.124 app/models/renalware/pd/create_regime.rb
renalware-core-2.0.123 app/models/renalware/pd/create_regime.rb
renalware-core-2.0.121 app/models/renalware/pd/create_regime.rb
renalware-core-2.0.120 app/models/renalware/pd/create_regime.rb
renalware-core-2.0.119 app/models/renalware/pd/create_regime.rb
renalware-core-2.0.118 app/models/renalware/pd/create_regime.rb
renalware-core-2.0.117 app/models/renalware/pd/create_regime.rb
renalware-core-2.0.116 app/models/renalware/pd/create_regime.rb
renalware-core-2.0.115 app/models/renalware/pd/create_regime.rb
renalware-core-2.0.113 app/models/renalware/pd/create_regime.rb
renalware-core-2.0.112 app/models/renalware/pd/create_regime.rb
renalware-core-2.0.111 app/models/renalware/pd/create_regime.rb
renalware-core-2.0.110 app/models/renalware/pd/create_regime.rb
renalware-core-2.0.109 app/models/renalware/pd/create_regime.rb
renalware-core-2.0.108 app/models/renalware/pd/create_regime.rb
renalware-core-2.0.106 app/models/renalware/pd/create_regime.rb
renalware-core-2.0.105 app/models/renalware/pd/create_regime.rb
renalware-core-2.0.104 app/models/renalware/pd/create_regime.rb
renalware-core-2.0.103 app/models/renalware/pd/create_regime.rb
renalware-core-2.0.102 app/models/renalware/pd/create_regime.rb