SMERM/EMUForm

View on GitHub
app/controllers/roles_controller.rb

Summary

Maintainability
A
0 mins
Test Coverage
class RolesController < EndUserBaseController
  before_action :set_role, only: [:show, :edit, :update, :destroy]

  # POST /roles/select
  # POST /roles/select.json
  def select
    @work = Work.find(role_selection_params[:work_id])
    @authors = Author.find(role_selection_params[:authors_attributes].map { |a| a[:id] unless a[:id].blank? }.compact)
    @roles = Role.ordered_all
  end

  # GET /roles/1
  # GET /roles/1.json
  def show
  end

  # GET /roles/new
  def new
    @role = Role.new
  end

  # GET /roles/1/edit
  def edit
  end

  # POST /roles
  # POST /roles.json
  def create
    @role = Role.new(role_params)

    respond_to do |format|
      if @role.save
        format.html { redirect_to @role, notice: 'Role was successfully created.' }
        format.json { render :show, status: :created, location: @role }
      else
        format.html { render :new }
        format.json { render json: @role.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /roles/1
  # PATCH/PUT /roles/1.json
  def update
    respond_to do |format|
      if @role.update(role_params)
        format.html { redirect_to @role, notice: 'Role was successfully updated.' }
        format.json { render :show, status: :ok, location: @role }
      else
        format.html { render :edit }
        format.json { render json: @role.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /roles/1
  # DELETE /roles/1.json
  def destroy
    @role.destroy
    respond_to do |format|
      format.html { redirect_to roles_url, notice: 'Role was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

private
  # Use callbacks to share common setup or constraints between actions.
  def set_role
    @role = Role.find(params[:id])
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def role_params
    params.require(:role).permit(:description)
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def role_selection_params
    params.require(:role).permit(:work_id, authors_attributes: [:id])
  end

end