Skip to content

Commit beb1cd8

Browse files
authored
Merge pull request #442 from platanus/enum-translations
Enum translations
2 parents 00fb5fd + e2bd5f9 commit beb1cd8

File tree

14 files changed

+299
-25
lines changed

14 files changed

+299
-25
lines changed

.circleci/config.yml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
version: 2.1
22

3+
ruby-image: &ruby-image cimg/ruby:<<parameters.ruby-version>>
34
env-vars: &env-vars
45
RAILS_ENV: test
56
NODE_ENV: test
67
BUNDLE_PATH: vendor/bundle
78
SPROCKETS: false
89

910
orbs:
10-
ruby: circleci/[email protected]
11-
node: circleci/[email protected]
1211
browser-tools: circleci/[email protected]
1312

1413
executors:
@@ -19,7 +18,7 @@ executors:
1918
default: "2.7"
2019
type: string
2120
docker:
22-
- image: cimg/ruby:<<parameters.ruby-version>>-node
21+
- image: *ruby-image
2322
environment: *env-vars
2423

2524
commands:
@@ -39,6 +38,7 @@ commands:
3938
cd spec/dummy/
4039
xargs -a Aptfile sudo apt-get install
4140
sudo apt-get install libvips
41+
sudo apt-get install file
4242
- run:
4343
name: Install bundle dependencies
4444
command: |
@@ -52,6 +52,13 @@ commands:
5252
- run:
5353
name: Setup database
5454
command: (cd spec/dummy && bundle exec rake db:setup)
55+
- run:
56+
name: Install node and yarn
57+
command: |
58+
curl -sL https://deb.nodesource.com/setup_$(cat .node-version).x | sudo -E bash -
59+
sudo apt-get install -y nodejs
60+
curl -o- -sL https://yarnpkg.com/install.sh | bash
61+
sudo ln -s $HOME/.yarn/bin/yarn /usr/local/bin/yarn
5562
- run:
5663
name: Prepare Assets
5764
command: |

.node-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
16

.rspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
--format=doc
12
--require spec_helper
23
--color

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).
44

55
### Unreleased
66

7+
#### Changed
8+
9+
* Added translation support for Rails built-in enums in select filters and tag column/row [#442](https://github.com/platanus/activeadmin_addons/pull/442)
10+
711
### 1.9.0
812

913
##### Changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ Installing this gem will enable the following changes by default:
6464

6565
* The default date input will be `:datepicker` instead of `:date_select`
6666
* Filters and selects will offer integration with [enumerize](https://github.com/brainspec/enumerize)
67+
* Select filters will show translated values when used with Rails built-in `enums`
6768
* All select boxes will use select2
6869

6970
## Addons

docs/enum_integration.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,19 @@ ActiveAdmin.register Invoice do
8686
end
8787
```
8888

89+
## Translation
90+
91+
Tag row and column, as well as the interactive option of the latter, support automatic translation with `I18n` when using Rails built-in `enums`. For this, you just have to define the translations in the correct path in your YAML files:
92+
93+
```yaml
94+
# en.yml
95+
en:
96+
activerecord:
97+
attributes:
98+
invoice:
99+
statuses:
100+
active: Activo
101+
archived: Archivado
102+
```
103+
104+
This is the same structure used for tanslation of select inputs in filters and edit/create forms.

lib/activeadmin_addons/addons/tag_builder.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ def select_tag
2525

2626
context.div(interactive_tag_select_params) do
2727
context.select do
28-
possible_values.each do |val|
29-
context.option(value: val, selected: val == data) do
30-
context.text_node val.capitalize
28+
possible_values.each do |label, value|
29+
context.option(value: value, selected: value == data) do
30+
context.text_node label
3131
end
3232
end
3333
end
@@ -36,7 +36,11 @@ def select_tag
3636
end
3737

3838
def display_data
39-
@enum_attr == :enumerize ? data.text : data
39+
if @enum_attr == :enumerize
40+
data.text
41+
else
42+
EnumUtils.translate_enum_option(model.class, attribute.to_s, data)
43+
end
4044
end
4145

4246
def interactive_params(klass)
@@ -69,9 +73,9 @@ def interactive_tag_select_params
6973
def possible_values
7074
klass = model.class
7175
if @enum_attr == :enumerize
72-
klass.enumerized_attributes[attribute.to_s].values
76+
klass.enumerized_attributes[attribute.to_s].values.map { |value| [value.capitalize, value] }
7377
else
74-
klass.defined_enums[attribute.to_s].keys
78+
EnumUtils.options_for_select(klass, attribute.to_s)
7579
end
7680
end
7781

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module ActiveAdminAddons
2+
class EnumUtils
3+
def self.options_for_select(klass, enum_name, use_db_value: false)
4+
enum_options_hash = klass.defined_enums[enum_name]
5+
enum_options_hash.map do |enum_option_name, db_value|
6+
value = use_db_value ? db_value : enum_option_name
7+
[translate_enum_option(klass, enum_name, enum_option_name), value]
8+
end
9+
end
10+
11+
def self.translate_enum_option(klass, enum_name, enum_option_name)
12+
klass_key = klass.model_name.i18n_key
13+
key = "activerecord.attributes.#{klass_key}.#{enum_name.pluralize}.#{enum_option_name}"
14+
I18n.t(key, default: enum_option_name)
15+
end
16+
end
17+
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module ActiveAdminAddons
2+
module SelectFilterInputExtension
3+
def collection_from_enum?
4+
klass.respond_to?(:defined_enums) && klass.defined_enums.has_key?(method.to_s)
5+
end
6+
7+
def collection
8+
if !options[:collection] && collection_from_enum?
9+
EnumUtils.options_for_select(klass, method.to_s, use_db_value: true)
10+
else
11+
super
12+
end
13+
end
14+
end
15+
end
16+
17+
::ActiveAdmin::Inputs::Filters::SelectInput.send(
18+
:prepend, ActiveAdminAddons::SelectFilterInputExtension
19+
)

spec/dummy/config/locales/en.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ en:
2020
attributes:
2121
invoice:
2222
item_ids: Items
23+
statuses:
24+
active: Activo
25+
archived: Archivado
2326
activeadmin:
2427
addons:
2528
boolean:

0 commit comments

Comments
 (0)