From 2c59d78cde32cfd6f6fcda1f9aa78f94b38e5930 Mon Sep 17 00:00:00 2001 From: hukl Date: Tue, 6 Oct 2009 20:54:47 +0200 Subject: updated globalize2 --- vendor/plugins/globalize2/Rakefile | 22 +++ .../lib/globalize/model/active_record.rb | 17 +- .../globalize/model/active_record/translated.rb | 86 +++++----- vendor/plugins/globalize2/test/all.rb | 2 + vendor/plugins/globalize2/test/data/models.rb | 29 ++++ .../globalize2/test/data/no_globalize_schema.rb | 4 +- vendor/plugins/globalize2/test/data/post.rb | 24 --- .../test/model/active_record/migration_test.rb | 82 ++++++++-- .../model/active_record/sti_translated_test.rb | 26 +-- .../test/model/active_record/translated_test.rb | 174 ++++++++++++--------- vendor/plugins/globalize2/test/test_helper.rb | 12 +- 11 files changed, 303 insertions(+), 175 deletions(-) create mode 100644 vendor/plugins/globalize2/Rakefile create mode 100644 vendor/plugins/globalize2/test/all.rb create mode 100644 vendor/plugins/globalize2/test/data/models.rb delete mode 100644 vendor/plugins/globalize2/test/data/post.rb (limited to 'vendor/plugins') diff --git a/vendor/plugins/globalize2/Rakefile b/vendor/plugins/globalize2/Rakefile new file mode 100644 index 0000000..bca49a1 --- /dev/null +++ b/vendor/plugins/globalize2/Rakefile @@ -0,0 +1,22 @@ +require 'rake' +require 'rake/testtask' +require 'rake/rdoctask' + +desc 'Default: run unit tests.' +task :default => :test + +desc 'Test the globalize2 plugin.' +Rake::TestTask.new(:test) do |t| + t.libs << 'lib' + t.pattern = 'test/**/*_test.rb' + t.verbose = true +end + +desc 'Generate documentation for the globalize2 plugin.' +Rake::RDocTask.new(:rdoc) do |rdoc| + rdoc.rdoc_dir = 'rdoc' + rdoc.title = 'Globalize2' + rdoc.options << '--line-numbers' << '--inline-source' + rdoc.rdoc_files.include('README') + rdoc.rdoc_files.include('lib/**/*.rb') +end diff --git a/vendor/plugins/globalize2/lib/globalize/model/active_record.rb b/vendor/plugins/globalize2/lib/globalize/model/active_record.rb index 9645842..9218054 100644 --- a/vendor/plugins/globalize2/lib/globalize/model/active_record.rb +++ b/vendor/plugins/globalize2/lib/globalize/model/active_record.rb @@ -6,15 +6,19 @@ require 'globalize/model/active_record/translated' module Globalize module Model module ActiveRecord - class << self + class << self def create_proxy_class(klass) - Object.const_set "#{klass.name}Translation", Class.new(::ActiveRecord::Base){ - belongs_to "#{klass.name.underscore}".intern - + module_names = klass.name.split('::') + klass_name = module_names.pop + target = module_names.empty? ? Object : module_names.join('::').constantize + + target.const_set "#{klass_name}Translation", Class.new(::ActiveRecord::Base) { + belongs_to "#{klass.name.underscore.gsub('/', '_')}".intern + def locale read_attribute(:locale).to_sym end - + def locale=(locale) write_attribute(:locale, locale.to_s) end @@ -26,6 +30,9 @@ module Globalize klass.send :define_method, attr_name, lambda { globalize.fetch self.class.locale, attr_name } + klass.send :define_method, "#{attr_name}_before_type_cast", lambda { + globalize.fetch self.class.locale, attr_name + } klass.send :define_method, "#{attr_name}=", lambda {|val| globalize.stash self.class.locale, attr_name, val self[attr_name] = val diff --git a/vendor/plugins/globalize2/lib/globalize/model/active_record/translated.rb b/vendor/plugins/globalize2/lib/globalize/model/active_record/translated.rb index f8fb230..4f75c8a 100644 --- a/vendor/plugins/globalize2/lib/globalize/model/active_record/translated.rb +++ b/vendor/plugins/globalize2/lib/globalize/model/active_record/translated.rb @@ -1,11 +1,11 @@ +require 'digest/sha1' + module Globalize module Model - class MigrationError < StandardError; end - class UntranslatedMigrationField < MigrationError; end class MigrationMissingTranslatedField < MigrationError; end class BadMigrationFieldType < MigrationError; end - + module ActiveRecord module Translated def self.included(base) @@ -20,10 +20,10 @@ module Globalize # Only set up once per class unless included_modules.include? InstanceMethods class_inheritable_accessor :globalize_options, :globalize_proxy - + include InstanceMethods extend ClassMethods - + self.globalize_proxy = Globalize::Model::ActiveRecord.create_proxy_class(self) has_many( :globalize_translations, @@ -33,40 +33,40 @@ module Globalize :foreign_key => class_name.foreign_key ) - after_save :update_globalize_record + after_save :update_globalize_record end self.globalize_options = options Globalize::Model::ActiveRecord.define_accessors(self, attr_names) - + # Import any callbacks that have been defined by extensions to Globalize2 # and run them. extend Callbacks - Callbacks.instance_methods.each {|cb| send cb } + Callbacks.instance_methods.each { |callback| send(callback) } end def locale=(locale) @@locale = locale end - + def locale (defined?(@@locale) && @@locale) || I18n.locale - end + end end # Dummy Callbacks module. Extensions to Globalize2 can insert methods into here # and they'll be called at the end of the translates class method. module Callbacks end - + # Extension to the has_many :globalize_translations association module Extensions def by_locales(locales) find :all, :conditions => { :locale => locales.map(&:to_s) } end end - - module ClassMethods + + module ClassMethods def method_missing(method, *args) if method.to_s =~ /^find_by_(\w+)$/ && globalize_options[:translated_attributes].include?($1.to_sym) find(:first, :joins => :globalize_translations, @@ -76,77 +76,87 @@ module Globalize super end end - + def create_translation_table!(fields) translated_fields = self.globalize_options[:translated_attributes] translated_fields.each do |f| raise MigrationMissingTranslatedField, "Missing translated field #{f}" unless fields[f] end + fields.each do |name, type| - unless translated_fields.member? name - raise UntranslatedMigrationField, "Can't migrate untranslated field: #{name}" - end - unless [ :string, :text ].member? type + if translated_fields.include?(name) && ![:string, :text].include?(type) raise BadMigrationFieldType, "Bad field type for #{name}, should be :string or :text" - end + end end - translation_table_name = self.name.underscore + '_translations' + self.connection.create_table(translation_table_name) do |t| t.references self.table_name.singularize t.string :locale fields.each do |name, type| t.column name, type end - t.timestamps + t.timestamps end + + self.connection.add_index(translation_table_name, "#{self.table_name.singularize}_id", :name => translation_index_name) + end + + def translation_table_name + self.name.underscore.gsub('/', '_') + '_translations' + end + + def translation_index_name + # FIXME what's the max size of an index name? + index_name = "index_#{translation_table_name}_on_#{self.table_name.singularize}_id" + index_name.size < 50 ? index_name : "index_#{Digest::SHA1.hexdigest(index_name)}" end def drop_translation_table! - translation_table_name = self.name.underscore + '_translations' + self.connection.remove_index(translation_table_name, :name => translation_index_name) self.connection.drop_table translation_table_name end - + private - + def i18n_attr(attribute_name) - self.base_class.name.underscore + "_translations.#{attribute_name}" - end + self.base_class.name.underscore.gsub('/', '_') + "_translations.#{attribute_name}" + end end - + module InstanceMethods def reload(options = nil) globalize.clear - + # clear all globalized attributes # TODO what's the best way to handle this? self.class.globalize_options[:translated_attributes].each do |attr| - @attributes.delete attr.to_s + @attributes.delete(attr.to_s) end - - super options + + super(options) end - + def globalize @globalize ||= Adapter.new self end - + def update_globalize_record globalize.update_translations! end - + def translated_locales - globalize_translations.scoped(:select => 'DISTINCT locale').map {|gt| gt.locale.to_sym } + globalize_translations.scoped(:select => 'DISTINCT locale').map do |translation| + translation.locale.to_sym + end end - + def set_translations options options.keys.each do |key| - translation = globalize_translations.find_by_locale(key.to_s) || globalize_translations.build(:locale => key.to_s) translation.update_attributes!(options[key]) end end - end end end diff --git a/vendor/plugins/globalize2/test/all.rb b/vendor/plugins/globalize2/test/all.rb new file mode 100644 index 0000000..ff467a1 --- /dev/null +++ b/vendor/plugins/globalize2/test/all.rb @@ -0,0 +1,2 @@ +files = Dir[File.dirname(__FILE__) + '/**/*_test.rb'] +files.each { |file| require file } \ No newline at end of file diff --git a/vendor/plugins/globalize2/test/data/models.rb b/vendor/plugins/globalize2/test/data/models.rb new file mode 100644 index 0000000..f6dab90 --- /dev/null +++ b/vendor/plugins/globalize2/test/data/models.rb @@ -0,0 +1,29 @@ +class Post < ActiveRecord::Base + translates :subject, :content + validates_presence_of :subject +end + +class Blog < ActiveRecord::Base + has_many :posts, :order => 'id ASC' +end + +class Parent < ActiveRecord::Base + translates :content +end + +class Child < Parent +end + +class Comment < ActiveRecord::Base + validates_presence_of :content + belongs_to :post +end + +class TranslatedComment < Comment + translates :content +end + +class UltraLongModelNameWithoutProper < ActiveRecord::Base + translates :subject, :content + validates_presence_of :subject +end \ No newline at end of file diff --git a/vendor/plugins/globalize2/test/data/no_globalize_schema.rb b/vendor/plugins/globalize2/test/data/no_globalize_schema.rb index 7cfaf69..379455d 100644 --- a/vendor/plugins/globalize2/test/data/no_globalize_schema.rb +++ b/vendor/plugins/globalize2/test/data/no_globalize_schema.rb @@ -1,5 +1,5 @@ # This schema creates tables without columns for the translated fields -ActiveRecord::Schema.define do +ActiveRecord::Schema.define do create_table :blogs, :force => true do |t| t.string :name end @@ -8,4 +8,4 @@ ActiveRecord::Schema.define do t.references :blog end end - + diff --git a/vendor/plugins/globalize2/test/data/post.rb b/vendor/plugins/globalize2/test/data/post.rb deleted file mode 100644 index 6673dc4..0000000 --- a/vendor/plugins/globalize2/test/data/post.rb +++ /dev/null @@ -1,24 +0,0 @@ -class Post < ActiveRecord::Base - translates :subject, :content - validates_presence_of :subject -end - -class Blog < ActiveRecord::Base - has_many :posts, :order => 'id ASC' -end - -class Parent < ActiveRecord::Base - translates :content -end - -class Child < Parent -end - -class Comment < ActiveRecord::Base - validates_presence_of :content - belongs_to :post -end - -class TranslatedComment < Comment - translates :content -end \ No newline at end of file diff --git a/vendor/plugins/globalize2/test/model/active_record/migration_test.rb b/vendor/plugins/globalize2/test/model/active_record/migration_test.rb index c539fb6..eb6533d 100644 --- a/vendor/plugins/globalize2/test/model/active_record/migration_test.rb +++ b/vendor/plugins/globalize2/test/model/active_record/migration_test.rb @@ -6,17 +6,19 @@ require 'globalize/model/active_record' ActiveRecord::Base.send(:include, Globalize::Model::ActiveRecord::Translated) # Load Post model -require File.join( File.dirname(__FILE__), '..', '..', 'data', 'post' ) +require File.join( File.dirname(__FILE__), '..', '..', 'data', 'models' ) class MigrationTest < ActiveSupport::TestCase def setup reset_db! File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'data', 'no_globalize_schema.rb')) end - + test 'globalize table added' do assert !Post.connection.table_exists?( :post_translations ) + assert !Post.connection.index_exists?( :post_translations, :post_id ) Post.create_translation_table! :subject => :string, :content => :text - assert Post.connection.table_exists?( :post_translations ) + assert Post.connection.table_exists?( :post_translations ) + assert Post.connection.index_exists?( :post_translations, :post_id ) columns = Post.connection.columns( :post_translations ) assert locale = columns.detect {|c| c.name == 'locale' } assert_equal :string, locale.type @@ -31,43 +33,93 @@ class MigrationTest < ActiveSupport::TestCase assert updated_at = columns.detect {|c| c.name == 'updated_at' } assert_equal :datetime, updated_at.type end - + test 'globalize table dropped' do assert !Post.connection.table_exists?( :post_translations ) + assert !Post.connection.index_exists?( :post_translations, :post_id ) Post.create_translation_table! :subject => :string, :content => :text - assert Post.connection.table_exists?( :post_translations ) + assert Post.connection.table_exists?( :post_translations ) + assert Post.connection.index_exists?( :post_translations, :post_id ) Post.drop_translation_table! assert !Post.connection.table_exists?( :post_translations ) + assert !Post.connection.index_exists?( :post_translations, :post_id ) end - test 'exception on untranslated field inputs' do - assert_raise Globalize::Model::UntranslatedMigrationField do - Post.create_translation_table! :subject => :string, :content => :text, :bogus => :string - end - end - test 'exception on missing field inputs' do assert_raise Globalize::Model::MigrationMissingTranslatedField do Post.create_translation_table! :content => :text end end - + test 'exception on bad input type' do assert_raise Globalize::Model::BadMigrationFieldType do Post.create_translation_table! :subject => :string, :content => :integer end end - + + test "exception on bad input type isn't raised for untranslated fields" do + assert_nothing_raised do + Post.create_translation_table! :subject => :string, :content => :string, :views_count => :integer + end + end + test 'create_translation_table! should not be called on non-translated models' do assert_raise NoMethodError do - Blog.create_translation_table! :name => :string + Blog.create_translation_table! :name => :string end end test 'drop_translation_table! should not be called on non-translated models' do assert_raise NoMethodError do - Blog.drop_translation_table! + Blog.drop_translation_table! + end + end + + test "translation_index_name returns a readable index name when it's not longer than 50 characters" do + assert_equal 'index_post_translations_on_post_id', Post.send(:translation_index_name) + end + + test "translation_index_name returns a hashed index name when it's longer than 50 characters" do + class UltraLongModelNameWithoutProper < ActiveRecord::Base + translates :foo end + expected = 'index_44eba0f057e01a590ffccd0b8a3b5c78979539cd' + actual = UltraLongModelNameWithoutProper.send(:translation_index_name) + + assert_equal expected, actual + end + + test 'globalize table added when table has long name' do + UltraLongModelNameWithoutProper.create_translation_table!( + :subject => :string, :content => :text + ) + + assert UltraLongModelNameWithoutProper.connection.table_exists?( + :ultra_long_model_name_without_proper_translations + ) + assert UltraLongModelNameWithoutProper.connection.index_exists?( + :ultra_long_model_name_without_proper_translations, + :name => UltraLongModelNameWithoutProper.send( + :translation_index_name + ) + ) + end + + test 'globalize table dropped when table has long name' do + UltraLongModelNameWithoutProper.create_translation_table!( + :subject => :string, :content => :text + ) + + UltraLongModelNameWithoutProper.drop_translation_table! + + assert !UltraLongModelNameWithoutProper.connection.table_exists?( + :ultra_long_model_name_without_proper_translations + ) + + assert !UltraLongModelNameWithoutProper.connection.index_exists?( + :ultra_long_model_name_without_proper_translations, + :ultra_long_model_name_without_proper_id + ) end end \ No newline at end of file diff --git a/vendor/plugins/globalize2/test/model/active_record/sti_translated_test.rb b/vendor/plugins/globalize2/test/model/active_record/sti_translated_test.rb index 14d7d0f..6980ba0 100644 --- a/vendor/plugins/globalize2/test/model/active_record/sti_translated_test.rb +++ b/vendor/plugins/globalize2/test/model/active_record/sti_translated_test.rb @@ -6,17 +6,17 @@ require 'globalize/model/active_record' ActiveRecord::Base.send(:include, Globalize::Model::ActiveRecord::Translated) # Load Post model -require File.join( File.dirname(__FILE__), '..', '..', 'data', 'post' ) +require File.join( File.dirname(__FILE__), '..', '..', 'data', 'models' ) class StiTranslatedTest < ActiveSupport::TestCase def setup I18n.locale = :'en-US' - I18n.fallbacks.clear + I18n.fallbacks.clear reset_db! File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'data', 'schema.rb')) end - + def teardown - I18n.fallbacks.clear + I18n.fallbacks.clear end test "works with simple dynamic finders" do @@ -34,7 +34,7 @@ class StiTranslatedTest < ActiveSupport::TestCase child.content = 'baz' assert_member 'content', child.changed end - + test 'change attribute on globalized model after locale switching' do child = Child.create :content => 'foo' assert_equal [], child.changed @@ -46,17 +46,17 @@ class StiTranslatedTest < ActiveSupport::TestCase test 'fallbacks with lots of locale switching' do I18n.fallbacks.map :'de-DE' => [ :'en-US' ] child = Child.create :content => 'foo' - + I18n.locale = :'de-DE' assert_equal 'foo', child.content - + I18n.locale = :'en-US' child.update_attribute :content, 'bar' - + I18n.locale = :'de-DE' assert_equal 'bar', child.content end - + test "saves all locales, even after locale switching" do child = Child.new :content => 'foo' I18n.locale = 'de-DE' @@ -66,10 +66,10 @@ class StiTranslatedTest < ActiveSupport::TestCase child.save I18n.locale = 'en-US' child = Child.first - assert_equal 'foo', child.content + assert_equal 'foo', child.content I18n.locale = 'de-DE' - assert_equal 'bar', child.content + assert_equal 'bar', child.content I18n.locale = 'he-IL' - assert_equal 'baz', child.content - end + assert_equal 'baz', child.content + end end \ No newline at end of file diff --git a/vendor/plugins/globalize2/test/model/active_record/translated_test.rb b/vendor/plugins/globalize2/test/model/active_record/translated_test.rb index 3e9ea00..f54894e 100644 --- a/vendor/plugins/globalize2/test/model/active_record/translated_test.rb +++ b/vendor/plugins/globalize2/test/model/active_record/translated_test.rb @@ -6,25 +6,25 @@ require 'globalize/model/active_record' ActiveRecord::Base.send(:include, Globalize::Model::ActiveRecord::Translated) # Load Post model -require File.join( File.dirname(__FILE__), '..', '..', 'data', 'post' ) +require File.join( File.dirname(__FILE__), '..', '..', 'data', 'models' ) class TranslatedTest < ActiveSupport::TestCase def setup I18n.locale = :'en-US' - I18n.fallbacks.clear + I18n.fallbacks.clear reset_db! File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'data', 'schema.rb')) ActiveRecord::Base.locale = nil end - + def teardown - I18n.fallbacks.clear + I18n.fallbacks.clear end test "modifiying translated fields" do post = Post.create :subject => 'foo' assert_equal 'foo', post.subject post.subject = 'bar' - assert_equal 'bar', post.subject + assert_equal 'bar', post.subject end test "modifiying translated fields while switching locales" do @@ -38,7 +38,7 @@ class TranslatedTest < ActiveSupport::TestCase I18n.locale = :'de-DE' post.subject = 'bar' end - + test "has post_translations" do post = Post.create assert_nothing_raised { post.globalize_translations } @@ -49,9 +49,9 @@ class TranslatedTest < ActiveSupport::TestCase post = Post.create :subject => 'foo' assert_equal 1, post.globalize_translations.size I18n.locale = :en - assert_equal 1, post.globalize_translations.size + assert_equal 1, post.globalize_translations.size end - + test "returns the value passed to :subject" do post = Post.new assert_equal 'foo', (post.subject = 'foo') @@ -59,12 +59,12 @@ class TranslatedTest < ActiveSupport::TestCase test "translates subject and content into en-US" do post = Post.create :subject => 'foo', :content => 'bar' - assert_equal 'foo', post.subject - assert_equal 'bar', post.content + assert_equal 'foo', post.subject + assert_equal 'bar', post.content assert post.save post.reload - assert_equal 'foo', post.subject - assert_equal 'bar', post.content + assert_equal 'foo', post.subject + assert_equal 'bar', post.content end test "finds a German post" do @@ -73,9 +73,9 @@ class TranslatedTest < ActiveSupport::TestCase post = Post.first post.subject = 'baz (de)' post.save - assert_equal 'baz (de)', Post.first.subject + assert_equal 'baz (de)', Post.first.subject I18n.locale = :'en-US' - assert_equal 'foo (en)', Post.first.subject + assert_equal 'foo (en)', Post.first.subject end test "saves an English post and loads test correctly" do @@ -83,23 +83,23 @@ class TranslatedTest < ActiveSupport::TestCase post = Post.create :subject => 'foo', :content => 'bar' assert post.save post = Post.first - assert_equal 'foo', post.subject - assert_equal 'bar', post.content + assert_equal 'foo', post.subject + assert_equal 'bar', post.content end test "updates an attribute" do post = Post.create :subject => 'foo', :content => 'bar' post.update_attribute :subject, 'baz' - assert_equal 'baz', Post.first.subject + assert_equal 'baz', Post.first.subject end test "update_attributes failure" do post = Post.create :subject => 'foo', :content => 'bar' assert !post.update_attributes( { :subject => '' } ) assert_nil post.reload.attributes['subject'] - assert_equal 'foo', post.subject + assert_equal 'foo', post.subject end - + test "validates presence of :subject" do post = Post.new assert !post.save @@ -115,35 +115,35 @@ class TranslatedTest < ActiveSupport::TestCase post.save I18n.locale = 'en-US' post = Post.first - assert_equal 'foo', post.subject + assert_equal 'foo', post.subject I18n.locale = 'de-DE' - assert_equal 'bar', post.subject + assert_equal 'bar', post.subject end test "keeping one field in new locale when other field is changed" do - I18n.fallbacks.map 'de-DE' => [ 'en-US' ] + I18n.fallbacks.map 'de-DE' => [ 'en-US' ] post = Post.create :subject => 'foo' I18n.locale = 'de-DE' post.content = 'bar' - assert_equal 'foo', post.subject + assert_equal 'foo', post.subject end - + test "modifying non-required field in a new locale" do - I18n.fallbacks.map 'de-DE' => [ 'en-US' ] + I18n.fallbacks.map 'de-DE' => [ 'en-US' ] post = Post.create :subject => 'foo' I18n.locale = 'de-DE' post.content = 'bar' - assert post.save + assert post.save end - + test "returns the value for the correct locale, after locale switching, without saving" do post = Post.create :subject => 'foo' I18n.locale = 'de-DE' post.subject = 'bar' I18n.locale = 'en-US' - assert_equal 'foo', post.subject + assert_equal 'foo', post.subject I18n.locale = 'de-DE' - assert_equal 'bar', post.subject + assert_equal 'bar', post.subject end test "saves all locales, even after locale switching" do @@ -155,11 +155,11 @@ class TranslatedTest < ActiveSupport::TestCase post.save I18n.locale = 'en-US' post = Post.first - assert_equal 'foo', post.subject + assert_equal 'foo', post.subject I18n.locale = 'de-DE' - assert_equal 'bar', post.subject + assert_equal 'bar', post.subject I18n.locale = 'he-IL' - assert_equal 'baz', post.subject + assert_equal 'baz', post.subject end test "resolves a simple fallback" do @@ -170,8 +170,8 @@ class TranslatedTest < ActiveSupport::TestCase post.content = 'bar' post.save I18n.locale = 'de-DE' - assert_equal 'foo', post.subject - assert_equal 'bar', post.content + assert_equal 'foo', post.subject + assert_equal 'bar', post.content end test "resolves a simple fallback without reloading" do @@ -181,8 +181,8 @@ class TranslatedTest < ActiveSupport::TestCase post.subject = 'baz' post.content = 'bar' I18n.locale = 'de-DE' - assert_equal 'foo', post.subject - assert_equal 'bar', post.content + assert_equal 'foo', post.subject + assert_equal 'bar', post.content end test "resolves a complex fallback without reloading" do @@ -195,8 +195,8 @@ class TranslatedTest < ActiveSupport::TestCase post.subject = 'baz' post.content = 'bar' I18n.locale = 'de' - assert_equal 'foo', post.subject - assert_equal 'bar', post.content + assert_equal 'foo', post.subject + assert_equal 'bar', post.content end test "returns nil if no translations are found" do @@ -224,14 +224,14 @@ class TranslatedTest < ActiveSupport::TestCase I18n.locale = 'de-DE' assert_equal 'bar', blog.posts.last.subject end - + test "works with simple dynamic finders" do foo = Post.create :subject => 'foo' Post.create :subject => 'bar' post = Post.find_by_subject('foo') assert_equal foo, post end - + test 'change attribute on globalized model' do post = Post.create :subject => 'foo', :content => 'bar' assert_equal [], post.changed @@ -253,30 +253,30 @@ class TranslatedTest < ActiveSupport::TestCase test 'fallbacks with lots of locale switching' do I18n.fallbacks.map :'de-DE' => [ :'en-US' ] post = Post.create :subject => 'foo' - + I18n.locale = :'de-DE' assert_equal 'foo', post.subject - + I18n.locale = :'en-US' post.update_attribute :subject, 'bar' - + I18n.locale = :'de-DE' assert_equal 'bar', post.subject end - + test 'reload' do post = Post.create :subject => 'foo', :content => 'bar' post.subject = 'baz' - assert_equal 'foo', post.reload.subject + assert_equal 'foo', post.reload.subject end - + test 'complex writing and stashing' do post = Post.create :subject => 'foo', :content => 'bar' post.subject = nil assert_nil post.subject - assert !post.valid? + assert !post.valid? end - + test 'translated class locale setting' do assert ActiveRecord::Base.respond_to?(:locale) assert_equal :'en-US', I18n.locale @@ -291,11 +291,11 @@ class TranslatedTest < ActiveSupport::TestCase assert_equal :fr, I18n.locale assert_equal :es, ActiveRecord::Base.locale end - + test "untranslated class responds to locale" do assert Blog.respond_to?(:locale) end - + test "to ensure locales in different classes are the same" do ActiveRecord::Base.locale = :de assert_equal :de, ActiveRecord::Base.locale @@ -304,30 +304,30 @@ class TranslatedTest < ActiveSupport::TestCase assert_equal :es, ActiveRecord::Base.locale assert_equal :es, Parent.locale end - + test "attribute saving goes by content locale and not global locale" do ActiveRecord::Base.locale = :de assert_equal :'en-US', I18n.locale Post.create :subject => 'foo' assert_equal :de, Post.first.globalize_translations.first.locale end - + test "attribute loading goes by content locale and not global locale" do post = Post.create :subject => 'foo' assert_equal :'en-US', ActiveRecord::Base.locale ActiveRecord::Base.locale = :de assert_equal :'en-US', I18n.locale post.update_attribute :subject, 'foo [de]' - assert_equal 'foo [de]', Post.first.subject + assert_equal 'foo [de]', Post.first.subject ActiveRecord::Base.locale = :'en-US' - assert_equal 'foo', Post.first.subject + assert_equal 'foo', Post.first.subject end test "access content locale before setting" do Globalize::Model::ActiveRecord::Translated::ActMethods.class_eval "remove_class_variable(:@@locale)" assert_nothing_raised { ActiveRecord::Base.locale } end - + test "translated_locales" do Post.locale = :de post = Post.create :subject => 'foo' @@ -338,121 +338,141 @@ class TranslatedTest < ActiveSupport::TestCase assert_equal [ :de, :es, :fr ], post.translated_locales assert_equal [ :de, :es, :fr ], Post.first.translated_locales end - + test "including globalize_translations" do I18n.locale = :de Post.create :subject => "Foo1", :content => "Bar1" Post.create :subject => "Foo2", :content => "Bar2" - + class << Post def tranlsations_included self.all(:include => :globalize_translations) end end - + default = Post.all.map {|x| [x.subject, x.content]} with_include = Post.tranlsations_included.map {|x| [x.subject, x.content]} assert_equal default, with_include end - + test "setting multiple translations at once with options hash" do Post.locale = :de post = Post.create :subject => "foo1", :content => "foo1" Post.locale = :en post.update_attributes( :subject => "bar1", :content => "bar1" ) - + options = { :de => {:subject => "foo2", :content => "foo2"}, :en => {:subject => "bar2", :content => "bar2"} } post.set_translations options post.reload - + assert ["bar2", "bar2"], [post.subject, post.content] Post.locale = :de assert ["foo2", "foo2"], [post.subject, post.content] end - + test "setting only one translation with set_translations" do Post.locale = :de post = Post.create :subject => "foo1", :content => "foo1" Post.locale = :en post.update_attributes( :subject => "bar1", :content => "bar1" ) - + options = { :en => {:subject => "bar2", :content => "bar2"} } post.set_translations options post.reload - + assert ["bar2", "bar2"], [post.subject, post.content] Post.locale = :de assert ["foo1", "foo1"], [post.subject, post.content] end - + test "setting only selected attributes with set_translations" do Post.locale = :de post = Post.create :subject => "foo1", :content => "foo1" Post.locale = :en post.update_attributes( :subject => "bar1", :content => "bar1" ) - + options = { :de => {:content => "foo2"}, :en => {:subject => "bar2"} } post.set_translations options post.reload - + assert ["bar2", "bar1"], [post.subject, post.content] Post.locale = :de assert ["foo1", "foo2"], [post.subject, post.content] end - + test "setting invalid attributes raises ArgumentError" do Post.locale = :de post = Post.create :subject => "foo1", :content => "foo1" Post.locale = :en post.update_attributes( :subject => "bar1", :content => "bar1" ) - + options = { :de => {:fake => "foo2"} } exception = assert_raise(ActiveRecord::UnknownAttributeError) do post.set_translations options end assert_equal "unknown attribute: fake", exception.message end - + test "reload accepting find options" do p = Post.create :subject => "Foo", :content => "Bar" assert p.reload(:readonly => true, :lock => true) assert_raise(ArgumentError) { p.reload(:foo => :bar) } end - + test "dependent destroy of translation" do p = Post.create :subject => "Foo", :content => "Bar" assert_equal 1, PostTranslation.count p.destroy assert_equal 0, PostTranslation.count end - + test "translating subclass of untranslated comment model" do translated_comment = TranslatedComment.create(:post => @post) assert_nothing_raised { translated_comment.globalize_translations } end - + test "modifiying translated comments works as expected" do I18n.locale = :en translated_comment = TranslatedComment.create(:post => @post, :content => 'foo') assert_equal 'foo', translated_comment.content - + I18n.locale = :de translated_comment.content = 'bar' assert translated_comment.save assert_equal 'bar', translated_comment.content - + I18n.locale = :en assert_equal 'foo', translated_comment.content - + assert_equal 2, translated_comment.globalize_translations.size end + + test "can create a proxy class for a namespaced model" do + module Foo + module Bar + class Baz < ActiveRecord::Base + translates :bumm + end + end + end + end + + test "attribute translated before type cast" do + Post.locale = :en + post = Post.create :subject => 'foo', :content => 'bar' + Post.locale = :de + post.update_attribute :subject, "German foo" + assert_equal 'German foo', post.subject_before_type_cast + Post.locale = :en + assert_equal 'foo', post.subject_before_type_cast + end end # TODO should validate_presence_of take fallbacks into account? maybe we need # an extra validation call, or more options for validate_presence_of. # TODO error checking for fields that exist in main table, don't exist in # proxy table, aren't strings or text -# +# # TODO allow finding by translated attributes in conditions? # TODO generate advanced dynamic finders? diff --git a/vendor/plugins/globalize2/test/test_helper.rb b/vendor/plugins/globalize2/test/test_helper.rb index 956b352..3a1c8c4 100644 --- a/vendor/plugins/globalize2/test/test_helper.rb +++ b/vendor/plugins/globalize2/test/test_helper.rb @@ -11,7 +11,7 @@ class ActiveSupport::TestCase ::ActiveRecord::Migration.verbose = false # Quiet down the migration engine ::ActiveRecord::Base.establish_connection({ :adapter => 'sqlite3', - :dbfile => ':memory:' + :database => ':memory:' }) ::ActiveRecord::Base.silence do load schema_path @@ -23,4 +23,14 @@ class ActiveSupport::TestCase arr.member? item end end +end + +module ActiveRecord + module ConnectionAdapters + class AbstractAdapter + def index_exists?(table_name, column_name) + indexes(table_name).any? { |index| index.name == index_name(table_name, column_name) } + end + end + end end \ No newline at end of file -- cgit v1.3