i18n-active_record 1.0.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 402118f9fb5b5b88dec6745249ded8e131b509c3bb4b8b35e5857182c85e6ca8
4
- data.tar.gz: f120411a8ffc00c065ef50eda91d32515e23f51d6e4125184513c5a34496edb5
3
+ metadata.gz: 696d09fb79116c47ec3924b359f31ba4c632d68a075d6da1af591dcb0412548f
4
+ data.tar.gz: eff1a17efd57a2f09546e80aa0d1269b07751f8615b65377c891d89186407d45
5
5
  SHA512:
6
- metadata.gz: f36d522e0792d4c5460fe9d7b3af69d2fa27817f2f2ecd58eb18ecdcea13012e542bfe4022d2e27c18f454107e63c1406450c3d709830caeb1c33f1a0b2997b3
7
- data.tar.gz: 7ee1bec18a25efbad1fbec55e89b061b050cfdf2fc51c2453f6dee559ad71bf60ab8c5d8397900c4d87be82dff59f830e494c427c42214d60469c047d06c08fc
6
+ metadata.gz: 8dc72a6d40f4ce0da1d98f5ade7bc26f660766aa72814771f359b9c6c8e943a4c51acfce8f612794cd3ede72c3ad9808e9442524c951fded1c2ec2457be0201f
7
+ data.tar.gz: cc0fcb1c5121cefa325f52c4edbae8308a095d896cd6a8e3917d5c0144c1460f4f7795a4de6be9f25f399e678b4ce9fa2e95472e0cd651ee08aeead47e30ab03
data/README.md CHANGED
@@ -115,7 +115,7 @@ The `interpolations` field is otherwise unused since the "value" in `Translation
115
115
 
116
116
  ## Examples
117
117
 
118
- * http://collectiveidea.com/blog/archives/2016/05/31/beyond-yml-files-dynamic-translations/
118
+ * http://collectiveidea.com/blog/archives/2016/05/31/beyond-yml-files-dynamic-translations/ ([use `before_action`](https://github.com/svenfuchs/i18n-active_record/issues/138) for Rails 5.1+)
119
119
 
120
120
  ## Contributing
121
121
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module I18n
4
4
  module ActiveRecord
5
- VERSION = '1.0.1'
5
+ VERSION = '1.2.0'
6
6
  end
7
7
  end
@@ -53,8 +53,14 @@ module I18n
53
53
 
54
54
  self.table_name = 'translations'
55
55
 
56
- serialize :value
57
- serialize :interpolations, Array
56
+ if ::ActiveRecord.version >= Gem::Version.new('7.1.0.alpha')
57
+ serialize :value, coder: YAML
58
+ serialize :interpolations, coder: YAML, type: Array
59
+ else
60
+ serialize :value
61
+ serialize :interpolations, Array
62
+ end
63
+
58
64
  after_commit :invalidate_translations_cache
59
65
 
60
66
  class << self
@@ -79,7 +85,7 @@ module I18n
79
85
  Translation.select('DISTINCT locale').to_a.map { |t| t.locale.to_sym }
80
86
  end
81
87
 
82
- def to_hash
88
+ def to_h
83
89
  Translation.all.each.with_object({}) do |t, memo|
84
90
  locale_hash = (memo[t.locale.to_sym] ||= {})
85
91
  keys = t.key.split('.')
@@ -11,9 +11,6 @@ module I18n
11
11
  autoload :Translation, 'i18n/backend/active_record/translation'
12
12
  autoload :Configuration, 'i18n/backend/active_record/configuration'
13
13
 
14
- include Base
15
- include Flatten
16
-
17
14
  class << self
18
15
  def configure
19
16
  yield(config) if block_given?
@@ -24,109 +21,114 @@ module I18n
24
21
  end
25
22
  end
26
23
 
27
- def initialize(*args)
28
- super
29
-
24
+ def initialize
30
25
  reload!
31
26
  end
32
27
 
33
- def available_locales
34
- Translation.available_locales
35
- rescue ::ActiveRecord::StatementInvalid
36
- []
37
- end
28
+ module Implementation
29
+ include Base
30
+ include Flatten
38
31
 
39
- def store_translations(locale, data, options = {})
40
- escape = options.fetch(:escape, true)
32
+ def available_locales
33
+ Translation.available_locales
34
+ rescue ::ActiveRecord::StatementInvalid
35
+ []
36
+ end
41
37
 
42
- flatten_translations(locale, data, escape, false).each do |key, value|
43
- translation = Translation.locale(locale).lookup(expand_keys(key))
38
+ def store_translations(locale, data, options = {})
39
+ escape = options.fetch(:escape, true)
44
40
 
45
- if self.class.config.cleanup_with_destroy
46
- translation.destroy_all
47
- else
48
- translation.delete_all
41
+ flatten_translations(locale, data, escape, false).each do |key, value|
42
+ translation = Translation.locale(locale).lookup(expand_keys(key))
43
+
44
+ if self.class.config.cleanup_with_destroy
45
+ translation.destroy_all
46
+ else
47
+ translation.delete_all
48
+ end
49
+
50
+ Translation.create(locale: locale.to_s, key: key.to_s, value: value)
49
51
  end
50
52
 
51
- Translation.create(locale: locale.to_s, key: key.to_s, value: value)
53
+ reload! if self.class.config.cache_translations
52
54
  end
53
55
 
54
- reload! if self.class.config.cache_translations
55
- end
56
-
57
- def reload!
58
- @translations = nil
56
+ def reload!
57
+ @translations = nil
59
58
 
60
- self
61
- end
59
+ self
60
+ end
62
61
 
63
- def initialized?
64
- !@translations.nil?
65
- end
62
+ def initialized?
63
+ !@translations.nil?
64
+ end
66
65
 
67
- def init_translations
68
- @translations = Translation.to_hash
69
- end
66
+ def init_translations
67
+ @translations = Translation.to_h
68
+ end
70
69
 
71
- def translations(do_init: false)
72
- init_translations if do_init || !initialized?
73
- @translations ||= {}
74
- end
70
+ def translations(do_init: false)
71
+ init_translations if do_init || !initialized?
72
+ @translations ||= {}
73
+ end
75
74
 
76
- protected
75
+ protected
77
76
 
78
- def lookup(locale, key, scope = [], options = {})
79
- key = normalize_flat_keys(locale, key, scope, options[:separator])
80
- key = key[1..-1] if key.first == '.'
81
- key = key[0..-2] if key.last == '.'
77
+ def lookup(locale, key, scope = [], options = {})
78
+ key = normalize_flat_keys(locale, key, scope, options[:separator])
79
+ key = key[1..-1] if key.first == '.'
80
+ key = key[0..-2] if key.last == '.'
82
81
 
83
- if self.class.config.cache_translations
84
- keys = ([locale] + key.split(I18n::Backend::Flatten::FLATTEN_SEPARATOR)).map(&:to_sym)
82
+ if self.class.config.cache_translations
83
+ keys = ([locale] + key.split(I18n::Backend::Flatten::FLATTEN_SEPARATOR)).map(&:to_sym)
85
84
 
86
- return translations.dig(*keys)
87
- end
85
+ return translations.dig(*keys)
86
+ end
88
87
 
89
- result = if key == ''
90
- Translation.locale(locale).all
91
- else
92
- Translation.locale(locale).lookup(key)
93
- end
88
+ result = if key == ''
89
+ Translation.locale(locale).all
90
+ else
91
+ Translation.locale(locale).lookup(key)
92
+ end
94
93
 
95
- if result.empty?
96
- nil
97
- elsif result.first.key == key
98
- result.first.value
99
- else
100
- result = result.inject({}) do |hash, translation|
101
- hash.deep_merge build_translation_hash_by_key(key, translation)
94
+ if result.empty?
95
+ nil
96
+ elsif result.first.key == key
97
+ result.first.value
98
+ else
99
+ result = result.inject({}) do |hash, translation|
100
+ hash.deep_merge build_translation_hash_by_key(key, translation)
101
+ end
102
+ result.deep_symbolize_keys
102
103
  end
103
- result.deep_symbolize_keys
104
104
  end
105
- end
106
105
 
107
- def build_translation_hash_by_key(lookup_key, translation)
108
- hash = {}
106
+ def build_translation_hash_by_key(lookup_key, translation)
107
+ hash = {}
109
108
 
110
- chop_range = if lookup_key == ''
111
- 0..-1
112
- else
113
- (lookup_key.size + FLATTEN_SEPARATOR.size)..-1
114
- end
115
- translation_nested_keys = translation.key.slice(chop_range).split(FLATTEN_SEPARATOR)
116
- translation_nested_keys.each.with_index.inject(hash) do |iterator, (key, index)|
117
- iterator[key] = translation_nested_keys[index + 1] ? {} : translation.value
118
- iterator[key]
119
- end
109
+ chop_range = if lookup_key == ''
110
+ 0..-1
111
+ else
112
+ (lookup_key.size + FLATTEN_SEPARATOR.size)..-1
113
+ end
114
+ translation_nested_keys = translation.key.slice(chop_range).split(FLATTEN_SEPARATOR)
115
+ translation_nested_keys.each.with_index.inject(hash) do |iterator, (key, index)|
116
+ iterator[key] = translation_nested_keys[index + 1] ? {} : translation.value
117
+ iterator[key]
118
+ end
120
119
 
121
- hash
122
- end
120
+ hash
121
+ end
123
122
 
124
- # For a key :'foo.bar.baz' return ['foo', 'foo.bar', 'foo.bar.baz']
125
- def expand_keys(key)
126
- key.to_s.split(FLATTEN_SEPARATOR).inject([]) do |keys, k|
127
- keys << [keys.last, k].compact.join(FLATTEN_SEPARATOR)
123
+ # For a key :'foo.bar.baz' return ['foo', 'foo.bar', 'foo.bar.baz']
124
+ def expand_keys(key)
125
+ key.to_s.split(FLATTEN_SEPARATOR).inject([]) do |keys, k|
126
+ keys << [keys.last, k].compact.join(FLATTEN_SEPARATOR)
127
+ end
128
128
  end
129
129
  end
130
+
131
+ include Implementation
130
132
  end
131
133
  end
132
134
  end
@@ -123,7 +123,7 @@ class I18nBackendActiveRecordTest < I18n::TestCase
123
123
  I18n.t('.') # Fixes test flakiness by loading available locales
124
124
  I18n::Backend::ActiveRecord::Translation.destroy_all
125
125
 
126
- assert_equal 'translation missing: en.no key', I18n.t('.')
126
+ assert_match(/[Tt]ranslation missing: en\.no key/, I18n.t('.'))
127
127
  end
128
128
 
129
129
  test 'intially unitinitialized' do
data/test/test_helper.rb CHANGED
@@ -10,35 +10,35 @@ require 'i18n/tests'
10
10
 
11
11
  begin
12
12
  require 'active_record'
13
- ::ActiveRecord::Base.connection
13
+ ActiveRecord::Base.connection
14
14
  rescue LoadError => e
15
15
  puts "can't use ActiveRecord backend because: #{e.message}"
16
- rescue ::ActiveRecord::ConnectionNotEstablished
16
+ rescue ActiveRecord::ConnectionNotEstablished
17
17
  require 'i18n/backend/active_record'
18
18
 
19
- case ENV['DB']
19
+ case ENV.fetch('DB', nil)
20
20
  when 'postgres'
21
- ::ActiveRecord::Base.establish_connection(
21
+ ActiveRecord::Base.establish_connection(
22
22
  adapter: 'postgresql',
23
23
  database: 'i18n_unittest',
24
- username: ENV['PG_USER'] || 'postgres',
25
- password: ENV['PG_PASSWORD'] || 'postgres',
24
+ username: ENV.fetch('PG_USER', 'postgres'),
25
+ password: ENV.fetch('PG_PASSWORD', 'postgres'),
26
26
  host: 'localhost'
27
27
  )
28
28
  when 'mysql'
29
- ::ActiveRecord::Base.establish_connection(
29
+ ActiveRecord::Base.establish_connection(
30
30
  adapter: 'mysql2',
31
31
  database: 'i18n_unittest',
32
- username: ENV['MYSQL_USER'] || 'root',
33
- password: ENV['MYSQL_PASSWORD'] || '',
32
+ username: ENV.fetch('MYSQL_USER', 'root'),
33
+ password: ENV.fetch('MYSQL_PASSWORD', ''),
34
34
  host: '127.0.0.1'
35
35
  )
36
36
  else
37
- ::ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
37
+ ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
38
38
  end
39
39
 
40
- ::ActiveRecord::Migration.verbose = false
41
- ::ActiveRecord::Schema.define(version: 1) do
40
+ ActiveRecord::Migration.verbose = false
41
+ ActiveRecord::Schema.define(version: 1) do
42
42
  create_table :translations, force: true do |t|
43
43
  t.string :locale
44
44
  t.string :key
@@ -48,6 +48,12 @@ rescue ::ActiveRecord::ConnectionNotEstablished
48
48
  end
49
49
  add_index :translations, %i[locale key], unique: true
50
50
  end
51
+
52
+ if ActiveRecord::Base.respond_to?(:yaml_column_permitted_classes=)
53
+ ActiveRecord::Base.yaml_column_permitted_classes = [Symbol]
54
+ elsif ActiveRecord.respond_to?(:yaml_column_permitted_classes=)
55
+ ActiveRecord.yaml_column_permitted_classes = [Symbol]
56
+ end
51
57
  end
52
58
 
53
59
  TEST_CASE = defined?(Minitest::Test) ? Minitest::Test : MiniTest::Unit::TestCase
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: i18n-active_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sven Fuchs
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-11-15 00:00:00.000000000 Z
11
+ date: 2023-08-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: i18n
@@ -150,7 +150,8 @@ files:
150
150
  homepage: http://github.com/svenfuchs/i18n-active_record
151
151
  licenses:
152
152
  - MIT
153
- metadata: {}
153
+ metadata:
154
+ rubygems_mfa_required: 'true'
154
155
  post_install_message:
155
156
  rdoc_options: []
156
157
  require_paths:
@@ -166,7 +167,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
166
167
  - !ruby/object:Gem::Version
167
168
  version: '0'
168
169
  requirements: []
169
- rubygems_version: 3.2.29
170
+ rubygems_version: 3.4.10
170
171
  signing_key:
171
172
  specification_version: 4
172
173
  summary: I18n ActiveRecord backend