diff options
author | Ryan Manuel <rfmanuel@alum.mit.edu> | 2016-01-28 12:43:24 -0600 |
---|---|---|
committer | Ryan Manuel <rfmanuel@alum.mit.edu> | 2016-01-28 12:43:24 -0600 |
commit | 31701fbdf28688ba53b8f2d2d26904954ae1b976 (patch) | |
tree | 1db61358471014397f66f0e34399cdfee1806cdf | |
parent | 2637fb75d82e1c69333855abd58c2470994995d3 (diff) | |
download | rails-31701fbdf28688ba53b8f2d2d26904954ae1b976.tar.gz rails-31701fbdf28688ba53b8f2d2d26904954ae1b976.tar.bz2 rails-31701fbdf28688ba53b8f2d2d26904954ae1b976.zip |
Add an after_bundle callback in Rails plugin templates
-rw-r--r-- | guides/source/5_0_release_notes.md | 3 | ||||
-rw-r--r-- | railties/CHANGELOG.md | 7 | ||||
-rw-r--r-- | railties/lib/rails/generators/rails/plugin/plugin_generator.rb | 6 | ||||
-rw-r--r-- | railties/test/generators/app_generator_test.rb | 5 | ||||
-rw-r--r-- | railties/test/generators/plugin_generator_test.rb | 28 |
5 files changed, 47 insertions, 2 deletions
diff --git a/guides/source/5_0_release_notes.md b/guides/source/5_0_release_notes.md index 2650384df3..6537b089a9 100644 --- a/guides/source/5_0_release_notes.md +++ b/guides/source/5_0_release_notes.md @@ -120,6 +120,9 @@ Please refer to the [Changelog][railties] for detailed changes. ([Pull Request](https://github.com/rails/rails/pull/22457), [Pull Request](https://github.com/rails/rails/pull/22288)) +* Introduced an `after_bundle` callback for use in Rails plugin templates. + ([Pull Request](https://github.com/rails/rails/pull/)) + Action Pack ----------- diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md index 2c363c55da..91e879dd73 100644 --- a/railties/CHANGELOG.md +++ b/railties/CHANGELOG.md @@ -448,4 +448,11 @@ *Alex Robbin* +* Add `after_bundle` callbacks in Rails plugin templates. Useful for allowing + templates to perform actions that are dependent upon `bundle install`. + + Fixes #<> + + *Ryan Manuel* + Please check [4-2-stable](https://github.com/rails/rails/blob/4-2-stable/railties/CHANGELOG.md) for previous changes. diff --git a/railties/lib/rails/generators/rails/plugin/plugin_generator.rb b/railties/lib/rails/generators/rails/plugin/plugin_generator.rb index 0c7a73a54e..b5e836b584 100644 --- a/railties/lib/rails/generators/rails/plugin/plugin_generator.rb +++ b/railties/lib/rails/generators/rails/plugin/plugin_generator.rb @@ -259,6 +259,12 @@ task default: :test public_task :apply_rails_template, :run_bundle + def run_after_bundle_callbacks + @after_bundle_callbacks.each do |callback| + callback.call + end + end + def name @name ||= begin # same as ActiveSupport::Inflector#underscore except not replacing '-' diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb index 136bdd1694..53f8ed6af0 100644 --- a/railties/test/generators/app_generator_test.rb +++ b/railties/test/generators/app_generator_test.rb @@ -703,9 +703,8 @@ class AppGeneratorTest < Rails::Generators::TestCase end sequence = ['install', 'exec spring binstub --all', 'echo ran after_bundle'] - ensure_bundler_first = -> command do @sequence_step ||= 0 - + ensure_bundler_first = -> command do assert_equal sequence[@sequence_step], command, "commands should be called in sequence #{sequence}" @sequence_step += 1 end @@ -717,6 +716,8 @@ class AppGeneratorTest < Rails::Generators::TestCase end end end + + assert_equal 3, @sequence_step end protected diff --git a/railties/test/generators/plugin_generator_test.rb b/railties/test/generators/plugin_generator_test.rb index 874bda17b7..ef33cd4ff7 100644 --- a/railties/test/generators/plugin_generator_test.rb +++ b/railties/test/generators/plugin_generator_test.rb @@ -634,6 +634,34 @@ class PluginGeneratorTest < Rails::Generators::TestCase assert_file "app/models/bukkits/article.rb", /class Article < ApplicationRecord/ end + def test_after_bundle_callback + path = 'http://example.org/rails_template' + template = %{ after_bundle { run 'echo ran after_bundle' } } + template.instance_eval "def read; self; end" # Make the string respond to read + + check_open = -> *args do + assert_equal [ path, 'Accept' => 'application/x-thor-template' ], args + template + end + + sequence = ['install', 'echo ran after_bundle'] + @sequence_step ||= 0 + ensure_bundler_first = -> command do + assert_equal sequence[@sequence_step], command, "commands should be called in sequence #{sequence}" + @sequence_step += 1 + end + + generator([destination_root], template: path).stub(:open, check_open, template) do + generator.stub(:bundle_command, ensure_bundler_first) do + generator.stub(:run, ensure_bundler_first) do + quietly { generator.invoke_all } + end + end + end + + assert_equal 2, @sequence_step + end + protected def action(*args, &block) silence(:stdout){ generator.send(*args, &block) } |