From f60fbfaee6be9618f306c55fc2d6e0dc4c6ab77f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linas=20Ju=C5=A1kevi=C4=8Dius?= Date: Tue, 24 Aug 2021 22:48:23 +0300 Subject: [PATCH 1/2] Support gem comments in Rails templates --- railties/lib/rails/generators/actions.rb | 18 ++++++++++-- railties/test/generators/actions_test.rb | 36 ++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/railties/lib/rails/generators/actions.rb b/railties/lib/rails/generators/actions.rb index 3ec64f8580..dcc12faa43 100644 --- a/railties/lib/rails/generators/actions.rb +++ b/railties/lib/rails/generators/actions.rb @@ -18,6 +18,7 @@ module Rails # gem "technoweenie-restful-authentication", lib: "restful-authentication", source: "http://gems.github.com/" # gem "rails", "3.0", git: "https://github.com/rails/rails" # gem "RedCloth", ">= 4.1.0", "< 4.2.0" + # gem "rspec", comment: "Put this comment above the gem declaration" def gem(*args) options = args.extract_options! name, *versions = args @@ -26,6 +27,9 @@ module Rails # otherwise use name (version). parts, message = [ quote(name) ], name.dup + # Output a comment above the gem declaration. + comment = options.delete(:comment) + if versions = versions.any? ? versions : options.delete(:version) _versions = Array(versions) _versions.each do |version| @@ -40,9 +44,17 @@ module Rails parts << quote(options) unless options.empty? in_root do - str = "gem #{parts.join(", ")}" - str = indentation + str - append_file_with_newline "Gemfile", str, verbose: false + str = [] + unless comment.nil? + comment.each_line do |comment_line| + str << indentation + str << "# #{comment_line}" + end + str << "\n" + end + str << indentation + str << "gem #{parts.join(", ")}" + append_file_with_newline "Gemfile", str.join, verbose: false end end diff --git a/railties/test/generators/actions_test.rb b/railties/test/generators/actions_test.rb index 4118156c06..a050352de3 100644 --- a/railties/test/generators/actions_test.rb +++ b/railties/test/generators/actions_test.rb @@ -116,6 +116,22 @@ class ActionsTest < Rails::Generators::TestCase assert_file "Gemfile", /gem "rspec", github: "dchelimsky\/rspec", tag: "1\.2\.9\.rc1"/ end + def test_gem_should_put_the_comment_before_gem_declaration + run_generator + + action :gem, "rspec", comment: "Use RSpec" + + assert_file "Gemfile", /# Use RSpec\ngem "rspec"/ + end + + def test_gem_should_support_multiline_comments + run_generator + + action :gem, "rspec", comment: "Use RSpec\nReplaces minitest" + + assert_file "Gemfile", /# Use RSpec\n# Replaces minitest\ngem "rspec"/ + end + def test_gem_with_non_string_options run_generator @@ -156,6 +172,26 @@ class ActionsTest < Rails::Generators::TestCase assert_file "Gemfile", /\n\ngroup :development, :test do\n gem "rspec-rails"\nend\n\ngroup :test do\n gem "fakeweb"\nend\n\z/ end + def test_gem_group_should_indent_comments + run_generator + + action :gem_group, :test do + gem "fakeweb", comment: "Fake requests" + end + + assert_file "Gemfile", /\n\ngroup :test do\n # Fake requests\n gem "fakeweb"\nend\n\z/ + end + + def test_gem_group_should_indent_multiline_comments + run_generator + + action :gem_group, :test do + gem "fakeweb", comment: "Fake requests\nNeeded in tests" + end + + assert_file "Gemfile", /\n\ngroup :test do\n # Fake requests\n # Needed in tests\n gem "fakeweb"\nend\n\z/ + end + def test_github_should_create_an_indented_block run_generator From 18ff32827ce68bf7b1c973f3da94929bc8dc3ea0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linas=20Ju=C5=A1kevi=C4=8Dius?= Date: Sat, 28 Aug 2021 16:30:32 +0300 Subject: [PATCH 2/2] Improve gem comment condition --- railties/CHANGELOG.md | 4 ++++ railties/lib/rails/generators/actions.rb | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md index 3af0eb966c..253d3631e9 100644 --- a/railties/CHANGELOG.md +++ b/railties/CHANGELOG.md @@ -104,5 +104,9 @@ *Gannon McGibbon* +* Add support for comments above gem declaration in Rails application templates, e.g. `gem("nokogiri", comment: "For XML")`. + + *Linas Juškevičius* + Please check [6-1-stable](https://github.com/rails/rails/blob/6-1-stable/railties/CHANGELOG.md) for previous changes. diff --git a/railties/lib/rails/generators/actions.rb b/railties/lib/rails/generators/actions.rb index dcc12faa43..8328897f2c 100644 --- a/railties/lib/rails/generators/actions.rb +++ b/railties/lib/rails/generators/actions.rb @@ -45,7 +45,7 @@ module Rails in_root do str = [] - unless comment.nil? + if comment comment.each_line do |comment_line| str << indentation str << "# #{comment_line}"