From 9a7f011da196f93cec285a6227ad9499434da4d1 Mon Sep 17 00:00:00 2001 From: Robin Dupret Date: Thu, 23 Jul 2015 17:48:47 +0200 Subject: Add the `--api` option for the plugin generator --- railties/CHANGELOG.md | 5 +++++ railties/lib/rails/generators/rails/plugin/plugin_generator.rb | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md index 573b1f8f69..30449aa670 100644 --- a/railties/CHANGELOG.md +++ b/railties/CHANGELOG.md @@ -1,3 +1,8 @@ +* Add a `--api` option in order to generate plugins that can be added + inside an API application. + + *Robin Dupret* + * Fix `NoMethodError` when generating a scaffold inside a full engine. *Yuji Yaginuma* diff --git a/railties/lib/rails/generators/rails/plugin/plugin_generator.rb b/railties/lib/rails/generators/rails/plugin/plugin_generator.rb index 66111004aa..84f67069c6 100644 --- a/railties/lib/rails/generators/rails/plugin/plugin_generator.rb +++ b/railties/lib/rails/generators/rails/plugin/plugin_generator.rb @@ -176,6 +176,9 @@ task default: :test desc: "If creating plugin in application's directory " + "skip adding entry to Gemfile" + class_option :api, type: :boolean, default: false, + desc: "Generate a smaller stack for API application plugins" + def initialize(*args) @dummy_path = nil super @@ -305,6 +308,10 @@ task default: :test options[:skip_test].blank? || options[:dummy_path] != 'test/dummy' end + def api? + options[:api] + end + def self.banner "rails plugin new #{self.arguments.map(&:usage).join(' ')} [options]" end -- cgit v1.2.3 From 67863aa8440805628477647e1015372d11913ca7 Mon Sep 17 00:00:00 2001 From: Robin Dupret Date: Thu, 23 Jul 2015 17:52:51 +0200 Subject: Skip assets, helpers and views generation for API plugins --- .../generators/rails/plugin/plugin_generator.rb | 23 ++++++++++++++-------- railties/test/generators/plugin_generator_test.rb | 12 +++++++++++ 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/railties/lib/rails/generators/rails/plugin/plugin_generator.rb b/railties/lib/rails/generators/rails/plugin/plugin_generator.rb index 84f67069c6..c986066aa8 100644 --- a/railties/lib/rails/generators/rails/plugin/plugin_generator.rb +++ b/railties/lib/rails/generators/rails/plugin/plugin_generator.rb @@ -17,15 +17,22 @@ module Rails def app if mountable? - directory 'app' - empty_directory_with_keep_file "app/assets/images/#{namespaced_name}" + if api? + directory 'app', exclude_pattern: %r{app/(views|helpers)} + else + directory 'app' + empty_directory_with_keep_file "app/assets/images/#{namespaced_name}" + end elsif full? empty_directory_with_keep_file 'app/models' empty_directory_with_keep_file 'app/controllers' - empty_directory_with_keep_file 'app/views' - empty_directory_with_keep_file 'app/helpers' empty_directory_with_keep_file 'app/mailers' - empty_directory_with_keep_file "app/assets/images/#{namespaced_name}" + + unless api? + empty_directory_with_keep_file "app/assets/images/#{namespaced_name}" + empty_directory_with_keep_file 'app/helpers' + empty_directory_with_keep_file 'app/views' + end end end @@ -213,15 +220,15 @@ task default: :test end def create_public_stylesheets_files - build(:stylesheets) + build(:stylesheets) unless api? end def create_javascript_files - build(:javascripts) + build(:javascripts) unless api? end def create_images_directory - build(:images) + build(:images) unless api? end def create_bin_files diff --git a/railties/test/generators/plugin_generator_test.rb b/railties/test/generators/plugin_generator_test.rb index 73e68863f4..2f6c66cade 100644 --- a/railties/test/generators/plugin_generator_test.rb +++ b/railties/test/generators/plugin_generator_test.rb @@ -544,6 +544,18 @@ class PluginGeneratorTest < Rails::Generators::TestCase end end + def test_skipping_useless_folders_generation_for_api_engines + ['--full', '--mountable'].each do |option| + run_generator [destination_root, option, '--api'] + + assert_no_directory "app/assets" + assert_no_directory "app/helpers" + assert_no_directory "app/views" + + FileUtils.rm_rf destination_root + end + end + protected def action(*args, &block) silence(:stdout){ generator.send(*args, &block) } -- cgit v1.2.3 From e3cfd5b174f37146b81026c0d1856b672bd6bbec Mon Sep 17 00:00:00 2001 From: Robin Dupret Date: Thu, 23 Jul 2015 17:58:02 +0200 Subject: Make ApplicationController inherit from AC::API for API plugins --- .../controllers/%namespaced_name%/application_controller.rb.tt | 2 +- railties/test/generators/plugin_generator_test.rb | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/railties/lib/rails/generators/rails/plugin/templates/app/controllers/%namespaced_name%/application_controller.rb.tt b/railties/lib/rails/generators/rails/plugin/templates/app/controllers/%namespaced_name%/application_controller.rb.tt index 7157e48c42..7fe4e5034d 100644 --- a/railties/lib/rails/generators/rails/plugin/templates/app/controllers/%namespaced_name%/application_controller.rb.tt +++ b/railties/lib/rails/generators/rails/plugin/templates/app/controllers/%namespaced_name%/application_controller.rb.tt @@ -1,5 +1,5 @@ <%= wrap_in_modules <<-rb.strip_heredoc - class ApplicationController < ActionController::Base + class ApplicationController < ActionController::#{api? ? "API" : "Base"} end rb %> diff --git a/railties/test/generators/plugin_generator_test.rb b/railties/test/generators/plugin_generator_test.rb index 2f6c66cade..86a776f840 100644 --- a/railties/test/generators/plugin_generator_test.rb +++ b/railties/test/generators/plugin_generator_test.rb @@ -556,6 +556,14 @@ class PluginGeneratorTest < Rails::Generators::TestCase end end + def test_application_controller_parent_for_mountable_api_plugins + run_generator [destination_root, '--mountable', '--api'] + + assert_file "app/controllers/bukkits/application_controller.rb" do |content| + assert_match "ApplicationController < ActionController::API", content + end + end + protected def action(*args, &block) silence(:stdout){ generator.send(*args, &block) } -- cgit v1.2.3 From ce32c9da962a9dd7a894c9a4457db9dd5f5a5a1f Mon Sep 17 00:00:00 2001 From: Robin Dupret Date: Thu, 23 Jul 2015 18:01:53 +0200 Subject: Generate an API dummy application for API plugins --- railties/lib/rails/generators/rails/plugin/plugin_generator.rb | 1 + railties/test/generators/plugin_generator_test.rb | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/railties/lib/rails/generators/rails/plugin/plugin_generator.rb b/railties/lib/rails/generators/rails/plugin/plugin_generator.rb index c986066aa8..0e1326ce4f 100644 --- a/railties/lib/rails/generators/rails/plugin/plugin_generator.rb +++ b/railties/lib/rails/generators/rails/plugin/plugin_generator.rb @@ -89,6 +89,7 @@ task default: :test opts = (options || {}).slice(*PASSTHROUGH_OPTIONS) opts[:force] = force opts[:skip_bundle] = true + opts[:api] = options.api? invoke Rails::Generators::AppGenerator, [ File.expand_path(dummy_path, destination_root) ], opts diff --git a/railties/test/generators/plugin_generator_test.rb b/railties/test/generators/plugin_generator_test.rb index 86a776f840..2a89e2160b 100644 --- a/railties/test/generators/plugin_generator_test.rb +++ b/railties/test/generators/plugin_generator_test.rb @@ -564,6 +564,14 @@ class PluginGeneratorTest < Rails::Generators::TestCase end end + def test_dummy_api_application_for_api_plugins + run_generator [destination_root, '--api'] + + assert_file "test/dummy/config/application.rb" do |content| + assert_match "config.api_only = true", content + end + end + protected def action(*args, &block) silence(:stdout){ generator.send(*args, &block) } -- cgit v1.2.3 From a120f2716e794f49677804b1e0733c4baa61e96a Mon Sep 17 00:00:00 2001 From: Robin Dupret Date: Thu, 23 Jul 2015 18:03:40 +0200 Subject: Enable the `api_only` option for API plugins' generators This way, running a generator inside the plugin's directory, files that are not relevant won't be generated (e.g. views or assets). This won't interfere with the application's generators configuration. --- .../templates/lib/%namespaced_name%/engine.rb | 1 + railties/test/generators/plugin_generator_test.rb | 26 ++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/railties/lib/rails/generators/rails/plugin/templates/lib/%namespaced_name%/engine.rb b/railties/lib/rails/generators/rails/plugin/templates/lib/%namespaced_name%/engine.rb index 17afd52177..8938770fc4 100644 --- a/railties/lib/rails/generators/rails/plugin/templates/lib/%namespaced_name%/engine.rb +++ b/railties/lib/rails/generators/rails/plugin/templates/lib/%namespaced_name%/engine.rb @@ -1,6 +1,7 @@ <%= wrap_in_modules <<-rb.strip_heredoc class Engine < ::Rails::Engine #{mountable? ? ' isolate_namespace ' + camelized_modules : ' '} + #{api? ? " config.generators.api_only = true" : ' '} end rb %> diff --git a/railties/test/generators/plugin_generator_test.rb b/railties/test/generators/plugin_generator_test.rb index 2a89e2160b..6f5a815173 100644 --- a/railties/test/generators/plugin_generator_test.rb +++ b/railties/test/generators/plugin_generator_test.rb @@ -572,6 +572,32 @@ class PluginGeneratorTest < Rails::Generators::TestCase end end + + def test_api_generators_configuration_for_api_engines + run_generator [destination_root, '--full', '--api'] + + assert_file "lib/bukkits/engine.rb" do |content| + assert_match "config.generators.api_only = true", content + end + end + + def test_scaffold_generator_for_mountable_api_plugins + run_generator [destination_root, '--mountable', '--api'] + + capture(:stdout) do + `#{destination_root}/bin/rails g scaffold article` + end + + assert_file "app/models/bukkits/article.rb" + assert_file "app/controllers/bukkits/articles_controller.rb" do |content| + assert_match "only: [:show, :update, :destroy]", content + end + + assert_no_directory "app/assets" + assert_no_directory "app/helpers" + assert_no_directory "app/views" + end + protected def action(*args, &block) silence(:stdout){ generator.send(*args, &block) } -- cgit v1.2.3