Permalink
Browse files

sorting scopes

  • Loading branch information...
1 parent 64886c7 commit 6f4399c4b48e7aafc31b5c9b91888cf8afa20550 @moskyt committed Jan 9, 2010
Showing with 14 additions and 0 deletions.
  1. +14 −0 lib/globalize/active_record.rb
@@ -66,6 +66,20 @@ def translates(*attr_names)
}
attr_names.each { |attr_name| translated_attr_accessor(attr_name) }
+
+ locale_list = translation_class.scoped(:select => 'DISTINCT locale').map(&:locale) rescue ['en']
+ 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|
+ join = :"#{self.name.underscore}_translations_#{I18n.locale}"
+ field = "#{self.name.underscore}_translations.#{key}"
+ self.named_scope :"ascend_by_#{key}", lambda{ { :order => "#{field} ASC", :joins => join } }
+ self.named_scope :"descend_by_#{key}", lambda{ { :order => "#{field} DESC", :joins => join } }
+ end
end
def translates?

0 comments on commit 6f4399c

Please sign in to comment.