Skip to content

Commit

Permalink
Rename i18n create_missing* config/option to auto_create*
Browse files Browse the repository at this point in the history
  • Loading branch information
miks committed Oct 11, 2016
1 parent 2034a46 commit 96edb45
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 30 deletions.
Expand Up @@ -103,7 +103,7 @@ def missing?(locale, key)
def missing(locale, key, options)
# mark translation as missing
missing_keys["#{locale}.#{key}"] = true
create_missing(key, options) if create_missing?(key, options)
auto_create(key, options) if auto_create?(key, options)
end

def locales_pluralizations
Expand All @@ -112,9 +112,9 @@ def locales_pluralizations
end.flatten.uniq.compact
end

def create_missing?(key, options)
return false unless config.i18n_database.create_missing_translations
return false if options[:create_missing] == false
def auto_create?(key, options)
return false unless config.i18n_database.auto_creation
return false if options[:auto_create] == false
return false if auto_creation_exception?(key)
return false if stored_keys.key?(key)
true
Expand All @@ -124,7 +124,7 @@ def auto_creation_exception?(key)
config.i18n_database.auto_creation_exception_patterns.find{|pattern| key.match(pattern) }.present?
end

def create_missing(key, options)
def auto_create(key, options)
if pluralizable_translation?(options)
locales_pluralizations.each do|pluralization|
Releaf::I18nDatabase::I18nEntry.create(key: "#{key}.#{pluralization}")
Expand Down
2 changes: 1 addition & 1 deletion releaf-i18n_database/lib/releaf/i18n_database/backend.rb
Expand Up @@ -6,7 +6,7 @@ class Backend

include ::I18n::Backend::Base, ::I18n::Backend::Flatten
UPDATED_AT_KEY = 'releaf.i18n_database.translations.updated_at'
DEFAULT_CONFIG = {create_missing_translations: true, auto_creation_exception_patterns: [/^attributes\./]}
DEFAULT_CONFIG = {auto_creation: true, auto_creation_exception_patterns: [/^attributes\./]}
attr_accessor :translations_cache

def self.initialize_component
Expand Down
@@ -1,7 +1,7 @@
module Releaf::I18nDatabase
class Configuration
include Virtus.model(strict: true)
attribute :create_missing_translations, Boolean
attribute :auto_creation, Boolean
attribute :auto_creation_exception_patterns, Array
end
end
2 changes: 1 addition & 1 deletion releaf-i18n_database/spec/features/translations_spec.rb
Expand Up @@ -195,7 +195,7 @@
describe "Lookup" do
background do
I18n.backend.backends.first.translations_cache = nil # reset cache
allow( Releaf.application.config.i18n_database ).to receive(:create_missing_translations).and_return(true)
allow( Releaf.application.config.i18n_database ).to receive(:auto_creation).and_return(true)
end

context "when translation exists within higher level key (instead of being scope)" do
Expand Down
@@ -1,10 +1,10 @@
require "rails_helper"

describe Releaf::I18nDatabase::Configuration do
subject{ described_class.new(create_missing_translations: true, auto_creation_exception_patterns: [1, 2]) }
subject{ described_class.new(auto_creation: true, auto_creation_exception_patterns: [1, 2]) }

it do
is_expected.to have_attributes(create_missing_translations: true)
is_expected.to have_attributes(auto_creation: true)
is_expected.to have_attributes(auto_creation_exception_patterns: [1, 2])
end
end
Expand Down
Expand Up @@ -403,16 +403,16 @@

context "when missing translation creation is available for given key and options" do
it "creates missing translation" do
allow(subject).to receive(:create_missing?).with("ps.asda", a: "x").and_return(true)
expect(subject).to receive(:create_missing).with("ps.asda", a: "x")
allow(subject).to receive(:auto_create?).with("ps.asda", a: "x").and_return(true)
expect(subject).to receive(:auto_create).with("ps.asda", a: "x")
subject.missing(:de, "ps.asda", a: "x")
end
end

context "when missing translation creation is not available for given key and options" do
it "does not create missing translation" do
allow(subject).to receive(:create_missing?).with("ps.asda", a: "x").and_return(false)
expect(subject).to_not receive(:create_missing)
allow(subject).to receive(:auto_create?).with("ps.asda", a: "x").and_return(false)
expect(subject).to_not receive(:auto_create)
subject.missing(:de, "ps.asda", a: "x")
end
end
Expand All @@ -434,45 +434,45 @@
end
end

describe "#create_missing?" do
describe "#auto_create?" do
before do
allow(subject.config.i18n_database ).to receive(:create_missing_translations).and_return(true)
allow(subject.config.i18n_database ).to receive(:auto_creation).and_return(true)
allow(subject).to receive(:stored_keys).and_return("xxxome.save" => "xxxome.save")
allow(subject).to receive(:auto_creation_exception?).with("some.save").and_return(false)
end

context "when missing translation creation is enabled globally by i18n config and not disabled by `create_missing` option" do
context "when missing translation creation is enabled globally by i18n config and not disabled by `auto_create` option" do
it "returns true" do
expect(subject.create_missing?("some.save", {})).to be true
expect(subject.create_missing?("some.save", create_missing: true)).to be true
expect(subject.create_missing?("some.save", create_missing: nil)).to be true
expect(subject.auto_create?("some.save", {})).to be true
expect(subject.auto_create?("some.save", auto_create: true)).to be true
expect(subject.auto_create?("some.save", auto_create: nil)).to be true
end
end

context "when auto creation exception" do
it "returns false" do
allow(subject).to receive(:auto_creation_exception?).with("some.save").and_return(true)
expect(subject.create_missing?("some.save", {})).to be false
expect(subject.auto_create?("some.save", {})).to be false
end
end

context "when missing translation creation is disabled globally by i18n config" do
it "returns false" do
allow(subject.config.i18n_database ).to receive(:create_missing_translations).and_return(false)
expect(subject.create_missing?("some.save", {})).to be false
allow(subject.config.i18n_database ).to receive(:auto_creation).and_return(false)
expect(subject.auto_create?("some.save", {})).to be false
end
end

context "when missing translation creation is disabled by `create_missing` option" do
context "when missing translation creation is disabled by `auto_create` option" do
it "returns false" do
expect(subject.create_missing?("some.save", create_missing: false)).to be false
expect(subject.auto_create?("some.save", auto_create: false)).to be false
end
end

context "when key already exists within stored keys hash" do
it "returns false" do
allow(subject).to receive(:stored_keys).and_return("some.save" => "some.save")
expect(subject.create_missing?("some.save", {})).to be false
expect(subject.auto_create?("some.save", {})).to be false
end
end
end
Expand All @@ -493,7 +493,7 @@
end
end

describe "#create_missing" do
describe "#auto_create" do
before do
allow(subject).to receive(:locales_pluralizations).and_return([:one, :many, :other])
end
Expand All @@ -504,15 +504,15 @@
expect(Releaf::I18nDatabase::I18nEntry).to receive(:create).with(key: "aasd.oihgja.sd.one")
expect(Releaf::I18nDatabase::I18nEntry).to receive(:create).with(key: "aasd.oihgja.sd.many")
expect(Releaf::I18nDatabase::I18nEntry).to receive(:create).with(key: "aasd.oihgja.sd.other")
subject.create_missing("aasd.oihgja.sd", a: "b")
subject.auto_create("aasd.oihgja.sd", a: "b")
end
end

context "when non pluralizable translation given" do
it "creates translation" do
allow(subject).to receive(:pluralizable_translation?).with(a: "b").and_return(false)
expect(Releaf::I18nDatabase::I18nEntry).to receive(:create).with(key: "aasd.oihgja.sd")
subject.create_missing("aasd.oihgja.sd", a: "b")
subject.auto_create("aasd.oihgja.sd", a: "b")
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/rails_helper.rb
Expand Up @@ -95,7 +95,7 @@ def write(message)

config.before(:each) do |example|
Rails.cache.clear
allow( Releaf.application.config.i18n_database ).to receive(:create_missing_translations).and_return(false)
allow( Releaf.application.config.i18n_database ).to receive(:auto_creation).and_return(false)

if example.metadata[:db_strategy]
DatabaseCleaner.strategy = example.metadata[:db_strategy]
Expand Down

0 comments on commit 96edb45

Please sign in to comment.