Skip to content

Commit

Permalink
[versioning with paper trail] Add translation caches, rollback suppor…
Browse files Browse the repository at this point in the history
…t. Only thing left now is Version default_scope not being dynamic as locale changes.
  • Loading branch information
Josh Adams committed May 21, 2011
1 parent 5a0b5bb commit 8db4c93
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 15 deletions.
3 changes: 1 addition & 2 deletions lib/globalize/active_record/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ def unserializable_attribute?(name, column)
end

def fetch_attribute(locale, name)
translations ||= record.translations
translation = translations.detect{|t| t.locale == locale}
translation = record.translation_for(locale)
return translation && translation.send(name)
end

Expand Down
22 changes: 17 additions & 5 deletions lib/globalize/active_record/instance_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def translated?(name)

def translated_attributes
translated_attribute_names.inject({}) do |attributes, name|
attributes.merge(name.to_s => send(name))
attributes.merge(name.to_s => translation.send(name))
end
end

Expand Down Expand Up @@ -107,11 +107,22 @@ def clone
end

def translation
if @translation.nil? or @translation.try(:locale) != ::Globalize.locale
@translation = translations.with_locale(::Globalize.locale).first
@translation ||= translations.build(:locale => ::Globalize.locale)
translation_for(::Globalize.locale)
end

def translation_for(locale)
@translation_caches ||= {}
unless @translation_caches[locale]
_translation = translations.with_locale(locale).first
debugger if $_bar
_translation ||= translations.build(:locale => locale)
@translation_caches[locale] = _translation
end
@translation
@translation_caches[locale]
end

def rollback
@translation_caches[::Globalize.locale] = translation.previous_version
end

protected
Expand All @@ -132,6 +143,7 @@ def used_locales

def save_translations!
globalize.save_translations!
@translation_caches = {}
end

def with_given_locale(attributes, &block)
Expand Down
10 changes: 9 additions & 1 deletion lib/globalize/active_record/translation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,12 @@ def locale=(locale)
end
end
end
end
end

# Setting this will force polymorphic associations to subclassed objects
# to use their table_name rather than the parent object's table name,
# which will allow you to get their models back in a more appropriate
# format.
#
# See http://www.ruby-forum.com/topic/159894 for details.
Globalize::ActiveRecord::Translation.abstract_class = true
14 changes: 7 additions & 7 deletions test/globalize3/versioning_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
class VersioningTest < Test::Unit::TestCase
test "versions are scoped to the current Globalize locale" do
post = Post.create!(:title => 'title v1', :content => '')
debugger
true
post.update_attributes!(:title => 'title v2')
assert_equal ['en', 'en'], post.versions.map(&:locale)

Expand All @@ -28,9 +26,10 @@ class VersioningTest < Test::Unit::TestCase
post.update_attributes!(:title => 'title v3')

# Roll back 2 versions in default locale
post.versions.last.reify
post.versions.last.reify
post.rollback
post.rollback

debugger
assert_equal 'title v1', post.title(:en)
assert_equal 'Titel v1', post.title(:de)
end
Expand All @@ -57,17 +56,18 @@ class VersioningTest < Test::Unit::TestCase
end

with_locale(:en) do
post.versions.last.reify
post.rollback
assert_equal 'title v2', post.title
assert !post.published?

post.versions.last.reify
post.rollback
assert_equal 'title v1', post.title
assert !post.published?
end

with_locale(:de) do
post.versions.last.reify
debugger
post.rollback
assert_equal 'Titel v1', post.title
assert !post.published?
end
Expand Down

0 comments on commit 8db4c93

Please sign in to comment.