Skip to content

Commit d62bdd8

Browse files
committed
Change snippet interpolation in ingredients logic
1 parent d1e4730 commit d62bdd8

File tree

4 files changed

+81
-16
lines changed

4 files changed

+81
-16
lines changed

app/models/ingredient.rb

+38-1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ def dependencies_satisfied?(recipe)
6363
requires.all? { |dep| recipe.ingredients.any? { |i| i.name == dep } }
6464
end
6565

66+
def template_with_interpolated_snippets
67+
interpolate_snippets
68+
template_content
69+
end
70+
6671
def configuration_for(configuration)
6772
# Validate configuration against configures_with schema
6873
configures_with.each do |key, validator|
@@ -106,6 +111,18 @@ def to_commit_message
106111
COMMIT_MESSAGE
107112
end
108113

114+
def template_with_interpolated_snippets
115+
return template_content if snippets.blank? || template_content.blank?
116+
117+
template_content.tap do |content|
118+
snippets.each_with_index do |snippet, index|
119+
placeholder = "{{#{index + 1}}}"
120+
content.gsub!(placeholder, to_literal(snippet, index))
121+
end
122+
end
123+
end
124+
125+
109126
private
110127

111128
def cleanup_ui_elements
@@ -125,7 +142,27 @@ def process_snippets
125142

126143
self.snippets ||= []
127144
filtered_snippets = new_snippets.reject(&:blank?)
128-
# Only add snippets that aren't already in the list
145+
129146
self.snippets += filtered_snippets.reject { |s| snippets.include?(s) }
130147
end
148+
149+
def to_literal(snippet, index)
150+
if snippet.include?("\n")
151+
if snippet.start_with?("<<")
152+
snippet
153+
else
154+
<<~SNIPPET
155+
<<~SNIPPET_#{index+1}
156+
#{snippet}
157+
SNIPPET_#{index+1}
158+
SNIPPET
159+
end
160+
else
161+
if snippet.start_with?("'") || snippet.start_with?('"')
162+
snippet
163+
else
164+
"\"#{snippet}\""
165+
end
166+
end
167+
end
131168
end

app/services/data_repository_service.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def initialize_repository
4444
def write_ingredient(ingredient, repo_name:)
4545
tree_items = []
4646

47-
template_content = ingredient.template_content
47+
template_content = ingredient.template_with_interpolated_snippets
4848

4949
tree_items << {
5050
path: github_template_path(ingredient),

test/models/ingredient_test.rb

+42
Original file line numberDiff line numberDiff line change
@@ -266,4 +266,46 @@ class IngredientTest < ActiveSupport::TestCase
266266
# Snippets should remain unchanged
267267
assert_equal [ "existing snippet" ], ingredient.snippets
268268
end
269+
270+
test "interpolates snippets into template content" do
271+
ingredient = Ingredient.new(
272+
name: "Test Ingredient",
273+
category: "Testing",
274+
template_content: 'say "This is my template"\ncreate_file "myfile.rb", {{1}}\ncreate_file "my_other_file.rb", {{2}}',
275+
created_by: users(:john)
276+
)
277+
ingredient.new_snippets = [ '"foo"', '"bar"' ]
278+
ingredient.save!
279+
280+
expected_content = 'say "This is my template"\ncreate_file "myfile.rb", "foo"\ncreate_file "my_other_file.rb", "bar"'
281+
assert_equal expected_content, ingredient.template_content
282+
end
283+
284+
test "handles missing snippets gracefully" do
285+
ingredient = Ingredient.new(
286+
name: "Test Ingredient",
287+
category: "Testing",
288+
template_content: 'say "This is my template"\ncreate_file "myfile.rb", {{1}}\ncreate_file "my_other_file.rb", {{2}}',
289+
created_by: users(:john)
290+
)
291+
ingredient.new_snippets = [ '"foo"' ] # Only one snippet provided
292+
ingredient.save!
293+
294+
# The second placeholder should remain unchanged
295+
expected_content = 'say "This is my template"\ncreate_file "myfile.rb", "foo"\ncreate_file "my_other_file.rb", {{2}}'
296+
assert_equal expected_content, ingredient.template_content
297+
end
298+
299+
test "handles template without placeholders" do
300+
ingredient = Ingredient.new(
301+
name: "Test Ingredient",
302+
category: "Testing",
303+
template_content: 'say "This is my template"',
304+
created_by: users(:john)
305+
)
306+
ingredient.new_snippets = [ '"foo"', '"bar"' ]
307+
ingredient.save!
308+
309+
assert_equal 'say "This is my template"', ingredient.template_content
310+
end
269311
end

test/system/ingredients_test.rb

-14
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,4 @@ class IngredientsTest < ApplicationSystemTestCase
9797

9898
assert_text "Ingredient was successfully deleted"
9999
end
100-
101-
test "autosaving ingredient changes" do
102-
visit edit_ingredient_path(@ingredient)
103-
104-
fill_in "Name", with: "Autosaved Ingredient"
105-
106-
# Wait for autosave
107-
sleep 2
108-
109-
# Reload the page
110-
visit edit_ingredient_path(@ingredient)
111-
112-
assert_field "Name", with: "Autosaved Ingredient"
113-
end
114100
end

0 commit comments

Comments
 (0)