Use standard config/application.rb in plugin dummy

The `config/application.rb` file of a plugin test dummy app should be
the same as in a standard Rails app, with the exception of an additional
`require` statement to load the plugin itself.  However, prior to this
commit, the plugin generator used a specialized template for a portion
of this file, which tended to drift out-of-sync with the standard app
generator's template.  Specifically, this drift could result in missing
`require` statements of newer Rails frameworks when the plugin generator
was run with any `--skip-xxxx` option.

This commit changes the plugin generator to insert its additional
`require` statement into the standard `config/application.rb` file
generated by the app generator.
This commit is contained in:
Jonathan Hefner 2020-07-28 14:33:40 -05:00
parent 23229d2453
commit adfa417fbb
3 changed files with 14 additions and 42 deletions

View File

@ -1,5 +1,6 @@
# frozen_string_literal: true
require "active_support/core_ext/hash/except"
require "rails/generators/rails/app/app_generator"
require "date"
@ -92,13 +93,10 @@ task default: :test
end
end
PASSTHROUGH_OPTIONS = [
:skip_active_record, :skip_active_storage, :skip_action_mailer, :skip_javascript, :skip_action_cable, :skip_sprockets, :database,
:api, :quiet, :pretend, :skip
]
DUMMY_IGNORE_OPTIONS = %i[dev edge master template]
def generate_test_dummy(force = false)
opts = (options.dup || {}).keep_if { |k, _| PASSTHROUGH_OPTIONS.map(&:to_s).include?(k) }
opts = options.transform_keys(&:to_sym).except(*DUMMY_IGNORE_OPTIONS)
opts[:force] = force
opts[:skip_bundle] = true
opts[:skip_listen] = true
@ -113,7 +111,11 @@ task default: :test
def test_dummy_config
template "rails/boot.rb", "#{dummy_path}/config/boot.rb", force: true
template "rails/application.rb", "#{dummy_path}/config/application.rb", force: true
insert_into_file "#{dummy_path}/config/application.rb", <<~RUBY, after: /^Bundler\.require.+\n/
require #{namespaced_name.inspect}
RUBY
if mountable?
template "rails/routes.rb", "#{dummy_path}/config/routes.rb", force: true
end
@ -283,7 +285,6 @@ task default: :test
say_status :vendor_app, dummy_path
mute do
build(:generate_test_dummy)
store_application_definition!
build(:test_dummy_config)
build(:test_dummy_assets)
build(:test_dummy_clean)
@ -383,18 +384,6 @@ task default: :test
end
end
def application_definition
@application_definition ||= begin
dummy_application_path = File.expand_path("#{dummy_path}/config/application.rb", destination_root)
unless options[:pretend] || !File.exist?(dummy_application_path)
contents = File.read(dummy_application_path)
contents[(contents.index(/module ([\w]+)\n(.*)class Application/m))..-1]
end
end
end
alias :store_application_definition! :application_definition
def get_builder_class
defined?(::PluginBuilder) ? ::PluginBuilder : Rails::PluginBuilder
end

View File

@ -1,23 +0,0 @@
require_relative "boot"
<% if include_all_railties? -%>
require "rails/all"
<% else -%>
require "rails"
# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
<%= comment_if :skip_active_record %>require "active_record/railtie"
<%= comment_if :skip_active_storage %>require "active_storage/engine"
require "action_controller/railtie"
<%= comment_if :skip_action_mailer %>require "action_mailer/railtie"
require "action_view/railtie"
<%= comment_if :skip_action_cable %>require "action_cable/engine"
<%= comment_if :skip_sprockets %>require "sprockets/railtie"
<%= comment_if :skip_test %>require "rails/test_unit/railtie"
<% end -%>
Bundler.require(*Rails.groups)
require "<%= namespaced_name %>"
<%= application_definition %>

View File

@ -455,6 +455,12 @@ class PluginGeneratorTest < Rails::Generators::TestCase
end
end
def test_dummy_application_loads_plugin
run_generator
assert_file "test/dummy/config/application.rb", /^require "bukkits"/
end
def test_dummy_application_uses_dynamic_rails_version_number
run_generator