svenfuchs/globalize3 — pull request #183 by tomeric (2012-12-20 10:28:30+00:00)
Title: Fix updating of content in original columns

Last year we split our monolithic application up into separate little apps. Two of them share the same database, one to administrate the content and one to present it to our visitors.

I am planning on using the `globalize3` gem in these applications and was thinking about adding it to our backend first so we can slowly start translating stuff.

Unfortunately, it seems like `globalize3` alters your database in a non backwards compatible way by forcing you to put the content of your translatable columns in a separate table. I have also gathered from several tickets here that the content of your original columns is altered in unwanted ways.

From what I gather, the advice given is to get rid of these original columns, but I like to propose a better solution.

## Proposed solution:

We only update the original table's columns if `I18n.locale` is set to `I18n.default_locale`. In addition; if the original table has a `locale` column, we use that instead of `I18n.default_locale`.

I have created a pull request with 2 commits that implement this behaviour. I'm really new to the globalize3 codebase, so I might have overlooked something, but all the tests pass. I am willing to put more work into this if you guys think is something that is worth it.

### Example #1: Using I18n.default_locale

class Article < ActiveRecord::Base
translates :title, :body
end

class LegacyArticle < ActiveRecord::Base
self.table_name = 'articles'
end

class CreateArticles < ActiveRecord::Migration
def up
create_table :articles do |t|
t.string :title
t.text :body
end

Article.create_translation_table!(
title: :string,
text: :text
)
end
end

I18n.default_locale = :en
I18n.locale = :en

article = Article.create! title: "Hello World!", body: "How are you?"
I18n.locale = :nl
article.update_attributes title: "Hallo Wereld!", body: "Hoe gaat het?"

article = Article.last
article.title # => "Hallo Wereld!"

legacy_article = LegacyArticle.last
legacy_article.title # => "Hello World!"

### Example #2: Adding the locale column

class AddLocaleToArticles < ActiveRecord::Migration
def up
add_column :articles, :locale, default: 'en'
end
end

I18n.locale = :en
I18n.default_locale = :en

article = Article.create! locale: 'nl', title: 'Vaarwel Wereld!', body: 'Tot de volgende keer!'
article.update_attributes title: 'Goodbye World!', body: 'See you next time!'

article = Article.last
article.title # => "Goodbye World!"
I18n.locale = :nl
article.title # => "Vaarwel Wereld!"

article = LegacyArticle.last
article.title # => "Vaarwel Wereld!"

## Previous tickets:

- [Globalize3 translates original table fields](https://github.com/svenfuchs/globalize3/issues/67)
- [Why is original table's column set to current locale's translation?](https://github.com/svenfuchs/globalize3/issues/79)