CanCanCan Scaffold Controller Templates

Public
Overrides the default scaffold controllers to include Devise authentication and CanCanCan authorization.
Icons/chart bar
Used 19 times
Created by
D Dale Zak

Usage


Run this command in your Rails app directory in the terminal:

rails app:template LOCATION="https://railsbytes.com/script/VMysyp"
Template Source

Review the code before running this template on your machine.

file 'lib/templates/rails/scaffold_controller/controller.rb', <<-CODE
<% if namespaced? -%>
require_dependency "<%= namespaced_file_path %>/application_controller"

<% end -%>
<% module_namespacing do -%>
class <%= controller_class_name %>Controller < ApplicationController
  before_action :authenticate_user!
  before_action :set_<%= singular_table_name %>, only: [:show, :edit, :update, :destroy]

  def index
    authorize! :index, <%= class_name %>
    @<%= plural_table_name %> = <%= class_name %>.all
    respond_to do |format|
      format.html { }
      format.json { }
    end
  end

  def show
    authorize! :show, @<%= singular_table_name %>
  end

  def new
    authorize! :new, <%= class_name %>
    @<%= singular_table_name %> = <%= orm_class.build(class_name) %>
  end

  def edit
    authorize! :edit, @<%= singular_table_name %>
  end

  def create
    authorize! :create, <%= class_name %>
    @<%= singular_table_name %> = <%= orm_class.build(class_name, "\#{singular_table_name}_params") %>
    respond_to do |format|
      if @<%= orm_instance.save %>
        format.html { redirect_to @<%= singular_table_name %>, notice: <%= "'\#{human_name} was successfully created.'" %> }
        format.json { render :show, status: :created, location: <%= "@\#{singular_table_name}" %> }
      else
        format.html { render :new }
        format.json { render json: { error: <%= "@\#{singular_table_name}.errors.full_messages.to_sentence" %> }, status: :unprocessable_entity }
      end
    end
  end

  def update
    authorize! :update, @<%= singular_table_name %>
    respond_to do |format|
      if @<%= orm_instance.update("\#{singular_table_name}_params") %>
        format.html { redirect_to @<%= singular_table_name %>, notice: <%= "'\#{human_name} was successfully updated.'" %> }
        format.json { render :show, status: :ok, location: <%= "@\#{singular_table_name}" %> }
      else
        format.html { render :edit }
        format.json { render json: { error: <%= "@\#{singular_table_name}.errors.full_messages.to_sentence" %> }, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    authorize! :destroy, @<%= singular_table_name %>
    @<%= orm_instance.destroy %>
    respond_to do |format|
      format.html { redirect_to <%= index_helper %>_url, notice: <%= "'\#{human_name} was successfully deleted.'" %> }
      format.json { head :no_content }
    end
  end

  private

  def set_<%= singular_table_name %>
    @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
  end

  def <%= "\#{singular_table_name}_params" %>
    <%- if attributes_names.empty? -%>
    params.fetch(<%= ":\#{singular_table_name}" %>, {})
    <%- else -%>
    params.require(<%= ":\#{singular_table_name}" %>).permit(<%= permitted_params %>)
    <%- end -%>
  end

end
<% end -%>

CODE

file 'lib/templates/rails/scaffold_controller/api_controller.rb', <<-CODE
<% if namespaced? -%>
require_dependency "<%= namespaced_file_path %>/application_controller"

<% end -%>
<% module_namespacing do -%>
class <%= controller_class_name %>Controller < ApplicationController
  before_action :authenticate_user!
  before_action :set_<%= singular_table_name %>, only: [:show, :update, :destroy]

  def index
    authorize! :index, <%= class_name %>
    @<%= plural_table_name %> = <%= orm_class.all(class_name) %>
  end

  def show
    authorize! :show, @<%= singular_table_name %>
  end

  def create
    authorize! :new, <%= class_name %>
    @<%= singular_table_name %> = <%= orm_class.build(class_name, "\#{singular_table_name}_params") %>
    if @<%= orm_instance.save %>
      render :show, status: :created, location: <%= "@\#{singular_table_name}" %>
    else
      render json: <%= "@\#{orm_instance.errors}" %>, status: :unprocessable_entity
    end
  end

  def update
    authorize! :update, @<%= singular_table_name %>
    if @<%= orm_instance.update("\#{singular_table_name}_params") %>
      render :show, status: :ok, location: <%= "@\#{singular_table_name}" %>
    else
      render json: <%= "@\#{orm_instance.errors}" %>, status: :unprocessable_entity
    end
  end

  def destroy
    authorize! :destroy, @<%= singular_table_name %>
    @<%= orm_instance.destroy %>
  end

  private

  def set_<%= singular_table_name %>
    @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
  end

  def <%= "\#{singular_table_name}_params" %>
    <%- if attributes_names.empty? -%>
    params.fetch(<%= ":\#{singular_table_name}" %>, {})
    <%- else -%>
    params.require(<%= ":\#{singular_table_name}" %>).permit(<%= permitted_params %>)
    <%- end -%>
  end
end
<% end -%>
CODE
Comments

Sign up or Login to leave a comment.

© 2022 GoRails, LLC