Skip to content

Commit

Permalink
Add included_keys filter
Browse files Browse the repository at this point in the history
  • Loading branch information
h0jeZvgoxFepBQ2C committed Sep 17, 2019
1 parent 5fc8686 commit 315b6ab
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 8 deletions.
8 changes: 8 additions & 0 deletions lib/lit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
18 changes: 10 additions & 8 deletions lib/lit/i18n_backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ def can_dup_default(options = {})
return false unless options.key?(:default)
return true if options[:default].is_a?(String)
return true if options[:default].is_a?(Array) && \
(options[:default].first.is_a?(String) || \
options[:default].first.is_a?(Symbol) || \
options[:default].first.is_a?(Array))
false
(options[:default].first.is_a?(String) || \
options[:default].first.is_a?(Symbol) || \
options[:default].first.is_a?(Array))
false
end

def lookup(locale, key, scope = [], options = {})
Expand Down Expand Up @@ -124,9 +124,9 @@ def store_item(locale, data, scope = [], startup_process = false)
key = ([locale] + scope).join('.')
if data.respond_to?(:to_hash)
# ActiveRecord::Base.transaction do
data.to_hash.each do |k, value|
store_item(locale, value, scope + [k], startup_process)
end
data.to_hash.each do |k, value|
store_item(locale, value, scope + [k], startup_process)
end
# end
elsif data.respond_to?(:to_str) || data.is_a?(Array)
key = ([locale] + scope).join('.')
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions test/unit/i18n_backend_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def setup
@old_backend = I18n.backend
@old_locale = I18n.locale
@old_humanize_key = Lit.humanize_key
@old_included_keys = Lit.included_keys
@old_available_locales = ::Rails.configuration.i18n.available_locales
end

Expand All @@ -18,6 +19,7 @@ def teardown
I18n.backend = @old_backend
I18n.locale = @old_locale
Lit.humanize_key = @old_humanize_key
Lit.included_keys = @old_included_keys
end

test 'properly returns available locales' do
Expand Down
34 changes: 34 additions & 0 deletions test/unit/lit_behaviour_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 315b6ab

Please sign in to comment.