Skip to content

Commit 3a26fb9

Browse files
Add include_keys filter
1 parent ff6b892 commit 3a26fb9

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
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

+3-1
Original file line numberDiff line numberDiff line change
@@ -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/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)