From 336359e01acba454656f01d4996e24acc3f828aa Mon Sep 17 00:00:00 2001 From: Peter L Date: Tue, 17 Sep 2019 13:49:45 +0200 Subject: [PATCH 1/2] Add include_keys filter --- lib/lit.rb | 8 ++++++++ lib/lit/i18n_backend.rb | 4 +++- test/unit/lit_behaviour_test.rb | 34 +++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/lib/lit.rb b/lib/lit.rb index 9d9348b0..4f52506b 100644 --- a/lib/lit.rb +++ b/lib/lit.rb @@ -10,6 +10,7 @@ module Lit mattr_accessor :humanize_key mattr_accessor :humanize_key_ignored_keys mattr_accessor :humanize_key_ignored + mattr_accessor :included_keys mattr_accessor :ignored_keys mattr_accessor :ignore_yaml_on_startup mattr_accessor :api_enabled @@ -31,6 +32,13 @@ def self.init Lit.humanize_key_ignored = %w[i18n date datetime number time support ] Lit.humanize_key_ignored |= Lit.humanize_key_ignored_keys Lit.humanize_key_ignored = %r{(#{Lit.humanize_key_ignored.join('|')}).*} + + if Lit.included_keys.is_a?(String) + keys = Lit.included_keys.split(',').map(&:strip) + Lit.included_keys = keys + end + Lit.included_keys = [] unless Lit.included_keys.is_a?(Array) + if Lit.ignored_keys.is_a?(String) keys = Lit.ignored_keys.split(',').map(&:strip) Lit.ignored_keys = keys diff --git a/lib/lit/i18n_backend.rb b/lib/lit/i18n_backend.rb index 6fb492c1..c6b3a9db 100644 --- a/lib/lit/i18n_backend.rb +++ b/lib/lit/i18n_backend.rb @@ -177,7 +177,9 @@ def valid_locale?(locale) end def is_ignored_key(key_without_locale) - Lit.ignored_keys.any?{ |k| key_without_locale.start_with?(k) } + return true if Lit.included_keys.any? && !Lit.included_keys.any?{ |k| key_without_locale.start_with?(k) } + + Lit.ignored_keys.any?{ |k| key_without_locale.start_with?(k) } end def should_cache?(key_with_locale, options) diff --git a/test/unit/lit_behaviour_test.rb b/test/unit/lit_behaviour_test.rb index fe029635..7f7aba49 100644 --- a/test/unit/lit_behaviour_test.rb +++ b/test/unit/lit_behaviour_test.rb @@ -121,6 +121,40 @@ def teardown Lit.loader = old_loader end + + test 'it wont store key if prefix is added to ignored, but in included keys' do + old_loader = Lit.loader + key = 'test.of.storage' + existing_key = 'existing.string' + Lit.included_keys = ['existing'] + Lit.loader = nil + Lit.init + I18n.t(key) + I18n.t(existing_key) + assert !Lit::LocalizationKey.where(localization_key: key).exists? + assert Lit::LocalizationKey.where(localization_key: existing_key).exists? + Lit.loader = old_loader + end + + test 'it wont store key if prefix is added to ignored, but not in included keys' do + old_loader = Lit.loader + included_key = 'test.of.storage' + ignored_key = 'test.of.storage2' + ignored_key2 = 'existing.string' + Lit.included_keys = ['test'] + Lit.ignored_keys = ['test.of.storage2'] + Lit.loader = nil + Lit.init + I18n.t(included_key) + I18n.t(ignored_key) + I18n.t(ignored_key2) + assert !Lit::LocalizationKey.where(localization_key: ignored_key).exists? + assert !Lit::LocalizationKey.where(localization_key: ignored_key2).exists? + assert Lit::LocalizationKey.where(localization_key: included_key).exists? + Lit.loader = old_loader + end + + test 'it wont store key if ignored_key prefix is a string' do old_loader = Lit.loader first_key = 'test.of.storage' From bc67a3b247b395daacf28f70ca862e1bcb80e0a3 Mon Sep 17 00:00:00 2001 From: Peter L Date: Wed, 18 Sep 2019 23:28:38 +0200 Subject: [PATCH 2/2] Add freeze to included keys --- lib/lit.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/lit.rb b/lib/lit.rb index 4f52506b..dbf50550 100644 --- a/lib/lit.rb +++ b/lib/lit.rb @@ -38,6 +38,7 @@ def self.init Lit.included_keys = keys end Lit.included_keys = [] unless Lit.included_keys.is_a?(Array) + Lit.included_keys = Lit.included_keys.map(&:freeze).freeze if Lit.ignored_keys.is_a?(String) keys = Lit.ignored_keys.split(',').map(&:strip)