Skip to content

Instantly share code, notes, and snippets.

@skyporter
Last active February 9, 2019 23:11
Show Gist options
  • Save skyporter/2387ac5120383e662fadb25c65a99d33 to your computer and use it in GitHub Desktop.
Save skyporter/2387ac5120383e662fadb25c65a99d33 to your computer and use it in GitHub Desktop.
Bug on globalize gem with dirty attributes
# frozen_string_literal: true
begin
require "bundler/inline"
rescue LoadError => e
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
raise e
end
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rails", "5.1.6"
gem "sqlite3"
gem "globalize", github: "globalize/globalize"
end
require "active_record"
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)
class Post < ActiveRecord::Base
translates :title
attribute :title_en
def title_en=(value)
super(value)
write_attribute(:title, value, :locale => I18n.locale)
end
def title_en
globalize.fetch_stash(:en, :title) || globalize.fetch_attribute(:en, :title)
end
end
ActiveRecord::Schema.define do
create_table :posts, force: true do |t|
end
reversible do |dir|
dir.up do
Post.create_translation_table! title: :string
end
dir.down do
Post.drop_translation_table!
end
end
end
class BugTest < Minitest::Test
def test_dirty_changes
post = Post.create!(title_en: "v1")
assert_equal "v1", post.title
assert_equal [nil, "v1"], post.saved_changes[:title_en]
post.reload
assert_equal "v1", post.title
assert_equal "v1", post.title_en
assert post.update(title_en: "v2")
assert_equal "v2", post.title
assert_equal ["v1", "v2"], post.saved_changes[:title_en]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment