Skip to content
Open
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
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:
human_attribute_values (1.2.1)
human_attribute_values (1.2.2)
rails (>= 4.2.10)

GEM
Expand Down Expand Up @@ -135,7 +135,7 @@ GEM
websocket-driver (0.7.3)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
zeitwerk (2.4.0)
zeitwerk (2.4.1)

PLATFORMS
ruby
Expand Down
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,26 @@ en:
'42': 'the answer to life, the universe and everything'
```

### Arrays
If you have a serialized array or array attribute, each element in the array will attempt to translate.

```yml
en:
activerecord:
values:
magic_number:
array_attr:
currencies:
USD: Dollar
GBP: Pound
```

```ruby
item = Item.new(currencies: ['USD', 'GBP'])
item.human_attribute_values(:currencies)
=> ['Dollar', 'Pound']
```

### Strings and Symbols
Starting with version 1.2.0, dots in strings and symbols (actually in all values) are also replaced by an underscore for the lookup (before this was only done for numbers).

Expand Down
9 changes: 8 additions & 1 deletion lib/human_attribute_values/human_attribute_value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ module HumanAttributeValues

def human_attribute_value(attribute, options = {})
value = public_send(attribute)
self.class.human_attribute_value(attribute, value, options)

if value.is_a? Array
value.map do |v|
self.class.human_attribute_value(attribute, v, options)
end
else
self.class.human_attribute_value(attribute, value, options)
end
end

module ClassMethods
Expand Down
10 changes: 10 additions & 0 deletions test/active_model_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,14 @@ class ActiveModelTest < ActiveSupport::TestCase
assert_equal('1.5', instance.human_attribute_value(:float_attr), 'should return the stringified float if there is no mapping')
assert_equal('1001', instance.human_attribute_value(:integer_attr), 'should return the stringified integer if there is no mapping')
end

test 'resolution of array values' do
instance = ActiveModelModel.new

instance.array_attr = ['USD', 'GBP']
assert_equal(['Dollar', 'Pound'], instance.human_attribute_value(:array_attr), 'should translate each element')

instance.array_attr << "JPY"
assert_equal(['Dollar', 'Pound', 'JPY'], instance.human_attribute_value(:array_attr), 'should return the stringified array values if no translation is defined')
end
end
8 changes: 8 additions & 0 deletions test/active_record_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,12 @@ class ActiveRecordTest < ActiveSupport::TestCase
enum_model.status = 2
assert_equal('dead', enum_model.human_attribute_value(:status), 'should return the stringified enum value if no translation is defined 2')
end

test 'resolution for arrays' do
array_model = ArrayModel.new(currencies: ['USD', 'GBP'])
assert_equal(['Dollar', 'Pound'], array_model.human_attribute_value(:currencies), 'should translate each element')

array_model.currencies << "JPY"
assert_equal(['Dollar', 'Pound', 'JPY'], array_model.human_attribute_value(:currencies), 'should return the stringified array values if no translation is defined')
end
end
2 changes: 1 addition & 1 deletion test/dummy/app/models/active_model_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
class ActiveModelModel
include ActiveModel::Model

attr_accessor :boolean_attr, :decimal_attr, :integer_attr, :float_attr, :string_attr
attr_accessor :boolean_attr, :decimal_attr, :integer_attr, :float_attr, :string_attr, :array_attr
end
5 changes: 5 additions & 0 deletions test/dummy/app/models/array_model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class ArrayModel < ActiveRecord::Base
serialize :currencies, Array
end
7 changes: 7 additions & 0 deletions test/dummy/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ en:
one: one goose
other: some geese
'application/x-my_mime1': Custom Mime Type 1
array_attr:
USD: Dollar
GBP: Pound
active_model_parent:
inherited_attr:
one: 'active model parent value 1'
Expand Down Expand Up @@ -75,3 +78,7 @@ en:
enum_model:
status:
dead_and_alive: 'The box has not been opened yet'
array_model:
currencies:
USD: Dollar
GBP: Pound
8 changes: 8 additions & 0 deletions test/dummy/db/migrate/20201109211149_create_array_models.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class CreateArrayModels < ActiveRecord::Migration[6.0]
def change
create_table :array_models do |t|
t.string :currencies, array: true
t.timestamps
end
end
end
18 changes: 12 additions & 6 deletions test/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
# This file is the source Rails uses to define your schema when running `rails
# db:schema:load`. When creating a new database, `rails db:schema:load` tends to
# be faster and is potentially less error prone than running all of your
# migrations from scratch. Old migrations may fail to apply correctly if those
# migrations use external dependencies or application code.
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2015_01_31_170613) do
ActiveRecord::Schema.define(version: 2020_11_09_211149) do

create_table "array_models", force: :cascade do |t|
t.string "currencies"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end

create_table "boolean_models", force: :cascade do |t|
t.boolean "boolean_field"
Expand Down
7 changes: 7 additions & 0 deletions test/dummy/test/fixtures/array_models.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
things:

two:
things:
7 changes: 7 additions & 0 deletions test/dummy/test/models/array_model_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class ArrayModelTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end