Browse files

overloaded attributes and attribute_names which now include translate…

…d attributes as well. This enables things like to_xml to work out of the box
  • Loading branch information...
1 parent cd66e87 commit df011e1d2dd12287f473862ca931838a7c0d0903 @hukl hukl committed Apr 22, 2010
Showing with 28 additions and 0 deletions.
  1. +12 −0 lib/globalize/active_record.rb
  2. +16 −0 test/active_record_test.rb
View
12 lib/globalize/active_record.rb
@@ -154,6 +154,14 @@ def globalize
@globalize ||= Adapter.new self
end
+ def attributes
+ self.attribute_names.inject({}) do |attrs, name|
+ attrs[name] = read_attribute(name) ||
+ (globalize.fetch(I18n.locale, name) rescue nil)
+ attrs
+ end
+ end
+
def attributes=(attributes, *args)
if attributes.respond_to?(:delete) && locale = attributes.delete(:locale)
self.class.with_locale(locale) { super }
@@ -162,6 +170,10 @@ def attributes=(attributes, *args)
end
end
+ def attribute_names
+ translated_attribute_names.map(&:to_s) + super
+ end
+
def available_locales
translations.scoped(:select => 'DISTINCT locale').map(&:locale)
end
View
16 test/active_record_test.rb
@@ -442,6 +442,22 @@ class Baz < ActiveRecord::Base
User.expects(:reflect_on_all_validations => validations)
assert_equal [:name], User.required_attributes
end
+
+ test "attribute_names returns translated and regular attribute names" do
+ Post.create :subject => "foo", :content => "bar"
+ assert_equal Post.last.attribute_names.sort, %w[blog_id content id subject]
+ end
+
+ test "attributes returns translated and regular attributes" do
+ Post.create :subject => "foo", :content => "bar"
+ assert_equal Post.last.attributes.keys.sort, %w[blog_id content id subject]
+ end
+
+ test "to_xml includes translated fields" do
+ Post.create :subject => "foo", :content => "bar"
+ assert Post.last.to_xml =~ /subject/
+ assert Post.last.to_xml =~ /content/
+ end
end
# TODO error checking for fields that exist in main table, don't exist in

0 comments on commit df011e1

Please sign in to comment.