Sha256: bd26a5418ae06de78668b0bdd4becdb6f16101b205c1817a75f875702fb09959

Contents?: true

Size: 1.72 KB

Versions: 1

Compression:

Stored size: 1.72 KB

Contents

# frozen_string_literal: true

<% if namespaced? -%>
require_dependency "<%= namespaced_path %>/application_controller"

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

  def index
    @<%= plural_table_name %> = <%= singular_table_name.capitalize %>.accessible_by_user(current_user).all
  end

  def show
  end

  def new
    @<%= singular_table_name %> = <%= orm_class.build(class_name) %>
  end

  def edit
  end

  def create
    @<%= singular_table_name %> = <%= orm_class.build(class_name, "#{singular_table_name}_params") %>

    if @<%= orm_instance.save %>
      redirect_to @<%= singular_table_name %>, notice: <%= "'#{human_name} was successfully created.'" %>
    else
      render :new
    end
  end

  def update
    if @<%= orm_instance.update("#{singular_table_name}_params") %>
      redirect_to @<%= singular_table_name %>, notice: <%= "'#{human_name} was successfully updated.'" %>
    else
      render :edit
    end
  end

  def destroy
    @<%= orm_instance.destroy %>!
    redirect_to <%= index_helper %>_url, notice: <%= "'#{human_name} was successfully destroyed.'" %>
  end

private

  def set_<%= singular_table_name %>
    @<%= singular_table_name %> = <%= singular_table_name.capitalize %>.accessible_by_user(current_user).find(params[:id])
  end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
radius-rails-3.1.4 lib/templates/rails/scaffold_controller/controller.rb.tt