Skip to content

Commit 315b6ab

Browse files
Add included_keys filter
1 parent 5fc8686 commit 315b6ab

File tree

4 files changed

+54
-8
lines changed

4 files changed

+54
-8
lines changed

lib/lit.rb

+8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module Lit
1010
mattr_accessor :humanize_key
1111
mattr_accessor :humanize_key_ignored_keys
1212
mattr_accessor :humanize_key_ignored
13+
mattr_accessor :included_keys
1314
mattr_accessor :ignored_keys
1415
mattr_accessor :ignore_yaml_on_startup
1516
mattr_accessor :api_enabled
@@ -31,6 +32,13 @@ def self.init
3132
Lit.humanize_key_ignored = %w[i18n date datetime number time support ]
3233
Lit.humanize_key_ignored |= Lit.humanize_key_ignored_keys
3334
Lit.humanize_key_ignored = %r{(#{Lit.humanize_key_ignored.join('|')}).*}
35+
36+
if Lit.included_keys.is_a?(String)
37+
keys = Lit.included_keys.split(',').map(&:strip)
38+
Lit.included_keys = keys
39+
end
40+
Lit.included_keys = [] unless Lit.included_keys.is_a?(Array)
41+
3442
if Lit.ignored_keys.is_a?(String)
3543
keys = Lit.ignored_keys.split(',').map(&:strip)
3644
Lit.ignored_keys = keys

lib/lit/i18n_backend.rb

+10-8
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ def can_dup_default(options = {})
5858
return false unless options.key?(:default)
5959
return true if options[:default].is_a?(String)
6060
return true if options[:default].is_a?(Array) && \
61-
(options[:default].first.is_a?(String) || \
62-
options[:default].first.is_a?(Symbol) || \
63-
options[:default].first.is_a?(Array))
64-
false
61+
(options[:default].first.is_a?(String) || \
62+
options[:default].first.is_a?(Symbol) || \
63+
options[:default].first.is_a?(Array))
64+
false
6565
end
6666

6767
def lookup(locale, key, scope = [], options = {})
@@ -124,9 +124,9 @@ def store_item(locale, data, scope = [], startup_process = false)
124124
key = ([locale] + scope).join('.')
125125
if data.respond_to?(:to_hash)
126126
# ActiveRecord::Base.transaction do
127-
data.to_hash.each do |k, value|
128-
store_item(locale, value, scope + [k], startup_process)
129-
end
127+
data.to_hash.each do |k, value|
128+
store_item(locale, value, scope + [k], startup_process)
129+
end
130130
# end
131131
elsif data.respond_to?(:to_str) || data.is_a?(Array)
132132
key = ([locale] + scope).join('.')
@@ -177,7 +177,9 @@ def valid_locale?(locale)
177177
end
178178

179179
def is_ignored_key(key_without_locale)
180-
Lit.ignored_keys.any?{ |k| key_without_locale.start_with?(k) }
180+
return true if Lit.included_keys.any? && !Lit.included_keys.any?{ |k| key_without_locale.start_with?(k) }
181+
182+
Lit.ignored_keys.any?{ |k| key_without_locale.start_with?(k) }
181183
end
182184

183185
def should_cache?(key_with_locale, options)

test/unit/i18n_backend_test.rb

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def setup
99
@old_backend = I18n.backend
1010
@old_locale = I18n.locale
1111
@old_humanize_key = Lit.humanize_key
12+
@old_included_keys = Lit.included_keys
1213
@old_available_locales = ::Rails.configuration.i18n.available_locales
1314
end
1415

@@ -18,6 +19,7 @@ def teardown
1819
I18n.backend = @old_backend
1920
I18n.locale = @old_locale
2021
Lit.humanize_key = @old_humanize_key
22+
Lit.included_keys = @old_included_keys
2123
end
2224

2325
test 'properly returns available locales' do

test/unit/lit_behaviour_test.rb

+34
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,40 @@ def teardown
121121
Lit.loader = old_loader
122122
end
123123

124+
125+
test 'it wont store key if prefix is added to ignored, but in included keys' do
126+
old_loader = Lit.loader
127+
key = 'test.of.storage'
128+
existing_key = 'existing.string'
129+
Lit.included_keys = ['existing']
130+
Lit.loader = nil
131+
Lit.init
132+
I18n.t(key)
133+
I18n.t(existing_key)
134+
assert !Lit::LocalizationKey.where(localization_key: key).exists?
135+
assert Lit::LocalizationKey.where(localization_key: existing_key).exists?
136+
Lit.loader = old_loader
137+
end
138+
139+
test 'it wont store key if prefix is added to ignored, but not in included keys' do
140+
old_loader = Lit.loader
141+
included_key = 'test.of.storage'
142+
ignored_key = 'test.of.storage2'
143+
ignored_key2 = 'existing.string'
144+
Lit.included_keys = ['test']
145+
Lit.ignored_keys = ['test.of.storage2']
146+
Lit.loader = nil
147+
Lit.init
148+
I18n.t(included_key)
149+
I18n.t(ignored_key)
150+
I18n.t(ignored_key2)
151+
assert !Lit::LocalizationKey.where(localization_key: ignored_key).exists?
152+
assert !Lit::LocalizationKey.where(localization_key: ignored_key2).exists?
153+
assert Lit::LocalizationKey.where(localization_key: included_key).exists?
154+
Lit.loader = old_loader
155+
end
156+
157+
124158
test 'it wont store key if ignored_key prefix is a string' do
125159
old_loader = Lit.loader
126160
first_key = 'test.of.storage'

0 commit comments

Comments
 (0)