Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat disable translates #17

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,55 @@ post.enable_fallback do
post.title_nl # => This database rocks!
end
```

## Temporarily disable translations / Migrating Data

If you are integrating this gem to an application with existing data, you are probably concerned about how to migrate your data from the untranslated column to your new `attr_translations` column. You can easily disable translations to do what you need when migrating the data over.

Older Schema:
```ruby
class CreatePosts < ActiveRecord::Migration
def up
create_table :posts do |t|
t.column :title, :string
t.timestamps
end
end
def down
drop_table :posts
end
end
```
Let's say you originally had something important saved in `title`:
```ruby
post.title = "something important"
```

You might think it disappeared, but with disable_translations you can still get the old data.

From:

```ruby
post.title # => nil
```

To:

```ruby
post.title # => nil
post.disable_translations
post.title # => "something important"
```

To migrate data, you could do something like:

```ruby
post.disable_translations
migrated_title = post.title
post.enable_translations

I18n.locale = :en
post.title = migrated_title
```


30 changes: 29 additions & 1 deletion lib/json_translate/translates/instance_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,19 @@ def enable_fallback
yield if block_given?
end

def disable_translations
toggle_translations(false)
yield if block_given?
end

def enable_translations
toggle_translations(true)
yield if block_given?
end

protected

attr_reader :enabled_fallback
attr_reader :enabled_fallback, :enabled_translations

def json_translate_fallback_locales(locale)
if enabled_fallback != false && I18n.respond_to?(:fallbacks)
Expand All @@ -24,6 +34,8 @@ def json_translate_fallback_locales(locale)
end

def read_json_translation(attr_name, locale = I18n.locale, fallback = true, **params)
return attributes["#{attr_name}"] if enabled_translations == false

translations = public_send("#{attr_name}#{SUFFIX}") || {}

selected_locale = locale
Expand All @@ -46,6 +58,8 @@ def read_json_translation(attr_name, locale = I18n.locale, fallback = true, **pa
end

def write_json_translation(attr_name, value, locale = I18n.locale)
return assign_attributes({attr_name => value}) if enabled_translations == false

value = value.presence
translation_store = "#{attr_name}#{SUFFIX}"
translations = public_send(translation_store) || {}
Expand All @@ -72,6 +86,20 @@ def toggle_fallback(enabled)
@enabled_fallback = enabled
end
end

def toggle_translations(enabled)
if block_given?
old_value = @enabled_translations
begin
@enabled_translations = enabled
yield
ensure
@enabled_translations = old_value
end
else
@enabled_translations = enabled
end
end
end
end
end