Skip to content

Commit

Permalink
Namespace option view generators (#1832)
Browse files Browse the repository at this point in the history
Resolves issue [1632](#1632) 
Makes `rails generate administrate:views:index Modelname --namespace=<Namespace>` possible:

```
create  app/views/<namespace>/modelnames/index.html.erb
create  app/views/<namespace>/modelnames/_collection.html.erb
```
  • Loading branch information
RaeRachael authored Dec 3, 2020
1 parent 1a35ab2 commit 30a34ca
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 8 deletions.
8 changes: 7 additions & 1 deletion lib/administrate/view_generator.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
require "rails/generators/base"
require "administrate/generator_helpers"
require "administrate/namespace"

module Administrate
class ViewGenerator < Rails::Generators::Base
include Administrate::GeneratorHelpers
class_option :namespace, type: :string, default: "admin"

def self.template_source_path
File.expand_path(
Expand All @@ -14,12 +16,16 @@ def self.template_source_path

private

def namespace
options[:namespace]
end

def copy_resource_template(template_name)
template_file = "#{template_name}.html.erb"

copy_file(
template_file,
"app/views/admin/#{resource_path}/#{template_file}",
"app/views/#{namespace}/#{resource_path}/#{template_file}",
)
end

Expand Down
9 changes: 5 additions & 4 deletions lib/generators/administrate/views/views_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ module Administrate
module Generators
class ViewsGenerator < Administrate::ViewGenerator
def copy_templates
call_generator("administrate:views:index", resource_path)
call_generator("administrate:views:show", resource_path)
call_generator("administrate:views:new", resource_path)
call_generator("administrate:views:edit", resource_path)
view = "administrate:views:"
call_generator("#{view}index", resource_path, "--namespace", namespace)
call_generator("#{view}show", resource_path, "--namespace", namespace)
call_generator("#{view}new", resource_path, "--namespace", namespace)
call_generator("#{view}edit", resource_path, "--namespace", namespace)
end
end
end
Expand Down
20 changes: 20 additions & 0 deletions spec/generators/views/edit_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,24 @@
expect(contents).to eq(expected_contents)
end
end

describe "administrate:views:edit resource --namespace=<namespace>" do
it "copies the edit template into the `namespace/resource` namespace" do
expected_contents = contents_for_application_template("edit")

run_generator ["LineItem", "--namespace", "console"]
contents = File.read(file("app/views/console/line_items/edit.html.erb"))

expect(contents).to eq(expected_contents)
end

it "copies the form partial into the `namespace/resource` namespace" do
expected_contents = contents_for_application_template("_form")

run_generator ["users", "--namespace", "console"]
contents = File.read(file("app/views/console/users/_form.html.erb"))

expect(contents).to eq(expected_contents)
end
end
end
20 changes: 20 additions & 0 deletions spec/generators/views/index_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,24 @@
expect(contents).to eq(expected_contents)
end
end

describe "administrate:views:index resource --namespace=<namespace>" do
it "copies the index view into the `namespace/resource` namespace" do
expected_contents = contents_for_application_template("index")

run_generator ["users", "--namespace", "console"]
contents = File.read(file("app/views/console/users/index.html.erb"))

expect(contents).to eq(expected_contents)
end

it "copies the collection partial into the `namespace/resource` namespace" do
expected_contents = contents_for_application_template("_collection")

run_generator ["users", "--namespace", "console"]
contents = File.read(file("app/views/console/users/_collection.html.erb"))

expect(contents).to eq(expected_contents)
end
end
end
20 changes: 20 additions & 0 deletions spec/generators/views/new_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,24 @@
expect(contents).to eq(expected_contents)
end
end

describe "administrate:views:new resource --namespace=<namespace>" do
it "copies the new template into the `namspace/resource` namespace" do
expected_contents = contents_for_application_template("new")

run_generator ["users", "--namespace", "console"]
contents = File.read(file("app/views/console/users/new.html.erb"))

expect(contents).to eq(expected_contents)
end

it "copies the form partial into the `namespace/resource` namespace" do
expected_contents = contents_for_application_template("_form")

run_generator ["users", "--namespace", "console"]
contents = File.read(file("app/views/console/users/_form.html.erb"))

expect(contents).to eq(expected_contents)
end
end
end
11 changes: 11 additions & 0 deletions spec/generators/views/show_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,15 @@
expect(contents).to eq(expected_contents)
end
end

describe "administrate:views:show resource --namespace=<namespace>" do
it "copies the show view into the `namespace/resource` namespace" do
expected_contents = contents_for_application_template("show")

run_generator ["users", "--namespace", "console"]
contents = File.read(file("app/views/console/users/show.html.erb"))

expect(contents).to eq(expected_contents)
end
end
end
27 changes: 24 additions & 3 deletions spec/generators/views_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@

%w[index show new edit].each do |generator|
expect(Rails::Generators).
to invoke_generator("administrate:views:#{generator}", [resource])
to invoke_generator(
"administrate:views:#{generator}",
[resource, "--namespace", "admin"],
)
end
end

Expand All @@ -24,7 +27,7 @@

expect(Rails::Generators).to invoke_generator(
"administrate:views:index",
[resource],
[resource, "--namespace", "admin"],
behavior: :revoke,
)
end
Expand All @@ -41,10 +44,28 @@
%w[index show new edit].each do |generator|
expect(Rails::Generators). to invoke_generator(
"administrate:views:#{generator}",
[application_resource_path],
[application_resource_path, "--namespace", "admin"],
)
end
end
end

context "when run with a namespace specified" do
it "runs all sub-generators with a namespace" do
allow(Rails::Generators).to receive(:invoke)
resource = "users"
namespace = "console"

run_generator [resource, "--namespace", namespace]

%w[index show new edit].each do |generator|
expect(Rails::Generators).
to invoke_generator(
"administrate:views:#{generator}",
[resource, "--namespace", namespace],
)
end
end
end
end
end

0 comments on commit 30a34ca

Please sign in to comment.