Browse files

clean a few things up

  • Loading branch information...
1 parent 803b8ec commit 86e75690ac37ffc2bdcc68d0e55508689294d9f3 @svenfuchs svenfuchs committed Dec 14, 2009
Showing with 15 additions and 15 deletions.
  1. +7 −3 lib/globalize/active_record.rb
  2. +2 −8 lib/globalize/active_record/adapter.rb
  3. +6 −4 test/active_record_test.rb
View
10 lib/globalize/active_record.rb
@@ -88,16 +88,20 @@ def respond_to?(method)
def method_missing(method, *args)
if method.to_s =~ /^find_by_(\w+)$/ && translated_attribute_names.include?($1.to_sym)
- find(:first, :joins => :translations, :conditions => [
- "#{translated_attr_name($1)} = ? AND #{translated_attr_name('locale')} IN (?)",
- args.first, Globalize.fallbacks(locale).map(&:to_s)])
+ find_first_by_translated_attr_and_locales($1, args.first)
else
super
end
end
protected
+ def find_first_by_translated_attr_and_locales(name, value)
+ query = "#{translated_attr_name(name)} = ? AND #{translated_attr_name('locale')} IN (?)"
+ locales = Globalize.fallbacks(locale).map(&:to_s)
+ find(:first, :joins => :translations, :conditions => [query, value, locales])
+ end
+
def translated_attr_accessor(name)
define_method "#{name}=", lambda { |value|
globalize.write(self.class.locale, name, value)
View
10 lib/globalize/active_record/adapter.rb
@@ -54,15 +54,9 @@ def fetch_attribute(locale, attr_name)
translations = fetch_translations(locale)
value, requested_locale = nil, locale
- # Walk through the fallbacks, starting with the current locale itself,
- # and moving to the next best choice, until we find a match. Check the
- # cache first to see if we've changed the attribute and not saved yet.
Globalize.fallbacks(locale).each do |fallback|
- # TODO should we be checking stash or just cache?
- value = cache.read(fallback, attr_name) || begin
- translation = translations.detect { |t| t.locale == fallback }
- translation && translation.send(attr_name)
- end
+ translation = translations.detect { |t| t.locale == fallback }
+ value = translation && translation.send(attr_name)
locale = fallback && break if value
end
View
10 test/active_record_test.rb
@@ -1,7 +1,7 @@
require File.dirname(__FILE__) + '/test_helper'
require File.join( File.dirname(__FILE__) + '/data/models' )
-class TranslatedTest < ActiveSupport::TestCase
+class ActiveRecordTest < ActiveSupport::TestCase
def setup
I18n.locale = :'en-US'
reset_db!
@@ -178,24 +178,26 @@ def setup
end
test 'change attribute on globalized model after locale switching' do
- post = Post.create :subject => 'foo', :content => 'bar'
+ post = Post.create(:subject => 'foo', :content => 'bar')
assert_equal [], post.changed
post.subject = 'baz'
I18n.locale = :de
assert_equal [ 'subject' ], post.changed
end
test 'reload' do
- post = Post.create :subject => 'foo', :content => 'bar'
+ post = Post.create(:subject => 'foo', :content => 'bar')
post.subject = 'baz'
assert_equal 'foo', post.reload.subject
end
test 'complex writing and stashing' do
- post = Post.create :subject => 'foo', :content => 'bar'
+ post = Post.create(:subject => 'foo', :content => 'bar')
post.subject = nil
assert_nil post.subject
assert !post.valid?
+ post.subject = 'stashed_foo'
+ assert_equal 'stashed_foo', post.subject
end
test 'translated class locale setting' do

0 comments on commit 86e7569

Please sign in to comment.