From 8568fc1502be094fc666ab0027b0b6e333ae615e Mon Sep 17 00:00:00 2001 From: nakamura Date: Mon, 17 Mar 2025 17:17:36 +0000 Subject: [PATCH 1/2] Add requireness to HasOne form --- app/views/fields/has_one/_form.html.erb | 2 +- spec/features/form_spec.rb | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/app/views/fields/has_one/_form.html.erb b/app/views/fields/has_one/_form.html.erb index 1460d1f20a..2e8c985132 100644 --- a/app/views/fields/has_one/_form.html.erb +++ b/app/views/fields/has_one/_form.html.erb @@ -27,7 +27,7 @@ The form will be rendered as nested_from to parent relationship. <% end %> <% attributes.each do |attribute| %> -
+
<%= render_field attribute, f: has_one_f %>
<% hint_key = "administrate.field_hints.#{field.name}.#{attribute.name}" %> diff --git a/spec/features/form_spec.rb b/spec/features/form_spec.rb index 9d87a8375e..b6070765d8 100644 --- a/spec/features/form_spec.rb +++ b/spec/features/form_spec.rb @@ -36,7 +36,9 @@ Product.human_attribute_name(:name), Product.human_attribute_name(:description), Product.human_attribute_name(:price), - Product.human_attribute_name(:image_url) + Product.human_attribute_name(:image_url), + ProductMetaTag.human_attribute_name(:meta_title), + ProductMetaTag.human_attribute_name(:meta_description) ] required_field_labels = find_all(".field-unit--required").map(&:text) From 0d833b185dd54ae481ebfdc6fbdfe46e9b53905f Mon Sep 17 00:00:00 2001 From: nakamura Date: Mon, 24 Mar 2025 09:49:04 +0000 Subject: [PATCH 2/2] Add tests for nested resource field requireness in application helper --- .../administrate/application_helper_spec.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/spec/helpers/administrate/application_helper_spec.rb b/spec/helpers/administrate/application_helper_spec.rb index 4434251e70..e612b72643 100644 --- a/spec/helpers/administrate/application_helper_spec.rb +++ b/spec/helpers/administrate/application_helper_spec.rb @@ -112,6 +112,23 @@ end expect(requireness(release_year)).to eq("optional") end + + context "when the resource is nested" do + it "returns 'required' if nested field is required" do + field = page.attributes.values.flatten.detect { |i| i.attribute == :product_meta_tag } + nested_field = field.nested_form.attributes.values.flatten.detect { |i| i.attribute == :meta_title } + expect(requireness(nested_field)).to eq("required") + end + + it "returns 'optional' if nested field is not required" do + field = page.attributes.values.flatten.detect { |i| i.attribute == :product_meta_tag } + nested_field = field.nested_form.attributes.values.flatten.detect { |i| i.attribute == :meta_title } + + allow_any_instance_of(ActiveRecord::Validations::PresenceValidator).to receive(:options).and_return({on: :any}) + + expect(requireness(nested_field)).to eq("optional") + end + end end describe "#sort_order" do