Skip to content

Commit 0d31319

Browse files
Merge pull request rails#43089 from kaukas/template-gem-comments
Support gem comments in Rails templates
2 parents 0c9f6b7 + 18ff328 commit 0d31319

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

railties/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,5 +137,9 @@
137137

138138
*Gannon McGibbon*
139139

140+
* Add support for comments above gem declaration in Rails application templates, e.g. `gem("nokogiri", comment: "For XML")`.
141+
142+
*Linas Juškevičius*
143+
140144

141145
Please check [6-1-stable](https://github.com/rails/rails/blob/6-1-stable/railties/CHANGELOG.md) for previous changes.

railties/lib/rails/generators/actions.rb

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def initialize(*) # :nodoc:
1818
# gem "technoweenie-restful-authentication", lib: "restful-authentication", source: "http://gems.github.com/"
1919
# gem "rails", "3.0", git: "https://github.com/rails/rails"
2020
# gem "RedCloth", ">= 4.1.0", "< 4.2.0"
21+
# gem "rspec", comment: "Put this comment above the gem declaration"
2122
def gem(*args)
2223
options = args.extract_options!
2324
name, *versions = args
@@ -26,6 +27,9 @@ def gem(*args)
2627
# otherwise use name (version).
2728
parts, message = [ quote(name) ], name.dup
2829

30+
# Output a comment above the gem declaration.
31+
comment = options.delete(:comment)
32+
2933
if versions = versions.any? ? versions : options.delete(:version)
3034
_versions = Array(versions)
3135
_versions.each do |version|
@@ -40,9 +44,17 @@ def gem(*args)
4044
parts << quote(options) unless options.empty?
4145

4246
in_root do
43-
str = "gem #{parts.join(", ")}"
44-
str = indentation + str
45-
append_file_with_newline "Gemfile", str, verbose: false
47+
str = []
48+
if comment
49+
comment.each_line do |comment_line|
50+
str << indentation
51+
str << "# #{comment_line}"
52+
end
53+
str << "\n"
54+
end
55+
str << indentation
56+
str << "gem #{parts.join(", ")}"
57+
append_file_with_newline "Gemfile", str.join, verbose: false
4658
end
4759
end
4860

railties/test/generators/actions_test.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,22 @@ def test_gem_should_include_options
116116
assert_file "Gemfile", /gem "rspec", github: "dchelimsky\/rspec", tag: "1\.2\.9\.rc1"/
117117
end
118118

119+
def test_gem_should_put_the_comment_before_gem_declaration
120+
run_generator
121+
122+
action :gem, "rspec", comment: "Use RSpec"
123+
124+
assert_file "Gemfile", /# Use RSpec\ngem "rspec"/
125+
end
126+
127+
def test_gem_should_support_multiline_comments
128+
run_generator
129+
130+
action :gem, "rspec", comment: "Use RSpec\nReplaces minitest"
131+
132+
assert_file "Gemfile", /# Use RSpec\n# Replaces minitest\ngem "rspec"/
133+
end
134+
119135
def test_gem_with_non_string_options
120136
run_generator
121137

@@ -156,6 +172,26 @@ def test_gem_group_should_wrap_gems_in_a_group
156172
assert_file "Gemfile", /\n\ngroup :development, :test do\n gem "rspec-rails"\nend\n\ngroup :test do\n gem "fakeweb"\nend\n\z/
157173
end
158174

175+
def test_gem_group_should_indent_comments
176+
run_generator
177+
178+
action :gem_group, :test do
179+
gem "fakeweb", comment: "Fake requests"
180+
end
181+
182+
assert_file "Gemfile", /\n\ngroup :test do\n # Fake requests\n gem "fakeweb"\nend\n\z/
183+
end
184+
185+
def test_gem_group_should_indent_multiline_comments
186+
run_generator
187+
188+
action :gem_group, :test do
189+
gem "fakeweb", comment: "Fake requests\nNeeded in tests"
190+
end
191+
192+
assert_file "Gemfile", /\n\ngroup :test do\n # Fake requests\n # Needed in tests\n gem "fakeweb"\nend\n\z/
193+
end
194+
159195
def test_github_should_create_an_indented_block
160196
run_generator
161197

0 commit comments

Comments
 (0)