Skip to content

Commit

Permalink
Improve admin generator to support path helpers in templates
Browse files Browse the repository at this point in the history
  • Loading branch information
sfnelson authored and AlanCornthwaiteKatalyst committed Jun 12, 2024
1 parent b5ca27d commit 21efc32
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 69 deletions.
8 changes: 7 additions & 1 deletion lib/generators/koi/admin/admin_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ class AdminGenerator < Rails::Generators::ScaffoldGenerator
remove_hook_for(:scaffold_controller)
remove_hook_for(:resource_route)

hook_for :admin_controller, in: :koi, as: :admin, type: :boolean, default: true
hook_for :admin_controller, in: :koi, as: :admin, type: :boolean, default: true do |instance, controller|
args, opts, config = @_initializer
opts ||= {}

# setting model_name so that generators will use the controller_class_path
instance.invoke controller, args, { model_name: instance.name, **opts }, config
end

Rails::Generators::ModelGenerator.hook_for :admin_search, type: :boolean, default: true
Rails::Generators::ModelGenerator.hook_for :ordinal_scope, type: :boolean, default: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ class AdminControllerGenerator < Rails::Generators::NamedBase

def create_controller_files
template("controller.rb",
File.join("app/controllers/admin",
File.join("app/controllers",
controller_class_path,
"#{controller_file_name}_controller.rb"))
end

def create_spec_files
template("controller_spec.rb",
File.join("spec/requests/admin",
File.join("spec/requests",
controller_class_path,
"#{controller_file_name}_controller_spec.rb"))
end
Expand All @@ -32,6 +32,10 @@ def create_spec_files

private

def controller_class_path
["admin"] + super
end

def permitted_params
attachments, others = attributes_names.partition { |name| attachments?(name) }
params = others.map { |name| ":#{name}" }
Expand Down
104 changes: 51 additions & 53 deletions lib/generators/koi/admin_controller/templates/controller.rb.tt
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require "rails_helper"

RSpec.describe Admin::<%= controller_class_name %>Controller do
RSpec.describe <%= controller_class_name %>Controller do
let(:admin) { create(:admin) }
let(:model) { create(:<%= singular_name %>) }

Expand Down
8 changes: 6 additions & 2 deletions lib/generators/koi/admin_views/admin_views_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ class AdminViewsGenerator < Rails::Generators::NamedBase
argument :attributes, type: :array, default: [], banner: "field:type field:type"

def create_root_folder
empty_directory File.join("app/views/admin", controller_file_path)
empty_directory File.join("app/views", controller_file_path)
end

def copy_view_files
available_views.each do |filename|
target = filename.gsub("record", singular_name)
template filename, File.join("app/views/admin", controller_file_path, target)
template filename, File.join("app/views", controller_file_path, target)
end
end

Expand All @@ -28,6 +28,10 @@ def available_views
%w(index.html.erb edit.html.erb show.html.erb new.html.erb _fields.html.erb)
end

def controller_class_path
["admin"] + super
end

def govuk_input_for(attribute)
case attribute.type
when :string
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/koi/admin_views/templates/edit.html.erb.tt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<%%= render(Koi::Header::EditComponent.new(resource: <%= singular_name %>)) %>
<%% end %>

<%%= form_with(model: <%= singular_name %>, url: [:admin, <%= singular_name %>], builder: Koi::FormBuilder) do |form| %>
<%%= form_with(model: <%= singular_name %>, url: <%= show_helper(type: :path) %>) do |form| %>
<%%= render "fields", form: %>

<div class="actions">
Expand Down
14 changes: 7 additions & 7 deletions lib/generators/koi/admin_views/templates/index.html.erb.tt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
<%%= koi_index_actions create: true, search: true %>

<%%= table_with(collection:) do |row, <%= singular_name %>| %>
<% index_attributes.each_with_index do |attribute, index| %>
<% if index.zero? %>
<%% row.link :<%= attribute.name %> %>
<% else %>
<%= index_attribute_for attribute %>
<% end %>
<% end %>
<%- index_attributes.each_with_index do |attribute, index| -%>
<%- if index.zero? -%>
<%% row.link :<%= attribute.name %> %>
<%- else -%>
<%= index_attribute_for attribute %>
<%- end -%>
<%- end -%>
<%% end %>

<%%== pagy_nav(collection.pagination) %>
2 changes: 1 addition & 1 deletion lib/generators/koi/admin_views/templates/new.html.erb.tt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<%%= render(Koi::Header::NewComponent.new(model: <%= class_name %>)) %>
<%% end %>

<%%= form_with(model: <%= singular_name %>, url: [:admin, <%= class_name %>], builder: Koi::FormBuilder) do |form| %>
<%%= form_with(model: <%= singular_name %>, url: <%= index_helper(type: :path) %>) do |form| %>
<%%= render "fields", form: %>

<div class="actions">
Expand Down
2 changes: 1 addition & 1 deletion spec/templates/app/views/admin/banners/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<%= table_with(collection:) do |row, banner| %>
<% row.ordinal %>
<% row.link :name, url: [:admin, banner] %>
<% row.link :name %>
<% row.attachment :image %>
<% end %>

Expand Down

0 comments on commit 21efc32

Please sign in to comment.