From 2e3cf72a523f48d1dd12874398460952a425e754 Mon Sep 17 00:00:00 2001 From: Grayson Wright Date: Sun, 28 Feb 2016 20:25:01 -0800 Subject: [PATCH] Add a test to verify that translated labels work Problem: There was uncertainty about how to create custom labels for fields. In https://github.com/thoughtbot/administrate/issues/451, it was suggested that we could use I18n translations to solve the problem. If we accept that as the standard way to set custom labels, we should have a test for it. Solution: - Add a test to verify that labels can be customized through I18n. - Add documentation about how to customize labels To Do: - Update view code so the same translation definition applies on the show and collection pages, in addition to the form page. --- docs/customizing_dashboards.md | 11 +++++++++++ spec/features/form_spec.rb | 20 ++++++++++++++++++++ spec/support/features/page_elements.rb | 4 ++++ 3 files changed, 35 insertions(+) diff --git a/docs/customizing_dashboards.md b/docs/customizing_dashboards.md index b157027b81..198616c8ea 100644 --- a/docs/customizing_dashboards.md +++ b/docs/customizing_dashboards.md @@ -76,4 +76,15 @@ if the value is stored by the number of cents: ) ``` +To change the user-facing label for an attribute, +define a custom I18n translation: + +```yaml +en: + helpers: + label: + customer: + name: Full Name +``` + [define your own]: /adding_custom_field_types diff --git a/spec/features/form_spec.rb b/spec/features/form_spec.rb index 2383024702..414d322c0e 100644 --- a/spec/features/form_spec.rb +++ b/spec/features/form_spec.rb @@ -8,4 +8,24 @@ expect(page).to have_css("form.form") end + + it "displays translated labels" do + custom_label = "Newsletter Subscriber" + + translations = { + helpers: { + label: { + customer: { + email_subscriber: custom_label, + }, + }, + }, + } + + with_translations(:en, translations) do + visit new_admin_customer_path + + expect(page).to have_label(custom_label) + end + end end diff --git a/spec/support/features/page_elements.rb b/spec/support/features/page_elements.rb index 15eae2e001..7e5e435a7e 100644 --- a/spec/support/features/page_elements.rb +++ b/spec/support/features/page_elements.rb @@ -2,4 +2,8 @@ module Features def have_header(title) have_css("h1", text: title) end + + def have_label(title) + have_css("label", text: title) + end end