# frozen_string_literal: true module Admin class <%= controller_class_name %>Controller < ApplicationController before_action :set_<%= singular_name %>, only: %i[show edit update destroy] def index collection = Collection.new.with_params(params).apply(::<%= class_name %>.strict_loading.all) table = Koi::IndexTableComponent.new(collection:) respond_to do |format| format.turbo_stream { render table } if self_referred? format.html { render :index, locals: { table:, collection: } } end end def show render locals: { <%= singular_name %>: @<%= singular_name %> } end def new render locals: { <%= singular_name %>: ::<%= class_name %>.new } end def edit render locals: { <%= singular_name %>: @<%= singular_name %> } end def create @<%= singular_name %> = ::<%= class_name %>.new(<%= singular_name %>_params) if @<%= singular_name %>.save redirect_to [:admin, @<%= singular_name %>] else render :new, locals: { <%= singular_name %>: @<%= singular_name %> }, status: :unprocessable_entity end end def update if @<%= singular_name %>.update(<%= singular_name %>_params) redirect_to action: :show else render :edit, locals: { <%= singular_name %>: @<%= singular_name %> }, status: :unprocessable_entity end end def destroy @<%= singular_name %>.destroy redirect_to action: :index end private # Only allow a list of trusted parameters through. 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 # Use callbacks to share common setup or constraints between actions. def set_<%= singular_table_name %> @<%= singular_table_name %> = ::<%= class_name %>.find(params[:id]) end class Collection < Katalyst::Tables::Collection::Base attribute :search, :string config.sorting = :<%= search_attribute %> config.paginate = true def filter self.items = items.admin_search(search) if search.present? end end end end