Sha256: d230099637671288168e8824f51237a080c362d77027fd393ee0f8079b2b4df9

Contents?: true

Size: 1.78 KB

Versions: 20

Compression:

Stored size: 1.78 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)
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
renalware-core-2.0.46 app/controllers/renalware/hospitals/wards_controller.rb
renalware-core-2.0.45 app/controllers/renalware/hospitals/wards_controller.rb
renalware-core-2.0.44 app/controllers/renalware/hospitals/wards_controller.rb
renalware-core-2.0.43 app/controllers/renalware/hospitals/wards_controller.rb
renalware-core-2.0.42 app/controllers/renalware/hospitals/wards_controller.rb
renalware-core-2.0.41 app/controllers/renalware/hospitals/wards_controller.rb
renalware-core-2.0.40 app/controllers/renalware/hospitals/wards_controller.rb
renalware-core-2.0.39 app/controllers/renalware/hospitals/wards_controller.rb
renalware-core-2.0.38 app/controllers/renalware/hospitals/wards_controller.rb
renalware-core-2.0.37 app/controllers/renalware/hospitals/wards_controller.rb
renalware-core-2.0.36 app/controllers/renalware/hospitals/wards_controller.rb
renalware-core-2.0.35 app/controllers/renalware/hospitals/wards_controller.rb
renalware-core-2.0.34 app/controllers/renalware/hospitals/wards_controller.rb
renalware-core-2.0.33 app/controllers/renalware/hospitals/wards_controller.rb
renalware-core-2.0.32 app/controllers/renalware/hospitals/wards_controller.rb
renalware-core-2.0.31 app/controllers/renalware/hospitals/wards_controller.rb
renalware-core-2.0.30 app/controllers/renalware/hospitals/wards_controller.rb
renalware-core-2.0.28 app/controllers/renalware/hospitals/wards_controller.rb
renalware-core-2.0.27 app/controllers/renalware/hospitals/wards_controller.rb
renalware-core-2.0.26 app/controllers/renalware/hospitals/wards_controller.rb