Sha256: ce75caf94b1898988a77ffc5b46ee835983e6ad94b9f3fcfe1b4095368a4c0ed

Contents?: true

Size: 1.82 KB

Versions: 114

Compression:

Stored size: 1.82 KB

Contents

# frozen_string_literal: true

module Renalware
  module Accesses
    class PlansController < Accesses::BaseController
      def new
        plan = Plan.new
        authorize plan
        render_new(plan)
      end

      def create
        plan = patient.plans.new(plan_params.merge!(by: current_user))
        authorize plan
        if save_as_current(plan)
          redirect_to patient_accesses_dashboard_path(patient),
                      notice: success_msg_for("access plan")
        else
          render_new(plan)
        end
      end

      # Because editing a plan means creating a new current version (and terminating the previous
      # current one), we just render the new template using an instance of the current plan.
      def edit
        render_new(find_and_authorize_plan.dup)
      end

      def show
        render locals: {
          patient: patient,
          plan: find_and_authorize_plan
        }
      end

      private

      def find_and_authorize_plan
        plan = Plan.find(params[:id])
        authorize plan
        plan
      end

      def save_as_current(plan)
        Plan.transaction do
          if current_plan.present?
            return true if current_plan.identical_to?(plan) # no change, no new plan to be created

            current_plan.terminate_by(current_user)
          end
          plan.save!
        end
      rescue ActiveRecord::RecordInvalid
        false
      end

      def render_new(plan)
        render :new, locals: { patient: patient, plan: plan }
      end

      def render_edit(plan)
        render :edit, locals: { patient: patient, plan: plan }
      end

      def plan_params
        params.require(:accesses_plan).permit(:plan_type_id, :notes, :decided_by_id)
      end

      def current_plan
        @current_plan ||= patient.plans.current.first
      end
    end
  end
end

Version data entries

114 entries across 114 versions & 1 rubygems

Version Path
renalware-core-2.1.1 app/controllers/renalware/accesses/plans_controller.rb
renalware-core-2.1.0 app/controllers/renalware/accesses/plans_controller.rb
renalware-core-2.0.167 app/controllers/renalware/accesses/plans_controller.rb
renalware-core-2.0.166 app/controllers/renalware/accesses/plans_controller.rb
renalware-core-2.0.165 app/controllers/renalware/accesses/plans_controller.rb
renalware-core-2.0.164 app/controllers/renalware/accesses/plans_controller.rb
renalware-core-2.0.163 app/controllers/renalware/accesses/plans_controller.rb
renalware-core-2.0.162 app/controllers/renalware/accesses/plans_controller.rb
renalware-core-2.0.161 app/controllers/renalware/accesses/plans_controller.rb
renalware-core-2.0.160 app/controllers/renalware/accesses/plans_controller.rb
renalware-core-2.0.159 app/controllers/renalware/accesses/plans_controller.rb
renalware-core-2.0.158 app/controllers/renalware/accesses/plans_controller.rb
renalware-core-2.0.157 app/controllers/renalware/accesses/plans_controller.rb
renalware-core-2.0.156 app/controllers/renalware/accesses/plans_controller.rb
renalware-core-2.0.155 app/controllers/renalware/accesses/plans_controller.rb
renalware-core-2.0.153 app/controllers/renalware/accesses/plans_controller.rb
renalware-core-2.0.152 app/controllers/renalware/accesses/plans_controller.rb
renalware-core-2.0.151 app/controllers/renalware/accesses/plans_controller.rb
renalware-core-2.0.149 app/controllers/renalware/accesses/plans_controller.rb
renalware-core-2.0.148 app/controllers/renalware/accesses/plans_controller.rb