1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #43089 from kaukas/template-gem-comments

Support gem comments in Rails templates
This commit is contained in:
Guillermo Iguaran 2021-08-29 09:57:38 -07:00 committed by GitHub
commit 0d31319562
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 3 deletions

View file

@ -137,5 +137,9 @@
*Gannon McGibbon* *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. Please check [6-1-stable](https://github.com/rails/rails/blob/6-1-stable/railties/CHANGELOG.md) for previous changes.

View file

@ -18,6 +18,7 @@ module Rails
# gem "technoweenie-restful-authentication", lib: "restful-authentication", source: "http://gems.github.com/" # gem "technoweenie-restful-authentication", lib: "restful-authentication", source: "http://gems.github.com/"
# gem "rails", "3.0", git: "https://github.com/rails/rails" # gem "rails", "3.0", git: "https://github.com/rails/rails"
# gem "RedCloth", ">= 4.1.0", "< 4.2.0" # gem "RedCloth", ">= 4.1.0", "< 4.2.0"
# gem "rspec", comment: "Put this comment above the gem declaration"
def gem(*args) def gem(*args)
options = args.extract_options! options = args.extract_options!
name, *versions = args name, *versions = args
@ -26,6 +27,9 @@ module Rails
# otherwise use name (version). # otherwise use name (version).
parts, message = [ quote(name) ], name.dup 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) if versions = versions.any? ? versions : options.delete(:version)
_versions = Array(versions) _versions = Array(versions)
_versions.each do |version| _versions.each do |version|
@ -40,9 +44,17 @@ module Rails
parts << quote(options) unless options.empty? parts << quote(options) unless options.empty?
in_root do in_root do
str = "gem #{parts.join(", ")}" str = []
str = indentation + str if comment
append_file_with_newline "Gemfile", str, verbose: false 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
end end

View file

@ -116,6 +116,22 @@ class ActionsTest < Rails::Generators::TestCase
assert_file "Gemfile", /gem "rspec", github: "dchelimsky\/rspec", tag: "1\.2\.9\.rc1"/ assert_file "Gemfile", /gem "rspec", github: "dchelimsky\/rspec", tag: "1\.2\.9\.rc1"/
end 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 def test_gem_with_non_string_options
run_generator 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/ assert_file "Gemfile", /\n\ngroup :development, :test do\n gem "rspec-rails"\nend\n\ngroup :test do\n gem "fakeweb"\nend\n\z/
end 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 def test_github_should_create_an_indented_block
run_generator run_generator