Skip to content

Commit

Permalink
Fix generator path for namespaced models
Browse files Browse the repository at this point in the history
Running a generator with a model that is namespaced, for example
`Foo::Bar` puts the generated files in:
- `app/controllers/admin/bars_controller.rb`
- `app/dashboards/bar_dashboard.rb`

The correct location is:
- `app/controllers/admin/foo/bars_controller.rb`
- `app/dashboards/foo/bar_dashboard.rb`

This change ensures files are generated in the proper location. Without
it the directory needs to be created and files need to be moved
manually.
  • Loading branch information
sdwolfz committed Jan 25, 2024
1 parent aa92381 commit 3cbb02e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/generators/administrate/dashboard/dashboard_generator.rb
Expand Up @@ -37,15 +37,18 @@ class DashboardGenerator < Rails::Generators::NamedBase
source_root File.expand_path("../templates", __FILE__)

def create_dashboard_definition
scope = regular_class_path.join("/")

template(
"dashboard.rb.erb",
Rails.root.join("app/dashboards/#{file_name}_dashboard.rb"),
Rails.root.join("app/dashboards/#{scope}/#{file_name}_dashboard.rb"),
)
end

def create_resource_controller
scope = "#{namespace}/#{regular_class_path.join('/')}"
destination = Rails.root.join(
"app/controllers/#{namespace}/#{file_name.pluralize}_controller.rb",
"app/controllers/#{scope}/#{file_name.pluralize}_controller.rb",
)

template("controller.rb.erb", destination)
Expand Down
18 changes: 18 additions & 0 deletions spec/generators/dashboard_generator_spec.rb
Expand Up @@ -462,5 +462,23 @@ class ApplicationController < Administrate::ApplicationController; end
Manager.send(:remove_const, :FoosController)
end
end

it "uses the given model scope to create controllers" do
begin
ActiveRecord::Schema.define { create_table :foo_bars }
module Foo
class Bar < Administrate::Generators::TestRecord; end
end

run_generator ["Foo::Bar"]
load file("app/controllers/admin/foo/bars_controller.rb")

expect(Admin::Foo::BarsController.ancestors).
to include(Admin::ApplicationController)
ensure
remove_constants :Bar
Admin.send(:remove_const, :BarsController)
end
end
end
end

0 comments on commit 3cbb02e

Please sign in to comment.