Sha256: e389d0795947d28800cc76ed6deaf789e9b66f1460c9ea1831f23ef14a7cf314

Contents?: true

Size: 1.79 KB

Versions: 87

Compression:

Stored size: 1.79 KB

Contents

# frozen_string_literal: true

require_dependency "renalware/hospitals"

module Renalware
  module Hospitals
    class WardsController < BaseController
      def index
        authorize Ward, :index?
        wards = Ward.where(hospital_unit_id: unit.id).order(name: :asc)
        respond_to do |format|
          format.json do
            render json: wards.select(:id, :name).to_json
          end
          format.html do
            render locals: { unit: unit, wards: wards }
          end
        end
      end

      def new
        ward = unit.wards.build
        authorize ward
        render_new(ward)
      end

      def create
        ward = unit.wards.build(ward_params)
        authorize ward
        if ward.save
          redirect_to hospitals_unit_wards_path(unit), notice: success_msg_for(:ward)
        else
          render_new(ward)
        end
      end

      def edit
        render_edit(find_an_authorise_ward)
      end

      def update
        ward = find_an_authorise_ward
        if ward.update(ward_params)
          redirect_to hospitals_unit_wards_path(unit), notice: success_msg_for(:ward)
        else
          render_edit(find_an_authorise_ward)
        end
      end

      def destroy
        find_an_authorise_ward.destroy!
        redirect_to hospitals_unit_wards_path(unit), notice: success_msg_for(:ward)
      end

      private

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

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

      def unit
        @unit ||= Unit.find(params[:unit_id])
      end

      def find_an_authorise_ward
        unit.wards.find(params[:id]).tap { |ward| authorize(ward) }
      end

      def ward_params
        params.require(:ward).permit(:code, :name, :active)
      end
    end
  end
end

Version data entries

87 entries across 87 versions & 1 rubygems

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