Skip to content

Instantly share code, notes, and snippets.

@woahdae
Last active February 13, 2018 21:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save woahdae/ec3a2445ade123882643478e0898a572 to your computer and use it in GitHub Desktop.
Save woahdae/ec3a2445ade123882643478e0898a572 to your computer and use it in GitHub Desktop.
source 'https://rubygems.org'
gem 'activerecord'
gem 'globalize'
gem 'sqlite3'
gem 'activerecord', '5.1.4'
gem 'globalize', '5.1.0'
gem 'minitest', '5.10.3' # https://github.com/seattlerb/minitest/issues/730
require 'active_record'
require 'globalize'
require 'minitest/autorun'
require 'logger'
# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
I18n.available_locales = [:en, :es]
ActiveRecord::Base.establish_connection(
adapter: 'sqlite3',
database: ':memory:'
)
silence_warnings do
ActiveRecord::Schema.define(version: Time.now.to_i) do
create_table :posts, force: true do |t|
end
create_table "post_translations", force: true do |t|
t.integer "post_id", null: false
t.string "locale", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "title"
t.text "content"
end
end
end
class Post < ActiveRecord::Base
translates :content, :title
attr_reader :after_save_locale_values
after_save do
@after_save_locale_values = {}
@after_save_locale_values[:i18n] = I18n.locale
@after_save_locale_values[:globalize] = Globalize.locale
@after_save_locale_values[:globalize_inside_i18n] =
I18n.with_locale(:es) { Globalize.locale }
@after_save_locale_values[:i18n_inside_globalize] =
Globalize.with_locale(:es) { I18n.locale }
end
end
class AfterSaveTest < Minitest::Test
def setup
@post = Post.new(title: 'Example')
@post.save
end
def test_i18n_eq_default
# I18n.locale in #after_save should be 'en'
assert_equal :en, @post.after_save_locale_values[:i18n]
end
def test_globalize_eq_default
# Globalize.locale in #after_save should be 'en'
assert_equal :en, @post.after_save_locale_values[:globalize]
end
def test_i18n_inside_globalize_eq_globalize
# Globalize.with_locale(:es) { I18n.locale } in #after_save should be :es
assert_equal :es, @post.after_save_locale_values[:i18n_inside_globalize]
end
def test_globalize_inside_i18n_eq_i18n
# I18n.with_locale(:es) { Globalize.locale } in #after_save should be :es
assert_equal :es, @post.after_save_locale_values[:globalize_inside_i18n]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment