Sha256: 13b5d1f88d17e0d7ee895dddb2e6a4d993214ede287873bd178497dd250c940c
Contents?: true
Size: 1.42 KB
Versions: 10
Compression:
Stored size: 1.42 KB
Contents
# frozen_string_literal: true require_dependency "renalware/pd" require "success" require "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 ::Success.new(regime) unless regime.anything_changed? unless regime.valid? return ::Failure.new(regime.with_bag_destruction_marks_removed) end new_regime = revise_regime(by: by) ::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
10 entries across 10 versions & 1 rubygems