From 30a34ca360670d3a8666ddb5eefd2ab642a46392 Mon Sep 17 00:00:00 2001 From: Rachael Ewins Date: Thu, 3 Dec 2020 14:22:01 +0000 Subject: [PATCH] Namespace option view generators (#1832) Resolves issue [1632](https://github.com/thoughtbot/administrate/issues/1632) Makes `rails generate administrate:views:index Modelname --namespace=` possible: ``` create app/views//modelnames/index.html.erb create app/views//modelnames/_collection.html.erb ``` --- lib/administrate/view_generator.rb | 8 +++++- .../administrate/views/views_generator.rb | 9 ++++--- spec/generators/views/edit_generator_spec.rb | 20 ++++++++++++++ spec/generators/views/index_generator_spec.rb | 20 ++++++++++++++ spec/generators/views/new_generator_spec.rb | 20 ++++++++++++++ spec/generators/views/show_generator_spec.rb | 11 ++++++++ spec/generators/views_generator_spec.rb | 27 ++++++++++++++++--- 7 files changed, 107 insertions(+), 8 deletions(-) diff --git a/lib/administrate/view_generator.rb b/lib/administrate/view_generator.rb index c1fa550c13..f41da1b647 100644 --- a/lib/administrate/view_generator.rb +++ b/lib/administrate/view_generator.rb @@ -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( @@ -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 diff --git a/lib/generators/administrate/views/views_generator.rb b/lib/generators/administrate/views/views_generator.rb index 338d06c031..278c0abc0b 100644 --- a/lib/generators/administrate/views/views_generator.rb +++ b/lib/generators/administrate/views/views_generator.rb @@ -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 diff --git a/spec/generators/views/edit_generator_spec.rb b/spec/generators/views/edit_generator_spec.rb index 365eeab77c..a13ae3f0ba 100644 --- a/spec/generators/views/edit_generator_spec.rb +++ b/spec/generators/views/edit_generator_spec.rb @@ -41,4 +41,24 @@ expect(contents).to eq(expected_contents) end end + + describe "administrate:views:edit resource --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 diff --git a/spec/generators/views/index_generator_spec.rb b/spec/generators/views/index_generator_spec.rb index 75901c83fc..0e8175d6a0 100644 --- a/spec/generators/views/index_generator_spec.rb +++ b/spec/generators/views/index_generator_spec.rb @@ -42,4 +42,24 @@ expect(contents).to eq(expected_contents) end end + + describe "administrate:views:index resource --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 diff --git a/spec/generators/views/new_generator_spec.rb b/spec/generators/views/new_generator_spec.rb index c9d6c60ea2..f82fa744c8 100644 --- a/spec/generators/views/new_generator_spec.rb +++ b/spec/generators/views/new_generator_spec.rb @@ -41,4 +41,24 @@ expect(contents).to eq(expected_contents) end end + + describe "administrate:views:new resource --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 diff --git a/spec/generators/views/show_generator_spec.rb b/spec/generators/views/show_generator_spec.rb index ab9d0c62c0..65eb58a485 100644 --- a/spec/generators/views/show_generator_spec.rb +++ b/spec/generators/views/show_generator_spec.rb @@ -23,4 +23,15 @@ expect(contents).to eq(expected_contents) end end + + describe "administrate:views:show resource --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 diff --git a/spec/generators/views_generator_spec.rb b/spec/generators/views_generator_spec.rb index b6284c6f03..87e936e4f7 100644 --- a/spec/generators/views_generator_spec.rb +++ b/spec/generators/views_generator_spec.rb @@ -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 @@ -24,7 +27,7 @@ expect(Rails::Generators).to invoke_generator( "administrate:views:index", - [resource], + [resource, "--namespace", "admin"], behavior: :revoke, ) end @@ -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