Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ Rails.application.routes.draw do
end
```

## Caching empty values ##
By default, copycat will cache empty values in the database. If a lookup results to a missing translation a new copycat entry will be created with an empty value for this locale and key.
This may not be desirable, especially during development, as after the first lookup copycat will always serve the empty translation, preventing the YML files from being consulted again for this locale and key.
Changing the default behaviour can be achieved through the following setting on the initializer:

```ruby
config.create_nils = false
```

## Logging ##
Because Copycat does a SQL query for each token, it can produce a lot of noise in the log output.
Therefore by default the logger is disabled for the Copycat ActiveRecord class.
Expand Down
2 changes: 2 additions & 0 deletions lib/copycat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ module Copycat
mattr_accessor :username
mattr_accessor :password
mattr_accessor :route
mattr_accessor :create_nils
@@route = 'copycat_translations'
@@create_nils = true

def self.setup
yield self
Expand Down
2 changes: 1 addition & 1 deletion lib/copycat/implementation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def lookup(locale, key, scope = [], options = {})
cct = CopycatTranslation.where(locale: locale.to_s, key: key.to_s).first
return cct.value if cct
value = super(locale, key, scope, options)
if value.is_a?(String) || value.nil?
if value.is_a?(String) || value.nil? && Copycat.create_nils
CopycatTranslation.create(locale: locale.to_s, key: key.to_s, value: value)
end
value
Expand Down
1 change: 1 addition & 0 deletions lib/tasks/copycat_tasks.rake
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace :copycat do
Copycat.setup do |config|
config.username = '#{username}'
config.password = '#{password}'
#config.create_nils = true
#config.route = 'copycat_translations'
end
CONFIG
Expand Down
10 changes: 9 additions & 1 deletion spec/integration/dummy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,20 @@
CopycatTranslation.find_by_key('site.index.header').should_not be_nil
end

it "creates a copycat_translation if the yaml does not have an entry" do
it "by default creates an empty copycat_translation if the yaml does not have an entry" do
Copycat.create_nils = true
CopycatTranslation.find_by_key('site.index.intro').should be_nil
visit root_path
CopycatTranslation.find_by_key('site.index.intro').should_not be_nil
end

it "does not create an empty copycat_translation when create_nils is set to false" do
Copycat.create_nils = false
CopycatTranslation.find_by_key('site.index.intro').should be_nil
visit root_path
CopycatTranslation.find_by_key('site.index.intro').should be_nil
end

it "shows the copycat_translation instead of the yaml" do
FactoryGirl.create(:copycat_translation, key: 'site.index.header', value: 'A different header')
visit root_path
Expand Down