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

Namespace option view generators #1832

Merged
merged 5 commits into from
Dec 3, 2020
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
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
Copy link

Choose a reason for hiding this comment

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

Metrics/LineLength: Line is too long. [81/80]

Copy link
Collaborator

Choose a reason for hiding this comment

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

Don't worry about this specific warning.

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