Skip to content

Commit

Permalink
translate labels for belongs_to/_form when available
Browse files Browse the repository at this point in the history
  • Loading branch information
lubieniebieski committed May 17, 2024
1 parent 9efb851 commit 03e37cb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/views/fields/belongs_to/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ that displays all possible records to associate with.
%>

<div class="field-unit__label">
<%= f.label field.permitted_attribute %>
<%= f.label field.attribute, for: "#{f.object_name}_#{field.permitted_attribute}" %>
</div>
<div class="field-unit__field">
<%= f.select(field.permitted_attribute,
Expand Down
40 changes: 40 additions & 0 deletions spec/features/orders_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,26 @@
expected = order.customer.id.to_s
expect(find_field("Customer").value).to eq expected
end

it "displays translated label when translation for the attribute is available" do
order = create(:order)
custom_attribute_name = "Client"
translations = {
activerecord: {
attributes: {
order: {
customer: custom_attribute_name
}
}
}
}

with_translations(:en, translations) do
visit edit_admin_order_path(order)

expect(page).to have_css("label", text: custom_attribute_name)
end
end
end

describe "has_many relationships" do
Expand Down Expand Up @@ -92,6 +112,26 @@
end
end

it "displays translated label when translation for the attribute is available" do
order = create(:order)
custom_attribute_name = "Lines"
translations = {
activerecord: {
attributes: {
order: {
line_items: custom_attribute_name
}
}
}
}

with_translations(:en, translations) do
visit edit_admin_order_path(order)

expect(page).to have_css("label", text: custom_attribute_name)
end
end

def find_option(associated_model, field_locator)
field = find_field(field_locator)
field.find("option", text: displayed(associated_model))
Expand Down

0 comments on commit 03e37cb

Please sign in to comment.