Commit 267edf27 authored by Grayson Wright's avatar Grayson Wright
Browse files

Generate a single controller for all resources

Why?

When we ran the generators on Hound, there was a lot of feedback that
people didn't want the extra generated controller files. We didn't
need to customize the controllers, and we ended up changing the
routing so all dashboards were served by a single controller.

This change makes a single controller the default. As demonstrated with
the still-present `Admin::CustomersController`, users can still override
the controllers themselves.

We left the `Admin::CustomersController` in place because there are
tests that exercise `Administrate::ApplicationController` through it.

- Update changelog
- Update route generator
- Update specs to actually read generated routes file
parent 0c296737
main add-action-text-field add-webpacker attachment-field dependabot/bundler/activesupport-7.0.4.3 ev-ruby-3-upgrade github/fork/DavidGeismarLtd/add-scope-to-polymorphic-field github/fork/RaeRachael/namespace_generators github/fork/assembleco/redescribed github/fork/bhtabor/master github/fork/bikramwp/add-namespaced-models-support-to-generators github/fork/cabe56/patch-1 github/fork/das3in/guard-against-uninitialized-model github/fork/dgmstuart/decorators-revised github/fork/edimossilva/fix-redirect-after-destroy-resource-without-index-route github/fork/elrosa/order-for-has-one-relationships github/fork/fastruby/feature/simplecov-support github/fork/geniuskidkanyi/asset-fix github/fork/headwayio/bugfix/add_permitted_has_many_params_for_pagination github/fork/headwayio/feature/custom_sorting_via_options github/fork/jumjamjohn/master github/fork/mauriciozaffari/master github/fork/muriloime/main github/fork/n-studio/compile-assets github/fork/pablobm/associated-primary-key github/fork/pablobm/multimodel github/fork/pablobm/rails70 github/fork/rinsed-org/selectize github/fork/sedubois/collection-select github/fork/sedubois/punditize github/fork/sedubois/rich-text github/fork/upper-hand/master jm-namespace-controller more-permissive-params nc-sassc-seg-fault-fix nc-switch-to-github-actions pr/1941 remove-scss remove-system-tests-compatibility upgrade-ruby-3.1.0 v0.18.0 v0.17.0 v0.16.0 v0.15.0 v0.14.0 v0.13.0 v0.12.0 v0.11.0 v0.10.0 v0.9.0 v0.8.1 v0.8.0 v0.7.0 v0.6.0 v0.5.0 v0.4.0 v0.3.0 v0.2.2 v0.2.1 v0.2.0 v0.1.5 v0.1.4 v0.1.3 v0.1.2 v0.1.1 v0.1.0
1 merge request!77Generate a single controller for all resources
Showing with 79 additions and 84 deletions
+79 -84
New in 0.1.0:
* Improvement: Generate a single controller to serve all resources,
to reduce noise after running the install generator.
* Improvement: Add `Administrate::Field::DateTime`
for displaying dates, times, and datetimes.
* Improvement: Add `Administrate::Field::Number`
......
module Administrate
class ApplicationController < ActionController::Base
def index
@resources = resource_class.all
@resources = resolver.resource_class.all
@page = Administrate::Page::Table.new(dashboard)
end
......@@ -12,7 +12,9 @@ module Administrate
end
def new
@page = Administrate::Page::Form.new(dashboard, resource_class.new)
@page = Administrate::Page::Form.new(
dashboard, resolver.resource_class.new
)
end
def edit
......@@ -22,7 +24,7 @@ module Administrate
end
def create
set_resource(resource_class.new(resource_params))
set_resource(resolver.resource_class.new(resource_params))
if resource.save
redirect_to(
......@@ -61,7 +63,7 @@ module Administrate
helper_method :nav_link_state
def nav_link_state(resource)
if resource_name.to_s.pluralize == resource.to_s
if resolver.resource_name.to_s.pluralize == resource.to_s
:active
else
:inactive
......@@ -69,11 +71,11 @@ module Administrate
end
def dashboard
@dashboard ||= resource_resolver.dashboard_class.new
@dashboard ||= resolver.dashboard_class.new
end
def set_resource(resource = nil)
resource ||= resource_class.find(params[:id])
resource ||= resolver.resource_class.find(params[:id])
instance_variable_set(instance_variable, resource)
end
......@@ -82,7 +84,7 @@ module Administrate
end
def resource_params
params.require(resource_name).permit(*permitted_attributes)
params.require(resolver.resource_name).permit(*permitted_attributes)
end
def permitted_attributes
......@@ -90,20 +92,21 @@ module Administrate
end
def instance_variable
"@#{resource_name}"
"@#{resolver.resource_name}"
end
delegate :resource_class, :resource_name, to: :resource_resolver
def resolver
@resolver ||= Administrate::ResourceResolver.new(resource_class)
end
def resource_resolver
@resource_resolver ||=
Administrate::ResourceResolver.new(controller_path)
def resource_class
params.fetch(:resource_class, controller_path).to_s
end
def translate(key)
t(
"administrate.controller.#{key}",
resource: resource_resolver.resource_title,
resource: resolver.resource_title,
)
end
end
......
......@@ -24,13 +24,6 @@ module Administrate
template "dashboard.rb.erb", "app/dashboards/#{file_name}_dashboard.rb"
end
def create_resource_controller
template(
"controller.rb.erb",
"app/controllers/admin/#{file_name.pluralize}_controller.rb"
)
end
private
def attributes
......
class Admin::<%= class_name.pluralize %>Controller < Admin::ApplicationController
# To customize the behavior of this controller,
# simply overwrite any of the RESTful actions. For example:
#
# def index
# super
# @resources = <%= class_name %>.all.paginate(10, params[:page])
# end
# See https://administrate-docs.herokuapp.com/customizing_controller_actions
# for more information
end
namespace :admin do
DashboardManifest::DASHBOARDS.each do |dashboard_resource|
resources dashboard_resource
DashboardManifest::DASHBOARDS.each do |resource_class|
resources(
resource_class,
controller: :application,
resource_class: resource_class,
)
end
root controller: DashboardManifest::ROOT_DASHBOARD, action: :index
root(
action: :index,
controller: :application,
resource_class: DashboardManifest::ROOT_DASHBOARD,
)
end
class Admin::LineItemsController < Admin::ApplicationController
end
class Admin::OrdersController < Admin::ApplicationController
end
class Admin::ProductsController < Admin::ApplicationController
end
Rails.application.routes.draw do
namespace :admin do
DashboardManifest::DASHBOARDS.each do |dashboard_resource|
resources dashboard_resource
resources :customers
DashboardManifest::DASHBOARDS.each do |resource_class|
resources(
resource_class,
controller: :application,
resource_class: resource_class,
)
end
root controller: DashboardManifest::ROOT_DASHBOARD, action: :index
root(
action: :index,
controller: :application,
resource_class: DashboardManifest::ROOT_DASHBOARD,
)
end
end
......@@ -235,25 +235,4 @@ describe Administrate::Generators::DashboardGenerator, :generator do
end
end
end
describe "resource controller" do
it "has valid syntax" do
controller = file("app/controllers/admin/customers_controller.rb")
run_generator ["customer"]
expect(controller).to exist
expect(controller).to have_correct_syntax
end
it "subclasses Admin::ApplicationController" do
controller = file("app/controllers/admin/customers_controller.rb")
run_generator ["customer"]
expect(controller).to contain(
"class Admin::CustomersController < Admin::ApplicationController",
)
end
end
end
......@@ -56,28 +56,37 @@ describe Administrate::Generators::InstallGenerator, :generator do
describe "config/routes.rb" do
it "inserts an admin namespace with dashboard resources" do
stub_generator_dependencies
routes = file("config/routes.rb")
run_generator
expect(routes).to have_correct_syntax
expect(routes).to contain("namespace :admin do")
expect(routes).to contain(
"DashboardManifest::DASHBOARDS.each do |dashboard_resource|",
)
expect(routes).to contain("resources dashboard_resource")
begin
stub_generator_dependencies
clear_routes
run_generator
load file("config/routes.rb")
routes = Rails.application.routes.routes.named_routes
customers_controller = routes["admin_customers"].defaults[:controller]
expect(customers_controller).to eq("admin/application")
ensure
reset_routes
end
end
it "creates a root route for the admin namespace" do
stub_generator_dependencies
routes = file("config/routes.rb")
run_generator
expect(routes).to contain(
"root controller: DashboardManifest::ROOT_DASHBOARD, action: :index",
)
begin
stub_generator_dependencies
clear_routes
run_generator
load file("config/routes.rb")
routes = Rails.application.routes.routes.named_routes
root_info = routes["admin_root"].defaults
expect(root_info[:controller]).to eq("admin/application")
expect(root_info[:resource_class]).
to eq(DashboardManifest::ROOT_DASHBOARD)
ensure
reset_routes
end
end
end
......@@ -98,4 +107,13 @@ describe Administrate::Generators::InstallGenerator, :generator do
provide_existing_routes_file
allow(Rails::Generators).to receive(:invoke)
end
def reset_routes
clear_routes
load Rails.root.to_s + "/config/routes.rb"
end
def clear_routes
Rails.application.routes.clear!
end
end
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment