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 Feb 15, 2024
1 parent 21602c3 commit 6b53361
Show file tree
Hide file tree
Showing 2 changed files with 28 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('/')}"

Check failure on line 49 in lib/generators/administrate/dashboard/dashboard_generator.rb

View workflow job for this annotation

GitHub Actions / standardrb

Style/StringLiteralsInInterpolation: Prefer double-quoted strings inside interpolations.
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
23 changes: 23 additions & 0 deletions spec/generators/dashboard_generator_spec.rb
Expand Up @@ -431,5 +431,28 @@ class ApplicationController < Administrate::ApplicationController; end
remove_constants :Foo
Manager.send(:remove_const, :FoosController)
end

it "uses the given model namespace to create controllers" do
ActiveRecord::Schema.define { create_table :foo_bars }
module Foo
def self.table_name_prefix
"foo_"
end

class Bar < Administrate::Generators::TestRecord; end
end

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

Check failure on line 446 in spec/generators/dashboard_generator_spec.rb

View workflow job for this annotation

GitHub Actions / standardrb

Layout/ExtraSpacing: Unnecessary spacing detected.
result = File.foreach(path).first(2).join

Check failure on line 447 in spec/generators/dashboard_generator_spec.rb

View workflow job for this annotation

GitHub Actions / standardrb

Layout/ExtraSpacing: Unnecessary spacing detected.
expected = <<~EXPECTED
module Admin
class Foo::BarsController < Admin::ApplicationController
EXPECTED

expect(result).to eq(expected)
ensure
remove_constants :Foo
end
end
end

0 comments on commit 6b53361

Please sign in to comment.