Skip to content

Commit

Permalink
add namespace option to generators
Browse files Browse the repository at this point in the history
  • Loading branch information
Rodrigora committed Aug 1, 2017
1 parent 736266d commit feb785a
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 32 deletions.
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
66 changes: 47 additions & 19 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,35 +20,59 @@ 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
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 {}
stub_generator_dependencies
Rails.application.routes.draw {}

run_generator
run_generator

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

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

run_generator
run_generator

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

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

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

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

Expand All @@ -58,7 +84,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

0 comments on commit feb785a

Please sign in to comment.