Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(form): Display hint for has_one nested fields #2735

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix(form): Display hint for has_one nested fields
Previously, hints were not displayed for has_one nested fields in forms. This fix ensures that hints are properly shown for nested fields as well.
mizoR committed Dec 31, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 6fa2a990c53be91d6673ebab61c7fcc30a1d29cb
5 changes: 5 additions & 0 deletions app/assets/builds/administrate/application.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/assets/builds/administrate/application.css.map

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -46,6 +46,12 @@
.field-unit__label {
width: 10rem;
}

.field-unit__hint {
font-size: 90%;
margin-left: calc(13rem);
width: 100%;
}
}

.field-unit--required {
7 changes: 7 additions & 0 deletions app/views/fields/has_one/_form.html.erb
Original file line number Diff line number Diff line change
@@ -29,6 +29,13 @@ The form will be rendered as nested_from to parent relationship.
<% attributes.each do |attribute| %>
<div class="field-unit field-unit--<%= attribute.html_class %>">
<%= render_field attribute, f: has_one_f %>

<% hint_key = "administrate.field_hints.#{field.nested_form.resource_name}.#{attribute.name}" %>
<% if I18n.exists?(hint_key) -%>
<div class="field-unit__hint">
<%= I18n.t(hint_key) %>
</div>
<% end -%>
</div>
<% end %>
</fieldset>
59 changes: 53 additions & 6 deletions spec/administrate/views/fields/has_one/_form_spec.rb
Original file line number Diff line number Diff line change
@@ -2,21 +2,60 @@
require "administrate/field/has_one"

describe "fields/has_one/_form", type: :view do
it "displays the resource name" do
has_one = instance_double(
let!(:has_one) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instance_double(
"Administrate::Field::HasOne",
attribute: "Meta",
data: nil,
nested_form: nested_form,
name: "product_tag"
)
}

it "displays the resource name" do
allow(view).to receive(:render_field).and_return(<<~HTML.html_safe)
<div class="field-unit__field">
<input type="text" value="" name="product[product_meta_tag_attributes][meta_title]">
</div>
HTML

render(
partial: "fields/has_one/form",
locals: {field: has_one, f: form_builder}
)

expect(rendered.strip).to include("Product Tag")
expect(rendered.strip)
.to include("Product Tag")
.and include('<input type="text" value="" name="product[product_meta_tag_attributes][meta_title]">')
end

it "displays the nested field hint" do
allow(view).to receive(:render_field).and_return(<<~HTML.html_safe)
<div class="field-unit__field">
<input type="text" value="" name="product[product_meta_tag_attributes][meta_title]">
</div>
HTML

translations = {
administrate: {
field_hints: {
product_meta_tag: {
simple_string_field: "Field hint",

Check failure on line 43 in spec/administrate/views/fields/has_one/_form_spec.rb

GitHub Actions / standardrb

Style/TrailingCommaInHashLiteral: Avoid comma after the last item of a hash.
},

Check failure on line 44 in spec/administrate/views/fields/has_one/_form_spec.rb

GitHub Actions / standardrb

Style/TrailingCommaInHashLiteral: Avoid comma after the last item of a hash.
},

Check failure on line 45 in spec/administrate/views/fields/has_one/_form_spec.rb

GitHub Actions / standardrb

Style/TrailingCommaInHashLiteral: Avoid comma after the last item of a hash.
},

Check failure on line 46 in spec/administrate/views/fields/has_one/_form_spec.rb

GitHub Actions / standardrb

Style/TrailingCommaInHashLiteral: Avoid comma after the last item of a hash.
}

with_translations(:en, translations) do
render(
partial: "fields/has_one/form",
locals: {field: has_one, f: form_builder}
)
end

expect(rendered.strip)
.to include('<input type="text" value="" name="product[product_meta_tag_attributes][meta_title]">')
.and include(%r[<div class="field-unit__hint">\s+Field hint\s+</div>])

Check failure on line 58 in spec/administrate/views/fields/has_one/_form_spec.rb

GitHub Actions / standardrb

Style/PercentLiteralDelimiters: `%r`-literals should be delimited by `{` and `}`.
end

def form_builder
@@ -30,12 +69,20 @@

def nested_form
instance_double(
"Administrate::Page::Show",
"Administrate::Page::Form",
resource: double(
class: ProductMetaTag
),
attributes: [],
resource_name: "Product Tag"
attributes: {"simple_string_field" => [
instance_double(
"Administrate::Field::String",
name: "simple_string_field",
truncate: "string value",
html_class: "string",
to_partial_path: "fields/string/index"
)
]},
resource_name: "product_meta_tag",

Check failure on line 85 in spec/administrate/views/fields/has_one/_form_spec.rb

GitHub Actions / standardrb

Style/TrailingCommaInArguments: Avoid comma after the last parameter of a method call.
)
end
end