Permalink
Browse files

this version allows nice sorting without superfluous stupid associati…

…ons. tests included.
  • Loading branch information...
1 parent 0702ca0 commit e8c312534d493c519480dcee38540ed06f0eb483 @moskyt committed Jan 23, 2010
Showing with 54 additions and 15 deletions.
  1. +8 −15 lib/globalize/active_record.rb
  2. +5 −0 test/data/models.rb
  3. +9 −0 test/data/schema.rb
  4. +32 −0 test/sorting_scopes_test.rb
View
23 lib/globalize/active_record.rb
@@ -66,23 +66,16 @@ def translates(*attr_names)
}
attr_names.each { |attr_name| translated_attr_accessor(attr_name) }
-
- locale_list = self.locale_list rescue []
- unless locale_list.blank?
- locale_list.each do |lang_code|
- self.has_many :"#{self.name.underscore}_translations_#{lang_code}",
- :class_name => "#{self.name}::Translation",
- :conditions => "#{self.name.underscore}_translations.locale = '#{lang_code}'"
- end
-
- self.translated_attribute_names.each do |key|
- field = "#{self.name.underscore}_translations.#{key}"
- self.named_scope :"ascend_by_#{key}", lambda{ { :order => "#{field} ASC", :joins => :"#{self.name.underscore}_translations_#{I18n.locale}" } }
- self.named_scope :"descend_by_#{key}", lambda{ { :order => "#{field} DESC", :joins => :"#{self.name.underscore}_translations_#{I18n.locale}" } }
- end
+ end
+
+ def create_sorting_scopes
+ self.translated_attribute_names.each do |key|
+ field = "#{quoted_translation_table_name}.#{key}"
+ self.named_scope :"ascend_by_#{key}", lambda{ { :order => "#{field} ASC", :joins => :translations, :conditions => ["#{quoted_translation_table_name}.locale = ?", I18n.locale.to_s] } }
+ self.named_scope :"descend_by_#{key}", lambda{ { :order => "#{field} DESC", :joins => :translations, :conditions => ["#{quoted_translation_table_name}.locale = ?", I18n.locale.to_s] } }
end
end
-
+
def translates?
included_modules.include?(InstanceMethods)
end
View
5 test/data/models.rb
@@ -49,3 +49,8 @@ def do_reload
class Validatee < ActiveRecord::Base
translates :string
end
+
+class Sortable < ActiveRecord::Base
+ translates :title
+ create_sorting_scopes
+end
View
9 test/data/schema.rb
@@ -43,4 +43,13 @@
t.references :validatee
t.string :string
end
+
+ create_table :sortables, :force => true do |t|
+ end
+
+ create_table :sortable_translations, :force => true do |t|
+ t.string :locale
+ t.references :sortable
+ t.string :title
+ end
end
View
32 test/sorting_scopes_test.rb
@@ -0,0 +1,32 @@
+require File.expand_path(File.dirname(__FILE__) + '/test_helper')
+require File.expand_path(File.dirname(__FILE__) + '/data/models')
+
+# Higher level tests.
+
+class SortingScopesTest < ActiveSupport::TestCase
+ def setup
+ I18n.locale = :en
+ reset_db!
+ ActiveRecord::Base.locale = nil
+ end
+
+ test "sorting is performed correctly for well-defined translations" do
+ item1 = Sortable.create
+ item2 = Sortable.create
+ item3 = Sortable.create
+ I18n.locale = :en
+ item1.update_attribute(:title, 'alpha')
+ item2.update_attribute(:title, 'beta')
+ item3.update_attribute(:title, 'gamma')
+ I18n.locale = :cs
+ item1.update_attribute(:title, 'zetko')
+ item2.update_attribute(:title, 'tecko')
+ item3.update_attribute(:title, 'vecko')
+
+ I18n.locale = :en
+ assert_equal [item1, item2, item3], Sortable.ascend_by_title.all
+ I18n.locale = :cs
+ assert_equal [item2, item3, item1], Sortable.ascend_by_title.all
+ end
+
+end

0 comments on commit e8c3125

Please sign in to comment.