Skip to content

Commit

Permalink
clean a few things up
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Fuchs committed Dec 26, 2009
1 parent 803b8ec commit 86e7569
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
10 changes: 7 additions & 3 deletions lib/globalize/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 2 additions & 8 deletions lib/globalize/active_record/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 6 additions & 4 deletions test/active_record_test.rb
Original file line number Diff line number Diff line change
@@ -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!
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 86e7569

Please sign in to comment.