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