Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

upgraded to work with AR:B.locale

  • Loading branch information...
commit 89ff530e97b077a12b988be80c3bc3653b5a4024 1 parent 4fff48a
@joshmh authored
Showing with 40 additions and 39 deletions.
  1. +10 −10 lib/globalize2_versioning.rb
  2. +30 −29 test/active_record/versioning_test.rb
View
20 lib/globalize2_versioning.rb
@@ -33,7 +33,7 @@ def update_translations!
@stash.clear
end
- def highest_version(locale = I18n.locale)
+ def highest_version(locale = ::ActiveRecord::Base.locale)
@record.globalize_translations.maximum(:version,
:conditions => { :locale => locale.to_s, reference_field => @record.id }) || 0
end
@@ -112,12 +112,12 @@ def drop_translation_table!
module InstanceMethods
def versioned?; true end
- def version(locale = I18n.locale)
+ def version(locale = self.class.locale)
translation = globalize_translations.find_by_locale_and_current(locale.to_s, true)
translation ? translation.version : nil
end
- def revert_to(version, locale = I18n.locale)
+ def revert_to(version, locale = self.class.locale)
version = version.to_i
return true if version == self.version
new_translation = globalize_translations.find_by_locale_and_version(locale.to_s, version)
@@ -155,7 +155,7 @@ def versions
private
- def clear_old_versions(locale = I18n.locale)
+ def clear_old_versions(locale = self.class.locale)
if self.class.max_version_limit > 0
old_version = version - self.class.max_version_limit
if old_version > 0
@@ -174,33 +174,33 @@ def initialize(rec)
end
def [](ver)
- rec = @rec.globalize_translations.find_by_locale_and_version( I18n.locale.to_s, ver )
+ rec = @rec.globalize_translations.find_by_locale_and_version( @rec.class.locale.to_s, ver )
rec.readonly! if rec
rec
end
def count
- @rec.globalize_translations.count( :conditions => [ 'locale = ?', I18n.locale.to_s ] )
+ @rec.globalize_translations.count( :conditions => [ 'locale = ?', @rec.class.locale.to_s ] )
end
def first
- @rec.globalize_translations.minimum( :version, :conditions => [ 'locale = ?', I18n.locale.to_s ] )
+ @rec.globalize_translations.minimum( :version, :conditions => [ 'locale = ?', @rec.class.locale.to_s ] )
end
def second
- rec = @rec.globalize_translations.first :conditions => [ 'locale = ?', I18n.locale.to_s ],
+ rec = @rec.globalize_translations.first :conditions => [ 'locale = ?', @rec.class.locale.to_s ],
:offset => 1, :order => 'version ASC'
rec && rec.version
end
def third
- rec = @rec.globalize_translations.first :conditions => [ 'locale = ?', I18n.locale.to_s ],
+ rec = @rec.globalize_translations.first :conditions => [ 'locale = ?', @rec.class.locale.to_s ],
:offset => 2, :order => 'version ASC'
rec && rec.version
end
def last
- @rec.globalize_translations.maximum( :version, :conditions => [ 'locale = ?', I18n.locale.to_s ] )
+ @rec.globalize_translations.maximum( :version, :conditions => [ 'locale = ?', @rec.class.locale.to_s ] )
end
def empty?
View
59 test/active_record/versioning_test.rb
@@ -24,7 +24,8 @@ class VersioningTest < ActiveSupport::TestCase
def setup
I18n.fallbacks.clear
reset_db! File.expand_path(File.join(File.dirname(__FILE__), '..', 'data', 'schema.rb'))
- I18n.locale = :en
+ ActiveRecord::Base.locale = :de
+ ActiveRecord::Base.locale = :en
end
test 'versioned? method' do
@@ -63,7 +64,7 @@ def setup
end
test 'current version with locale switching' do
- I18n.locale = :de
+ ActiveRecord::Base.locale = :de
section = Section.create :content => 'foo (de)'
assert_equal 1, section.globalize_translations.size
@@ -74,7 +75,7 @@ def setup
assert_equal 2, section.version
assert_equal 'bar (de)', section.content
- I18n.locale = :en
+ ActiveRecord::Base.locale = :en
section.update_attribute :content, 'foo'
assert_equal 1, section.version
section.update_attribute :content, 'bar'
@@ -84,7 +85,7 @@ def setup
section.reload
assert_equal 3, section.version
- I18n.locale = :de
+ ActiveRecord::Base.locale = :de
assert_equal 2, section.version
assert_equal 'bar (de)', section.content
end
@@ -93,14 +94,14 @@ def setup
I18n.fallbacks.map :de => [ :en ]
section = Section.create :content => 'foo'
- I18n.locale = :de
+ ActiveRecord::Base.locale = :de
assert_equal 'foo', section.content
assert_nil section.version
- I18n.locale = :en
+ ActiveRecord::Base.locale = :en
section.update_attribute :content, 'bar'
- I18n.locale = :de
+ ActiveRecord::Base.locale = :de
assert_equal 'bar', section.content
# no translation record for :de, so version is nil
@@ -112,9 +113,9 @@ def setup
assert_nil section.version
# load from db, then switch locale
- I18n.locale = :en
+ ActiveRecord::Base.locale = :en
section = Section.first
- I18n.locale = :de
+ ActiveRecord::Base.locale = :de
assert_equal 'bar', section.content
assert_nil section.version
end
@@ -123,14 +124,14 @@ def setup
I18n.fallbacks.map :de => [ :en ]
section = Section.create :content => 'foo'
- I18n.locale = :de
+ ActiveRecord::Base.locale = :de
assert_equal 'foo', section.content
assert_nil section.version
- I18n.locale = :en
+ ActiveRecord::Base.locale = :en
section.update_attribute :content, 'bar'
- I18n.locale = :de
+ ActiveRecord::Base.locale = :de
section.update_attribute :content, 'bar (de)'
assert_equal 1, section.version
@@ -149,14 +150,14 @@ def setup
assert_equal 2, section.version
# load from db, then switch locale
- I18n.locale = :en
+ ActiveRecord::Base.locale = :en
section = Section.first
- I18n.locale = :de
+ ActiveRecord::Base.locale = :de
assert_equal 'baz (de)', section.content
assert_equal 2, section.version
# continue versioning in :en
- I18n.locale = :en
+ ActiveRecord::Base.locale = :en
assert_equal 'bar', section.content
assert_equal 2, section.version
section.update_attribute :content, 'baz'
@@ -239,20 +240,20 @@ def setup
assert_equal 3, section.version
# :de
- I18n.locale = :de
+ ActiveRecord::Base.locale = :de
section.update_attribute :content, 'baz (de)'
section.update_attribute :content, 'qux (de)'
assert_equal 2, section.version
- I18n.locale = :en
+ ActiveRecord::Base.locale = :en
section.revert_to 1
assert_equal 'foo', section.content
assert_equal 1, section.version
- I18n.locale = :de
+ ActiveRecord::Base.locale = :de
assert_equal 2, section.version
assert_equal 'qux (de)', section.content
- I18n.locale = :en
+ ActiveRecord::Base.locale = :en
section.revert_to 2
assert_equal 'bar', section.content
@@ -263,7 +264,7 @@ def setup
assert_equal 'bar', section.content
assert_equal 2, section.version
- I18n.locale = :de
+ ActiveRecord::Base.locale = :de
assert_equal 2, section.version
assert_equal 'qux (de)', section.content
@@ -271,19 +272,19 @@ def setup
assert_equal 1, section.version
assert_equal 'baz (de)', section.content
- I18n.locale = :en
+ ActiveRecord::Base.locale = :en
assert_equal 'bar', section.content
assert_equal 2, section.version
- I18n.locale = :de
+ ActiveRecord::Base.locale = :de
section = Section.first
assert_equal 1, section.version
assert_equal 'baz (de)', section.content
- I18n.locale = :en
+ ActiveRecord::Base.locale = :en
assert_equal 'bar', section.content
assert_equal 2, section.version
- I18n.locale = :de
+ ActiveRecord::Base.locale = :de
end
test 'revert_to and then saving another version' do
@@ -327,13 +328,13 @@ def setup
section.update_attribute :content, 'foo3'
section.update_attribute :content, 'foo4'
section.update_attribute :content, 'foo5'
- assert_not_nil section.globalize_translations.find_by_locale_and_version(I18n.locale.to_s, 1)
+ assert_not_nil section.globalize_translations.find_by_locale_and_version(ActiveRecord::Base.locale.to_s, 1)
section.update_attribute :content, 'foo6'
- assert_nil section.globalize_translations.find_by_locale_and_version(I18n.locale.to_s, 1)
- assert_not_nil section.globalize_translations.find_by_locale_and_version(I18n.locale.to_s, 2)
+ assert_nil section.globalize_translations.find_by_locale_and_version(ActiveRecord::Base.locale.to_s, 1)
+ assert_not_nil section.globalize_translations.find_by_locale_and_version(ActiveRecord::Base.locale.to_s, 2)
section.update_attribute :content, 'foo7'
- assert_nil section.globalize_translations.find_by_locale_and_version(I18n.locale.to_s, 2)
- assert_not_nil section.globalize_translations.find_by_locale_and_version(I18n.locale.to_s, 3)
+ assert_nil section.globalize_translations.find_by_locale_and_version(ActiveRecord::Base.locale.to_s, 2)
+ assert_not_nil section.globalize_translations.find_by_locale_and_version(ActiveRecord::Base.locale.to_s, 3)
end
test 'empty versions' do
Please sign in to comment.
Something went wrong with that request. Please try again.