Skip to content

Commit

Permalink
use 'defaults' scope for the attributes that used across several models
Browse files Browse the repository at this point in the history
  • Loading branch information
nashby committed Mar 31, 2014
1 parent 6431cdd commit 58e39df
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
25 changes: 25 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
## master

### enhancements
* Use 'defaults' scope in the localization file for the attributes that used across several models. This will help to avoid conflicting keys with model names and attribute names. Example:

```yml
en:
enumerize:
defaults:
sex:
male: Male
female: Female
```
You still can use the old solution without "default" scope:
```yml
en:
enumerize:
sex:
male: Male
female: Female
```
(by [@nashby](https://github.com/nashby))
## 0.8.0 (March 4, 2014) ##
### enhancements
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,15 @@ en:
female: "Female"
```

or if you use `sex` attribute across several models you can use this:
or if you use `sex` attribute across several models you can use `defaults` scope:

```ruby
en:
enumerize:
sex:
male: "Male"
female: "Female"
defaults:
sex:
male: "Male"
female: "Female"
```

You can also pass `i18n_scope` option to specify scope (or array of scopes) storring the translations. Note that `i18n_scope` option does not accept scope as array:
Expand Down
1 change: 1 addition & 0 deletions lib/enumerize/value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def #{value}?
def i18n_keys
@i18n_keys ||= begin
i18n_keys = i18n_scopes
i18n_keys << [:"enumerize.defaults.#{@attr.name}.#{self}"]
i18n_keys << [:"enumerize.#{@attr.name}.#{self}"]
i18n_keys << self.humanize # humanize value if there are no translations
i18n_keys.flatten
Expand Down
6 changes: 6 additions & 0 deletions test/value_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
end
end

it 'uses default translation from the "default" section if its present' do
store_translations(:en, :enumerize => {:defaults => {:attribute_name => {:test_value => "Common translation"}}}) do
value.text.must_be :==, "Common translation"
end
end

it 'uses model specific translation' do
attr.i18n_scopes = ["enumerize.model_name.attribute_name"]

Expand Down

0 comments on commit 58e39df

Please sign in to comment.