summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhukl <contact@smyck.org>2009-10-06 20:54:47 +0200
committerhukl <contact@smyck.org>2009-10-06 20:54:47 +0200
commit2c59d78cde32cfd6f6fcda1f9aa78f94b38e5930 (patch)
tree23935202a8c45ae1997739d4173a3d9559e080e5
parentcd6ebe068cf42badc07ea0d803476ef4b1be3177 (diff)
updated globalize2
-rw-r--r--vendor/plugins/globalize2/Rakefile22
-rw-r--r--vendor/plugins/globalize2/lib/globalize/model/active_record.rb17
-rw-r--r--vendor/plugins/globalize2/lib/globalize/model/active_record/translated.rb86
-rw-r--r--vendor/plugins/globalize2/test/all.rb2
-rw-r--r--vendor/plugins/globalize2/test/data/models.rb (renamed from vendor/plugins/globalize2/test/data/post.rb)5
-rw-r--r--vendor/plugins/globalize2/test/data/no_globalize_schema.rb4
-rw-r--r--vendor/plugins/globalize2/test/model/active_record/migration_test.rb82
-rw-r--r--vendor/plugins/globalize2/test/model/active_record/sti_translated_test.rb26
-rw-r--r--vendor/plugins/globalize2/test/model/active_record/translated_test.rb174
-rw-r--r--vendor/plugins/globalize2/test/test_helper.rb12
10 files changed, 279 insertions, 151 deletions
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 @@
1require 'rake'
2require 'rake/testtask'
3require 'rake/rdoctask'
4
5desc 'Default: run unit tests.'
6task :default => :test
7
8desc 'Test the globalize2 plugin.'
9Rake::TestTask.new(:test) do |t|
10 t.libs << 'lib'
11 t.pattern = 'test/**/*_test.rb'
12 t.verbose = true
13end
14
15desc 'Generate documentation for the globalize2 plugin.'
16Rake::RDocTask.new(:rdoc) do |rdoc|
17 rdoc.rdoc_dir = 'rdoc'
18 rdoc.title = 'Globalize2'
19 rdoc.options << '--line-numbers' << '--inline-source'
20 rdoc.rdoc_files.include('README')
21 rdoc.rdoc_files.include('lib/**/*.rb')
22end
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'
6module Globalize 6module Globalize
7 module Model 7 module Model
8 module ActiveRecord 8 module ActiveRecord
9 class << self 9 class << self
10 def create_proxy_class(klass) 10 def create_proxy_class(klass)
11 Object.const_set "#{klass.name}Translation", Class.new(::ActiveRecord::Base){ 11 module_names = klass.name.split('::')
12 belongs_to "#{klass.name.underscore}".intern 12 klass_name = module_names.pop
13 13 target = module_names.empty? ? Object : module_names.join('::').constantize
14
15 target.const_set "#{klass_name}Translation", Class.new(::ActiveRecord::Base) {
16 belongs_to "#{klass.name.underscore.gsub('/', '_')}".intern
17
14 def locale 18 def locale
15 read_attribute(:locale).to_sym 19 read_attribute(:locale).to_sym
16 end 20 end
17 21
18 def locale=(locale) 22 def locale=(locale)
19 write_attribute(:locale, locale.to_s) 23 write_attribute(:locale, locale.to_s)
20 end 24 end
@@ -26,6 +30,9 @@ module Globalize
26 klass.send :define_method, attr_name, lambda { 30 klass.send :define_method, attr_name, lambda {
27 globalize.fetch self.class.locale, attr_name 31 globalize.fetch self.class.locale, attr_name
28 } 32 }
33 klass.send :define_method, "#{attr_name}_before_type_cast", lambda {
34 globalize.fetch self.class.locale, attr_name
35 }
29 klass.send :define_method, "#{attr_name}=", lambda {|val| 36 klass.send :define_method, "#{attr_name}=", lambda {|val|
30 globalize.stash self.class.locale, attr_name, val 37 globalize.stash self.class.locale, attr_name, val
31 self[attr_name] = val 38 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 @@
1require 'digest/sha1'
2
1module Globalize 3module Globalize
2 module Model 4 module Model
3
4 class MigrationError < StandardError; end 5 class MigrationError < StandardError; end
5 class UntranslatedMigrationField < MigrationError; end
6 class MigrationMissingTranslatedField < MigrationError; end 6 class MigrationMissingTranslatedField < MigrationError; end
7 class BadMigrationFieldType < MigrationError; end 7 class BadMigrationFieldType < MigrationError; end
8 8
9 module ActiveRecord 9 module ActiveRecord
10 module Translated 10 module Translated
11 def self.included(base) 11 def self.included(base)
@@ -20,10 +20,10 @@ module Globalize
20 # Only set up once per class 20 # Only set up once per class
21 unless included_modules.include? InstanceMethods 21 unless included_modules.include? InstanceMethods
22 class_inheritable_accessor :globalize_options, :globalize_proxy 22 class_inheritable_accessor :globalize_options, :globalize_proxy
23 23
24 include InstanceMethods 24 include InstanceMethods
25 extend ClassMethods 25 extend ClassMethods
26 26
27 self.globalize_proxy = Globalize::Model::ActiveRecord.create_proxy_class(self) 27 self.globalize_proxy = Globalize::Model::ActiveRecord.create_proxy_class(self)
28 has_many( 28 has_many(
29 :globalize_translations, 29 :globalize_translations,
@@ -33,40 +33,40 @@ module Globalize
33 :foreign_key => class_name.foreign_key 33 :foreign_key => class_name.foreign_key
34 ) 34 )
35 35
36 after_save :update_globalize_record 36 after_save :update_globalize_record
37 end 37 end
38 38
39 self.globalize_options = options 39 self.globalize_options = options
40 Globalize::Model::ActiveRecord.define_accessors(self, attr_names) 40 Globalize::Model::ActiveRecord.define_accessors(self, attr_names)
41 41
42 # Import any callbacks that have been defined by extensions to Globalize2 42 # Import any callbacks that have been defined by extensions to Globalize2
43 # and run them. 43 # and run them.
44 extend Callbacks 44 extend Callbacks
45 Callbacks.instance_methods.each {|cb| send cb } 45 Callbacks.instance_methods.each { |callback| send(callback) }
46 end 46 end
47 47
48 def locale=(locale) 48 def locale=(locale)
49 @@locale = locale 49 @@locale = locale
50 end 50 end
51 51
52 def locale 52 def locale
53 (defined?(@@locale) && @@locale) || I18n.locale 53 (defined?(@@locale) && @@locale) || I18n.locale
54 end 54 end
55 end 55 end
56 56
57 # Dummy Callbacks module. Extensions to Globalize2 can insert methods into here 57 # Dummy Callbacks module. Extensions to Globalize2 can insert methods into here
58 # and they'll be called at the end of the translates class method. 58 # and they'll be called at the end of the translates class method.
59 module Callbacks 59 module Callbacks
60 end 60 end
61 61
62 # Extension to the has_many :globalize_translations association 62 # Extension to the has_many :globalize_translations association
63 module Extensions 63 module Extensions
64 def by_locales(locales) 64 def by_locales(locales)
65 find :all, :conditions => { :locale => locales.map(&:to_s) } 65 find :all, :conditions => { :locale => locales.map(&:to_s) }
66 end 66 end
67 end 67 end
68 68
69 module ClassMethods 69 module ClassMethods
70 def method_missing(method, *args) 70 def method_missing(method, *args)
71 if method.to_s =~ /^find_by_(\w+)$/ && globalize_options[:translated_attributes].include?($1.to_sym) 71 if method.to_s =~ /^find_by_(\w+)$/ && globalize_options[:translated_attributes].include?($1.to_sym)
72 find(:first, :joins => :globalize_translations, 72 find(:first, :joins => :globalize_translations,
@@ -76,77 +76,87 @@ module Globalize
76 super 76 super
77 end 77 end
78 end 78 end
79 79
80 def create_translation_table!(fields) 80 def create_translation_table!(fields)
81 translated_fields = self.globalize_options[:translated_attributes] 81 translated_fields = self.globalize_options[:translated_attributes]
82 translated_fields.each do |f| 82 translated_fields.each do |f|
83 raise MigrationMissingTranslatedField, "Missing translated field #{f}" unless fields[f] 83 raise MigrationMissingTranslatedField, "Missing translated field #{f}" unless fields[f]
84 end 84 end
85
85 fields.each do |name, type| 86 fields.each do |name, type|
86 unless translated_fields.member? name 87 if translated_fields.include?(name) && ![:string, :text].include?(type)
87 raise UntranslatedMigrationField, "Can't migrate untranslated field: #{name}"
88 end
89 unless [ :string, :text ].member? type
90 raise BadMigrationFieldType, "Bad field type for #{name}, should be :string or :text" 88 raise BadMigrationFieldType, "Bad field type for #{name}, should be :string or :text"
91 end 89 end
92 end 90 end
93 translation_table_name = self.name.underscore + '_translations' 91
94 self.connection.create_table(translation_table_name) do |t| 92 self.connection.create_table(translation_table_name) do |t|
95 t.references self.table_name.singularize 93 t.references self.table_name.singularize
96 t.string :locale 94 t.string :locale
97 fields.each do |name, type| 95 fields.each do |name, type|
98 t.column name, type 96 t.column name, type
99 end 97 end
100 t.timestamps 98 t.timestamps
101 end 99 end
100
101 self.connection.add_index(translation_table_name, "#{self.table_name.singularize}_id", :name => translation_index_name)
102 end
103
104 def translation_table_name
105 self.name.underscore.gsub('/', '_') + '_translations'
106 end
107
108 def translation_index_name
109 # FIXME what's the max size of an index name?
110 index_name = "index_#{translation_table_name}_on_#{self.table_name.singularize}_id"
111 index_name.size < 50 ? index_name : "index_#{Digest::SHA1.hexdigest(index_name)}"
102 end 112 end
103 113
104 def drop_translation_table! 114 def drop_translation_table!
105 translation_table_name = self.name.underscore + '_translations' 115 self.connection.remove_index(translation_table_name, :name => translation_index_name)
106 self.connection.drop_table translation_table_name 116 self.connection.drop_table translation_table_name
107 end 117 end
108 118
109 private 119 private
110 120
111 def i18n_attr(attribute_name) 121 def i18n_attr(attribute_name)
112 self.base_class.name.underscore + "_translations.#{attribute_name}" 122 self.base_class.name.underscore.gsub('/', '_') + "_translations.#{attribute_name}"
113 end 123 end
114 end 124 end
115 125
116 module InstanceMethods 126 module InstanceMethods
117 def reload(options = nil) 127 def reload(options = nil)
118 globalize.clear 128 globalize.clear
119 129
120 # clear all globalized attributes 130 # clear all globalized attributes
121 # TODO what's the best way to handle this? 131 # TODO what's the best way to handle this?
122 self.class.globalize_options[:translated_attributes].each do |attr| 132 self.class.globalize_options[:translated_attributes].each do |attr|
123 @attributes.delete attr.to_s 133 @attributes.delete(attr.to_s)
124 end 134 end
125 135
126 super options 136 super(options)
127 end 137 end
128 138
129 def globalize 139 def globalize
130 @globalize ||= Adapter.new self 140 @globalize ||= Adapter.new self
131 end 141 end
132 142
133 def update_globalize_record 143 def update_globalize_record
134 globalize.update_translations! 144 globalize.update_translations!
135 end 145 end
136 146
137 def translated_locales 147 def translated_locales
138 globalize_translations.scoped(:select => 'DISTINCT locale').map {|gt| gt.locale.to_sym } 148 globalize_translations.scoped(:select => 'DISTINCT locale').map do |translation|
149 translation.locale.to_sym
150 end
139 end 151 end
140 152
141 def set_translations options 153 def set_translations options
142 options.keys.each do |key| 154 options.keys.each do |key|
143
144 translation = globalize_translations.find_by_locale(key.to_s) || 155 translation = globalize_translations.find_by_locale(key.to_s) ||
145 globalize_translations.build(:locale => key.to_s) 156 globalize_translations.build(:locale => key.to_s)
146 translation.update_attributes!(options[key]) 157 translation.update_attributes!(options[key])
147 end 158 end
148 end 159 end
149
150 end 160 end
151 end 161 end
152 end 162 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 @@
1files = Dir[File.dirname(__FILE__) + '/**/*_test.rb']
2files.each { |file| require file } \ No newline at end of file
diff --git a/vendor/plugins/globalize2/test/data/post.rb b/vendor/plugins/globalize2/test/data/models.rb
index 6673dc4..f6dab90 100644
--- a/vendor/plugins/globalize2/test/data/post.rb
+++ b/vendor/plugins/globalize2/test/data/models.rb
@@ -21,4 +21,9 @@ end
21 21
22class TranslatedComment < Comment 22class TranslatedComment < Comment
23 translates :content 23 translates :content
24end
25
26class UltraLongModelNameWithoutProper < ActiveRecord::Base
27 translates :subject, :content
28 validates_presence_of :subject
24end \ No newline at end of file 29end \ 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 @@
1# This schema creates tables without columns for the translated fields 1# This schema creates tables without columns for the translated fields
2ActiveRecord::Schema.define do 2ActiveRecord::Schema.define do
3 create_table :blogs, :force => true do |t| 3 create_table :blogs, :force => true do |t|
4 t.string :name 4 t.string :name
5 end 5 end
@@ -8,4 +8,4 @@ ActiveRecord::Schema.define do
8 t.references :blog 8 t.references :blog
9 end 9 end
10end 10end
11 11
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'
6ActiveRecord::Base.send(:include, Globalize::Model::ActiveRecord::Translated) 6ActiveRecord::Base.send(:include, Globalize::Model::ActiveRecord::Translated)
7 7
8# Load Post model 8# Load Post model
9require File.join( File.dirname(__FILE__), '..', '..', 'data', 'post' ) 9require File.join( File.dirname(__FILE__), '..', '..', 'data', 'models' )
10 10
11class MigrationTest < ActiveSupport::TestCase 11class MigrationTest < ActiveSupport::TestCase
12 def setup 12 def setup
13 reset_db! File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'data', 'no_globalize_schema.rb')) 13 reset_db! File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'data', 'no_globalize_schema.rb'))
14 end 14 end
15 15
16 test 'globalize table added' do 16 test 'globalize table added' do
17 assert !Post.connection.table_exists?( :post_translations ) 17 assert !Post.connection.table_exists?( :post_translations )
18 assert !Post.connection.index_exists?( :post_translations, :post_id )
18 Post.create_translation_table! :subject => :string, :content => :text 19 Post.create_translation_table! :subject => :string, :content => :text
19 assert Post.connection.table_exists?( :post_translations ) 20 assert Post.connection.table_exists?( :post_translations )
21 assert Post.connection.index_exists?( :post_translations, :post_id )
20 columns = Post.connection.columns( :post_translations ) 22 columns = Post.connection.columns( :post_translations )
21 assert locale = columns.detect {|c| c.name == 'locale' } 23 assert locale = columns.detect {|c| c.name == 'locale' }
22 assert_equal :string, locale.type 24 assert_equal :string, locale.type
@@ -31,43 +33,93 @@ class MigrationTest < ActiveSupport::TestCase
31 assert updated_at = columns.detect {|c| c.name == 'updated_at' } 33 assert updated_at = columns.detect {|c| c.name == 'updated_at' }
32 assert_equal :datetime, updated_at.type 34 assert_equal :datetime, updated_at.type
33 end 35 end
34 36
35 test 'globalize table dropped' do 37 test 'globalize table dropped' do
36 assert !Post.connection.table_exists?( :post_translations ) 38 assert !Post.connection.table_exists?( :post_translations )
39 assert !Post.connection.index_exists?( :post_translations, :post_id )
37 Post.create_translation_table! :subject => :string, :content => :text 40 Post.create_translation_table! :subject => :string, :content => :text
38 assert Post.connection.table_exists?( :post_translations ) 41 assert Post.connection.table_exists?( :post_translations )
42 assert Post.connection.index_exists?( :post_translations, :post_id )
39 Post.drop_translation_table! 43 Post.drop_translation_table!
40 assert !Post.connection.table_exists?( :post_translations ) 44 assert !Post.connection.table_exists?( :post_translations )
45 assert !Post.connection.index_exists?( :post_translations, :post_id )
41 end 46 end
42 47
43 test 'exception on untranslated field inputs' do
44 assert_raise Globalize::Model::UntranslatedMigrationField do
45 Post.create_translation_table! :subject => :string, :content => :text, :bogus => :string
46 end
47 end
48
49 test 'exception on missing field inputs' do 48 test 'exception on missing field inputs' do
50 assert_raise Globalize::Model::MigrationMissingTranslatedField do 49 assert_raise Globalize::Model::MigrationMissingTranslatedField do
51 Post.create_translation_table! :content => :text 50 Post.create_translation_table! :content => :text
52 end 51 end
53 end 52 end
54 53
55 test 'exception on bad input type' do 54 test 'exception on bad input type' do
56 assert_raise Globalize::Model::BadMigrationFieldType do 55 assert_raise Globalize::Model::BadMigrationFieldType do
57 Post.create_translation_table! :subject => :string, :content => :integer 56 Post.create_translation_table! :subject => :string, :content => :integer
58 end 57 end
59 end 58 end
60 59
60 test "exception on bad input type isn't raised for untranslated fields" do
61 assert_nothing_raised do
62 Post.create_translation_table! :subject => :string, :content => :string, :views_count => :integer
63 end
64 end
65
61 test 'create_translation_table! should not be called on non-translated models' do 66 test 'create_translation_table! should not be called on non-translated models' do
62 assert_raise NoMethodError do 67 assert_raise NoMethodError do
63 Blog.create_translation_table! :name => :string 68 Blog.create_translation_table! :name => :string
64 end 69 end
65 end 70 end
66 71
67 test 'drop_translation_table! should not be called on non-translated models' do 72 test 'drop_translation_table! should not be called on non-translated models' do
68 assert_raise NoMethodError do 73 assert_raise NoMethodError do
69 Blog.drop_translation_table! 74 Blog.drop_translation_table!
75 end
76 end
77
78 test "translation_index_name returns a readable index name when it's not longer than 50 characters" do
79 assert_equal 'index_post_translations_on_post_id', Post.send(:translation_index_name)
80 end
81
82 test "translation_index_name returns a hashed index name when it's longer than 50 characters" do
83 class UltraLongModelNameWithoutProper < ActiveRecord::Base
84 translates :foo
70 end 85 end
86 expected = 'index_44eba0f057e01a590ffccd0b8a3b5c78979539cd'
87 actual = UltraLongModelNameWithoutProper.send(:translation_index_name)
88
89 assert_equal expected, actual
90 end
91
92 test 'globalize table added when table has long name' do
93 UltraLongModelNameWithoutProper.create_translation_table!(
94 :subject => :string, :content => :text
95 )
96
97 assert UltraLongModelNameWithoutProper.connection.table_exists?(
98 :ultra_long_model_name_without_proper_translations
99 )
100 assert UltraLongModelNameWithoutProper.connection.index_exists?(
101 :ultra_long_model_name_without_proper_translations,
102 :name => UltraLongModelNameWithoutProper.send(
103 :translation_index_name
104 )
105 )
106 end
107
108 test 'globalize table dropped when table has long name' do
109 UltraLongModelNameWithoutProper.create_translation_table!(
110 :subject => :string, :content => :text
111 )
112
113 UltraLongModelNameWithoutProper.drop_translation_table!
114
115 assert !UltraLongModelNameWithoutProper.connection.table_exists?(
116 :ultra_long_model_name_without_proper_translations
117 )
118
119 assert !UltraLongModelNameWithoutProper.connection.index_exists?(
120 :ultra_long_model_name_without_proper_translations,
121 :ultra_long_model_name_without_proper_id
122 )
71 end 123 end
72 124
73end \ No newline at end of file 125end \ 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'
6ActiveRecord::Base.send(:include, Globalize::Model::ActiveRecord::Translated) 6ActiveRecord::Base.send(:include, Globalize::Model::ActiveRecord::Translated)
7 7
8# Load Post model 8# Load Post model
9require File.join( File.dirname(__FILE__), '..', '..', 'data', 'post' ) 9require File.join( File.dirname(__FILE__), '..', '..', 'data', 'models' )
10 10
11class StiTranslatedTest < ActiveSupport::TestCase 11class StiTranslatedTest < ActiveSupport::TestCase
12 def setup 12 def setup
13 I18n.locale = :'en-US' 13 I18n.locale = :'en-US'
14 I18n.fallbacks.clear 14 I18n.fallbacks.clear
15 reset_db! File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'data', 'schema.rb')) 15 reset_db! File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'data', 'schema.rb'))
16 end 16 end
17 17
18 def teardown 18 def teardown
19 I18n.fallbacks.clear 19 I18n.fallbacks.clear
20 end 20 end
21 21
22 test "works with simple dynamic finders" do 22 test "works with simple dynamic finders" do
@@ -34,7 +34,7 @@ class StiTranslatedTest < ActiveSupport::TestCase
34 child.content = 'baz' 34 child.content = 'baz'
35 assert_member 'content', child.changed 35 assert_member 'content', child.changed
36 end 36 end
37 37
38 test 'change attribute on globalized model after locale switching' do 38 test 'change attribute on globalized model after locale switching' do
39 child = Child.create :content => 'foo' 39 child = Child.create :content => 'foo'
40 assert_equal [], child.changed 40 assert_equal [], child.changed
@@ -46,17 +46,17 @@ class StiTranslatedTest < ActiveSupport::TestCase
46 test 'fallbacks with lots of locale switching' do 46 test 'fallbacks with lots of locale switching' do
47 I18n.fallbacks.map :'de-DE' => [ :'en-US' ] 47 I18n.fallbacks.map :'de-DE' => [ :'en-US' ]
48 child = Child.create :content => 'foo' 48 child = Child.create :content => 'foo'
49 49
50 I18n.locale = :'de-DE' 50 I18n.locale = :'de-DE'
51 assert_equal 'foo', child.content 51 assert_equal 'foo', child.content
52 52
53 I18n.locale = :'en-US' 53 I18n.locale = :'en-US'
54 child.update_attribute :content, 'bar' 54 child.update_attribute :content, 'bar'
55 55
56 I18n.locale = :'de-DE' 56 I18n.locale = :'de-DE'
57 assert_equal 'bar', child.content 57 assert_equal 'bar', child.content
58 end 58 end
59 59
60 test "saves all locales, even after locale switching" do 60 test "saves all locales, even after locale switching" do
61 child = Child.new :content => 'foo' 61 child = Child.new :content => 'foo'
62 I18n.locale = 'de-DE' 62 I18n.locale = 'de-DE'
@@ -66,10 +66,10 @@ class StiTranslatedTest < ActiveSupport::TestCase
66 child.save 66 child.save
67 I18n.locale = 'en-US' 67 I18n.locale = 'en-US'
68 child = Child.first 68 child = Child.first
69 assert_equal 'foo', child.content 69 assert_equal 'foo', child.content
70 I18n.locale = 'de-DE' 70 I18n.locale = 'de-DE'
71 assert_equal 'bar', child.content 71 assert_equal 'bar', child.content
72 I18n.locale = 'he-IL' 72 I18n.locale = 'he-IL'
73 assert_equal 'baz', child.content 73 assert_equal 'baz', child.content
74 end 74 end
75end \ No newline at end of file 75end \ 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'
6ActiveRecord::Base.send(:include, Globalize::Model::ActiveRecord::Translated) 6ActiveRecord::Base.send(:include, Globalize::Model::ActiveRecord::Translated)
7 7
8# Load Post model 8# Load Post model
9require File.join( File.dirname(__FILE__), '..', '..', 'data', 'post' ) 9require File.join( File.dirname(__FILE__), '..', '..', 'data', 'models' )
10 10
11class TranslatedTest < ActiveSupport::TestCase 11class TranslatedTest < ActiveSupport::TestCase
12 def setup 12 def setup
13 I18n.locale = :'en-US' 13 I18n.locale = :'en-US'
14 I18n.fallbacks.clear 14 I18n.fallbacks.clear
15 reset_db! File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'data', 'schema.rb')) 15 reset_db! File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'data', 'schema.rb'))
16 ActiveRecord::Base.locale = nil 16 ActiveRecord::Base.locale = nil
17 end 17 end
18 18
19 def teardown 19 def teardown
20 I18n.fallbacks.clear 20 I18n.fallbacks.clear
21 end 21 end
22 22
23 test "modifiying translated fields" do 23 test "modifiying translated fields" do
24 post = Post.create :subject => 'foo' 24 post = Post.create :subject => 'foo'
25 assert_equal 'foo', post.subject 25 assert_equal 'foo', post.subject
26 post.subject = 'bar' 26 post.subject = 'bar'
27 assert_equal 'bar', post.subject 27 assert_equal 'bar', post.subject
28 end 28 end
29 29
30 test "modifiying translated fields while switching locales" do 30 test "modifiying translated fields while switching locales" do
@@ -38,7 +38,7 @@ class TranslatedTest < ActiveSupport::TestCase
38 I18n.locale = :'de-DE' 38 I18n.locale = :'de-DE'
39 post.subject = 'bar' 39 post.subject = 'bar'
40 end 40 end
41 41
42 test "has post_translations" do 42 test "has post_translations" do
43 post = Post.create 43 post = Post.create
44 assert_nothing_raised { post.globalize_translations } 44 assert_nothing_raised { post.globalize_translations }
@@ -49,9 +49,9 @@ class TranslatedTest < ActiveSupport::TestCase
49 post = Post.create :subject => 'foo' 49 post = Post.create :subject => 'foo'
50 assert_equal 1, post.globalize_translations.size 50 assert_equal 1, post.globalize_translations.size
51 I18n.locale = :en 51 I18n.locale = :en
52 assert_equal 1, post.globalize_translations.size 52 assert_equal 1, post.globalize_translations.size
53 end 53 end
54 54
55 test "returns the value passed to :subject" do 55 test "returns the value passed to :subject" do
56 post = Post.new 56 post = Post.new
57 assert_equal 'foo', (post.subject = 'foo') 57 assert_equal 'foo', (post.subject = 'foo')
@@ -59,12 +59,12 @@ class TranslatedTest < ActiveSupport::TestCase
59 59
60 test "translates subject and content into en-US" do 60 test "translates subject and content into en-US" do
61 post = Post.create :subject => 'foo', :content => 'bar' 61 post = Post.create :subject => 'foo', :content => 'bar'
62 assert_equal 'foo', post.subject 62 assert_equal 'foo', post.subject
63 assert_equal 'bar', post.content 63 assert_equal 'bar', post.content
64 assert post.save 64 assert post.save
65 post.reload 65 post.reload
66 assert_equal 'foo', post.subject 66 assert_equal 'foo', post.subject
67 assert_equal 'bar', post.content 67 assert_equal 'bar', post.content
68 end 68 end
69 69
70 test "finds a German post" do 70 test "finds a German post" do
@@ -73,9 +73,9 @@ class TranslatedTest < ActiveSupport::TestCase
73 post = Post.first 73 post = Post.first
74 post.subject = 'baz (de)' 74 post.subject = 'baz (de)'
75 post.save 75 post.save
76 assert_equal 'baz (de)', Post.first.subject 76 assert_equal 'baz (de)', Post.first.subject
77 I18n.locale = :'en-US' 77 I18n.locale = :'en-US'
78 assert_equal 'foo (en)', Post.first.subject 78 assert_equal 'foo (en)', Post.first.subject
79 end 79 end
80 80
81 test "saves an English post and loads test correctly" do 81 test "saves an English post and loads test correctly" do
@@ -83,23 +83,23 @@ class TranslatedTest < ActiveSupport::TestCase
83 post = Post.create :subject => 'foo', :content => 'bar' 83 post = Post.create :subject => 'foo', :content => 'bar'
84 assert post.save 84 assert post.save
85 post = Post.first 85 post = Post.first
86 assert_equal 'foo', post.subject 86 assert_equal 'foo', post.subject
87 assert_equal 'bar', post.content 87 assert_equal 'bar', post.content
88 end 88 end
89 89
90 test "updates an attribute" do 90 test "updates an attribute" do
91 post = Post.create :subject => 'foo', :content => 'bar' 91 post = Post.create :subject => 'foo', :content => 'bar'
92 post.update_attribute :subject, 'baz' 92 post.update_attribute :subject, 'baz'
93 assert_equal 'baz', Post.first.subject 93 assert_equal 'baz', Post.first.subject
94 end 94 end
95 95
96 test "update_attributes failure" do 96 test "update_attributes failure" do
97 post = Post.create :subject => 'foo', :content => 'bar' 97 post = Post.create :subject => 'foo', :content => 'bar'
98 assert !post.update_attributes( { :subject => '' } ) 98 assert !post.update_attributes( { :subject => '' } )
99 assert_nil post.reload.attributes['subject'] 99 assert_nil post.reload.attributes['subject']
100 assert_equal 'foo', post.subject 100 assert_equal 'foo', post.subject
101 end 101 end
102 102
103 test "validates presence of :subject" do 103 test "validates presence of :subject" do
104 post = Post.new 104 post = Post.new
105 assert !post.save 105 assert !post.save
@@ -115,35 +115,35 @@ class TranslatedTest < ActiveSupport::TestCase
115 post.save 115 post.save
116 I18n.locale = 'en-US' 116 I18n.locale = 'en-US'
117 post = Post.first 117 post = Post.first
118 assert_equal 'foo', post.subject 118 assert_equal 'foo', post.subject
119 I18n.locale = 'de-DE' 119 I18n.locale = 'de-DE'
120 assert_equal 'bar', post.subject 120 assert_equal 'bar', post.subject
121 end 121 end
122 122
123 test "keeping one field in new locale when other field is changed" do 123 test "keeping one field in new locale when other field is changed" do
124 I18n.fallbacks.map 'de-DE' => [ 'en-US' ] 124 I18n.fallbacks.map 'de-DE' => [ 'en-US' ]
125 post = Post.create :subject => 'foo' 125 post = Post.create :subject => 'foo'
126 I18n.locale = 'de-DE' 126 I18n.locale = 'de-DE'
127 post.content = 'bar' 127 post.content = 'bar'
128 assert_equal 'foo', post.subject 128 assert_equal 'foo', post.subject
129 end 129 end
130 130
131 test "modifying non-required field in a new locale" do 131 test "modifying non-required field in a new locale" do
132 I18n.fallbacks.map 'de-DE' => [ 'en-US' ] 132 I18n.fallbacks.map 'de-DE' => [ 'en-US' ]
133 post = Post.create :subject => 'foo' 133 post = Post.create :subject => 'foo'
134 I18n.locale = 'de-DE' 134 I18n.locale = 'de-DE'
135 post.content = 'bar' 135 post.content = 'bar'
136 assert post.save 136 assert post.save
137 end 137 end
138 138
139 test "returns the value for the correct locale, after locale switching, without saving" do 139 test "returns the value for the correct locale, after locale switching, without saving" do
140 post = Post.create :subject => 'foo' 140 post = Post.create :subject => 'foo'
141 I18n.locale = 'de-DE' 141 I18n.locale = 'de-DE'
142 post.subject = 'bar' 142 post.subject = 'bar'
143 I18n.locale = 'en-US' 143 I18n.locale = 'en-US'
144 assert_equal 'foo', post.subject 144 assert_equal 'foo', post.subject
145 I18n.locale = 'de-DE' 145 I18n.locale = 'de-DE'
146 assert_equal 'bar', post.subject 146 assert_equal 'bar', post.subject
147 end 147 end
148 148
149 test "saves all locales, even after locale switching" do 149 test "saves all locales, even after locale switching" do
@@ -155,11 +155,11 @@ class TranslatedTest < ActiveSupport::TestCase
155 post.save 155 post.save
156 I18n.locale = 'en-US' 156 I18n.locale = 'en-US'
157 post = Post.first 157 post = Post.first
158 assert_equal 'foo', post.subject 158 assert_equal 'foo', post.subject
159 I18n.locale = 'de-DE' 159 I18n.locale = 'de-DE'
160 assert_equal 'bar', post.subject 160 assert_equal 'bar', post.subject
161 I18n.locale = 'he-IL' 161 I18n.locale = 'he-IL'
162 assert_equal 'baz', post.subject 162 assert_equal 'baz', post.subject
163 end 163 end
164 164
165 test "resolves a simple fallback" do 165 test "resolves a simple fallback" do
@@ -170,8 +170,8 @@ class TranslatedTest < ActiveSupport::TestCase
170 post.content = 'bar' 170 post.content = 'bar'
171 post.save 171 post.save
172 I18n.locale = 'de-DE' 172 I18n.locale = 'de-DE'
173 assert_equal 'foo', post.subject 173 assert_equal 'foo', post.subject
174 assert_equal 'bar', post.content 174 assert_equal 'bar', post.content
175 end 175 end
176 176
177 test "resolves a simple fallback without reloading" do 177 test "resolves a simple fallback without reloading" do
@@ -181,8 +181,8 @@ class TranslatedTest < ActiveSupport::TestCase
181 post.subject = 'baz' 181 post.subject = 'baz'
182 post.content = 'bar' 182 post.content = 'bar'
183 I18n.locale = 'de-DE' 183 I18n.locale = 'de-DE'
184 assert_equal 'foo', post.subject 184 assert_equal 'foo', post.subject
185 assert_equal 'bar', post.content 185 assert_equal 'bar', post.content
186 end 186 end
187 187
188 test "resolves a complex fallback without reloading" do 188 test "resolves a complex fallback without reloading" do
@@ -195,8 +195,8 @@ class TranslatedTest < ActiveSupport::TestCase
195 post.subject = 'baz' 195 post.subject = 'baz'
196 post.content = 'bar' 196 post.content = 'bar'
197 I18n.locale = 'de' 197 I18n.locale = 'de'
198 assert_equal 'foo', post.subject 198 assert_equal 'foo', post.subject
199 assert_equal 'bar', post.content 199 assert_equal 'bar', post.content
200 end 200 end
201 201
202 test "returns nil if no translations are found" do 202 test "returns nil if no translations are found" do
@@ -224,14 +224,14 @@ class TranslatedTest < ActiveSupport::TestCase
224 I18n.locale = 'de-DE' 224 I18n.locale = 'de-DE'
225 assert_equal 'bar', blog.posts.last.subject 225 assert_equal 'bar', blog.posts.last.subject
226 end 226 end
227 227
228 test "works with simple dynamic finders" do 228 test "works with simple dynamic finders" do
229 foo = Post.create :subject => 'foo' 229 foo = Post.create :subject => 'foo'
230 Post.create :subject => 'bar' 230 Post.create :subject => 'bar'
231 post = Post.find_by_subject('foo') 231 post = Post.find_by_subject('foo')
232 assert_equal foo, post 232 assert_equal foo, post
233 end 233 end
234 234
235 test 'change attribute on globalized model' do 235 test 'change attribute on globalized model' do
236 post = Post.create :subject => 'foo', :content => 'bar' 236 post = Post.create :subject => 'foo', :content => 'bar'
237 assert_equal [], post.changed 237 assert_equal [], post.changed
@@ -253,30 +253,30 @@ class TranslatedTest < ActiveSupport::TestCase
253 test 'fallbacks with lots of locale switching' do 253 test 'fallbacks with lots of locale switching' do
254 I18n.fallbacks.map :'de-DE' => [ :'en-US' ] 254 I18n.fallbacks.map :'de-DE' => [ :'en-US' ]
255 post = Post.create :subject => 'foo' 255 post = Post.create :subject => 'foo'
256 256
257 I18n.locale = :'de-DE' 257 I18n.locale = :'de-DE'
258 assert_equal 'foo', post.subject 258 assert_equal 'foo', post.subject
259 259
260 I18n.locale = :'en-US' 260 I18n.locale = :'en-US'
261 post.update_attribute :subject, 'bar' 261 post.update_attribute :subject, 'bar'
262 262
263 I18n.locale = :'de-DE' 263 I18n.locale = :'de-DE'
264 assert_equal 'bar', post.subject 264 assert_equal 'bar', post.subject
265 end 265 end
266 266
267 test 'reload' do 267 test 'reload' do
268 post = Post.create :subject => 'foo', :content => 'bar' 268 post = Post.create :subject => 'foo', :content => 'bar'
269 post.subject = 'baz' 269 post.subject = 'baz'
270 assert_equal 'foo', post.reload.subject 270 assert_equal 'foo', post.reload.subject
271 end 271 end
272 272
273 test 'complex writing and stashing' do 273 test 'complex writing and stashing' do
274 post = Post.create :subject => 'foo', :content => 'bar' 274 post = Post.create :subject => 'foo', :content => 'bar'
275 post.subject = nil 275 post.subject = nil
276 assert_nil post.subject 276 assert_nil post.subject
277 assert !post.valid? 277 assert !post.valid?
278 end 278 end
279 279
280 test 'translated class locale setting' do 280 test 'translated class locale setting' do
281 assert ActiveRecord::Base.respond_to?(:locale) 281 assert ActiveRecord::Base.respond_to?(:locale)
282 assert_equal :'en-US', I18n.locale 282 assert_equal :'en-US', I18n.locale
@@ -291,11 +291,11 @@ class TranslatedTest < ActiveSupport::TestCase
291 assert_equal :fr, I18n.locale 291 assert_equal :fr, I18n.locale
292 assert_equal :es, ActiveRecord::Base.locale 292 assert_equal :es, ActiveRecord::Base.locale
293 end 293 end
294 294
295 test "untranslated class responds to locale" do 295 test "untranslated class responds to locale" do
296 assert Blog.respond_to?(:locale) 296 assert Blog.respond_to?(:locale)
297 end 297 end
298 298
299 test "to ensure locales in different classes are the same" do 299 test "to ensure locales in different classes are the same" do
300 ActiveRecord::Base.locale = :de 300 ActiveRecord::Base.locale = :de
301 assert_equal :de, ActiveRecord::Base.locale 301 assert_equal :de, ActiveRecord::Base.locale
@@ -304,30 +304,30 @@ class TranslatedTest < ActiveSupport::TestCase
304 assert_equal :es, ActiveRecord::Base.locale 304 assert_equal :es, ActiveRecord::Base.locale
305 assert_equal :es, Parent.locale 305 assert_equal :es, Parent.locale
306 end 306 end
307 307
308 test "attribute saving goes by content locale and not global locale" do 308 test "attribute saving goes by content locale and not global locale" do
309 ActiveRecord::Base.locale = :de 309 ActiveRecord::Base.locale = :de
310 assert_equal :'en-US', I18n.locale 310 assert_equal :'en-US', I18n.locale
311 Post.create :subject => 'foo' 311 Post.create :subject => 'foo'
312 assert_equal :de, Post.first.globalize_translations.first.locale 312 assert_equal :de, Post.first.globalize_translations.first.locale
313 end 313 end
314 314
315 test "attribute loading goes by content locale and not global locale" do 315 test "attribute loading goes by content locale and not global locale" do
316 post = Post.create :subject => 'foo' 316 post = Post.create :subject => 'foo'
317 assert_equal :'en-US', ActiveRecord::Base.locale 317 assert_equal :'en-US', ActiveRecord::Base.locale
318 ActiveRecord::Base.locale = :de 318 ActiveRecord::Base.locale = :de
319 assert_equal :'en-US', I18n.locale 319 assert_equal :'en-US', I18n.locale
320 post.update_attribute :subject, 'foo [de]' 320 post.update_attribute :subject, 'foo [de]'
321 assert_equal 'foo [de]', Post.first.subject 321 assert_equal 'foo [de]', Post.first.subject
322 ActiveRecord::Base.locale = :'en-US' 322 ActiveRecord::Base.locale = :'en-US'
323 assert_equal 'foo', Post.first.subject 323 assert_equal 'foo', Post.first.subject
324 end 324 end
325 325
326 test "access content locale before setting" do 326 test "access content locale before setting" do
327 Globalize::Model::ActiveRecord::Translated::ActMethods.class_eval "remove_class_variable(:@@locale)" 327 Globalize::Model::ActiveRecord::Translated::ActMethods.class_eval "remove_class_variable(:@@locale)"
328 assert_nothing_raised { ActiveRecord::Base.locale } 328 assert_nothing_raised { ActiveRecord::Base.locale }
329 end 329 end
330 330
331 test "translated_locales" do 331 test "translated_locales" do
332 Post.locale = :de 332 Post.locale = :de
333 post = Post.create :subject => 'foo' 333 post = Post.create :subject => 'foo'
@@ -338,121 +338,141 @@ class TranslatedTest < ActiveSupport::TestCase
338 assert_equal [ :de, :es, :fr ], post.translated_locales 338 assert_equal [ :de, :es, :fr ], post.translated_locales
339 assert_equal [ :de, :es, :fr ], Post.first.translated_locales 339 assert_equal [ :de, :es, :fr ], Post.first.translated_locales
340 end 340 end
341 341
342 test "including globalize_translations" do 342 test "including globalize_translations" do
343 I18n.locale = :de 343 I18n.locale = :de
344 Post.create :subject => "Foo1", :content => "Bar1" 344 Post.create :subject => "Foo1", :content => "Bar1"
345 Post.create :subject => "Foo2", :content => "Bar2" 345 Post.create :subject => "Foo2", :content => "Bar2"
346 346
347 class << Post 347 class << Post
348 def tranlsations_included 348 def tranlsations_included
349 self.all(:include => :globalize_translations) 349 self.all(:include => :globalize_translations)
350 end 350 end
351 end 351 end
352 352
353 default = Post.all.map {|x| [x.subject, x.content]} 353 default = Post.all.map {|x| [x.subject, x.content]}
354 with_include = Post.tranlsations_included.map {|x| [x.subject, x.content]} 354 with_include = Post.tranlsations_included.map {|x| [x.subject, x.content]}
355 assert_equal default, with_include 355 assert_equal default, with_include
356 end 356 end
357 357
358 test "setting multiple translations at once with options hash" do 358 test "setting multiple translations at once with options hash" do
359 Post.locale = :de 359 Post.locale = :de
360 post = Post.create :subject => "foo1", :content => "foo1" 360 post = Post.create :subject => "foo1", :content => "foo1"
361 Post.locale = :en 361 Post.locale = :en
362 post.update_attributes( :subject => "bar1", :content => "bar1" ) 362 post.update_attributes( :subject => "bar1", :content => "bar1" )
363 363
364 options = { :de => {:subject => "foo2", :content => "foo2"}, 364 options = { :de => {:subject => "foo2", :content => "foo2"},
365 :en => {:subject => "bar2", :content => "bar2"} } 365 :en => {:subject => "bar2", :content => "bar2"} }
366 post.set_translations options 366 post.set_translations options
367 post.reload 367 post.reload
368 368
369 assert ["bar2", "bar2"], [post.subject, post.content] 369 assert ["bar2", "bar2"], [post.subject, post.content]
370 Post.locale = :de 370 Post.locale = :de
371 assert ["foo2", "foo2"], [post.subject, post.content] 371 assert ["foo2", "foo2"], [post.subject, post.content]
372 end 372 end
373 373
374 test "setting only one translation with set_translations" do 374 test "setting only one translation with set_translations" do
375 Post.locale = :de 375 Post.locale = :de
376 post = Post.create :subject => "foo1", :content => "foo1" 376 post = Post.create :subject => "foo1", :content => "foo1"
377 Post.locale = :en 377 Post.locale = :en
378 post.update_attributes( :subject => "bar1", :content => "bar1" ) 378 post.update_attributes( :subject => "bar1", :content => "bar1" )
379 379
380 options = { :en => {:subject => "bar2", :content => "bar2"} } 380 options = { :en => {:subject => "bar2", :content => "bar2"} }
381 post.set_translations options 381 post.set_translations options
382 post.reload 382 post.reload
383 383
384 assert ["bar2", "bar2"], [post.subject, post.content] 384 assert ["bar2", "bar2"], [post.subject, post.content]
385 Post.locale = :de 385 Post.locale = :de
386 assert ["foo1", "foo1"], [post.subject, post.content] 386 assert ["foo1", "foo1"], [post.subject, post.content]
387 end 387 end
388 388
389 test "setting only selected attributes with set_translations" do 389 test "setting only selected attributes with set_translations" do
390 Post.locale = :de 390 Post.locale = :de
391 post = Post.create :subject => "foo1", :content => "foo1" 391 post = Post.create :subject => "foo1", :content => "foo1"
392 Post.locale = :en 392 Post.locale = :en
393 post.update_attributes( :subject => "bar1", :content => "bar1" ) 393 post.update_attributes( :subject => "bar1", :content => "bar1" )
394 394
395 options = { :de => {:content => "foo2"}, :en => {:subject => "bar2"} } 395 options = { :de => {:content => "foo2"}, :en => {:subject => "bar2"} }
396 post.set_translations options 396 post.set_translations options
397 post.reload 397 post.reload
398 398
399 assert ["bar2", "bar1"], [post.subject, post.content] 399 assert ["bar2", "bar1"], [post.subject, post.content]
400 Post.locale = :de 400 Post.locale = :de
401 assert ["foo1", "foo2"], [post.subject, post.content] 401 assert ["foo1", "foo2"], [post.subject, post.content]
402 end 402 end
403 403
404 test "setting invalid attributes raises ArgumentError" do 404 test "setting invalid attributes raises ArgumentError" do
405 Post.locale = :de 405 Post.locale = :de
406 post = Post.create :subject => "foo1", :content => "foo1" 406 post = Post.create :subject => "foo1", :content => "foo1"
407 Post.locale = :en 407 Post.locale = :en
408 post.update_attributes( :subject => "bar1", :content => "bar1" ) 408 post.update_attributes( :subject => "bar1", :content => "bar1" )
409 409
410 options = { :de => {:fake => "foo2"} } 410 options = { :de => {:fake => "foo2"} }
411 exception = assert_raise(ActiveRecord::UnknownAttributeError) do 411 exception = assert_raise(ActiveRecord::UnknownAttributeError) do
412 post.set_translations options 412 post.set_translations options
413 end 413 end
414 assert_equal "unknown attribute: fake", exception.message 414 assert_equal "unknown attribute: fake", exception.message
415 end 415 end
416 416
417 test "reload accepting find options" do 417 test "reload accepting find options" do
418 p = Post.create :subject => "Foo", :content => "Bar" 418 p = Post.create :subject => "Foo", :content => "Bar"
419 assert p.reload(:readonly => true, :lock => true) 419 assert p.reload(:readonly => true, :lock => true)
420 assert_raise(ArgumentError) { p.reload(:foo => :bar) } 420 assert_raise(ArgumentError) { p.reload(:foo => :bar) }
421 end 421 end
422 422
423 test "dependent destroy of translation" do 423 test "dependent destroy of translation" do
424 p = Post.create :subject => "Foo", :content => "Bar" 424 p = Post.create :subject => "Foo", :content => "Bar"
425 assert_equal 1, PostTranslation.count 425 assert_equal 1, PostTranslation.count
426 p.destroy 426 p.destroy
427 assert_equal 0, PostTranslation.count 427 assert_equal 0, PostTranslation.count
428 end 428 end
429 429
430 test "translating subclass of untranslated comment model" do 430 test "translating subclass of untranslated comment model" do
431 translated_comment = TranslatedComment.create(:post => @post) 431 translated_comment = TranslatedComment.create(:post => @post)
432 assert_nothing_raised { translated_comment.globalize_translations } 432 assert_nothing_raised { translated_comment.globalize_translations }
433 end 433 end
434 434
435 test "modifiying translated comments works as expected" do 435 test "modifiying translated comments works as expected" do
436 I18n.locale = :en 436 I18n.locale = :en
437 translated_comment = TranslatedComment.create(:post => @post, :content => 'foo') 437 translated_comment = TranslatedComment.create(:post => @post, :content => 'foo')
438 assert_equal 'foo', translated_comment.content 438 assert_equal 'foo', translated_comment.content
439 439
440 I18n.locale = :de 440 I18n.locale = :de
441 translated_comment.content = 'bar' 441 translated_comment.content = 'bar'
442 assert translated_comment.save 442 assert translated_comment.save
443 assert_equal 'bar', translated_comment.content 443 assert_equal 'bar', translated_comment.content
444 444
445 I18n.locale = :en 445 I18n.locale = :en
446 assert_equal 'foo', translated_comment.content 446 assert_equal 'foo', translated_comment.content
447 447
448 assert_equal 2, translated_comment.globalize_translations.size 448 assert_equal 2, translated_comment.globalize_translations.size
449 end 449 end
450
451 test "can create a proxy class for a namespaced model" do
452 module Foo
453 module Bar
454 class Baz < ActiveRecord::Base
455 translates :bumm
456 end
457 end
458 end
459 end
460
461 test "attribute translated before type cast" do
462 Post.locale = :en
463 post = Post.create :subject => 'foo', :content => 'bar'
464 Post.locale = :de
465 post.update_attribute :subject, "German foo"
466 assert_equal 'German foo', post.subject_before_type_cast
467 Post.locale = :en
468 assert_equal 'foo', post.subject_before_type_cast
469 end
450end 470end
451 471
452# TODO should validate_presence_of take fallbacks into account? maybe we need 472# TODO should validate_presence_of take fallbacks into account? maybe we need
453# an extra validation call, or more options for validate_presence_of. 473# an extra validation call, or more options for validate_presence_of.
454# TODO error checking for fields that exist in main table, don't exist in 474# TODO error checking for fields that exist in main table, don't exist in
455# proxy table, aren't strings or text 475# proxy table, aren't strings or text
456# 476#
457# TODO allow finding by translated attributes in conditions? 477# TODO allow finding by translated attributes in conditions?
458# TODO generate advanced dynamic finders? 478# 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
11 ::ActiveRecord::Migration.verbose = false # Quiet down the migration engine 11 ::ActiveRecord::Migration.verbose = false # Quiet down the migration engine
12 ::ActiveRecord::Base.establish_connection({ 12 ::ActiveRecord::Base.establish_connection({
13 :adapter => 'sqlite3', 13 :adapter => 'sqlite3',
14 :dbfile => ':memory:' 14 :database => ':memory:'
15 }) 15 })
16 ::ActiveRecord::Base.silence do 16 ::ActiveRecord::Base.silence do
17 load schema_path 17 load schema_path
@@ -23,4 +23,14 @@ class ActiveSupport::TestCase
23 arr.member? item 23 arr.member? item
24 end 24 end
25 end 25 end
26end
27
28module ActiveRecord
29 module ConnectionAdapters
30 class AbstractAdapter
31 def index_exists?(table_name, column_name)
32 indexes(table_name).any? { |index| index.name == index_name(table_name, column_name) }
33 end
34 end
35 end
26end \ No newline at end of file 36end \ No newline at end of file