Skip to content

Commit dac667b

Browse files
Add confirm prompt if turbo is available
1 parent 6df40af commit dac667b

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

lib/generators/tailwindcss/scaffold/scaffold_generator.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ def copy_view_files
3030
def available_views
3131
%w(index edit show new _form)
3232
end
33+
34+
def turbo_available?
35+
defined? Turbo
36+
end
3337
end
3438
end
3539
end

lib/generators/tailwindcss/scaffold/templates/show.html.erb.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212
<%%= link_to "Edit this <%= human_name.downcase %>", <%= edit_helper(type: :path) %>, class: "mt-2 rounded-md px-3.5 py-2.5 bg-gray-100 hover:bg-gray-50 inline-block font-medium" %>
1313
<%%= link_to "Back to <%= human_name.pluralize.downcase %>", <%= index_helper %>_path, class: "ml-2 rounded-md px-3.5 py-2.5 bg-gray-100 hover:bg-gray-50 inline-block font-medium" %>
1414
<div class="inline-block ml-2">
15-
<%%= button_to "Destroy this <%= human_name.downcase %>", <%= model_resource_name(prefix: "@") %>, method: :delete, class: "mt-2 rounded-md px-3.5 py-2.5 text-white bg-red-600 hover:bg-red-500 font-medium" %>
15+
<%%= button_to "Destroy this <%= human_name.downcase %>", <%= model_resource_name(prefix: "@") %>, method: :delete, class: "mt-2 rounded-md px-3.5 py-2.5 text-white bg-red-600 hover:bg-red-500 font-medium"<%= ", data: { turbo_confirm: \"Are you sure?\" }" if turbo_available? %> %>
1616
</div>
1717
</div>

test/lib/generators/tailwindcss/scaffold_generator_test.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,24 @@ class Tailwindcss::Generators::ScaffoldGeneratorTest < Rails::Generators::TestCa
4646
end
4747
end
4848
end
49+
50+
test "adds a confirmation prompt to the destroy button when turbo is available" do
51+
::Turbo = Class.new
52+
53+
run_generator
54+
55+
assert_file "app/views/messages/show.html.erb" do |body|
56+
assert_match "data: { turbo_confirm: \"Are you sure?\" }", body, "confirmation prompt should be added for turbo"
57+
end
58+
59+
Object.send :remove_const, :Turbo
60+
end
61+
62+
test "doesn't add a confirmation prompt to the destroy button when turbo is not available" do
63+
run_generator
64+
65+
assert_file "app/views/messages/show.html.erb" do |body|
66+
assert_no_match "data: { turbo_confirm: \"Are you sure?\" }", body, "confirmation prompt shouldn't be added"
67+
end
68+
end
4969
end

0 commit comments

Comments
 (0)