Commit 0e0194a
committed
Handle missing attributes for AM::Translation#human_attribute_name
```ruby
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
#gem "rails", "~>7.0.0"
gem "rails", "~>7.2.0"
gem "sqlite3"
# Rails 7.0 deps:
#gem "sqlite3", "~>1.4"
#gem "timeout", "~>0.1"
#gem "securerandom"
#gem "concurrent-ruby", "1.3.4"
end
require "active_record"
require "minitest/autorun"
require "logger"
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = ActiveSupport::Logger.new(STDOUT)
I18n.backend.store_translations(
:ja,
{
activerecord: {
attributes: {
"post/status": {
draft: 'draft in Japanese',
published: 'published in Japanese',
},
"post/author": {
name: "Name in Japanese"
}
}
}
}
)
ActiveRecord::Schema.define do
create_table :posts, force: true do |t|
t.integer :status
end
create_table :authors, force: true do |t|
t.string :name
end
end
class Post < ActiveRecord::Base
enum :status, draft: 0, published: 1
belongs_to :author
end
class BugTest < ActiveSupport::TestCase
def test_association_stuff
value = nil
assert_nothing_raised { Post.human_attribute_name("status.#{value}", {locale: :ja}) }
attr = Post.human_attribute_name("status.#{value}", {locale: :ja})
assert_equal "Status", attr
assert_nothing_raised { Post.human_attribute_name("author.name", {locale: :ja}) }
attr = Post.human_attribute_name("author.name", {locale: :ja})
assert_equal "Name in Japanese", attr
end
end
```
Raises `I18n::InvalidPluralizationData`:
```
1) Error:
BugTest#test_association_stuff:
I18n::InvalidPluralizationData: translation data {:draft=>"draft in Japanese", :published=>"published in Japanese"} can not be used with :count => 1. key 'one' is missing.
/home/zzak/.rbenv/versions/3.3.6/lib/ruby/gems/3.3.0/gems/i18n-1.14.6/lib/i18n/backend/base.rb:187:in `pluralize'
/home/zzak/.rbenv/versions/3.3.6/lib/ruby/gems/3.3.0/gems/i18n-1.14.6/lib/i18n/backend/base.rb:50:in `translate'
/home/zzak/.rbenv/versions/3.3.6/lib/ruby/gems/3.3.0/gems/i18n-1.14.6/lib/i18n.rb:394:in `block in translate_key'
/home/zzak/.rbenv/versions/3.3.6/lib/ruby/gems/3.3.0/gems/i18n-1.14.6/lib/i18n.rb:393:in `catch'
/home/zzak/.rbenv/versions/3.3.6/lib/ruby/gems/3.3.0/gems/i18n-1.14.6/lib/i18n.rb:393:in `translate_key'
/home/zzak/.rbenv/versions/3.3.6/lib/ruby/gems/3.3.0/gems/i18n-1.14.6/lib/i18n.rb:223:in `translate'
bug.rb:87:in `translate'
/home/zzak/.rbenv/versions/3.3.6/lib/ruby/gems/3.3.0/gems/activemodel-7.2.2.1/lib/active_model/translation.rb:67:in `human_attribute_name'
bug.rb:98:in `block in test_association_stuff'
/home/zzak/.rbenv/versions/3.3.6/lib/ruby/gems/3.3.0/gems/activesupport-7.2.2.1/lib/active_support/testing/assertions.rb:49:in `assert_nothing_raised'
bug.rb:98:in `test_association_stuff'
1 runs, 0 assertions, 0 failures, 1 errors, 0 skips
```1 parent 94729d0 commit 0e0194a
File tree
2 files changed
+24
-3
lines changed- activemodel
- lib/active_model
- test/cases
2 files changed
+24
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
55 | 63 | | |
56 | | - | |
| 64 | + | |
57 | 65 | | |
58 | | - | |
| 66 | + | |
| 67 | + | |
59 | 68 | | |
60 | 69 | | |
61 | 70 | | |
| |||
69 | 78 | | |
70 | 79 | | |
71 | 80 | | |
72 | | - | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
73 | 84 | | |
74 | 85 | | |
75 | 86 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
104 | 104 | | |
105 | 105 | | |
106 | 106 | | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
107 | 117 | | |
108 | 118 | | |
109 | 119 | | |
| |||
0 commit comments