Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add namespace option to generators #956

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/generators/administrate/dashboard/USAGE
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Description:
pulling the attributes from database columns.

Example:
rails generate administrate:dashboard FooBar
rails generate administrate:dashboard FooBar [--namespace admin]

This will create:
app/dashboards/foo_bar_dashboard.rb
8 changes: 7 additions & 1 deletion lib/generators/administrate/dashboard/dashboard_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class DashboardGenerator < Rails::Generators::NamedBase
COLLECTION_ATTRIBUTE_LIMIT = 4
READ_ONLY_ATTRIBUTES = %w[id created_at updated_at]

class_option :namespace, type: :string, default: "admin"

source_root File.expand_path("../templates", __FILE__)

def create_dashboard_definition
Expand All @@ -35,14 +37,18 @@ def create_dashboard_definition

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

template("controller.rb.erb", destination)
end

private

def namespace
options[:namespace]
end

def attributes
klass.reflections.keys + klass.attribute_names - redundant_attributes
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Admin
class <%= class_name.pluralize %>Controller < Admin::ApplicationController
module <%= namespace.classify %>
class <%= class_name.pluralize %>Controller < <%= namespace.classify %>::ApplicationController
# To customize the behavior of this controller,
# you can overwrite any of the RESTful actions. For example:
#
Expand Down
19 changes: 13 additions & 6 deletions lib/generators/administrate/install/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,41 @@ class InstallGenerator < Rails::Generators::Base
include Administrate::GeneratorHelpers
source_root File.expand_path("../templates", __FILE__)

class_option :namespace, type: :string, default: "admin"

def run_routes_generator
if dashboard_resources.none?
call_generator("administrate:routes")
call_generator("administrate:routes", "--namespace", namespace)
load Rails.root.join("config/routes.rb")
end
end

def create_dashboard_controller
copy_file(
"application_controller.rb",
"app/controllers/admin/application_controller.rb"
template(
"application_controller.rb.erb",
"app/controllers/#{namespace}/application_controller.rb",
)
end

def run_dashboard_generators
singular_dashboard_resources.each do |resource|
call_generator("administrate:dashboard", resource)
call_generator "administrate:dashboard", resource,
"--namespace", namespace
end
end

private

def namespace
options[:namespace]
end

def singular_dashboard_resources
dashboard_resources.map(&:to_s).map(&:singularize)
end

def dashboard_resources
Administrate::Namespace.new(:admin).resources
Administrate::Namespace.new(namespace).resources
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
# If you want to add pagination or other controller-level concerns,
# you're free to overwrite the RESTful controller actions.
module Admin
module <%= namespace.classify %>
class ApplicationController < Administrate::ApplicationController
before_action :authenticate_admin

Expand Down
5 changes: 5 additions & 0 deletions lib/generators/administrate/routes/routes_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Administrate
module Generators
class RoutesGenerator < Rails::Generators::Base
source_root File.expand_path("../templates", __FILE__)
class_option :namespace, type: :string, default: "admin"

def insert_dashboard_routes
if should_route_dashboard?
Expand All @@ -32,6 +33,10 @@ def warn_about_invalid_models

private

def namespace
options[:namespace]
end

def dashboard_resources
valid_dashboard_models.map do |model|
model.to_s.pluralize.underscore
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/administrate/routes/templates/routes.rb.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace :admin do
namespace :<%= namespace %> do
<% dashboard_resources.each do |resource| %> resources :<%= resource %>
<%end%>
root to: "<%= dashboard_resources.first %>#index"
Expand Down
21 changes: 20 additions & 1 deletion spec/generators/dashboard_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ class Foo < ActiveRecord::Base
expect(controller).to have_correct_syntax
end

it "subclasses Admin::ApplicationController" do
it "subclasses Admin::ApplicationController by default" do
begin
ActiveRecord::Schema.define { create_table :foos }
class Foo < ActiveRecord::Base; end
Expand All @@ -456,5 +456,24 @@ class Foo < ActiveRecord::Base; end
Admin.send(:remove_const, :FoosController)
end
end

it "uses the given namespace to create controllers" do
begin
ActiveRecord::Schema.define { create_table :foos }
class Foo < ActiveRecord::Base; end
module Manager
class ApplicationController < Administrate::ApplicationController; end
end

run_generator ["foo", "--namespace", "manager"]
load file("app/controllers/manager/foos_controller.rb")

expect(Manager::FoosController.ancestors).
to include(Manager::ApplicationController)
ensure
remove_constants :Foo
Manager.send(:remove_const, :FoosController)
end
end
end
end
70 changes: 55 additions & 15 deletions spec/generators/install_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
require "support/constant_helpers"

describe Administrate::Generators::InstallGenerator, :generator do
describe "admin/application_controller" do
after { reset_routes }

describe "application_controller" do
it "is copied to the application" do
stub_generator_dependencies
controller = file("app/controllers/admin/application_controller.rb")
Expand All @@ -18,34 +20,70 @@ module Admin
class ApplicationController < Administrate::ApplicationController
RB
end

it "uses the namespace option" do
stub_generator_dependencies
controller = file("app/controllers/manager/application_controller.rb")

run_generator ["--namespace", "manager"]

expect(controller).to exist
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to check it exists if we're also ensuring it looks right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this verification as it will raise a proper error message when the file doesn't exist (expected "/home/rodrigo/Projects/administrate/tmp/app/controllers/manager/application_controller.rb" to exist)

If we use only the have_correct_syntax matcher and the generator fails to create the file, the error message would be No such file or directory @ rb_sysopen - /home/rodrigo/Projects/administrate/tmp/app/controllers/manager/application_controller.rb

What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good reason! Let's leave it in there.

expect(controller).to have_correct_syntax
expect(controller).to contain <<-RB.strip_heredoc
module Manager
class ApplicationController < Administrate::ApplicationController
RB
end
end

describe "routes generator" do
it "invokes the routes generator if there are no namespace resources" do
begin
stub_generator_dependencies
Rails.application.routes.draw {}

run_generator

expect(Rails::Generators).to invoke_generator(
"administrate:routes", ["--namespace", "admin"]
)
end

it "does not invoke routes generator if namespace routes already exist" do
stub_generator_dependencies
Rails.application.routes.draw do
namespace(:admin) { resources :customers }
end

run_generator

expect(Rails::Generators).not_to invoke_generator(
"administrate:routes", ["--namespace", "admin"]
)
end

context "using the namespace option" do
it "invokes the routes generator if there are no namespace resources" do
stub_generator_dependencies
Rails.application.routes.draw {}

run_generator
run_generator ["--namespace", "manager"]

expect(Rails::Generators).to invoke_generator("administrate:routes")
ensure
reset_routes
expect(Rails::Generators).to invoke_generator(
"administrate:routes", ["--namespace", "manager"]
)
end
end

it "does not invoke routes generator if namespace routes already exist" do
begin
it "does not invoke routes generator if namespace routes already exist" do
stub_generator_dependencies
Rails.application.routes.draw do
namespace(:admin) { resources :customers }
namespace(:manager) { resources :customers }
end

run_generator
run_generator ["--namespace", "manager"]

expect(Rails::Generators).not_to invoke_generator("administrate:routes")
ensure
reset_routes
expect(Rails::Generators).not_to invoke_generator(
"administrate:routes", ["--namespace", "manager"]
)
end
end
end
Expand All @@ -58,7 +96,9 @@ class ApplicationController < Administrate::ApplicationController

%w[customer order product line_item].each do |resource|
expect(Rails::Generators).
to invoke_generator("administrate:dashboard", [resource])
to invoke_generator(
"administrate:dashboard", [resource, "--namespace", "admin"]
)
end
end
end
Expand Down
8 changes: 8 additions & 0 deletions spec/generators/routes_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@
end
end

it "populates default dashboards under the given namespace" do
routes = file("config/routes.rb")

run_generator ["--namespace", "manager"]

expect(routes).to contain("namespace :manager")
end

it "does not populate routes when no models exist" do
routes = file("config/routes.rb")
allow(ActiveRecord::Base).to receive(:descendants).and_return([])
Expand Down