From 415e2404061e0ab4f238f3f11c959a293c6442da Mon Sep 17 00:00:00 2001 From: Jeremy Lecour Date: Thu, 4 Mar 2021 22:55:09 +0100 Subject: [PATCH] extraction d'un composant pour les erreurs de formulaires --- .../flash_notice_component.html.erb | 2 +- app/components/form_errors_component.html.erb | 24 +++++++++++++++++++ app/components/form_errors_component.rb | 8 +++++++ app/views/conditions/_form.html.erb | 14 +++-------- app/views/filters/_form.html.erb | 14 +++-------- app/views/filters/show.html.erb | 6 +++-- app/views/operations/_form.html.erb | 14 +++-------- test/components/form_errors_component_test.rb | 10 ++++++++ 8 files changed, 56 insertions(+), 36 deletions(-) create mode 100644 app/components/form_errors_component.html.erb create mode 100644 app/components/form_errors_component.rb create mode 100644 test/components/form_errors_component_test.rb diff --git a/app/components/flash_notice_component.html.erb b/app/components/flash_notice_component.html.erb index e46e50b..96f433e 100644 --- a/app/components/flash_notice_component.html.erb +++ b/app/components/flash_notice_component.html.erb @@ -1,4 +1,4 @@ -
+
diff --git a/app/components/form_errors_component.html.erb b/app/components/form_errors_component.html.erb new file mode 100644 index 0000000..0487b29 --- /dev/null +++ b/app/components/form_errors_component.html.erb @@ -0,0 +1,24 @@ + +
+
+
+ + +
+
+

+ <%= content %> +

+
+
    + <% @errors.each do |error| %> +
  • <%= error.full_message %>
  • + <% end %> +
+
+
+
+
+ diff --git a/app/components/form_errors_component.rb b/app/components/form_errors_component.rb new file mode 100644 index 0000000..70d210f --- /dev/null +++ b/app/components/form_errors_component.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +class FormErrorsComponent < ViewComponent::Base + def initialize(errors:) + @errors = errors + end + +end diff --git a/app/views/conditions/_form.html.erb b/app/views/conditions/_form.html.erb index 86f57d9..cb0bb3c 100644 --- a/app/views/conditions/_form.html.erb +++ b/app/views/conditions/_form.html.erb @@ -1,16 +1,8 @@ <%= form_with(model: [ condition.filter, condition ], data: { controller: "reset_form", action: "turbo:submit-end->reset_form#reset" }) do |form| %> - <% if condition.errors.any? %> -
-

<%= pluralize(condition.errors.count, "error") %> prohibited this condition from being saved:

- -
    - <% condition.errors.each do |error| %> -
  • <%= error.full_message %>
  • - <% end %> -
-
- <% end %> + <%= render(FormErrorsComponent.new(errors: condition.errors)) do %> + <%= pluralize(condition.errors.count, "error") %> prohibited this condition from being saved: + <% end if condition.errors.any? %>
diff --git a/app/views/filters/_form.html.erb b/app/views/filters/_form.html.erb index 069028d..468ff8c 100644 --- a/app/views/filters/_form.html.erb +++ b/app/views/filters/_form.html.erb @@ -1,16 +1,8 @@ <%= turbo_frame_tag "filter" do %> <%= form_with(model: filter) do |form| %> - <% if filter.errors.any? %> -
-

<%= pluralize(filter.errors.count, "error") %> prohibited this filter from being saved:

- -
    - <% filter.errors.each do |error| %> -
  • <%= error.full_message %>
  • - <% end %> -
-
- <% end %> + <%= render(FormErrorsComponent.new(errors: filter.errors)) do %> + <%= pluralize(filter.errors.count, "error") %> prohibited this filter from being saved: + <% end if filter.errors.any? %>
<%= form.label :description, class: "block text-sm font-medium text-gray-700" %> diff --git a/app/views/filters/show.html.erb b/app/views/filters/show.html.erb index 5cbe308..af81b34 100644 --- a/app/views/filters/show.html.erb +++ b/app/views/filters/show.html.erb @@ -4,8 +4,6 @@ <%= render(FlashNoticeComponent.new) { notice } if notice.present? %> -<%= link_to '← Back to filters list', filters_path %> - <%= turbo_frame_tag "filter" do %> <%= render @filter %> <% end %> @@ -30,3 +28,7 @@ <% end %>
+ +
+ <%= link_to '← Back to filters list', filters_path, class: "hover:text-blue-700" %> +
\ No newline at end of file diff --git a/app/views/operations/_form.html.erb b/app/views/operations/_form.html.erb index 433f14b..9a1d587 100644 --- a/app/views/operations/_form.html.erb +++ b/app/views/operations/_form.html.erb @@ -1,16 +1,8 @@ <%= form_with(model: [ operation.filter, operation ], data: { controller: "reset_form", operation: "turbo:submit-end->reset_form#reset" }) do |form| %> - <% if operation.errors.any? %> -
-

<%= pluralize(operation.errors.count, "error") %> prohibited this operation from being saved:

- -
    - <% operation.errors.each do |error| %> -
  • <%= error.full_message %>
  • - <% end %> -
-
- <% end %> + <%= render(FormErrorsComponent.new(errors: operation.errors)) do %> + <%= pluralize(operation.errors.count, "error") %> prohibited this operation from being saved: + <% end if operation.errors.any? %>
diff --git a/test/components/form_errors_component_test.rb b/test/components/form_errors_component_test.rb new file mode 100644 index 0000000..4772c85 --- /dev/null +++ b/test/components/form_errors_component_test.rb @@ -0,0 +1,10 @@ +require "test_helper" + +class FormErrorsComponentTest < ViewComponent::TestCase + def test_component_renders_something_useful + # assert_equal( + # %(Hello, components!), + # render_inline(FormErrorsComponent.new(message: "Hello, components!")).css("span").to_html + # ) + end +end