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 i18n inconsistencies in forms for associations #1192

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion app/views/fields/has_many/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ and is augmented with [Selectize].
%>

<div class="field-unit__label">
<%= f.label field.attribute_key, field.attribute %>
<%= f.label field.attribute, for: "#{f.object_name}_#{field.attribute_key}" %>
Copy link
Member Author

Choose a reason for hiding this comment

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

If there is an existing Rails helper I can use to generate this label key, I think that would be nice, but I couldn't find one.

</div>
<div class="field-unit__field">
<%= f.select(field.attribute_key, nil, {}, multiple: true) do %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/fields/has_one/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ The form will be rendered as nested_from to parent relationship.

<%= f.fields_for field.attribute, field.data || field.nested_form.resource.class.new do |has_one_f| %>
<fieldset class="field-unit--nested">
<legend><%= field.nested_form.resource_name.titleize %></legend>
<legend><%= t "helpers.label.#{f.object_name}.#{field.nested_form.resource_name}", default: field.nested_form.resource_name.titleize %></legend>
Copy link
Member Author

Choose a reason for hiding this comment

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

If there is an existing Rails helper or class method I could use to generate this i18n key, that would be nice, but I looked and couldn't find one.

<% field.nested_form.attributes.each do |attribute| -%>
<div class="field-unit field-unit--<%= attribute.html_class %>">
<%= render_field attribute, f: has_one_f %>
Expand Down
4 changes: 2 additions & 2 deletions spec/administrate/views/fields/has_many/_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
locals: { f: fake_form_builder, field: has_many },
)

expect(rendered).to include("Associated Objects")
expect(rendered).to include("Associated objects")
end
end

def fake_form_builder
double("Form Builder").as_null_object.tap do |form_builder|
allow(form_builder).to receive(:label) do |*args|
args.second.to_s.titleize
args.first.to_s.humanize
end
end
end
Expand Down
1 change: 1 addition & 0 deletions spec/administrate/views/fields/has_one/_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def form_builder
allow(builder).to receive(:fields_for) do |&block|
block.call(double("Fields For Form Builder"))
end
allow(builder).to receive(:object_name).and_return(:product)
builder
end

Expand Down
31 changes: 26 additions & 5 deletions spec/features/orders_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
line_items = create_list(:line_item, 3)

visit edit_admin_order_path(order)
find_option(line_items.first, "line_item_ids").select_option
find_option(line_items.last, "line_item_ids").select_option
find_option(line_items.first, "Line items").select_option
find_option(line_items.last, "Line items").select_option
click_on "Update Order"

order.reload
Expand All @@ -51,7 +51,7 @@
line_item = create(:line_item, order: order)

visit edit_admin_order_path(order)
find_option(line_item, "line_item_ids").unselect_option
find_option(line_item, "Line items").unselect_option
click_on "Update Order"

order.reload
Expand All @@ -71,8 +71,29 @@
expect(find("#order_line_item_ids").value).to match_array(expected)
end

def find_option(associated_model, field_id)
field = find("#order_" + field_id)
it "displays translated labels" do
custom_label = "Lines"
order = create(:order)

translations = {
helpers: {
label: {
order: {
line_items: custom_label,
},
},
},
}

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

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

def find_option(associated_model, field_locator)
field = find_field(field_locator)
field.find("option", text: displayed(associated_model))
end
end
Expand Down
25 changes: 25 additions & 0 deletions spec/features/products_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
fill_in "Meta title", with: "Example meta title"
fill_in "Meta description", with: "Example meta description"

expect(page).to have_css("legend", text: "Product Meta Tag")

click_on "Create Product"

expect(page).to have_link(ProductMetaTag.last.id)
Expand All @@ -31,4 +33,27 @@
t("administrate.controller.update.success", resource: "Product")
)
end

describe "has_one relationships" do
it "displays translated labels" do
custom_label = "Meta Tags"
product = create(:product)

translations = {
helpers: {
label: {
product: {
product_meta_tag: custom_label,
},
},
},
}

with_translations(:en, translations) do
visit edit_admin_product_path(product)

expect(page).to have_css("legend", text: custom_label)
end
end
end
end