Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

Commit

Permalink
Merge pull request #36 from ministryofjustice/i18n-attribute
Browse files Browse the repository at this point in the history
Allow alternative i18n locales
  • Loading branch information
zheileman authored Jun 24, 2019
2 parents ec65558 + 95983a0 commit 53ca7e7
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 13 deletions.
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
gov_uk_date_fields (4.0.0)
gov_uk_date_fields (4.0.1)
rails (>= 5.0)

GEM
Expand Down Expand Up @@ -129,7 +129,7 @@ GEM
thread_safe (~> 0.1)
websocket-driver (0.6.5)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.3)
websocket-extensions (0.1.4)

PLATFORMS
ruby
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ The FormBuilder method gov_uk_date_field takes two parameters:
#### 5.2. Options passed to gov_uk_date_field (versions >= 4.x of the gem)

Currently, only one hash of options can be provided:
* **i18n_attribute**: optionally, this will be the locales lookup key in hint and legend. The "real" attribute will still take precedence in all other places or if the `i18n_attribute` is not found in the locales.

* **legend_options**: it can contain any of the following:
- `class`: will append any given class or classes to those generated by the gem.
Expand All @@ -177,7 +177,7 @@ All the strings will be picked from i18n locales, including legend, hint if any,

Example:

<%= f.gov_uk_date_field :dob,
<%= f.gov_uk_date_field :dob, i18n_attribute: :applicant_dob,
legend_options: { page_heading: false, visually_hidden: true } %>

### 6. You're done!
Expand Down
16 changes: 16 additions & 0 deletions lib/gov_uk_date_fields/form_fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,23 @@ def error_full_message_for attribute
message&.sub default_label(attribute), localized_label(attribute)
end

# If a form view is reused but the attribute doesn't change (for example in
# partials) an `i18n_attribute` can be used to lookup the legend or hint locales
# based on this, instead of the original attribute.
#
# We prioritise the `i18n_attribute` if provided, and if no locale is found,
# we try the 'real' attribute as a fallback and finally the default value.
#
def localized scope, attribute, default
found = if @options[:i18n_attribute]
key = "#{@object_name}.#{@options[:i18n_attribute]}"

I18n.translate(key, default: '', scope: scope).presence ||
I18n.translate("#{key}_html", default: '', scope: scope).html_safe.presence
end

return found if found

key = "#{@object_name}.#{attribute}"

# Passes blank String as default because nil is interpreted as no default
Expand Down
2 changes: 1 addition & 1 deletion lib/gov_uk_date_fields/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module GovUkDateFields
VERSION = "4.0.0"
VERSION = "4.0.1"
end
2 changes: 2 additions & 0 deletions test/dummy/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ en:
fieldset:
employee:
dob: Date of birth
custom_i18n_attribute: A custom legend
label:
dob:
day: Day
Expand All @@ -38,3 +39,4 @@ en:
hint:
employee:
dob: For example, 12 11 2007
custom_i18n_attribute: A custom hint
2 changes: 1 addition & 1 deletion test/dummy/test/models/director_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test_invalid_day_raises_no_error_when_validation_disabled

def test_creating_a_new_director_with_invalid_dates_is_valid_when_validation_disabled
e = Director.new(name: 'Stephen', dob_dd: '13', dob_mm: 'XXX', dob_yyyy: '1963')
assert_equal nil, e.dob
assert_nil e.dob
assert_true e.valid?
end

Expand Down
4 changes: 2 additions & 2 deletions test/dummy/test/models/employee_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def test_that_we_can_assign_new_dates_to_the_form_date_object

def test_that_nil_can_be_assigned_to_a_date_field
@employee.dob = nil
assert_equal nil, @employee.dob
assert_nil @employee.dob
assert_equal '', @employee.dob_dd
assert_equal '', @employee.dob_mm
assert_equal '', @employee.dob_yyyy
Expand Down Expand Up @@ -118,7 +118,7 @@ def test_createing_a_new_employee_with_valid_dates_is_valid

def test_creating_a_new_employee_with_invalid_dates_is_invalid
e = Employee.new(name: 'Stephen', dob_dd: '13', dob_mm: 'XXX', dob_yyyy: '1963')
assert_equal nil, e.dob
assert_nil e.dob
assert_false e.valid?
end

Expand Down
7 changes: 2 additions & 5 deletions test/dummy/test/models/form_date_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def setup

def test_new_raises_error_if_called_from_outside_class
err = assert_raise NoMethodError do
fd = FormDate.new(2,3,2015)
FormDate.new(2,3,2015)
end
assert_equal "private method `new' called for GovUkDateFields::FormDate:Class", err.message
end
Expand Down Expand Up @@ -81,11 +81,8 @@ def test_setting_all_date_parts_to_nil_sets_date_to_nil_and_is_valid
assert_equal '', fd.dd
assert_equal '', fd.mm
assert_equal '', fd.yyyy
assert_equal nil, fd.date
assert_nil fd.date
assert_true fd.valid?, "fd.valid?"
end



end
end
16 changes: 16 additions & 0 deletions test/dummy/test/models/form_fields_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,20 @@ def setup
)
assert_html_equal(date_fields.raw_output, fixture)
end

test 'date_fields with custom i18n_attribute' do
date_fields = GovUkDateFields::FormFields.new(
@form_builder, :employee, :dob, i18n_attribute: :custom_i18n_attribute
)

assert_match(
/<h1 class="govuk-fieldset__heading">A custom legend<\/h1>/,
date_fields.raw_output
)

assert_match(
/<span class="govuk-hint" id="employee_dob_hint">A custom hint<\/span>/,
date_fields.raw_output
)
end
end

0 comments on commit 53ca7e7

Please sign in to comment.