-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve admin generator to support path helpers in templates
- Loading branch information
1 parent
b5ca27d
commit 21efc32
Showing
9 changed files
with
81 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 51 additions & 53 deletions
104
lib/generators/koi/admin_controller/templates/controller.rb.tt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,74 @@ | ||
# frozen_string_literal: true | ||
|
||
module Admin | ||
class <%= controller_class_name %>Controller < ApplicationController | ||
before_action :set_<%= singular_name %>, only: %i[show edit update destroy] | ||
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) | ||
def index | ||
collection = Collection.new.with_params(params).apply(::<%= class_name %>.strict_loading.all) | ||
|
||
render locals: { collection: } | ||
end | ||
render locals: { collection: } | ||
end | ||
|
||
def show | ||
render locals: { <%= singular_name %>: @<%= singular_name %> } | ||
end | ||
def show | ||
render locals: { <%= singular_name %>: @<%= singular_name %> } | ||
end | ||
|
||
def new | ||
render locals: { <%= singular_name %>: ::<%= class_name %>.new } | ||
end | ||
def new | ||
render locals: { <%= singular_name %>: ::<%= class_name %>.new } | ||
end | ||
|
||
def edit | ||
render locals: { <%= singular_name %>: @<%= singular_name %> } | ||
end | ||
def edit | ||
render locals: { <%= singular_name %>: @<%= singular_name %> } | ||
end | ||
|
||
def create | ||
@<%= singular_name %> = ::<%= class_name %>.new(<%= singular_table_name %>_params) | ||
def create | ||
@<%= singular_name %> = ::<%= class_name %>.new(<%= singular_table_name %>_params) | ||
|
||
if @<%= singular_name %>.save | ||
redirect_to [:admin, @<%= singular_name %>], status: :see_other | ||
else | ||
render :new, locals: { <%= singular_name %>: @<%= singular_name %> }, status: :unprocessable_entity | ||
end | ||
if @<%= singular_name %>.save | ||
redirect_to [:admin, @<%= singular_name %>], status: :see_other | ||
else | ||
render :new, locals: { <%= singular_name %>: @<%= singular_name %> }, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
def update | ||
if @<%= singular_name %>.update(<%= singular_table_name %>_params) | ||
redirect_to action: :show, status: :see_other | ||
else | ||
render :edit, locals: { <%= singular_name %>: @<%= singular_name %> }, status: :unprocessable_entity | ||
end | ||
def update | ||
if @<%= singular_name %>.update(<%= singular_table_name %>_params) | ||
redirect_to action: :show, status: :see_other | ||
else | ||
render :edit, locals: { <%= singular_name %>: @<%= singular_name %> }, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
def destroy | ||
@<%= singular_name %>.destroy! | ||
def destroy | ||
@<%= singular_name %>.destroy! | ||
|
||
redirect_to action: :index, status: :see_other | ||
end | ||
redirect_to action: :index, status: :see_other | ||
end | ||
|
||
private | ||
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 | ||
# 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_name %> | ||
@<%= singular_name %> = ::<%= class_name %>.find(params[:id]) | ||
end | ||
# Use callbacks to share common setup or constraints between actions. | ||
def set_<%= singular_name %> | ||
@<%= singular_name %> = ::<%= class_name %>.find(params[:id]) | ||
end | ||
|
||
class Collection < Katalyst::Tables::Collection::Base | ||
attribute :search, :string | ||
class Collection < Katalyst::Tables::Collection::Base | ||
attribute :search, :string | ||
|
||
config.sorting = :<%= search_attribute %> | ||
config.paginate = true | ||
config.sorting = :<%= search_attribute %> | ||
config.paginate = true | ||
|
||
def filter | ||
self.items = items.admin_search(search) if search.present? | ||
end | ||
def filter | ||
self.items = items.admin_search(search) if search.present? | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters