Skip to content

Commit 3dfa9b9

Browse files
committed
Fix plugin generation inside applications
Previously, generating a plugin inside an application would throw an error when trying to create a dummy_app due to the generator trying to reuse the application's name. This is fixed by extracting the logic used to determine the source of an application's name, and then only using the existing name as the source for the app:update task.
1 parent fd7c773 commit 3dfa9b9

File tree

4 files changed

+11
-16
lines changed

4 files changed

+11
-16
lines changed

railties/lib/rails/app_updater.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def app_generator
2121
private
2222
def generator_options
2323
options = { api: !!Rails.application.config.api_only, update: true }
24+
options[:name] = Rails.application.class.name.chomp("::Application").underscore
2425
options[:skip_active_job] = !defined?(ActiveJob::Railtie)
2526
options[:skip_active_record] = !defined?(ActiveRecord::Railtie)
2627
options[:skip_active_storage] = !defined?(ActiveStorage::Engine)

railties/lib/rails/generators/app_name.rb

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,11 @@ def app_name
1111
end
1212

1313
def original_app_name
14-
@original_app_name ||= defined_app_const_base? ? defined_app_name : (options[:name] || File.basename(destination_root))
14+
@original_app_name ||= (options[:name] || File.basename(destination_root))
1515
end
1616

17-
def defined_app_name
18-
defined_app_const_base.underscore
19-
end
20-
21-
def defined_app_const_base
22-
Rails.respond_to?(:application) && defined?(Rails::Application) &&
23-
Rails.application.is_a?(Rails::Application) && Rails.application.class.name.chomp("::Application")
24-
end
25-
26-
alias :defined_app_const_base? :defined_app_const_base
27-
2817
def app_const_base
29-
@app_const_base ||= defined_app_const_base || app_name.gsub(/\W/, "_").squeeze("_").camelize
18+
@app_const_base ||= app_name.gsub(/\W/, "_").squeeze("_").camelize
3019
end
3120
alias :camelized :app_const_base
3221

railties/test/application/generators_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ def with_bare_config
3535
assert File.exist?(File.join(rails_root, "vendor/plugins/bukkits/test/dummy/config/application.rb"))
3636
end
3737

38+
test "allow generating plugin inside Rails app directory" do
39+
rails "generate", "plugin", "vendor/plugins/bukkits"
40+
assert File.exist?(File.join(rails_root, "vendor/plugins/bukkits/test/dummy/config/application.rb"))
41+
end
42+
3843
test "generators default values" do
3944
with_bare_config do |c|
4045
assert_equal(true, c.generators.colorize_logging)

railties/test/generators/db_system_change_generator_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class ChangeGeneratorTest < Rails::Generators::TestCase
3636

3737
assert_file("config/database.yml") do |content|
3838
assert_match "adapter: postgresql", content
39-
assert_match "database: test_app", content
39+
assert_match "database: tmp_production", content
4040
end
4141

4242
assert_file("Gemfile") do |content|
@@ -50,7 +50,7 @@ class ChangeGeneratorTest < Rails::Generators::TestCase
5050

5151
assert_file("config/database.yml") do |content|
5252
assert_match "adapter: mysql2", content
53-
assert_match "database: test_app", content
53+
assert_match "database: tmp_production", content
5454
end
5555

5656
assert_file("Gemfile") do |content|
@@ -79,7 +79,7 @@ class ChangeGeneratorTest < Rails::Generators::TestCase
7979

8080
assert_file("config/database.yml") do |content|
8181
assert_match "adapter: mysql2", content
82-
assert_match "database: test_app", content
82+
assert_match "database: tmp_production", content
8383
end
8484

8585
assert_file("Gemfile") do |content|

0 commit comments

Comments
 (0)