From 79f3028686614f305c1162bc45a1d3232969667b Mon Sep 17 00:00:00 2001 From: Jonathan Johnson Date: Mon, 9 Nov 2020 15:25:36 -0600 Subject: [PATCH 1/3] set up failing spec --- Gemfile.lock | 4 ++-- test/active_record_test.rb | 8 ++++++++ test/dummy/app/models/array_model.rb | 5 +++++ .../20201109211149_create_array_models.rb | 8 ++++++++ test/dummy/db/schema.rb | 18 ++++++++++++------ test/dummy/test/fixtures/array_models.yml | 7 +++++++ test/dummy/test/models/array_model_test.rb | 7 +++++++ 7 files changed, 49 insertions(+), 8 deletions(-) create mode 100644 test/dummy/app/models/array_model.rb create mode 100644 test/dummy/db/migrate/20201109211149_create_array_models.rb create mode 100644 test/dummy/test/fixtures/array_models.yml create mode 100644 test/dummy/test/models/array_model_test.rb diff --git a/Gemfile.lock b/Gemfile.lock index ac2c4ed..307644f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - human_attribute_values (1.2.1) + human_attribute_values (1.2.2) rails (>= 4.2.10) GEM @@ -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 diff --git a/test/active_record_test.rb b/test/active_record_test.rb index a7ac501..a211026 100644 --- a/test/active_record_test.rb +++ b/test/active_record_test.rb @@ -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 diff --git a/test/dummy/app/models/array_model.rb b/test/dummy/app/models/array_model.rb new file mode 100644 index 0000000..33a2462 --- /dev/null +++ b/test/dummy/app/models/array_model.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +class ArrayModel < ActiveRecord::Base + serialize :currencies, Array +end diff --git a/test/dummy/db/migrate/20201109211149_create_array_models.rb b/test/dummy/db/migrate/20201109211149_create_array_models.rb new file mode 100644 index 0000000..79439d2 --- /dev/null +++ b/test/dummy/db/migrate/20201109211149_create_array_models.rb @@ -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 diff --git a/test/dummy/db/schema.rb b/test/dummy/db/schema.rb index 69298d6..f1abe5b 100644 --- a/test/dummy/db/schema.rb +++ b/test/dummy/db/schema.rb @@ -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" diff --git a/test/dummy/test/fixtures/array_models.yml b/test/dummy/test/fixtures/array_models.yml new file mode 100644 index 0000000..79dbfd8 --- /dev/null +++ b/test/dummy/test/fixtures/array_models.yml @@ -0,0 +1,7 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + things: + +two: + things: diff --git a/test/dummy/test/models/array_model_test.rb b/test/dummy/test/models/array_model_test.rb new file mode 100644 index 0000000..bfa263f --- /dev/null +++ b/test/dummy/test/models/array_model_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class ArrayModelTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end From bdd6aec76f8d1a543d7d69a4da2ff5508ae1d57a Mon Sep 17 00:00:00 2001 From: Jonathan Johnson Date: Mon, 9 Nov 2020 15:34:07 -0600 Subject: [PATCH 2/3] support arrays in translation --- lib/human_attribute_values/human_attribute_value.rb | 9 ++++++++- test/active_model_test.rb | 10 ++++++++++ test/dummy/app/models/active_model_model.rb | 2 +- test/dummy/config/locales/en.yml | 7 +++++++ 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/lib/human_attribute_values/human_attribute_value.rb b/lib/human_attribute_values/human_attribute_value.rb index 4bb3b59..6e2f111 100644 --- a/lib/human_attribute_values/human_attribute_value.rb +++ b/lib/human_attribute_values/human_attribute_value.rb @@ -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 diff --git a/test/active_model_test.rb b/test/active_model_test.rb index a354ccc..65039d8 100644 --- a/test/active_model_test.rb +++ b/test/active_model_test.rb @@ -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 diff --git a/test/dummy/app/models/active_model_model.rb b/test/dummy/app/models/active_model_model.rb index 583311d..28175be 100644 --- a/test/dummy/app/models/active_model_model.rb +++ b/test/dummy/app/models/active_model_model.rb @@ -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 diff --git a/test/dummy/config/locales/en.yml b/test/dummy/config/locales/en.yml index b3d6fbb..d950f56 100644 --- a/test/dummy/config/locales/en.yml +++ b/test/dummy/config/locales/en.yml @@ -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' @@ -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 From c902328045e188017cb51f2fffdd607b2cf83a0b Mon Sep 17 00:00:00 2001 From: Jonathan Johnson Date: Mon, 9 Nov 2020 17:43:33 -0600 Subject: [PATCH 3/3] add docs for arrays --- README.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/README.md b/README.md index ccf61b3..ac4f891 100644 --- a/README.md +++ b/README.md @@ -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).