summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhukl <contact@smyck.org>2010-01-14 22:21:43 +0100
committerhukl <contact@smyck.org>2010-01-14 22:21:43 +0100
commit57d1382013c85a7b11ac8ce5e683f6006b12b328 (patch)
tree19646c4fa5952fa1cf0a2c874952affa346373f1
parent1b86bf5f2e35d1820bfa32d979bcc2d9ff9a9a8e (diff)
Updated globalize2 to latest version. Modified existing code accoringly
-rw-r--r--app/models/node.rb2
-rw-r--r--app/models/page.rb14
-rw-r--r--test/unit/page_test.rb10
-rw-r--r--vendor/plugins/globalize2/README.textile161
-rw-r--r--vendor/plugins/globalize2/Rakefile17
-rw-r--r--vendor/plugins/globalize2/VERSION1
-rw-r--r--vendor/plugins/globalize2/generators/templates/db_backend_migration.rb6
-rw-r--r--vendor/plugins/globalize2/globalize2.gemspec82
-rw-r--r--vendor/plugins/globalize2/init.rb11
-rw-r--r--vendor/plugins/globalize2/lib/globalize.rb15
-rw-r--r--vendor/plugins/globalize2/lib/globalize/active_record.rb194
-rw-r--r--vendor/plugins/globalize2/lib/globalize/active_record/adapter.rb80
-rw-r--r--vendor/plugins/globalize2/lib/globalize/active_record/attributes.rb25
-rw-r--r--vendor/plugins/globalize2/lib/globalize/active_record/migration.rb40
-rw-r--r--vendor/plugins/globalize2/lib/globalize/backend/chain.rb102
-rw-r--r--vendor/plugins/globalize2/lib/globalize/backend/pluralizing.rb37
-rw-r--r--vendor/plugins/globalize2/lib/globalize/backend/static.rb60
-rw-r--r--vendor/plugins/globalize2/lib/globalize/load_path.rb63
-rw-r--r--vendor/plugins/globalize2/lib/globalize/locale/fallbacks.rb63
-rw-r--r--vendor/plugins/globalize2/lib/globalize/locale/language_tag.rb81
-rw-r--r--vendor/plugins/globalize2/lib/globalize/model/active_record.rb45
-rw-r--r--vendor/plugins/globalize2/lib/globalize/model/active_record/adapter.rb96
-rw-r--r--vendor/plugins/globalize2/lib/globalize/model/active_record/translated.rb164
-rw-r--r--vendor/plugins/globalize2/lib/globalize/translation.rb32
-rw-r--r--vendor/plugins/globalize2/lib/i18n/missing_translations_log_handler.rb (renamed from vendor/plugins/globalize2/lib/globalize/i18n/missing_translations_log_handler.rb)16
-rw-r--r--vendor/plugins/globalize2/lib/i18n/missing_translations_raise_handler.rb (renamed from vendor/plugins/globalize2/lib/globalize/i18n/missing_translations_raise_handler.rb)8
-rw-r--r--vendor/plugins/globalize2/lib/locale/root.yml3
-rw-r--r--vendor/plugins/globalize2/lib/rails_edge_load_path_patch.rb40
-rw-r--r--vendor/plugins/globalize2/notes.textile51
-rw-r--r--vendor/plugins/globalize2/test/active_record/fallbacks_test.rb102
-rw-r--r--vendor/plugins/globalize2/test/active_record/migration_test.rb (renamed from vendor/plugins/globalize2/test/model/active_record/migration_test.rb)71
-rw-r--r--vendor/plugins/globalize2/test/active_record/sti_translated_test.rb (renamed from vendor/plugins/globalize2/test/model/active_record/sti_translated_test.rb)34
-rw-r--r--vendor/plugins/globalize2/test/active_record/translates_test.rb87
-rw-r--r--vendor/plugins/globalize2/test/active_record/translation_class_test.rb30
-rw-r--r--vendor/plugins/globalize2/test/active_record/validation_tests.rb75
-rw-r--r--vendor/plugins/globalize2/test/active_record_test.rb442
-rw-r--r--vendor/plugins/globalize2/test/backends/chained_test.rb175
-rw-r--r--vendor/plugins/globalize2/test/backends/pluralizing_test.rb63
-rw-r--r--vendor/plugins/globalize2/test/backends/static_test.rb143
-rw-r--r--vendor/plugins/globalize2/test/data/locale/all.yml2
-rw-r--r--vendor/plugins/globalize2/test/data/locale/de-DE.yml2
-rw-r--r--vendor/plugins/globalize2/test/data/locale/en-US.yml2
-rw-r--r--vendor/plugins/globalize2/test/data/locale/en-US/module.yml2
-rw-r--r--vendor/plugins/globalize2/test/data/locale/fi-FI/module.yml2
-rw-r--r--vendor/plugins/globalize2/test/data/locale/root.yml0
-rw-r--r--vendor/plugins/globalize2/test/data/models.rb26
-rw-r--r--vendor/plugins/globalize2/test/i18n/missing_translations_test.rb12
-rw-r--r--vendor/plugins/globalize2/test/load_path_test.rb49
-rw-r--r--vendor/plugins/globalize2/test/locale/fallbacks_test.rb154
-rw-r--r--vendor/plugins/globalize2/test/locale/language_tag_test.rb130
-rw-r--r--vendor/plugins/globalize2/test/model/active_record/translated_test.rb478
-rw-r--r--vendor/plugins/globalize2/test/test_helper.rb70
-rw-r--r--vendor/plugins/globalize2/test/translation_test.rb54
53 files changed, 1351 insertions, 2373 deletions
diff --git a/app/models/node.rb b/app/models/node.rb
index 4f67dcb..db44b71 100644
--- a/app/models/node.rb
+++ b/app/models/node.rb
@@ -25,7 +25,7 @@ class Node < ActiveRecord::Base
25 25
26 # Index for Fulltext Search 26 # Index for Fulltext Search
27 define_index do 27 define_index do
28 indexes head.globalize_translations.title 28 indexes head.translations.title
29 indexes slug 29 indexes slug
30 indexes unique_name 30 indexes unique_name
31 end 31 end
diff --git a/app/models/page.rb b/app/models/page.rb
index 37b221e..a993e31 100644
--- a/app/models/page.rb
+++ b/app/models/page.rb
@@ -10,14 +10,14 @@ class Page < ActiveRecord::Base
10 named_scope( 10 named_scope(
11 :drafts, 11 :drafts,
12 :joins => :node, 12 :joins => :node,
13 :include => [:globalize_translations], 13 :include => [:translations],
14 :conditions => ["nodes.draft_id = pages.id"] 14 :conditions => ["nodes.draft_id = pages.id"]
15 ) 15 )
16 16
17 named_scope( 17 named_scope(
18 :heads, 18 :heads,
19 :joins => :node, 19 :joins => :node,
20 :include => [:globalize_translations], 20 :include => [:translations],
21 :conditions => ["nodes.head_id = pages.id"] 21 :conditions => ["nodes.head_id = pages.id"]
22 ) 22 )
23 23
@@ -89,7 +89,7 @@ class Page < ActiveRecord::Base
89 # outdated_translations? for more information. 89 # outdated_translations? for more information.
90 # Takes :locale => <locale> and :delta_time => 12.hours as options 90 # Takes :locale => <locale> and :delta_time => 12.hours as options
91 def self.find_with_outdated_translations options = {} 91 def self.find_with_outdated_translations options = {}
92 Page.all(:include => :globalize_translations).select do |page| 92 Page.all(:include => :translations).select do |page|
93 page.outdated_translations? options 93 page.outdated_translations? options
94 end 94 end
95 end 95 end
@@ -132,11 +132,11 @@ class Page < ActiveRecord::Base
132 self.published_at = page.published_at 132 self.published_at = page.published_at
133 133
134 # Getting rid of the auto-generated empty translations 134 # Getting rid of the auto-generated empty translations
135 self.globalize_translations.delete_all 135 self.translations.delete_all
136 136
137 # Clone translated attributes 137 # Clone translated attributes
138 page.globalize_translations.each do |translation| 138 page.translations.each do |translation|
139 self.globalize_translations.create!(translation.attributes) 139 self.translations.create!(translation.attributes)
140 end 140 end
141 141
142 # Clone asset references 142 # Clone asset references
@@ -163,7 +163,7 @@ class Page < ActiveRecord::Base
163 163
164 options = default_options.merge options 164 options = default_options.merge options
165 165
166 translations = self.globalize_translations 166 translations = self.translations
167 167
168 default = *(translations.select {|x| x.locale == I18n.default_locale}) 168 default = *(translations.select {|x| x.locale == I18n.default_locale})
169 custom = *(translations.select {|x| x.locale == options[:locale]}) 169 custom = *(translations.select {|x| x.locale == options[:locale]})
diff --git a/test/unit/page_test.rb b/test/unit/page_test.rb
index bb82da0..099b79d 100644
--- a/test/unit/page_test.rb
+++ b/test/unit/page_test.rb
@@ -108,20 +108,20 @@ class PageTest < ActiveSupport::TestCase
108 108
109 assert_not_nil page = Page.create!( :title => "Hallo" ) 109 assert_not_nil page = Page.create!( :title => "Hallo" )
110 page.reload 110 page.reload
111 assert_equal 2, page.globalize_translations.size 111 assert_equal 1, page.translations.size
112 assert_equal [], Page.find_with_outdated_translations 112 assert_equal [], Page.find_with_outdated_translations
113 113
114 I18n.locale = :en 114 I18n.locale = :en
115 page.title = "Hello" 115 page.title = "Hello"
116 page.save 116 page.save
117 117
118 assert_equal 3, page.globalize_translations.size 118 assert_equal 2, page.translations.size
119 assert_equal 0, Page.find_with_outdated_translations.size 119 assert_equal 0, Page.find_with_outdated_translations.size
120 120
121 english = *page.globalize_translations.select {|x| x.locale == :en} 121 english = *page.translations.select {|x| x.locale == :en}
122 PageTranslation.record_timestamps = false 122 Page::Translation.record_timestamps = false
123 english.update_attributes(:updated_at => (Time.now+25.hours)) 123 english.update_attributes(:updated_at => (Time.now+25.hours))
124 PageTranslation.record_timestamps = true 124 Page::Translation.record_timestamps = true
125 assert_equal 1, Page.find_with_outdated_translations.count 125 assert_equal 1, Page.find_with_outdated_translations.count
126 126
127 I18n.locale = :de 127 I18n.locale = :de
diff --git a/vendor/plugins/globalize2/README.textile b/vendor/plugins/globalize2/README.textile
index dbd7288..7155a7c 100644
--- a/vendor/plugins/globalize2/README.textile
+++ b/vendor/plugins/globalize2/README.textile
@@ -1,16 +1,17 @@
1h1. Globalize2 1h1. Globalize2
2 2
3Globalize2 is the successor of Globalize for Rails. 3Globalize2 is the successor of Globalize for Rails.
4 4
5It is compatible with and builds on the new "I18n api in Ruby on Rails":http://rails-i18n.org. and adds model translations as well as a bunch of other useful features, such as Locale fallbacks (RFC4647 compliant) and automatic loading of Locale data from defined directory/file locations. 5It is compatible with and builds on the new "I18n api in Ruby on Rails":http://guides.rubyonrails.org/i18n.html. and adds model translations to ActiveRecord.
6 6
7Globalize2 is much more lightweight and modular than its predecessor was. Content translations in Globalize2 use default ActiveRecord features and do not limit any functionality any more. 7Globalize2 is much more lightweight and compatible than its predecessor was. Model translations in Globalize2 use default ActiveRecord features and do not limit any ActiveRecord functionality any more.
8
9All features and tools in Globalize2 are implemented in the most unobstrusive and loosely-coupled way possible, so you can pick whatever features or tools you need for your application and combine them with other tools from other libraries or plugins.
10 8
11h2. Requirements 9h2. Requirements
12 10
13Rails 2.2 (currently Rails edge) 11ActiveRecord
12I18n
13
14(or Rails > 2.2)
14 15
15h2. Installation 16h2. Installation
16 17
@@ -20,19 +21,9 @@ To install Globalize2 with its default setup just use:
20script/plugin install git://github.com/joshmh/globalize2.git 21script/plugin install git://github.com/joshmh/globalize2.git
21</code></pre> 22</code></pre>
22 23
23This will:
24
25* activate model translations
26* set I18n.load_path to an instance of Globalize::LoadPath
27* set I18n.backend to an instance of Globalize::Backend::Static
28
29h2. Configuration
30
31You might want to add additional configuration to an initializer, e.g. config/initializers/globalize.rb
32
33h2. Model translations 24h2. Model translations
34 25
35Model translations (or content translations) allow you to translate your models' attribute values. E.g. 26Model translations allow you to translate your models' attribute values. E.g.
36 27
37<pre><code> 28<pre><code>
38class Post < ActiveRecord::Base 29class Post < ActiveRecord::Base
@@ -44,10 +35,10 @@ Allows you to values for the attributes :title and :text per locale:
44 35
45<pre><code> 36<pre><code>
46I18n.locale = :en 37I18n.locale = :en
47post.title # Globalize2 rocks! 38post.title # => Globalize2 rocks!
48 39
49I18n.locale = :he 40I18n.locale = :he
50post.title # גלובאלייז2 שולט! 41post.title # => גלובאלייז2 שולט!
51</code></pre> 42</code></pre>
52 43
53In order to make this work, you'll need to add the appropriate translation tables. Globalize2 comes with a handy helper method to help you do this. It's called @create_translation_table!@. Here's an example: 44In order to make this work, you'll need to add the appropriate translation tables. Globalize2 comes with a handy helper method to help you do this. It's called @create_translation_table!@. Here's an example:
@@ -69,134 +60,6 @@ end
69 60
70Note that the ActiveRecord model @Post@ must already exist and have a @translates@ directive listing the translated fields. 61Note that the ActiveRecord model @Post@ must already exist and have a @translates@ directive listing the translated fields.
71 62
72h2. Globalize::Backend::Static 63h2. Migration from Globalize
73
74Globalize2 ships with a Static backend that builds on the Simple backend from the I18n library (which is shipped with Rails) and adds the following features:
75
76* It uses locale fallbacks when looking up translation data.
77* It returns an instance of Globalize::Translation::Static instead of a plain Ruby String as a translation.
78* It allows to hook in custom pluralization logic as lambdas.
79
80h2. Custom pluralization logic
81
82The Simple backend has its pluralization algorithm baked in hardcoded. This algorithm is only suitable for English and other languages that have the same pluralization rules. It is not suitable for, e.g., Czech though.
83
84To add custom pluralization logic to Globalize' Static backend you can do something like this:
85
86<pre><code>
87@backend.add_pluralizer :cz, lambda{|c|
88 c == 1 ? :one : (2..4).include?(c) ? :few : :other
89}
90</code></pre>
91
92h2. Locale Fallbacks
93
94Globalize2 ships with a Locale fallback tool which extends the I18n module to hold a fallbacks instance which is set to an instance of Globalize::Locale::Fallbacks by default but can be swapped with a different implementation.
95
96Globalize2 fallbacks will compute a number of other locales for a given locale. For example:
97
98<pre><code>
99I18n.fallbacks[:"es-MX"] # => [:"es-MX", :es, :"en-US", :en]
100</code></pre>
101
102Globalize2 fallbacks always fall back to
103
104* all parents of a given locale (e.g. :es for :"es-MX"),
105* then to the fallbacks' default locales and all of their parents and
106* finally to the :root locale.
107
108The default locales are set to [:"en-US"] by default but can be set to something else. The root locale is a concept borrowed from "CLDR":http://unicode.org and makes sense for storing common locale data which works as a last default fallback (e.g. "ltr" for bidi directions).
109
110One can additionally add any number of additional fallback locales manually. These will be added before the default locales to the fallback chain. For example:
111
112<pre><code>
113fb = I18n.fallbacks
114
115fb.map :ca => :"es-ES"
116fb[:ca] # => [:ca, :"es-ES", :es, :"en-US", :en]
117
118fb.map :"ar-PS" => :"he-IL"
119fb[:"ar-PS"] # => [:"ar-PS", :ar, :"he-IL", :he, :"en-US", :en]
120fb[:"ar-EG"] # => [:"ar-EG", :ar, :"en-US", :en]
121
122fb.map :sms => [:"se-FI", :"fi-FI"]
123fb[:sms] # => [:sms, :"se-FI", :se, :"fi-FI", :fi, :"en-US", :en]
124</code></pre>
125
126h2. Globalize::LoadPath
127
128Globalize2 replaces the plain Ruby array that is set to I18n.load_path by default through an instance of Globalize::LoadPath.
129
130This object can be populated with both paths to files and directories. If a path to a directory is added to it it will look up all locale data files present in that directory enforcing the following convention:
131
132<pre><code>
133I18n.load_path << "#{RAILS_ROOT}/lib/locales"
134
135# will load all the following files if present:
136lib/locales/all.yml
137lib/locales/fr.yml
138lib/locales/fr/*.yaml
139lib/locales/ru.yml
140lib/locales/ru/*.yaml
141...
142</code></pre>
143
144One can also specify which locales are used. By default this is set to "*" meaning that files for all locales are added. To define that only files for the locale :es are added one can specify:
145
146<pre><code>
147I18n.load_path.locales = [:es]
148</code></pre>
149
150One can also specify which file extensions are used. By default this is set to ['rb', 'yml'] so plain Ruby and YAML files are added if found. To define that only *.sql files are added one can specify:
151
152<pre><code>
153I18n.load_path.extensions = ['sql']
154</code></pre>
155
156Note that Globalize::LoadPath "expands" a directory to its contained file paths immediately when you add it to the load_path. Thus, if you change the locales or extensions settings in the middle of your application the change won't be applied to already added file paths.
157
158
159h2. Globalize::Translation classes
160
161Globalize2's Static backend as well as Globalize2 model translations return instances of Globalize::Translation classes (instead of plain Ruby Strings). These are simple and lightweight value objects that carry some additional meta data about the translation and how it was looked up.
162
163Model translations return instances of Globalize::Translation::Attribute, the Static backend returns instances of Globalize::Translation::Static.
164
165For example:
166
167<pre><code>
168I18n.locale = :de
169
170# Translation::Attribute
171title = Post.first.title # assuming that no translation can be found:
172title.locale # => :en
173title.requested_locale # => :de
174title.fallback? # => true
175
176# Translation::Static
177rails = I18n.t :rails # assuming that no translation can be found:
178rails.locale # => :en
179rails.requested_locale # => :de
180rails.fallback? # => true
181rails.options # returns the options passed to #t
182rails.plural_key # returns the plural_key (e.g. :one, :other)
183rails.original # returns the original translation with no values
184 # interpolated to it (e.g. "Hi {{name}}!")
185</code></pre>
186
187h2. Missing Translations Log Handler
188
189A simple exception handler that behaves like the default exception handler but additionally logs missing translations to a given log.
190
191Useful for identifying missing translations during testing.
192
193E.g.
194
195 require 'globalize/i18n/missing_translations_log_handler
196 I18n.missing_translations_logger = RAILS_DEFAULT_LOGGER
197 I18n.exception_handler = :missing_translations_log_handler
198
199To set up a different log file:
200 64
201 logger = Logger.new("#{RAILS_ROOT}/log/missing_translations.log") 65See this script by Tomasz Stachewicz: http://gist.github.com/120867
202 I18n.missing_translations_logger = logger
diff --git a/vendor/plugins/globalize2/Rakefile b/vendor/plugins/globalize2/Rakefile
index bca49a1..ee80713 100644
--- a/vendor/plugins/globalize2/Rakefile
+++ b/vendor/plugins/globalize2/Rakefile
@@ -20,3 +20,20 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
20 rdoc.rdoc_files.include('README') 20 rdoc.rdoc_files.include('README')
21 rdoc.rdoc_files.include('lib/**/*.rb') 21 rdoc.rdoc_files.include('lib/**/*.rb')
22end 22end
23
24begin
25 require 'jeweler'
26 Jeweler::Tasks.new do |s|
27 s.name = "globalize2"
28 s.summary = "Rails I18n: de-facto standard library for ActiveRecord data translation"
29 s.description = "Rails I18n: de-facto standard library for ActiveRecord data translation"
30 s.email = "joshmh@gmail.com"
31 s.homepage = "http://github.com/joshmh/globalize2"
32 # s.rubyforge_project = ''
33 s.authors = ["Sven Fuchs, Joshua Harvey, Clemens Kofler"]
34 # s.add_development_dependency ''
35 end
36 Jeweler::GemcutterTasks.new
37rescue LoadError
38 puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
39end
diff --git a/vendor/plugins/globalize2/VERSION b/vendor/plugins/globalize2/VERSION
new file mode 100644
index 0000000..0ea3a94
--- /dev/null
+++ b/vendor/plugins/globalize2/VERSION
@@ -0,0 +1 @@
0.2.0
diff --git a/vendor/plugins/globalize2/generators/templates/db_backend_migration.rb b/vendor/plugins/globalize2/generators/templates/db_backend_migration.rb
index d4513a5..0f02611 100644
--- a/vendor/plugins/globalize2/generators/templates/db_backend_migration.rb
+++ b/vendor/plugins/globalize2/generators/templates/db_backend_migration.rb
@@ -8,16 +8,16 @@ class ActsAsTaggableMigration < ActiveRecord::Migration
8 end 8 end
9 9
10# TODO: FINISH DOING MIGRATION -- stopped in the middle 10# TODO: FINISH DOING MIGRATION -- stopped in the middle
11 11
12 create_table :globalize_translations_map do |t| 12 create_table :globalize_translations_map do |t|
13 t.string :key, :null => false 13 t.string :key, :null => false
14 t.integer :translation_id, :null => false 14 t.integer :translation_id, :null => false
15 end 15 end
16 16
17 add_index :taggings, :tag_id 17 add_index :taggings, :tag_id
18 add_index :taggings, [:taggable_id, :taggable_type] 18 add_index :taggings, [:taggable_id, :taggable_type]
19 end 19 end
20 20
21 def self.down 21 def self.down
22 drop_table :globalize_translations 22 drop_table :globalize_translations
23 drop_table :tags 23 drop_table :tags
diff --git a/vendor/plugins/globalize2/globalize2.gemspec b/vendor/plugins/globalize2/globalize2.gemspec
new file mode 100644
index 0000000..6ad93d3
--- /dev/null
+++ b/vendor/plugins/globalize2/globalize2.gemspec
@@ -0,0 +1,82 @@
1# Generated by jeweler
2# DO NOT EDIT THIS FILE DIRECTLY
3# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4# -*- encoding: utf-8 -*-
5
6Gem::Specification.new do |s|
7 s.name = %q{globalize2}
8 s.version = "0.2.0"
9
10 s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11 s.authors = ["Sven Fuchs, Joshua Harvey, Clemens Kofler"]
12 s.date = %q{2009-12-25}
13 s.description = %q{Rails I18n: de-facto standard library for ActiveRecord data translation}
14 s.email = %q{joshmh@gmail.com}
15 s.extra_rdoc_files = [
16 "LICENSE",
17 "README.textile"
18 ]
19 s.files = [
20 ".gitignore",
21 "LICENSE",
22 "README.textile",
23 "Rakefile",
24 "VERSION",
25 "generators/db_backend.rb",
26 "generators/templates/db_backend_migration.rb",
27 "globalize2.gemspec",
28 "init.rb",
29 "lib/globalize.rb",
30 "lib/globalize/active_record.rb",
31 "lib/globalize/active_record/adapter.rb",
32 "lib/globalize/active_record/attributes.rb",
33 "lib/globalize/active_record/migration.rb",
34 "lib/i18n/missing_translations_log_handler.rb",
35 "lib/i18n/missing_translations_raise_handler.rb",
36 "notes.textile",
37 "test/active_record/fallbacks_test.rb",
38 "test/active_record/migration_test.rb",
39 "test/active_record/sti_translated_test.rb",
40 "test/active_record/translates_test.rb",
41 "test/active_record/translation_class_test.rb",
42 "test/active_record/validation_tests.rb",
43 "test/active_record_test.rb",
44 "test/all.rb",
45 "test/data/models.rb",
46 "test/data/no_globalize_schema.rb",
47 "test/data/schema.rb",
48 "test/i18n/missing_translations_test.rb",
49 "test/test_helper.rb"
50 ]
51 s.homepage = %q{http://github.com/joshmh/globalize2}
52 s.rdoc_options = ["--charset=UTF-8"]
53 s.require_paths = ["lib"]
54 s.rubygems_version = %q{1.3.5}
55 s.summary = %q{Rails I18n: de-facto standard library for ActiveRecord data translation}
56 s.test_files = [
57 "test/active_record/fallbacks_test.rb",
58 "test/active_record/migration_test.rb",
59 "test/active_record/sti_translated_test.rb",
60 "test/active_record/translates_test.rb",
61 "test/active_record/translation_class_test.rb",
62 "test/active_record/validation_tests.rb",
63 "test/active_record_test.rb",
64 "test/all.rb",
65 "test/data/models.rb",
66 "test/data/no_globalize_schema.rb",
67 "test/data/schema.rb",
68 "test/i18n/missing_translations_test.rb",
69 "test/test_helper.rb"
70 ]
71
72 if s.respond_to? :specification_version then
73 current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
74 s.specification_version = 3
75
76 if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
77 else
78 end
79 else
80 end
81end
82
diff --git a/vendor/plugins/globalize2/init.rb b/vendor/plugins/globalize2/init.rb
index 7b5428e..a408925 100644
--- a/vendor/plugins/globalize2/init.rb
+++ b/vendor/plugins/globalize2/init.rb
@@ -1,10 +1 @@
1require 'rails_edge_load_path_patch.rb' unless I18n.respond_to?(:load_path) require 'globalize'
2
3ActiveRecord::Base.send :include, Globalize::Model::ActiveRecord::Translated
4
5I18n.backend = Globalize::Backend::Static.new
6
7I18n.load_path = Globalize::LoadPath.new I18n.load_path
8I18n.load_path << "#{File.dirname(__FILE__)}/lib/locale"
9I18n.load_path << "#{RAILS_ROOT}/lib/locale"
10
diff --git a/vendor/plugins/globalize2/lib/globalize.rb b/vendor/plugins/globalize2/lib/globalize.rb
new file mode 100644
index 0000000..67c1878
--- /dev/null
+++ b/vendor/plugins/globalize2/lib/globalize.rb
@@ -0,0 +1,15 @@
1module Globalize
2 autoload :ActiveRecord, 'globalize/active_record'
3
4 class << self
5 def fallbacks?
6 I18n.respond_to?(:fallbacks)
7 end
8
9 def fallbacks(locale)
10 fallbacks? ? I18n.fallbacks[locale] : [locale.to_sym]
11 end
12 end
13end
14
15ActiveRecord::Base.send(:include, Globalize::ActiveRecord)
diff --git a/vendor/plugins/globalize2/lib/globalize/active_record.rb b/vendor/plugins/globalize2/lib/globalize/active_record.rb
new file mode 100644
index 0000000..d2a843f
--- /dev/null
+++ b/vendor/plugins/globalize2/lib/globalize/active_record.rb
@@ -0,0 +1,194 @@
1module Globalize
2 class MigrationError < StandardError; end
3 class MigrationMissingTranslatedField < MigrationError; end
4 class BadMigrationFieldType < MigrationError; end
5
6 module ActiveRecord
7 autoload :Adapter, 'globalize/active_record/adapter'
8 autoload :Attributes, 'globalize/active_record/attributes'
9 autoload :Migration, 'globalize/active_record/migration'
10
11 def self.included(base)
12 base.extend ActMacro
13 end
14
15 class << self
16 def build_translation_class(target, options)
17 options[:table_name] ||= "#{target.table_name.singularize}_translations"
18
19 klass = target.const_defined?(:Translation) ?
20 target.const_get(:Translation) :
21 target.const_set(:Translation, Class.new(::ActiveRecord::Base))
22
23 klass.class_eval do
24 set_table_name(options[:table_name])
25 belongs_to target.name.underscore.gsub('/', '_')
26 def locale; read_attribute(:locale).to_sym; end
27 def locale=(locale); write_attribute(:locale, locale.to_s); end
28 end
29
30 klass
31 end
32 end
33
34 module ActMacro
35 def locale
36 (defined?(@@locale) && @@locale)
37 end
38
39 def locale=(locale)
40 @@locale = locale
41 end
42
43 def translates(*attr_names)
44 return if translates?
45 options = attr_names.extract_options!
46
47 class_inheritable_accessor :translation_class, :translated_attribute_names
48 self.translation_class = ActiveRecord.build_translation_class(self, options)
49 self.translated_attribute_names = attr_names.map(&:to_sym)
50
51 include InstanceMethods
52 extend ClassMethods, Migration
53
54 after_save :save_translations!
55 has_many :translations, :class_name => translation_class.name,
56 :foreign_key => class_name.foreign_key,
57 :dependent => :delete_all,
58 :extend => HasManyExtensions
59
60 named_scope :with_translations, lambda { |locale|
61 conditions = required_attributes.map do |attribute|
62 "#{quoted_translation_table_name}.#{attribute} IS NOT NULL"
63 end
64 conditions << "#{quoted_translation_table_name}.locale = ?"
65 { :include => :translations, :conditions => [conditions.join(' AND '), locale] }
66 }
67
68 attr_names.each { |attr_name| translated_attr_accessor(attr_name) }
69 end
70
71 def translates?
72 included_modules.include?(InstanceMethods)
73 end
74 end
75
76 module HasManyExtensions
77 def by_locale(locale)
78 first(:conditions => { :locale => locale.to_s })
79 end
80
81 def by_locales(locales)
82 all(:conditions => { :locale => locales.map(&:to_s) })
83 end
84 end
85
86 module ClassMethods
87 delegate :set_translation_table_name, :to => :translation_class
88
89 def with_locale(locale)
90 previous_locale, self.locale = self.locale, locale
91 result = yield
92 self.locale = previous_locale
93 result
94 end
95
96 def translation_table_name
97 translation_class.table_name
98 end
99
100 def quoted_translation_table_name
101 translation_class.quoted_table_name
102 end
103
104 def required_attributes
105 validations = reflect_on_all_validations.select do |validation|
106 validation.macro == :validates_presence_of
107 end.map(&:name)
108 end
109
110 def respond_to?(method, *args, &block)
111 method.to_s =~ /^find_by_(\w+)$/ && translated_attribute_names.include?($1.to_sym) || super
112 end
113
114 def method_missing(method, *args)
115 if method.to_s =~ /^find_by_(\w+)$/ && translated_attribute_names.include?($1.to_sym)
116 find_first_by_translated_attr_and_locales($1, args.first)
117 else
118 super
119 end
120 end
121
122 protected
123
124 def find_first_by_translated_attr_and_locales(name, value)
125 query = "#{translated_attr_name(name)} = ? AND #{translated_attr_name('locale')} IN (?)"
126 locales = Globalize.fallbacks(locale || I18n.locale).map(&:to_s)
127 find(:first, :joins => :translations, :conditions => [query, value, locales])
128 end
129
130 def translated_attr_accessor(name)
131 define_method "#{name}=", lambda { |value|
132 globalize.write(self.class.locale || I18n.locale, name, value)
133 self[name] = value
134 }
135 define_method name, lambda { |*args|
136 globalize.fetch(args.first || self.class.locale || I18n.locale, name)
137 }
138 alias_method "#{name}_before_type_cast", name
139 end
140
141 def translated_attr_name(name)
142 "#{translation_class.table_name}.#{name}"
143 end
144 end
145
146 module InstanceMethods
147 def globalize
148 @globalize ||= Adapter.new self
149 end
150
151 def attributes=(attributes, *args)
152 if attributes.respond_to?(:delete) && locale = attributes.delete(:locale)
153 self.class.with_locale(locale) { super }
154 else
155 super
156 end
157 end
158
159 def available_locales
160 translations.scoped(:select => 'DISTINCT locale').map(&:locale)
161 end
162
163 def translated_locales
164 translations.map(&:locale)
165 end
166
167 def translated_attributes
168 translated_attribute_names.inject({}) do |attributes, name|
169 attributes.merge(name => send(name))
170 end
171 end
172
173 def set_translations(options)
174 options.keys.each do |locale|
175 translation = translations.find_by_locale(locale.to_s) ||
176 translations.build(:locale => locale.to_s)
177 translation.update_attributes!(options[locale])
178 end
179 end
180
181 def reload(options = nil)
182 translated_attribute_names.each { |name| @attributes.delete(name.to_s) }
183 globalize.reset
184 super(options)
185 end
186
187 protected
188
189 def save_translations!
190 globalize.save_translations!
191 end
192 end
193 end
194end
diff --git a/vendor/plugins/globalize2/lib/globalize/active_record/adapter.rb b/vendor/plugins/globalize2/lib/globalize/active_record/adapter.rb
new file mode 100644
index 0000000..12f89ec
--- /dev/null
+++ b/vendor/plugins/globalize2/lib/globalize/active_record/adapter.rb
@@ -0,0 +1,80 @@
1module Globalize
2 module ActiveRecord
3 class Adapter
4 # The cache caches attributes that already were looked up for read access.
5 # The stash keeps track of new or changed values that need to be saved.
6 attr_reader :record, :cache, :stash
7
8 def initialize(record)
9 @record = record
10 @cache = Attributes.new
11 @stash = Attributes.new
12 end
13
14 def fetch(locale, attr_name)
15 cache.contains?(locale, attr_name) ?
16 cache.read(locale, attr_name) :
17 cache.write(locale, attr_name, fetch_attribute(locale, attr_name))
18 end
19
20 def write(locale, attr_name, value)
21 stash.write(locale, attr_name, value)
22 cache.write(locale, attr_name, value)
23 end
24
25 def save_translations!
26 stash.each do |locale, attrs|
27 translation = record.translations.find_or_initialize_by_locale(locale.to_s)
28 attrs.each { |attr_name, value| translation[attr_name] = value }
29 translation.save!
30 end
31 stash.clear
32 end
33
34 def reset
35 cache.clear
36 # stash.clear
37 end
38
39 protected
40
41 def fetch_translation(locale)
42 locale = locale.to_sym
43 record.translations.loaded? ? record.translations.detect { |t| t.locale == locale } :
44 record.translations.by_locale(locale)
45 end
46
47 def fetch_translations(locale)
48 # only query if not already included with :include => translations
49 record.translations.loaded? ? record.translations :
50 record.translations.by_locales(Globalize.fallbacks(locale))
51 end
52
53 def fetch_attribute(locale, attr_name)
54 translations = fetch_translations(locale)
55 value, requested_locale = nil, locale
56
57 Globalize.fallbacks(locale).each do |fallback|
58 translation = translations.detect { |t| t.locale == fallback }
59 value = translation && translation.send(attr_name)
60 locale = fallback && break if value
61 end
62
63 set_metadata(value, :locale => locale, :requested_locale => requested_locale)
64 value
65 end
66
67 def set_metadata(object, metadata)
68 if object.respond_to?(:translation_metadata)
69 object.translation_metadata.merge!(meta_data)
70 end
71 end
72
73 def translation_metadata_accessor(object)
74 return if obj.respond_to?(:translation_metadata)
75 class << object; attr_accessor :translation_metadata end
76 object.translation_metadata ||= {}
77 end
78 end
79 end
80end
diff --git a/vendor/plugins/globalize2/lib/globalize/active_record/attributes.rb b/vendor/plugins/globalize2/lib/globalize/active_record/attributes.rb
new file mode 100644
index 0000000..7bd923c
--- /dev/null
+++ b/vendor/plugins/globalize2/lib/globalize/active_record/attributes.rb
@@ -0,0 +1,25 @@
1# Helper class for storing values per locale. Used by Globalize::Adapter
2# to stash and cache attribute values.
3module Globalize
4 module ActiveRecord
5 class Attributes < Hash
6 def [](locale)
7 locale = locale.to_sym
8 self[locale] = {} unless has_key?(locale)
9 self.fetch(locale)
10 end
11
12 def contains?(locale, attr_name)
13 self[locale].has_key?(attr_name)
14 end
15
16 def read(locale, attr_name)
17 self[locale][attr_name]
18 end
19
20 def write(locale, attr_name, value)
21 self[locale][attr_name] = value
22 end
23 end
24 end
25end
diff --git a/vendor/plugins/globalize2/lib/globalize/active_record/migration.rb b/vendor/plugins/globalize2/lib/globalize/active_record/migration.rb
new file mode 100644
index 0000000..ebff3b6
--- /dev/null
+++ b/vendor/plugins/globalize2/lib/globalize/active_record/migration.rb
@@ -0,0 +1,40 @@
1module Globalize
2 module ActiveRecord
3 module Migration
4 def create_translation_table!(fields)
5 translated_attribute_names.each do |f|
6 raise MigrationMissingTranslatedField, "Missing translated field #{f}" unless fields[f]
7 end
8
9 fields.each do |name, type|
10 if translated_attribute_names.include?(name) && ![:string, :text].include?(type)
11 raise BadMigrationFieldType, "Bad field type for #{name}, should be :string or :text"
12 end
13 end
14
15 self.connection.create_table(translation_table_name) do |t|
16 t.references self.table_name.singularize
17 t.string :locale
18 fields.each do |name, type|
19 t.column name, type
20 end
21 t.timestamps
22 end
23
24 self.connection.add_index(translation_table_name, "#{self.table_name.singularize}_id", :name => translation_index_name)
25 end
26
27 def translation_index_name
28 require 'digest/sha1'
29 # FIXME what's the max size of an index name?
30 index_name = "index_#{translation_table_name}_on_#{self.table_name.singularize}_id"
31 index_name.size < 50 ? index_name : "index_#{Digest::SHA1.hexdigest(index_name)}"
32 end
33
34 def drop_translation_table!
35 self.connection.remove_index(translation_table_name, :name => translation_index_name) rescue nil
36 self.connection.drop_table(translation_table_name)
37 end
38 end
39 end
40end
diff --git a/vendor/plugins/globalize2/lib/globalize/backend/chain.rb b/vendor/plugins/globalize2/lib/globalize/backend/chain.rb
deleted file mode 100644
index bb8679e..0000000
--- a/vendor/plugins/globalize2/lib/globalize/backend/chain.rb
+++ /dev/null
@@ -1,102 +0,0 @@
1module I18n
2 class << self
3 def chain_backends(*args)
4 self.backend = Globalize::Backend::Chain.new(*args)
5 end
6 end
7end
8
9module Globalize
10 module Backend
11 class Chain
12 def initialize(*args)
13 add(*args) unless args.empty?
14 end
15
16 # Change this to a) accept any number of backends and b) accept classes.
17 # When classes are passed instantiate them and add the instances as backends.
18 # Return the added backends from #add.
19 #
20 # Add an initialize method that accepts the same arguments and passes them
21 # to #add, so we could:
22 # I18n.backend = Globalize::Backend::Chain.new(Globalize::Backend::Foo, Globalize::Backend::Bar)
23 # Globalize::Backend::Chain.new(:foo, :bar)
24 # Globalize.chain_backends :foo, :bar
25 def add(*backends)
26 backends.each do |backend|
27 backend = Globalize::Backend.const_get(backend.to_s.capitalize) if backend.is_a? Symbol
28 backend = backend.new if backend.is_a? Class
29 self.backends << backend
30 end
31 end
32
33 def load_translations(*args)
34 backends.each{|backend| backend.load_translations(*args) }
35 end
36
37 # For defaults:
38 # Never pass any default option to the backends but instead implement our own default
39 # mechanism (e.g. symbols as defaults would need to be passed to the whole chain to
40 # be translated).
41 #
42 # For namespace lookup:
43 # Only return if the result is not a hash OR count is not present, otherwise merge them.
44 # So in effect the count variable would control whether we have a namespace lookup or a
45 # pluralization going on.
46 #
47 # Exceptions:
48 # Make sure that we catch MissingTranslationData exceptions and raise
49 # one in the end when no translation was found at all.
50 #
51 # For bulk translation:
52 # If the key is an array we need to call #translate for each of the
53 # keys and collect the results.
54
55 def translate(locale, key, options = {})
56 raise I18n::InvalidLocale.new(locale) if locale.nil?
57 return key.map{|k| translate locale, k, options } if key.is_a? Array
58
59 default = options.delete(:default)
60 result = backends.inject({}) do |namespace, backend|
61 begin
62 translation = backend.translate(locale.to_sym, key, options)
63 if namespace_lookup?(translation, options)
64 namespace.merge! translation
65 elsif translation
66 return translation
67 end
68 rescue I18n::MissingTranslationData
69 end
70 end
71 result || default(locale, default, options) || raise(I18n::MissingTranslationData.new(locale, key, options))
72 end
73
74 def localize(locale, object, format = :default)
75 backends.each do |backend|
76 result = backend.localize(locale, object, format) and return result
77 end
78 end
79
80 protected
81 def backends
82 @backends ||= []
83 end
84
85 def default(locale, default, options = {})
86 case default
87 when String then default
88 when Symbol then translate locale, default, options
89 when Array then default.each do |obj|
90 result = default(locale, obj, options.dup) and return result
91 end and nil
92 end
93 rescue I18n::MissingTranslationData
94 nil
95 end
96
97 def namespace_lookup?(result, options)
98 result.is_a?(Hash) and not options.has_key?(:count)
99 end
100 end
101 end
102end \ No newline at end of file
diff --git a/vendor/plugins/globalize2/lib/globalize/backend/pluralizing.rb b/vendor/plugins/globalize2/lib/globalize/backend/pluralizing.rb
deleted file mode 100644
index 80016f2..0000000
--- a/vendor/plugins/globalize2/lib/globalize/backend/pluralizing.rb
+++ /dev/null
@@ -1,37 +0,0 @@
1require 'i18n/backend/simple'
2
3module Globalize
4 module Backend
5 class Pluralizing < I18n::Backend::Simple
6 def pluralize(locale, entry, count)
7 return entry unless entry.is_a?(Hash) and count
8 key = :zero if count == 0 && entry.has_key?(:zero)
9 key ||= pluralizer(locale).call(count)
10 raise InvalidPluralizationData.new(entry, count) unless entry.has_key?(key)
11 translation entry[key], :plural_key => key
12 end
13
14 def add_pluralizer(locale, pluralizer)
15 pluralizers[locale.to_sym] = pluralizer
16 end
17
18 def pluralizer(locale)
19 pluralizers[locale.to_sym] || default_pluralizer
20 end
21
22 protected
23 def default_pluralizer
24 pluralizers[:en]
25 end
26
27 def pluralizers
28 @pluralizers ||= { :en => lambda{|n| n == 1 ? :one : :other } }
29 end
30
31 # Overwrite this method to return something other than a String
32 def translation(string, attributes)
33 string
34 end
35 end
36 end
37end \ No newline at end of file
diff --git a/vendor/plugins/globalize2/lib/globalize/backend/static.rb b/vendor/plugins/globalize2/lib/globalize/backend/static.rb
deleted file mode 100644
index fb9e1fe..0000000
--- a/vendor/plugins/globalize2/lib/globalize/backend/static.rb
+++ /dev/null
@@ -1,60 +0,0 @@
1require 'globalize/backend/pluralizing'
2require 'globalize/locale/fallbacks'
3require 'globalize/translation'
4
5module Globalize
6 module Backend
7 class Static < Pluralizing
8 def initialize(*args)
9 add(*args) unless args.empty?
10 end
11
12 def translate(locale, key, options = {})
13 result, default, fallback = nil, options.delete(:default), nil
14 I18n.fallbacks[locale].each do |fallback|
15 begin
16 result = super(fallback, key, options) and break
17 rescue I18n::MissingTranslationData
18 end
19 end
20 result ||= default locale, default, options
21
22 attrs = {:requested_locale => locale, :locale => fallback, :key => key, :options => options}
23 translation(result, attrs) || raise(I18n::MissingTranslationData.new(locale, key, options))
24 end
25
26 protected
27
28 alias :orig_interpolate :interpolate unless method_defined? :orig_interpolate
29 def interpolate(locale, string, values = {})
30 result = orig_interpolate(locale, string, values)
31 translation = translation(string)
32 translation.nil? ? result : translation.replace(result)
33 end
34
35 def translation(result, meta = nil)
36 return unless result
37
38 case result
39 when Numeric
40 result
41 when String
42 result = Translation::Static.new(result) unless result.is_a? Translation::Static
43 result.set_meta meta
44 result
45 when Hash
46 Hash[*result.map do |key, value|
47 [key, translation(value, meta)]
48 end.flatten]
49 when Array
50 result.map do |value|
51 translation(value, meta)
52 end
53 else
54 result
55 # raise "unexpected translation type: #{result.inspect}"
56 end
57 end
58 end
59 end
60end \ No newline at end of file
diff --git a/vendor/plugins/globalize2/lib/globalize/load_path.rb b/vendor/plugins/globalize2/lib/globalize/load_path.rb
deleted file mode 100644
index a49825b..0000000
--- a/vendor/plugins/globalize2/lib/globalize/load_path.rb
+++ /dev/null
@@ -1,63 +0,0 @@
1# Locale load_path and Locale loading support.
2#
3# To use this include the Globalize::LoadPath::I18n module to I18n like this:
4#
5# I18n.send :include, Globalize::LoadPath::I18n
6#
7# Clients can add load_paths using:
8#
9# I18n.load_path.add load_path, 'rb', 'yml' # pass any number of extensions like this
10# I18n.load_path << 'path/to/dir' # usage without an extension, defaults to 'yml'
11#
12# And load locale data using either of:
13#
14# I18n.load_locales 'en-US', 'de-DE'
15# I18n.load_locale 'en-US'
16#
17# This will lookup all files named like:
18#
19# 'path/to/dir/all.yml'
20# 'path/to/dir/en-US.yml'
21# 'path/to/dir/en-US/*.yml'
22#
23# The filenames will be passed to I18n.load_translations which delegates to
24# the backend. So the actual behaviour depends on the implementation of the
25# backend. I18n::Backend::Simple will be able to read YAML and plain Ruby
26# files. See the documentation for I18n.load_translations for details.
27
28module Globalize
29 class LoadPath < Array
30 def extensions
31 @extensions ||= ['rb', 'yml']
32 end
33 attr_writer :extensions
34
35 def locales
36 @locales ||= ['*']
37 end
38 attr_writer :locales
39
40 def <<(path)
41 push path
42 end
43
44 def push(*paths)
45 super(*paths.map{|path| filenames(path) }.flatten.uniq.sort)
46 end
47
48 protected
49
50 def filenames(path)
51 return [path] if File.file? path
52 patterns(path).map{|pattern| Dir[pattern] }
53 end
54
55 def patterns(path)
56 locales.map do |locale|
57 extensions.map do |extension|
58 %W(#{path}/all.#{extension} #{path}/#{locale}.#{extension} #{path}/#{locale}/**/*.#{extension})
59 end
60 end.flatten.uniq
61 end
62 end
63end \ No newline at end of file
diff --git a/vendor/plugins/globalize2/lib/globalize/locale/fallbacks.rb b/vendor/plugins/globalize2/lib/globalize/locale/fallbacks.rb
deleted file mode 100644
index c4acd57..0000000
--- a/vendor/plugins/globalize2/lib/globalize/locale/fallbacks.rb
+++ /dev/null
@@ -1,63 +0,0 @@
1require 'globalize/locale/language_tag'
2
3module I18n
4 @@fallbacks = nil
5
6 class << self
7 # Returns the current fallbacks. Defaults to +Globalize::Locale::Fallbacks+.
8 def fallbacks
9 @@fallbacks ||= Globalize::Locale::Fallbacks.new
10 end
11
12 # Sets the current fallbacks. Used to set a custom fallbacks instance.
13 def fallbacks=(fallbacks)
14 @@fallbacks = fallbacks
15 end
16 end
17end
18
19module Globalize
20 module Locale
21 class Fallbacks < Hash
22 def initialize(*defaults)
23 @map = {}
24 map defaults.pop if defaults.last.is_a?(Hash)
25
26 defaults = [I18n.default_locale.to_sym] if defaults.empty?
27 self.defaults = defaults
28 end
29
30 def defaults=(defaults)
31 @defaults = defaults.map{|default| compute(default, false) }.flatten << :root
32 end
33 attr_reader :defaults
34
35 def [](tag)
36 tag = tag.to_sym
37 has_key?(tag) ? fetch(tag) : store(tag, compute(tag))
38 end
39
40 def map(mappings)
41 mappings.each do |from, to|
42 from, to = from.to_sym, Array(to)
43 to.each do |to|
44 @map[from] ||= []
45 @map[from] << to.to_sym
46 end
47 end
48 end
49
50 protected
51
52 def compute(tags, include_defaults = true)
53 result = Array(tags).collect do |tag|
54 tags = LanguageTag::tag(tag.to_sym).parents(true).map! {|t| t.to_sym }
55 tags.each{|tag| tags += compute(@map[tag]) if @map[tag] }
56 tags
57 end.flatten
58 result.push *defaults if include_defaults
59 result.uniq
60 end
61 end
62 end
63end
diff --git a/vendor/plugins/globalize2/lib/globalize/locale/language_tag.rb b/vendor/plugins/globalize2/lib/globalize/locale/language_tag.rb
deleted file mode 100644
index d9aae54..0000000
--- a/vendor/plugins/globalize2/lib/globalize/locale/language_tag.rb
+++ /dev/null
@@ -1,81 +0,0 @@
1# for specifications see http://en.wikipedia.org/wiki/IETF_language_tag
2#
3# SimpleParser does not implement advanced usages such as grandfathered tags
4
5module Globalize
6 module Locale
7 module Rfc4646
8 SUBTAGS = [:language, :script, :region, :variant, :extension, :privateuse, :grandfathered]
9 FORMATS = {:language => :downcase, :script => :capitalize, :region => :upcase, :variant => :downcase}
10 end
11
12 class LanguageTag < Struct.new(*Rfc4646::SUBTAGS)
13 class << self
14 def parser
15 @@parser ||= SimpleParser
16 end
17
18 def parser=(parser)
19 @@parser = parser
20 end
21
22 def tag(tag)
23 matches = parser.match(tag)
24 new *matches if matches
25 end
26 end
27
28 Rfc4646::FORMATS.each do |name, format|
29 define_method(name) { self[name].send(format) unless self[name].nil? }
30 end
31
32 def to_sym
33 to_s.to_sym
34 end
35
36 def to_s
37 @tag ||= to_a.compact.join("-")
38 end
39
40 def to_a
41 members.collect {|attr| self.send(attr) }
42 end
43
44 def parent
45 segs = to_a.compact
46 segs.length < 2 ? nil : LanguageTag.tag(segs[0..(segs.length-2)].join('-'))
47 end
48
49 def parents(include_self = true)
50 result, parent = [], self.dup
51 result << parent if include_self
52 while parent = parent.parent
53 result << parent
54 end
55 result
56 end
57
58 module SimpleParser
59 PATTERN = %r{\A(?:
60 ([a-z]{2,3}(?:(?:-[a-z]{3}){0,3})?|[a-z]{4}|[a-z]{5,8}) # language
61 (?:-([a-z]{4}))? # script
62 (?:-([a-z]{2}|\d{3}))? # region
63 (?:-([0-9a-z]{5,8}|\d[0-9a-z]{3}))* # variant
64 (?:-([0-9a-wyz](?:-[0-9a-z]{2,8})+))* # extension
65 (?:-(x(?:-[0-9a-z]{1,8})+))?| # privateuse subtag
66 (x(?:-[0-9a-z]{1,8})+)| # privateuse tag
67 /* ([a-z]{1,3}(?:-[0-9a-z]{2,8}){1,2}) */ # grandfathered
68 )\z}xi
69
70 class << self
71 def match(tag)
72 c = PATTERN.match(tag.to_s).captures
73 c[0..4] << (c[5].nil? ? c[6] : c[5]) << c[7] # TODO c[7] is grandfathered, throw a NotImplemented exception here?
74 rescue
75 false
76 end
77 end
78 end
79 end
80 end
81end
diff --git a/vendor/plugins/globalize2/lib/globalize/model/active_record.rb b/vendor/plugins/globalize2/lib/globalize/model/active_record.rb
deleted file mode 100644
index 9218054..0000000
--- a/vendor/plugins/globalize2/lib/globalize/model/active_record.rb
+++ /dev/null
@@ -1,45 +0,0 @@
1require 'globalize/translation'
2require 'globalize/locale/fallbacks'
3require 'globalize/model/active_record/adapter'
4require 'globalize/model/active_record/translated'
5
6module Globalize
7 module Model
8 module ActiveRecord
9 class << self
10 def create_proxy_class(klass)
11 module_names = klass.name.split('::')
12 klass_name = module_names.pop
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
18 def locale
19 read_attribute(:locale).to_sym
20 end
21
22 def locale=(locale)
23 write_attribute(:locale, locale.to_s)
24 end
25 }
26 end
27
28 def define_accessors(klass, attr_names)
29 attr_names.each do |attr_name|
30 klass.send :define_method, attr_name, lambda {
31 globalize.fetch self.class.locale, attr_name
32 }
33 klass.send :define_method, "#{attr_name}_before_type_cast", lambda {
34 globalize.fetch self.class.locale, attr_name
35 }
36 klass.send :define_method, "#{attr_name}=", lambda {|val|
37 globalize.stash self.class.locale, attr_name, val
38 self[attr_name] = val
39 }
40 end
41 end
42 end
43 end
44 end
45end \ No newline at end of file
diff --git a/vendor/plugins/globalize2/lib/globalize/model/active_record/adapter.rb b/vendor/plugins/globalize2/lib/globalize/model/active_record/adapter.rb
deleted file mode 100644
index 93355b8..0000000
--- a/vendor/plugins/globalize2/lib/globalize/model/active_record/adapter.rb
+++ /dev/null
@@ -1,96 +0,0 @@
1module Globalize
2 module Model
3 class AttributeStash < Hash
4 def contains?(locale, attr_name)
5 locale = locale.to_sym
6 self[locale] ||= {}
7 self[locale].has_key? attr_name
8 end
9
10 def read(locale, attr_name)
11 locale = locale.to_sym
12 self[locale] ||= {}
13 self[locale][attr_name]
14 end
15
16 def write(locale, attr_name, value)
17 locale = locale.to_sym
18 self[locale] ||= {}
19 self[locale][attr_name] = value
20 end
21 end
22
23 class Adapter
24 def initialize(record)
25 @record = record
26
27 # TODO what exactly are the roles of cache and stash
28 @cache = AttributeStash.new
29 @stash = AttributeStash.new
30 end
31
32 def fetch(locale, attr_name)
33 # locale = I18n.locale
34 is_cached = @cache.contains?(locale, attr_name)
35 is_cached ? @cache.read(locale, attr_name) : begin
36 value = fetch_attribute locale, attr_name
37 @cache.write locale, attr_name, value if value && value.locale == locale
38 value
39 end
40 end
41
42 def stash(locale, attr_name, value)
43 @stash.write locale, attr_name, value
44 @cache.write locale, attr_name, value
45 end
46
47 def update_translations!
48 @stash.each do |locale, attrs|
49 translation = @record.globalize_translations.find_or_initialize_by_locale(locale.to_s)
50 attrs.each{|attr_name, value| translation[attr_name] = value }
51 translation.save!
52 end
53 @stash.clear
54 end
55
56 # Clears the cache
57 def clear
58 @cache.clear
59 @stash.clear
60 end
61
62 private
63
64 def fetch_attribute(locale, attr_name)
65 fallbacks = I18n.fallbacks[locale].map{|tag| tag.to_s}.map(&:to_sym)
66
67 # If the translations were included with
68 # :include => globalize_translations
69 # there is no need to query them again.
70 unless @record.globalize_translations.loaded?
71 translations = @record.globalize_translations.by_locales(fallbacks)
72 else
73 translations = @record.globalize_translations
74 end
75 result, requested_locale = nil, locale
76
77 # Walk through the fallbacks, starting with the current locale itself, and moving
78 # to the next best choice, until we find a match.
79 # Check the @globalize_set_translations cache first to see if we've just changed the
80 # attribute and not saved yet.
81 fallbacks.each do |fallback|
82 # TODO should we be checking stash or just cache?
83 result = @stash.read(fallback, attr_name) || begin
84 translation = translations.detect {|tr| tr.locale == fallback }
85 translation && translation.send(attr_name)
86 end
87 if result
88 locale = fallback
89 break
90 end
91 end
92 result && Translation::Attribute.new(result, :locale => locale, :requested_locale => requested_locale)
93 end
94 end
95 end
96end
diff --git a/vendor/plugins/globalize2/lib/globalize/model/active_record/translated.rb b/vendor/plugins/globalize2/lib/globalize/model/active_record/translated.rb
deleted file mode 100644
index 4f75c8a..0000000
--- a/vendor/plugins/globalize2/lib/globalize/model/active_record/translated.rb
+++ /dev/null
@@ -1,164 +0,0 @@
1require 'digest/sha1'
2
3module Globalize
4 module Model
5 class MigrationError < StandardError; end
6 class MigrationMissingTranslatedField < MigrationError; end
7 class BadMigrationFieldType < MigrationError; end
8
9 module ActiveRecord
10 module Translated
11 def self.included(base)
12 base.extend ActMethods
13 end
14
15 module ActMethods
16 def translates(*attr_names)
17 options = attr_names.extract_options!
18 options[:translated_attributes] = attr_names
19
20 # Only set up once per class
21 unless included_modules.include? InstanceMethods
22 class_inheritable_accessor :globalize_options, :globalize_proxy
23
24 include InstanceMethods
25 extend ClassMethods
26
27 self.globalize_proxy = Globalize::Model::ActiveRecord.create_proxy_class(self)
28 has_many(
29 :globalize_translations,
30 :class_name => globalize_proxy.name,
31 :extend => Extensions,
32 :dependent => :delete_all,
33 :foreign_key => class_name.foreign_key
34 )
35
36 after_save :update_globalize_record
37 end
38
39 self.globalize_options = options
40 Globalize::Model::ActiveRecord.define_accessors(self, attr_names)
41
42 # Import any callbacks that have been defined by extensions to Globalize2
43 # and run them.
44 extend Callbacks
45 Callbacks.instance_methods.each { |callback| send(callback) }
46 end
47
48 def locale=(locale)
49 @@locale = locale
50 end
51
52 def locale
53 (defined?(@@locale) && @@locale) || I18n.locale
54 end
55 end
56
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.
59 module Callbacks
60 end
61
62 # Extension to the has_many :globalize_translations association
63 module Extensions
64 def by_locales(locales)
65 find :all, :conditions => { :locale => locales.map(&:to_s) }
66 end
67 end
68
69 module ClassMethods
70 def method_missing(method, *args)
71 if method.to_s =~ /^find_by_(\w+)$/ && globalize_options[:translated_attributes].include?($1.to_sym)
72 find(:first, :joins => :globalize_translations,
73 :conditions => [ "#{i18n_attr($1)} = ? AND #{i18n_attr('locale')} IN (?)",
74 args.first,I18n.fallbacks[I18n.locale].map{|tag| tag.to_s}])
75 else
76 super
77 end
78 end
79
80 def create_translation_table!(fields)
81 translated_fields = self.globalize_options[:translated_attributes]
82 translated_fields.each do |f|
83 raise MigrationMissingTranslatedField, "Missing translated field #{f}" unless fields[f]
84 end
85
86 fields.each do |name, type|
87 if translated_fields.include?(name) && ![:string, :text].include?(type)
88 raise BadMigrationFieldType, "Bad field type for #{name}, should be :string or :text"
89 end
90 end
91
92 self.connection.create_table(translation_table_name) do |t|
93 t.references self.table_name.singularize
94 t.string :locale
95 fields.each do |name, type|
96 t.column name, type
97 end
98 t.timestamps
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)}"
112 end
113
114 def drop_translation_table!
115 self.connection.remove_index(translation_table_name, :name => translation_index_name)
116 self.connection.drop_table translation_table_name
117 end
118
119 private
120
121 def i18n_attr(attribute_name)
122 self.base_class.name.underscore.gsub('/', '_') + "_translations.#{attribute_name}"
123 end
124 end
125
126 module InstanceMethods
127 def reload(options = nil)
128 globalize.clear
129
130 # clear all globalized attributes
131 # TODO what's the best way to handle this?
132 self.class.globalize_options[:translated_attributes].each do |attr|
133 @attributes.delete(attr.to_s)
134 end
135
136 super(options)
137 end
138
139 def globalize
140 @globalize ||= Adapter.new self
141 end
142
143 def update_globalize_record
144 globalize.update_translations!
145 end
146
147 def translated_locales
148 globalize_translations.scoped(:select => 'DISTINCT locale').map do |translation|
149 translation.locale.to_sym
150 end
151 end
152
153 def set_translations options
154 options.keys.each do |key|
155 translation = globalize_translations.find_by_locale(key.to_s) ||
156 globalize_translations.build(:locale => key.to_s)
157 translation.update_attributes!(options[key])
158 end
159 end
160 end
161 end
162 end
163 end
164end \ No newline at end of file
diff --git a/vendor/plugins/globalize2/lib/globalize/translation.rb b/vendor/plugins/globalize2/lib/globalize/translation.rb
deleted file mode 100644
index a80afcd..0000000
--- a/vendor/plugins/globalize2/lib/globalize/translation.rb
+++ /dev/null
@@ -1,32 +0,0 @@
1module Globalize
2 # Translations are simple value objects that carry some context information
3 # alongside the actual translation string.
4
5 class Translation < String
6 class Attribute < Translation
7 attr_accessor :requested_locale, :locale, :key
8 end
9
10 class Static < Translation
11 attr_accessor :requested_locale, :locale, :key, :options, :plural_key, :original
12
13 def initialize(string, meta = nil)
14 self.original = string
15 super
16 end
17 end
18
19 def initialize(string, meta = nil)
20 set_meta meta
21 super string
22 end
23
24 def fallback?
25 locale.to_sym != requested_locale.to_sym
26 end
27
28 def set_meta(meta)
29 meta.each {|name, value| send :"#{name}=", value } if meta
30 end
31 end
32end \ No newline at end of file
diff --git a/vendor/plugins/globalize2/lib/globalize/i18n/missing_translations_log_handler.rb b/vendor/plugins/globalize2/lib/i18n/missing_translations_log_handler.rb
index 3f06ac2..24c1089 100644
--- a/vendor/plugins/globalize2/lib/globalize/i18n/missing_translations_log_handler.rb
+++ b/vendor/plugins/globalize2/lib/i18n/missing_translations_log_handler.rb
@@ -2,10 +2,10 @@
2# but additionally logs missing translations to a given log. 2# but additionally logs missing translations to a given log.
3# 3#
4# Useful for identifying missing translations during testing. 4# Useful for identifying missing translations during testing.
5#
6# E.g.
7# 5#
8# require 'globalize/i18n/missing_translations_log_handler 6# E.g.
7#
8# require 'globalize/i18n/missing_translations_log_handler'
9# I18n.missing_translations_logger = RAILS_DEFAULT_LOGGER 9# I18n.missing_translations_logger = RAILS_DEFAULT_LOGGER
10# I18n.exception_handler = :missing_translations_log_handler 10# I18n.exception_handler = :missing_translations_log_handler
11# 11#
@@ -16,7 +16,7 @@
16 16
17module I18n 17module I18n
18 @@missing_translations_logger = nil 18 @@missing_translations_logger = nil
19 19
20 class << self 20 class << self
21 def missing_translations_logger 21 def missing_translations_logger
22 @@missing_translations_logger ||= begin 22 @@missing_translations_logger ||= begin
@@ -24,18 +24,18 @@ module I18n
24 Logger.new(STDOUT) 24 Logger.new(STDOUT)
25 end 25 end
26 end 26 end
27 27
28 def missing_translations_logger=(logger) 28 def missing_translations_logger=(logger)
29 @@missing_translations_logger = logger 29 @@missing_translations_logger = logger
30 end 30 end
31 31
32 def missing_translations_log_handler(exception, locale, key, options) 32 def missing_translations_log_handler(exception, locale, key, options)
33 if MissingTranslationData === exception 33 if MissingTranslationData === exception
34 missing_translations_logger.warn(exception.message) 34 missing_translations_logger.warn(exception.message)
35 return exception.message 35 return exception.message
36 else 36 else
37 raise exception 37 raise exception
38 end 38 end
39 end 39 end
40 end 40 end
41end \ No newline at end of file 41end
diff --git a/vendor/plugins/globalize2/lib/globalize/i18n/missing_translations_raise_handler.rb b/vendor/plugins/globalize2/lib/i18n/missing_translations_raise_handler.rb
index e32be28..18237b1 100644
--- a/vendor/plugins/globalize2/lib/globalize/i18n/missing_translations_raise_handler.rb
+++ b/vendor/plugins/globalize2/lib/i18n/missing_translations_raise_handler.rb
@@ -2,10 +2,10 @@
2# but also raises on missing translations. 2# but also raises on missing translations.
3# 3#
4# Useful for identifying missing translations during testing. 4# Useful for identifying missing translations during testing.
5#
6# E.g.
7# 5#
8# require 'globalize/i18n/missing_translations_raise_handler 6# E.g.
7#
8# require 'globalize/i18n/missing_translations_raise_handler'
9# I18n.exception_handler = :missing_translations_raise_handler 9# I18n.exception_handler = :missing_translations_raise_handler
10module I18n 10module I18n
11 class << self 11 class << self
@@ -13,8 +13,6 @@ module I18n
13 raise exception 13 raise exception
14 end 14 end
15 end 15 end
16
17# self.exception_handler = :missing_translations_raise_handler
18end 16end
19 17
20I18n.exception_handler = :missing_translations_raise_handler 18I18n.exception_handler = :missing_translations_raise_handler
diff --git a/vendor/plugins/globalize2/lib/locale/root.yml b/vendor/plugins/globalize2/lib/locale/root.yml
deleted file mode 100644
index 2ec2b3b..0000000
--- a/vendor/plugins/globalize2/lib/locale/root.yml
+++ /dev/null
@@ -1,3 +0,0 @@
1root:
2 bidi:
3 direction: left-to-right \ No newline at end of file
diff --git a/vendor/plugins/globalize2/lib/rails_edge_load_path_patch.rb b/vendor/plugins/globalize2/lib/rails_edge_load_path_patch.rb
deleted file mode 100644
index e6f90d8..0000000
--- a/vendor/plugins/globalize2/lib/rails_edge_load_path_patch.rb
+++ /dev/null
@@ -1,40 +0,0 @@
1module I18n
2 @@load_path = nil
3 @@default_locale = :'en-US'
4
5 class << self
6 def load_path
7 @@load_path ||= []
8 end
9
10 def load_path=(load_path)
11 @@load_path = load_path
12 end
13 end
14end
15
16I18n::Backend::Simple.module_eval do
17 def initialized?
18 @initialized ||= false
19 end
20
21 protected
22
23 def init_translations
24 load_translations(*I18n.load_path)
25 @initialized = true
26 end
27
28 def lookup(locale, key, scope = [])
29 return unless key
30 init_translations unless initialized?
31 keys = I18n.send :normalize_translation_keys, locale, key, scope
32 keys.inject(translations){|result, k| result[k.to_sym] or return nil }
33 end
34end
35
36rails_dir = File.expand_path "#{File.dirname(__FILE__)}/../../../rails/"
37paths = %w(actionpack/lib/action_view/locale/en-US.yml
38 activerecord/lib/active_record/locale/en-US.yml
39 activesupport/lib/active_support/locale/en-US.yml)
40paths.each{|path| I18n.load_path << "#{rails_dir}/#{path}" }
diff --git a/vendor/plugins/globalize2/notes.textile b/vendor/plugins/globalize2/notes.textile
deleted file mode 100644
index b686f41..0000000
--- a/vendor/plugins/globalize2/notes.textile
+++ /dev/null
@@ -1,51 +0,0 @@
1Stopped DB Backend in the middle, here's where we left off:
2
3h1. Some Notes
4
5* Started doing the migration generator in generators/db_backend.rb
6* Translation keys will be in dotted string format
7* Question: Do we need a plural_key column, or can we build it in to the dotted key?
8* We will probably have to code the following methods from scratch, to optimize db calls:
9** translate
10** localize
11** pluralize
12* We should refactor @interpolation@ code so that it can be included into backend code without inheriting SimpleBackend
13** Rationale: interpolation is something done entirely after a string is fetched from the data store
14** Alternately, it could be done from within the I18n module
15
16h1. Schema
17
18There will be two db tables.
19
20# globalize_translations will have: locale, key, translation, created_at, updated_at.
21# globalize_translations_map will have: key, translation_id.
22
23globalize_translations_map will let us easily fetch entire sub-trees of namespaces.
24However, this table may not be necessary, as it may be feasible to just use key LIKE "some.namespace.%".
25
26h1. Caching
27
28We'll almost certainly want to implement caching in the backend. Should probably be a customized
29implementation based on the Rails caching mechanism, to support memcached, etc.
30
31h1. Queries
32
33We'll want to pull in lots of stuff at once and return a single translation based on some
34quick Ruby selection. The query will look something like this:
35
36<pre>
37<code>
38SELECT * FROM globalize_translations
39WHERE locale in (<fallbacks>) AND
40key IN (key, default_key)
41</code>
42</pre>
43
44The Ruby code would then pick the first translation that satisfies a fallback, in fallback order.
45Of course, the records with the supplied key would take precedence of those with the default key.
46
47h1. Misc
48
49We should revisit the :zero plural code. On the one hand it's certainly useful for
50many apps in many languages. On the other hand it's not mentioned in CLDR, and not a real
51concept in language pluralization. Right now, I'm feeling it's still a good idea to keep it in.
diff --git a/vendor/plugins/globalize2/test/active_record/fallbacks_test.rb b/vendor/plugins/globalize2/test/active_record/fallbacks_test.rb
new file mode 100644
index 0000000..449ec8b
--- /dev/null
+++ b/vendor/plugins/globalize2/test/active_record/fallbacks_test.rb
@@ -0,0 +1,102 @@
1require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2require File.expand_path(File.dirname(__FILE__) + '/../data/models')
3
4if I18n.respond_to?(:fallbacks)
5 class TranslatedTest < ActiveSupport::TestCase
6 def setup
7 I18n.locale = :'en-US'
8 I18n.fallbacks.clear
9 reset_db!
10 ActiveRecord::Base.locale = nil
11 end
12
13 def teardown
14 I18n.fallbacks.clear
15 end
16
17 test "keeping one field in new locale when other field is changed" do
18 I18n.fallbacks.map 'de-DE' => [ 'en-US' ]
19 post = Post.create :subject => 'foo'
20 I18n.locale = 'de-DE'
21 post.content = 'bar'
22 assert_equal 'foo', post.subject
23 end
24
25 test "modifying non-required field in a new locale" do
26 I18n.fallbacks.map 'de-DE' => [ 'en-US' ]
27 post = Post.create :subject => 'foo'
28 I18n.locale = 'de-DE'
29 post.content = 'bar'
30 assert post.save
31 end
32
33 test "resolves a simple fallback" do
34 I18n.locale = 'de-DE'
35 post = Post.create :subject => 'foo'
36 I18n.locale = 'de'
37 post.subject = 'baz'
38 post.content = 'bar'
39 post.save
40 I18n.locale = 'de-DE'
41 assert_equal 'foo', post.subject
42 assert_equal 'bar', post.content
43 end
44
45 test "resolves a simple fallback without reloading" do
46 I18n.locale = 'de-DE'
47 post = Post.new :subject => 'foo'
48 I18n.locale = 'de'
49 post.subject = 'baz'
50 post.content = 'bar'
51 I18n.locale = 'de-DE'
52 assert_equal 'foo', post.subject
53 assert_equal 'bar', post.content
54 end
55
56 test "resolves a complex fallback without reloading" do
57 I18n.fallbacks.map 'de' => %w(en he)
58 I18n.locale = 'de'
59 post = Post.new
60 I18n.locale = 'en'
61 post.subject = 'foo'
62 I18n.locale = 'he'
63 post.subject = 'baz'
64 post.content = 'bar'
65 I18n.locale = 'de'
66 assert_equal 'foo', post.subject
67 assert_equal 'bar', post.content
68 end
69
70 test 'fallbacks with lots of locale switching' do
71 I18n.fallbacks.map :'de-DE' => [ :'en-US' ]
72 post = Post.create :subject => 'foo'
73
74 I18n.locale = :'de-DE'
75 assert_equal 'foo', post.subject
76
77 I18n.locale = :'en-US'
78 post.update_attribute :subject, 'bar'
79
80 I18n.locale = :'de-DE'
81 assert_equal 'bar', post.subject
82 end
83
84 test 'fallbacks with lots of locale switching' do
85 I18n.fallbacks.map :'de-DE' => [ :'en-US' ]
86 child = Child.create :content => 'foo'
87
88 I18n.locale = :'de-DE'
89 assert_equal 'foo', child.content
90
91 I18n.locale = :'en-US'
92 child.update_attribute :content, 'bar'
93
94 I18n.locale = :'de-DE'
95 assert_equal 'bar', child.content
96 end
97 end
98end
99
100# TODO should validate_presence_of take fallbacks into account? maybe we need
101# an extra validation call, or more options for validate_presence_of.
102
diff --git a/vendor/plugins/globalize2/test/model/active_record/migration_test.rb b/vendor/plugins/globalize2/test/active_record/migration_test.rb
index eb6533d..359d811 100644
--- a/vendor/plugins/globalize2/test/model/active_record/migration_test.rb
+++ b/vendor/plugins/globalize2/test/active_record/migration_test.rb
@@ -1,36 +1,32 @@
1require File.join( File.dirname(__FILE__), '..', '..', 'test_helper' ) 1require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2require 'active_record' 2require File.expand_path(File.dirname(__FILE__) + '/../data/models')
3require 'globalize/model/active_record'
4
5# Hook up model translation
6ActiveRecord::Base.send(:include, Globalize::Model::ActiveRecord::Translated)
7
8# Load Post model
9require File.join( File.dirname(__FILE__), '..', '..', 'data', 'models' )
10 3
11class MigrationTest < ActiveSupport::TestCase 4class MigrationTest < ActiveSupport::TestCase
12 def setup 5 def setup
13 reset_db! File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'data', 'no_globalize_schema.rb')) 6 reset_db!
7 Post.drop_translation_table!
14 end 8 end
15 9
16 test 'globalize table added' do 10 test 'globalize table added' do
17 assert !Post.connection.table_exists?( :post_translations ) 11 assert !Post.connection.table_exists?(:post_translations)
18 assert !Post.connection.index_exists?( :post_translations, :post_id ) 12 assert !Post.connection.index_exists?(:post_translations, :post_id)
19 Post.create_translation_table! :subject => :string, :content => :text 13
20 assert Post.connection.table_exists?( :post_translations ) 14 Post.create_translation_table!(:subject => :string, :content => :text)
21 assert Post.connection.index_exists?( :post_translations, :post_id ) 15 assert Post.connection.table_exists?(:post_translations)
22 columns = Post.connection.columns( :post_translations ) 16 assert Post.connection.index_exists?(:post_translations, :post_id)
23 assert locale = columns.detect {|c| c.name == 'locale' } 17
18 columns = Post.connection.columns(:post_translations)
19 assert locale = columns.detect { |c| c.name == 'locale' }
24 assert_equal :string, locale.type 20 assert_equal :string, locale.type
25 assert subject = columns.detect {|c| c.name == 'subject' } 21 assert subject = columns.detect { |c| c.name == 'subject' }
26 assert_equal :string, subject.type 22 assert_equal :string, subject.type
27 assert content = columns.detect {|c| c.name == 'content' } 23 assert content = columns.detect { |c| c.name == 'content' }
28 assert_equal :text, content.type 24 assert_equal :text, content.type
29 assert post_id = columns.detect {|c| c.name == 'post_id' } 25 assert post_id = columns.detect { |c| c.name == 'post_id' }
30 assert_equal :integer, post_id.type 26 assert_equal :integer, post_id.type
31 assert created_at = columns.detect {|c| c.name == 'created_at' } 27 assert created_at = columns.detect { |c| c.name == 'created_at' }
32 assert_equal :datetime, created_at.type 28 assert_equal :datetime, created_at.type
33 assert updated_at = columns.detect {|c| c.name == 'updated_at' } 29 assert updated_at = columns.detect { |c| c.name == 'updated_at' }
34 assert_equal :datetime, updated_at.type 30 assert_equal :datetime, updated_at.type
35 end 31 end
36 32
@@ -46,13 +42,13 @@ class MigrationTest < ActiveSupport::TestCase
46 end 42 end
47 43
48 test 'exception on missing field inputs' do 44 test 'exception on missing field inputs' do
49 assert_raise Globalize::Model::MigrationMissingTranslatedField do 45 assert_raise Globalize::MigrationMissingTranslatedField do
50 Post.create_translation_table! :content => :text 46 Post.create_translation_table! :content => :text
51 end 47 end
52 end 48 end
53 49
54 test 'exception on bad input type' do 50 test 'exception on bad input type' do
55 assert_raise Globalize::Model::BadMigrationFieldType do 51 assert_raise Globalize::BadMigrationFieldType do
56 Post.create_translation_table! :subject => :string, :content => :integer 52 Post.create_translation_table! :subject => :string, :content => :integer
57 end 53 end
58 end 54 end
@@ -74,31 +70,29 @@ class MigrationTest < ActiveSupport::TestCase
74 Blog.drop_translation_table! 70 Blog.drop_translation_table!
75 end 71 end
76 end 72 end
77 73
78 test "translation_index_name returns a readable index name when it's not longer than 50 characters" do 74 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) 75 assert_equal 'index_post_translations_on_post_id', Post.send(:translation_index_name)
80 end 76 end
81 77
82 test "translation_index_name returns a hashed index name when it's longer than 50 characters" do 78 test "translation_index_name returns a hashed index name when it's longer than 50 characters" do
83 class UltraLongModelNameWithoutProper < ActiveRecord::Base 79 class UltraLongModelNameWithoutProper < ActiveRecord::Base
84 translates :foo 80 translates :foo
85 end 81 end
86 expected = 'index_44eba0f057e01a590ffccd0b8a3b5c78979539cd' 82 name = UltraLongModelNameWithoutProper.send(:translation_index_name)
87 actual = UltraLongModelNameWithoutProper.send(:translation_index_name) 83 assert_match /^index_[a-z0-9]{40}$/, name
88
89 assert_equal expected, actual
90 end 84 end
91 85
92 test 'globalize table added when table has long name' do 86 test 'globalize table added when table has long name' do
93 UltraLongModelNameWithoutProper.create_translation_table!( 87 UltraLongModelNameWithoutProper.create_translation_table!(
94 :subject => :string, :content => :text 88 :subject => :string, :content => :text
95 ) 89 )
96 90
97 assert UltraLongModelNameWithoutProper.connection.table_exists?( 91 assert UltraLongModelNameWithoutProper.connection.table_exists?(
98 :ultra_long_model_name_without_proper_translations 92 :ultra_long_model_name_without_proper_translations
99 ) 93 )
100 assert UltraLongModelNameWithoutProper.connection.index_exists?( 94 assert UltraLongModelNameWithoutProper.connection.index_exists?(
101 :ultra_long_model_name_without_proper_translations, 95 :ultra_long_model_name_without_proper_translations,
102 :name => UltraLongModelNameWithoutProper.send( 96 :name => UltraLongModelNameWithoutProper.send(
103 :translation_index_name 97 :translation_index_name
104 ) 98 )
@@ -106,20 +100,19 @@ class MigrationTest < ActiveSupport::TestCase
106 end 100 end
107 101
108 test 'globalize table dropped when table has long name' do 102 test 'globalize table dropped when table has long name' do
103 UltraLongModelNameWithoutProper.drop_translation_table!
109 UltraLongModelNameWithoutProper.create_translation_table!( 104 UltraLongModelNameWithoutProper.create_translation_table!(
110 :subject => :string, :content => :text 105 :subject => :string, :content => :text
111 ) 106 )
112
113 UltraLongModelNameWithoutProper.drop_translation_table! 107 UltraLongModelNameWithoutProper.drop_translation_table!
114 108
115 assert !UltraLongModelNameWithoutProper.connection.table_exists?( 109 assert !UltraLongModelNameWithoutProper.connection.table_exists?(
116 :ultra_long_model_name_without_proper_translations 110 :ultra_long_model_name_without_proper_translations
117 ) 111 )
118
119 assert !UltraLongModelNameWithoutProper.connection.index_exists?( 112 assert !UltraLongModelNameWithoutProper.connection.index_exists?(
120 :ultra_long_model_name_without_proper_translations, 113 :ultra_long_model_name_without_proper_translations,
121 :ultra_long_model_name_without_proper_id 114 :ultra_long_model_name_without_proper_id
122 ) 115 )
123 end 116 end
124 117
125end \ No newline at end of file 118end
diff --git a/vendor/plugins/globalize2/test/model/active_record/sti_translated_test.rb b/vendor/plugins/globalize2/test/active_record/sti_translated_test.rb
index 6980ba0..f529b8d 100644
--- a/vendor/plugins/globalize2/test/model/active_record/sti_translated_test.rb
+++ b/vendor/plugins/globalize2/test/active_record/sti_translated_test.rb
@@ -1,22 +1,10 @@
1require File.join( File.dirname(__FILE__), '..', '..', 'test_helper' ) 1require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2require 'active_record' 2require File.expand_path(File.dirname(__FILE__) + '/../data/models')
3require 'globalize/model/active_record'
4
5# Hook up model translation
6ActiveRecord::Base.send(:include, Globalize::Model::ActiveRecord::Translated)
7
8# Load Post model
9require File.join( File.dirname(__FILE__), '..', '..', 'data', 'models' )
10 3
11class StiTranslatedTest < ActiveSupport::TestCase 4class StiTranslatedTest < ActiveSupport::TestCase
12 def setup 5 def setup
13 I18n.locale = :'en-US' 6 I18n.locale = :'en-US'
14 I18n.fallbacks.clear 7 reset_db!
15 reset_db! File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'data', 'schema.rb'))
16 end
17
18 def teardown
19 I18n.fallbacks.clear
20 end 8 end
21 9
22 test "works with simple dynamic finders" do 10 test "works with simple dynamic finders" do
@@ -43,20 +31,6 @@ class StiTranslatedTest < ActiveSupport::TestCase
43 assert_equal [ 'content' ], child.changed 31 assert_equal [ 'content' ], child.changed
44 end 32 end
45 33
46 test 'fallbacks with lots of locale switching' do
47 I18n.fallbacks.map :'de-DE' => [ :'en-US' ]
48 child = Child.create :content => 'foo'
49
50 I18n.locale = :'de-DE'
51 assert_equal 'foo', child.content
52
53 I18n.locale = :'en-US'
54 child.update_attribute :content, 'bar'
55
56 I18n.locale = :'de-DE'
57 assert_equal 'bar', child.content
58 end
59
60 test "saves all locales, even after locale switching" do 34 test "saves all locales, even after locale switching" do
61 child = Child.new :content => 'foo' 35 child = Child.new :content => 'foo'
62 I18n.locale = 'de-DE' 36 I18n.locale = 'de-DE'
@@ -72,4 +46,4 @@ class StiTranslatedTest < ActiveSupport::TestCase
72 I18n.locale = 'he-IL' 46 I18n.locale = 'he-IL'
73 assert_equal 'baz', child.content 47 assert_equal 'baz', child.content
74 end 48 end
75end \ No newline at end of file 49end
diff --git a/vendor/plugins/globalize2/test/active_record/translates_test.rb b/vendor/plugins/globalize2/test/active_record/translates_test.rb
new file mode 100644
index 0000000..4207bc4
--- /dev/null
+++ b/vendor/plugins/globalize2/test/active_record/translates_test.rb
@@ -0,0 +1,87 @@
1require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2require File.expand_path(File.dirname(__FILE__) + '/../data/models')
3
4class TranslatesTest < ActiveSupport::TestCase
5 def setup
6 I18n.locale = nil
7 ActiveRecord::Base.locale = nil
8 reset_db!
9 end
10
11 test 'defines a :locale accessors on ActiveRecord::Base' do
12 ActiveRecord::Base.locale = :de
13 assert_equal :de, ActiveRecord::Base.locale
14 end
15
16 test 'the :locale reader on ActiveRecord::Base does not default to I18n.locale (anymore)' do
17 I18n.locale = :en
18 assert_nil ActiveRecord::Base.locale
19 end
20
21 test 'ActiveRecord::Base.with_locale temporarily sets the given locale and yields the block' do
22 I18n.locale = :en
23 post = Post.with_locale(:de) do
24 Post.create!(:subject => 'Titel', :content => 'Inhalt')
25 end
26 assert_nil Post.locale
27 assert_equal :en, I18n.locale
28
29 I18n.locale = :de
30 assert_equal 'Titel', post.subject
31 end
32
33 test 'translation_class returns the Translation class' do
34 assert_equal Post::Translation, Post.translation_class
35 end
36
37 test 'defines a has_many association on the model class' do
38 assert_has_many Post, :translations
39 end
40
41 test 'defines a scope for retrieving locales that have complete translations' do
42 post = Post.create!(:subject => 'subject', :content => 'content')
43 assert_equal [:en], post.translated_locales
44 end
45
46 test 'sets the given attributes to translated_attribute_names' do
47 assert_equal [:subject, :content], Post.translated_attribute_names
48 end
49
50 test 'defines accessors for the translated attributes' do
51 post = Post.new
52 assert post.respond_to?(:subject)
53 assert post.respond_to?(:subject=)
54 end
55
56 test 'attribute reader without arguments will use the current locale on ActiveRecord::Base or I18n' do
57 post = Post.with_locale(:de) do
58 Post.create!(:subject => 'Titel', :content => 'Inhalt')
59 end
60 I18n.locale = :de
61 assert_equal 'Titel', post.subject
62
63 I18n.locale = :en
64 ActiveRecord::Base.locale = :de
65 assert_equal 'Titel', post.subject
66 end
67
68 test 'attribute reader when passed a locale will use the given locale' do
69 post = Post.with_locale(:de) do
70 Post.create!(:subject => 'Titel', :content => 'Inhalt')
71 end
72 assert_equal 'Titel', post.subject(:de)
73 end
74
75 test 'attribute reader will use the current locale on ActiveRecord::Base or I18n' do
76 post = Post.with_locale(:en) do
77 Post.create!(:subject => 'title', :content => 'content')
78 end
79 I18n.locale = :de
80 post.subject = 'Titel'
81 assert_equal 'Titel', post.subject
82
83 ActiveRecord::Base.locale = :en
84 post.subject = 'title'
85 assert_equal 'title', post.subject
86 end
87end
diff --git a/vendor/plugins/globalize2/test/active_record/translation_class_test.rb b/vendor/plugins/globalize2/test/active_record/translation_class_test.rb
new file mode 100644
index 0000000..1628416
--- /dev/null
+++ b/vendor/plugins/globalize2/test/active_record/translation_class_test.rb
@@ -0,0 +1,30 @@
1require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2require File.expand_path(File.dirname(__FILE__) + '/../data/models')
3
4class TranlationClassTest < ActiveSupport::TestCase
5 def setup
6 reset_db!
7 end
8
9 test 'defines a Translation class nested in the model class' do
10 assert Post.const_defined?(:Translation)
11 end
12
13 test 'defines a belongs_to association' do
14 assert_belongs_to Post::Translation, :post
15 end
16
17 test 'defines a reader for :locale that always returns a symbol' do
18 post = Post::Translation.new
19 post.write_attribute('locale', 'de')
20 assert_equal :de, post.locale
21 end
22
23 test 'defines a write for :locale that always writes a string' do
24 post = Post::Translation.new
25 post.locale = :de
26 assert_equal 'de', post.read_attribute('locale')
27 end
28end
29
30
diff --git a/vendor/plugins/globalize2/test/active_record/validation_tests.rb b/vendor/plugins/globalize2/test/active_record/validation_tests.rb
new file mode 100644
index 0000000..0148fa3
--- /dev/null
+++ b/vendor/plugins/globalize2/test/active_record/validation_tests.rb
@@ -0,0 +1,75 @@
1require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2require File.expand_path(File.dirname(__FILE__) + '/../data/models')
3
4class ValidationTest < ActiveSupport::TestCase
5 def setup
6 reset_db!
7 end
8
9 def teardown
10 Validatee.instance_variable_set(:@validate_callbacks, CallbackChain.new)
11 end
12
13 test "validates_presence_of" do
14 Validatee.class_eval { validates_presence_of :string }
15 assert !Validatee.new.valid?
16 assert Validatee.new(:string => 'foo').valid?
17 end
18
19 test "validates_confirmation_of" do
20 Validatee.class_eval { validates_confirmation_of :string }
21 assert !Validatee.new(:string => 'foo', :string_confirmation => 'bar').valid?
22 assert Validatee.new(:string => 'foo', :string_confirmation => 'foo').valid?
23 end
24
25 test "validates_acceptance_of" do
26 Validatee.class_eval { validates_acceptance_of :string, :accept => '1' }
27 assert !Validatee.new(:string => '0').valid?
28 assert Validatee.new(:string => '1').valid?
29 end
30
31 test "validates_length_of (:is)" do
32 Validatee.class_eval { validates_length_of :string, :is => 1 }
33 assert !Validatee.new(:string => 'aa').valid?
34 assert Validatee.new(:string => 'a').valid?
35 end
36
37 test "validates_format_of" do
38 Validatee.class_eval { validates_format_of :string, :with => /^\d+$/ }
39 assert !Validatee.new(:string => 'a').valid?
40 assert Validatee.new(:string => '1').valid?
41 end
42
43 test "validates_inclusion_of" do
44 Validatee.class_eval { validates_inclusion_of :string, :in => %(a) }
45 assert !Validatee.new(:string => 'b').valid?
46 assert Validatee.new(:string => 'a').valid?
47 end
48
49 test "validates_exclusion_of" do
50 Validatee.class_eval { validates_exclusion_of :string, :in => %(b) }
51 assert !Validatee.new(:string => 'b').valid?
52 assert Validatee.new(:string => 'a').valid?
53 end
54
55 test "validates_numericality_of" do
56 Validatee.class_eval { validates_numericality_of :string }
57 assert !Validatee.new(:string => 'a').valid?
58 assert Validatee.new(:string => '1').valid?
59 end
60
61 # This doesn't pass and Rails' validates_uniqueness_of implementation doesn't
62 # seem to be extensible easily. One can work around that by either defining
63 # a custom validation on the Validatee model itself, or by using validates_uniqueness_of
64 # on Validatee::Translation.
65 #
66 # test "validates_uniqueness_of" do
67 # Validatee.class_eval { validates_uniqueness_of :string }
68 # Validatee.create!(:string => 'a')
69 # assert !Validatee.new(:string => 'a').valid?
70 # assert Validatee.new(:string => 'b').valid?
71 # end
72
73 # test "validates_associated" do
74 # end
75end \ No newline at end of file
diff --git a/vendor/plugins/globalize2/test/active_record_test.rb b/vendor/plugins/globalize2/test/active_record_test.rb
new file mode 100644
index 0000000..e6b54f0
--- /dev/null
+++ b/vendor/plugins/globalize2/test/active_record_test.rb
@@ -0,0 +1,442 @@
1require File.expand_path(File.dirname(__FILE__) + '/test_helper')
2require File.expand_path(File.dirname(__FILE__) + '/data/models')
3
4# Higher level tests.
5
6class ActiveRecordTest < ActiveSupport::TestCase
7 def setup
8 I18n.locale = :en
9 reset_db!
10 ActiveRecord::Base.locale = nil
11 end
12
13 def assert_translated(locale, record, names, expected)
14 I18n.locale = locale
15 assert_equal Array(expected), Array(names).map { |name| record.send(name) }
16 end
17
18 test "a translated record has translations" do
19 assert_equal [], Post.new.translations
20 end
21
22 test "saves a translated version of the record for each locale" do
23 post = Post.create(:subject => 'title')
24 I18n.locale = :de
25 post.update_attributes(:subject => 'Titel')
26
27 assert_equal 2, post.translations.size
28 assert_equal %w(de en), post.translations.map(&:locale).map(&:to_s).sort
29 assert_equal %w(Titel title), post.translations.map(&:subject).sort
30 end
31
32 test "a translated record has German translations" do
33 I18n.locale = :de
34 post = Post.create(:subject => 'foo')
35 assert_equal 1, post.translations.size
36 assert_equal [:de], post.translations.map { |t| t.locale }
37 end
38
39 test "modifiying translated fields while switching locales" do
40 post = Post.create(:subject => 'title', :content => 'content')
41 assert_equal %w(title content), [post.subject, post.content]
42
43 I18n.locale = :de
44 post.subject, post.content = 'Titel', 'Inhalt'
45
46 assert_translated(:de, post, [:subject, :content], %w(Titel Inhalt))
47 assert_translated(:en, post, [:subject, :content], %w(title content))
48 assert_translated(:de, post, [:subject, :content], %w(Titel Inhalt))
49
50 post.save
51 post.reload
52
53 assert_translated(:en, post, [:subject, :content], %w(title content))
54 assert_translated(:de, post, [:subject, :content], %w(Titel Inhalt))
55 end
56
57 test "attribute writers do return their argument" do
58 value = Post.new.subject = 'foo'
59 assert_equal 'foo', value
60 end
61
62 test "update_attribute succeeds with valid values" do
63 post = Post.create(:subject => 'foo', :content => 'bar')
64 post.update_attribute(:subject, 'baz')
65 assert_equal 'baz', Post.first.subject
66 end
67
68 test "update_attributes fails with invalid values" do
69 post = Post.create(:subject => 'foo', :content => 'bar')
70 assert !post.update_attributes(:subject => '')
71 assert_nil post.reload.attributes['subject']
72 assert_equal 'foo', post.subject
73 end
74
75 test "passing the locale to create uses the given locale" do
76 post = Post.create(:subject => 'Titel', :content => 'Inhalt', :locale => :de)
77 assert_equal :en, I18n.locale
78 assert_nil ActiveRecord::Base.locale
79
80 I18n.locale = :de
81 assert_equal 'Titel', post.subject
82 end
83
84 test "passing the locale to attributes= uses the given locale" do
85 post = Post.create(:subject => 'title', :content => 'content')
86 post.update_attributes(:subject => 'Titel', :content => 'Inhalt', :locale => :de)
87 post.reload
88
89 assert_equal :en, I18n.locale
90 assert_nil ActiveRecord::Base.locale
91
92 assert_equal 'title', post.subject
93 I18n.locale = :de
94 assert_equal 'Titel', post.subject
95 end
96
97 test 'reload works' do
98 post = Post.create(:subject => 'foo', :content => 'bar')
99 post.subject = 'baz'
100 post.reload
101 assert_equal 'foo', post.subject
102 end
103
104 test "returns nil if no translations are found (unsaved record)" do
105 post = Post.new(:subject => 'foo')
106 assert_equal 'foo', post.subject
107 assert_nil post.content
108 end
109
110 test "returns nil if no translations are found (saved record)" do
111 post = Post.create(:subject => 'foo')
112 post.reload
113 assert_equal 'foo', post.subject
114 assert_nil post.content
115 end
116
117 test "finds a German post" do
118 post = Post.create(:subject => 'foo (en)', :content => 'bar')
119 I18n.locale = :de
120 post = Post.first
121 post.subject = 'baz (de)'
122 post.save
123 assert_equal 'baz (de)', Post.first.subject
124 I18n.locale = :en
125 assert_equal 'foo (en)', Post.first.subject
126 end
127
128 test "saves an English post and loads correctly" do
129 post = Post.create(:subject => 'foo', :content => 'bar')
130 assert post.save
131 post = Post.first
132 assert_equal 'foo', post.subject
133 assert_equal 'bar', post.content
134 end
135
136 test "returns the value for the correct locale, after locale switching" do
137 post = Post.create(:subject => 'foo')
138 I18n.locale = :de
139 post.subject = 'bar'
140 post.save
141 I18n.locale = :en
142 post = Post.first
143 assert_equal 'foo', post.subject
144 I18n.locale = :de
145 assert_equal 'bar', post.subject
146 end
147
148 test "returns the value for the correct locale, after locale switching, without saving" do
149 post = Post.create :subject => 'foo'
150 I18n.locale = :de
151 post.subject = 'bar'
152 I18n.locale = :en
153 assert_equal 'foo', post.subject
154 I18n.locale = :de
155 assert_equal 'bar', post.subject
156 end
157
158 test "saves all locales, even after locale switching" do
159 post = Post.new :subject => 'foo'
160 I18n.locale = :de
161 post.subject = 'bar'
162 I18n.locale = :he
163 post.subject = 'baz'
164 post.save
165 I18n.locale = :en
166 post = Post.first
167 assert_equal 'foo', post.subject
168 I18n.locale = :de
169 assert_equal 'bar', post.subject
170 I18n.locale = :he
171 assert_equal 'baz', post.subject
172 end
173
174 test "works with associations" do
175 blog = Blog.create
176 post1 = blog.posts.create(:subject => 'foo')
177
178 I18n.locale = :de
179 post2 = blog.posts.create(:subject => 'bar')
180 assert_equal 2, blog.posts.size
181
182 I18n.locale = :en
183 assert_equal 'foo', blog.posts.first.subject
184 assert_nil blog.posts.last.subject
185
186 I18n.locale = :de
187 assert_equal 'bar', blog.posts.last.subject
188 end
189
190 test "works with simple dynamic finders" do
191 foo = Post.create(:subject => 'foo')
192 Post.create(:subject => 'bar')
193 post = Post.find_by_subject('foo')
194 assert_equal foo, post
195 end
196
197 test 'change attribute on globalized model' do
198 post = Post.create(:subject => 'foo', :content => 'bar')
199 assert_equal [], post.changed
200 post.subject = 'baz'
201 assert_equal ['subject'], post.changed
202 post.content = 'quux'
203 assert_member 'subject', post.changed
204 assert_member 'content', post.changed
205 end
206
207 test 'change attribute on globalized model after locale switching' do
208 post = Post.create(:subject => 'foo', :content => 'bar')
209 assert_equal [], post.changed
210 post.subject = 'baz'
211 I18n.locale = :de
212 assert_equal ['subject'], post.changed
213 end
214
215 test 'complex writing and stashing' do
216 post = Post.create(:subject => 'foo', :content => 'bar')
217 post.subject = nil
218 assert_nil post.subject
219 assert !post.valid?
220 post.subject = 'stashed_foo'
221 assert_equal 'stashed_foo', post.subject
222 end
223
224 test 'translated class locale setting' do
225 assert ActiveRecord::Base.respond_to?(:locale)
226 assert_equal :en, I18n.locale
227 assert_nil ActiveRecord::Base.locale
228
229 I18n.locale = :de
230 assert_equal :de, I18n.locale
231 assert_nil ActiveRecord::Base.locale
232
233 ActiveRecord::Base.locale = :es
234 assert_equal :de, I18n.locale
235 assert_equal :es, ActiveRecord::Base.locale
236
237 I18n.locale = :fr
238 assert_equal :fr, I18n.locale
239 assert_equal :es, ActiveRecord::Base.locale
240 end
241
242 test "untranslated class responds to locale" do
243 assert Blog.respond_to?(:locale)
244 end
245
246 test "to ensure locales in different classes are the same" do
247 ActiveRecord::Base.locale = :de
248 assert_equal :de, ActiveRecord::Base.locale
249 assert_equal :de, Parent.locale
250
251 Parent.locale = :es
252 assert_equal :es, ActiveRecord::Base.locale
253 assert_equal :es, Parent.locale
254 end
255
256 test "attribute saving goes by content locale and not global locale" do
257 ActiveRecord::Base.locale = :de
258 assert_equal :en, I18n.locale
259 Post.create :subject => 'foo'
260 assert_equal :de, Post.first.translations.first.locale
261 end
262
263 test "attribute loading goes by content locale and not global locale" do
264 post = Post.create(:subject => 'foo')
265 assert_nil ActiveRecord::Base.locale
266
267 ActiveRecord::Base.locale = :de
268 assert_equal :en, I18n.locale
269 post.update_attribute(:subject, 'foo [de]')
270 assert_equal 'foo [de]', Post.first.subject
271
272 ActiveRecord::Base.locale = :en
273 assert_equal 'foo', Post.first.subject
274 end
275
276 test "access content locale before setting" do
277 Globalize::ActiveRecord::ActMacro.class_eval "remove_class_variable(:@@locale)"
278 assert_nothing_raised { ActiveRecord::Base.locale }
279 end
280
281 test "available_locales" do
282 Post.locale = :de
283 post = Post.create(:subject => 'foo')
284 Post.locale = :es
285 post.update_attribute(:subject, 'bar')
286 Post.locale = :fr
287 post.update_attribute(:subject, 'baz')
288 assert_equal [:de, :es, :fr], post.available_locales
289 assert_equal [:de, :es, :fr], Post.first.available_locales
290 end
291
292 test "saving record correctly after post-save reload" do
293 reloader = Reloader.create(:content => 'foo')
294 assert_equal 'foo', reloader.content
295 end
296
297 test "including translations" do
298 I18n.locale = :de
299 Post.create(:subject => "Foo1", :content => "Bar1")
300 Post.create(:subject => "Foo2", :content => "Bar2")
301
302 class << Post
303 def translations_included
304 self.all(:include => :translations)
305 end
306 end
307
308 default = Post.all.map { |x| [x.subject, x.content] }
309 with_include = Post.translations_included.map { |x| [x.subject, x.content] }
310 assert_equal default, with_include
311 end
312
313 test "setting multiple translations at once with options hash" do
314 Post.locale = :de
315 post = Post.create(:subject => "foo1", :content => "foo1")
316 Post.locale = :en
317 post.update_attributes(:subject => "bar1", :content => "bar1")
318
319 options = { :de => {:subject => "foo2", :content => "foo2"},
320 :en => {:subject => "bar2", :content => "bar2"} }
321 post.set_translations options
322 post.reload
323
324 assert ["bar2", "bar2"], [post.subject, post.content]
325 Post.locale = :de
326 assert ["foo2", "foo2"], [post.subject, post.content]
327 end
328
329 test "setting only one translation with set_translations" do
330 Post.locale = :de
331 post = Post.create(:subject => "foo1", :content => "foo1")
332 Post.locale = :en
333 post.update_attributes(:subject => "bar1", :content => "bar1")
334
335 options = { :en => { :subject => "bar2", :content => "bar2" } }
336 post.set_translations options
337 post.reload
338
339 assert ["bar2", "bar2"], [post.subject, post.content]
340 Post.locale = :de
341 assert ["foo1", "foo1"], [post.subject, post.content]
342 end
343
344 test "setting only selected attributes with set_translations" do
345 Post.locale = :de
346 post = Post.create(:subject => "foo1", :content => "foo1")
347 Post.locale = :en
348 post.update_attributes(:subject => "bar1", :content => "bar1")
349
350 options = { :de => { :content => "foo2" }, :en => { :subject => "bar2" } }
351 post.set_translations options
352 post.reload
353
354 assert ["bar2", "bar1"], [post.subject, post.content]
355 Post.locale = :de
356 assert ["foo1", "foo2"], [post.subject, post.content]
357 end
358
359 test "setting invalid attributes raises ArgumentError" do
360 Post.locale = :de
361 post = Post.create(:subject => "foo1", :content => "foo1")
362 Post.locale = :en
363 post.update_attributes(:subject => "bar1", :content => "bar1")
364
365 options = { :de => {:fake => "foo2"} }
366 exception = assert_raise(ActiveRecord::UnknownAttributeError) do
367 post.set_translations options
368 end
369 assert_equal "unknown attribute: fake", exception.message
370 end
371
372 test "reload accepting find options" do
373 p = Post.create(:subject => "Foo", :content => "Bar")
374 assert p.reload(:readonly => true, :lock => true)
375 assert_raise(ArgumentError) { p.reload(:foo => :bar) }
376 end
377
378 test "dependent destroy of translation" do
379 p = Post.create(:subject => "Foo", :content => "Bar")
380 assert_equal 1, PostTranslation.count
381 p.destroy
382 assert_equal 0, PostTranslation.count
383 end
384
385 test "translating subclass of untranslated comment model" do
386 translated_comment = TranslatedComment.create(:post => @post)
387 assert_nothing_raised { translated_comment.translations }
388 end
389
390 test "modifiying translated comments works as expected" do
391 I18n.locale = :en
392 translated_comment = TranslatedComment.create(:post => @post, :content => 'foo')
393 assert_equal 'foo', translated_comment.content
394
395 I18n.locale = :de
396 translated_comment.content = 'bar'
397 assert translated_comment.save
398 assert_equal 'bar', translated_comment.content
399
400 I18n.locale = :en
401 assert_equal 'foo', translated_comment.content
402
403 assert_equal 2, translated_comment.translations.size
404 end
405
406 test "can create a proxy class for a namespaced model" do
407 assert_nothing_raised do
408 module Foo
409 module Bar
410 class Baz < ActiveRecord::Base
411 translates :bumm
412 end
413 end
414 end
415 end
416 end
417
418 test "attribute translated before type cast" do
419 Post.locale = :en
420 post = Post.create(:subject => 'foo', :content => 'bar')
421 Post.locale = :de
422 post.update_attribute(:subject, "German foo")
423 assert_equal 'German foo', post.subject_before_type_cast
424 Post.locale = :en
425 assert_equal 'foo', post.subject_before_type_cast
426 end
427
428 test "don't override existing translation class" do
429 assert PostTranslation.new.respond_to?(:existing_method)
430 end
431
432 test "has_many and named scopes work with globalize" do
433 blog = Blog.create
434 assert_nothing_raised { blog.posts.foobar }
435 end
436end
437
438# TODO error checking for fields that exist in main table, don't exist in
439# proxy table, aren't strings or text
440#
441# TODO allow finding by translated attributes in conditions?
442# TODO generate advanced dynamic finders?
diff --git a/vendor/plugins/globalize2/test/backends/chained_test.rb b/vendor/plugins/globalize2/test/backends/chained_test.rb
deleted file mode 100644
index bb0b3e0..0000000
--- a/vendor/plugins/globalize2/test/backends/chained_test.rb
+++ /dev/null
@@ -1,175 +0,0 @@
1require File.join( File.dirname(__FILE__), '..', 'test_helper' )
2require 'globalize/backend/chain'
3
4module Globalize
5 module Backend
6 class Dummy
7 def translate(locale, key, options = {})
8 end
9 end
10 end
11end
12
13class ChainedTest < ActiveSupport::TestCase
14
15 test "instantiates a chained backend and sets test as backend" do
16 assert_nothing_raised { I18n.chain_backends }
17 assert_instance_of Globalize::Backend::Chain, I18n.backend
18 end
19
20 test "passes all given arguments to the chained backends #initialize method" do
21 Globalize::Backend::Chain.expects(:new).with(:spec, :simple)
22 I18n.chain_backends :spec, :simple
23 end
24
25 test "passes all given arguments to #add assuming that they are backends" do
26 # no idea how to spec that
27 end
28end
29
30class AddChainedTest < ActiveSupport::TestCase
31 def setup
32 I18n.backend = Globalize::Backend::Chain.new
33 end
34
35 test "accepts an instance of a backend" do
36 assert_nothing_raised { I18n.backend.add Globalize::Backend::Dummy.new }
37 assert_instance_of Globalize::Backend::Dummy, I18n.backend.send(:backends).first
38 end
39
40 test "accepts a class and instantiates the backend" do
41 assert_nothing_raised { I18n.backend.add Globalize::Backend::Dummy }
42 assert_instance_of Globalize::Backend::Dummy, I18n.backend.send(:backends).first
43 end
44
45 test "accepts a symbol, constantizes test as a backend class and instantiates the backend" do
46 assert_nothing_raised { I18n.backend.add :dummy }
47 assert_instance_of Globalize::Backend::Dummy, I18n.backend.send(:backends).first
48 end
49
50 test "accepts any number of backend instances, classes or symbols" do
51 assert_nothing_raised { I18n.backend.add Globalize::Backend::Dummy.new, Globalize::Backend::Dummy, :dummy }
52 assert_instance_of Globalize::Backend::Dummy, I18n.backend.send(:backends).first
53 assert_equal [ Globalize::Backend::Dummy, Globalize::Backend::Dummy, Globalize::Backend::Dummy ],
54 I18n.backend.send(:backends).map{|backend| backend.class }
55 end
56
57end
58
59class TranslateChainedTest < ActiveSupport::TestCase
60 def setup
61 I18n.locale = :en
62 I18n.backend = Globalize::Backend::Chain.new
63 @first_backend = I18n::Backend::Simple.new
64 @last_backend = I18n::Backend::Simple.new
65 I18n.backend.add @first_backend
66 I18n.backend.add @last_backend
67 end
68
69 test "delegates #translate to all backends in the order they were added" do
70 @first_backend.expects(:translate).with(:en, :foo, {})
71 @last_backend.expects(:translate).with(:en, :foo, {})
72 I18n.translate :foo
73 end
74
75 test "returns the result from #translate from the first backend if test's not nil" do
76 @first_backend.store_translations :en, {:foo => 'foo from first backend'}
77 @last_backend.store_translations :en, {:foo => 'foo from last backend'}
78 result = I18n.translate :foo
79 assert_equal 'foo from first backend', result
80 end
81
82 test "returns the result from #translate from the second backend if the first one returned nil" do
83 @first_backend.store_translations :en, {}
84 @last_backend.store_translations :en, {:foo => 'foo from last backend'}
85 result = I18n.translate :foo
86 assert_equal 'foo from last backend', result
87 end
88
89 test "looks up a namespace from all backends and merges them (if a result is a hash and no count option is present)" do
90 @first_backend.store_translations :en, {:foo => {:bar => 'bar from first backend'}}
91 @last_backend.store_translations :en, {:foo => {:baz => 'baz from last backend'}}
92 result = I18n.translate :foo
93 assert_equal( {:bar => 'bar from first backend', :baz => 'baz from last backend'}, result )
94 end
95
96 test "raises a MissingTranslationData exception if no translation was found" do
97 assert_raise( I18n::MissingTranslationData ) { I18n.translate :not_here, :raise => true }
98 end
99
100 test "raises an InvalidLocale exception if the locale is nil" do
101 assert_raise( I18n::InvalidLocale ) { Globalize::Backend::Chain.new.translate nil, :foo }
102 end
103
104 test "bulk translates a number of keys from different backends" do
105 @first_backend.store_translations :en, {:foo => 'foo from first backend'}
106 @last_backend.store_translations :en, {:bar => 'bar from last backend'}
107 result = I18n.translate [:foo, :bar]
108 assert_equal( ['foo from first backend', 'bar from last backend'], result )
109 end
110
111 test "still calls #translate on all the backends" do
112 @last_backend.expects :translate
113 I18n.translate :not_here, :default => 'default'
114 end
115
116 test "returns a given default string when no backend returns a translation" do
117 result = I18n.translate :not_here, :default => 'default'
118 assert_equal 'default', result
119 end
120
121end
122
123class CustomLocalizeBackend < I18n::Backend::Simple
124 def localize(locale, object, format = :default)
125 "result from custom localize backend" if locale == 'custom'
126 end
127end
128
129class LocalizeChainedTest < ActiveSupport::TestCase
130 def setup
131 I18n.locale = :en
132 I18n.backend = Globalize::Backend::Chain.new
133 @first_backend = CustomLocalizeBackend.new
134 @last_backend = I18n::Backend::Simple.new
135 I18n.backend.add @first_backend
136 I18n.backend.add @last_backend
137 @time = Time.now
138 end
139
140 test "delegates #localize to all backends in the order they were added" do
141 @first_backend.expects(:localize).with(:en, @time, :default)
142 @last_backend.expects(:localize).with(:en, @time, :default)
143 I18n.localize @time
144 end
145
146 test "returns the result from #localize from the first backend if test's not nil" do
147 @last_backend.expects(:localize).never
148 result = I18n.localize @time, :locale => 'custom'
149 assert_equal 'result from custom localize backend', result
150 end
151
152 test "returns the result from #localize from the second backend if the first one returned nil" do
153 @last_backend.expects(:localize).returns "value from last backend"
154 result = I18n.localize @time
155 assert_equal 'value from last backend', result
156 end
157end
158
159class NamespaceChainedTest < ActiveSupport::TestCase
160 def setup
161 @backend = Globalize::Backend::Chain.new
162 end
163
164 test "returns false if the given result is not a Hash" do
165 assert !@backend.send(:namespace_lookup?, 'foo', {})
166 end
167
168 test "returns false if a count option is present" do
169 assert !@backend.send(:namespace_lookup?, {:foo => 'foo'}, {:count => 1})
170 end
171
172 test "returns true if the given result is a Hash AND no count option is present" do
173 assert @backend.send(:namespace_lookup?, {:foo => 'foo'}, {})
174 end
175end
diff --git a/vendor/plugins/globalize2/test/backends/pluralizing_test.rb b/vendor/plugins/globalize2/test/backends/pluralizing_test.rb
deleted file mode 100644
index 7445e3f..0000000
--- a/vendor/plugins/globalize2/test/backends/pluralizing_test.rb
+++ /dev/null
@@ -1,63 +0,0 @@
1require File.join( File.dirname(__FILE__), '..', 'test_helper' )
2require 'globalize/backend/pluralizing'
3
4class PluralizingTest < ActiveSupport::TestCase
5 def setup
6 @backend = Globalize::Backend::Pluralizing.new
7 @cz_pluralizer = lambda{|c| c == 1 ? :one : (2..4).include?(c) ? :few : :other }
8 end
9
10 test "#pluralizer returns the pluralizer for a given locale if defined" do
11 assert_instance_of Proc, @backend.pluralizer(:en)
12 end
13
14 test "#pluralizer returns the default pluralizer if no pluralizer is defined for the given locale" do
15 assert_equal @backend.pluralizer(:en), @backend.pluralizer(:de)
16 end
17
18 test "#add_pluralizer allows to store a pluralizer per locale" do
19 assert_nothing_raised { @backend.add_pluralizer(:cz, @cz_pluralizer) }
20 assert_equal @cz_pluralizer, @backend.pluralizer(:cz)
21 end
22
23end
24
25class PluralizePluralizingTest < ActiveSupport::TestCase
26 def setup
27 @backend = Globalize::Backend::Pluralizing.new
28 @cz_pluralizer = lambda{|c| c == 1 ? :one : (2..4).include?(c) ? :few : :other }
29 @backend.store_translations :en, :foo => {:one => 'one en foo', :other => 'many en foos'}
30 @backend.store_translations :cz, :foo => {:one => 'one cz foo', :few => 'few cz foos', :other => 'many cz foos'}
31 end
32
33 test "looks up the :one translation when count is 1" do
34 assert_equal 'one en foo', @backend.translate(:en, :foo, :count => 1)
35 end
36
37 test "looks up the :other translation when count is 2" do
38 assert_equal 'many en foos', @backend.translate(:en, :foo, :count => 2)
39 end
40end
41
42class CzPluralizingTest < ActiveSupport::TestCase
43 def setup
44 @backend = Globalize::Backend::Pluralizing.new
45 @cz_pluralizer = lambda{|c| c == 1 ? :one : (2..4).include?(c) ? :few : :other }
46 @backend.store_translations :en, :foo => {:one => 'one en foo', :other => 'many en foos'}
47 @backend.store_translations :cz, :foo => {:one => 'one cz foo', :few => 'few cz foos', :other => 'many cz foos'}
48 @backend.add_pluralizer(:cz, @cz_pluralizer)
49 end
50
51 test "looks up the :one translation when count is 1 (:cz)" do
52 assert_equal 'one cz foo', @backend.translate(:cz, :foo, :count => 1)
53 end
54
55 test "looks up the :few translation when count is 2 (:cz)" do
56 assert_equal 'few cz foos', @backend.translate(:cz, :foo, :count => 2)
57 end
58
59 test "looks up the :other translation when count is 5 (:cz)" do
60 assert_equal 'many cz foos', @backend.translate(:cz, :foo, :count => 5)
61 end
62
63end
diff --git a/vendor/plugins/globalize2/test/backends/static_test.rb b/vendor/plugins/globalize2/test/backends/static_test.rb
deleted file mode 100644
index 133e547..0000000
--- a/vendor/plugins/globalize2/test/backends/static_test.rb
+++ /dev/null
@@ -1,143 +0,0 @@
1require File.join( File.dirname(__FILE__), '..', 'test_helper' )
2require 'globalize/backend/static'
3require 'globalize/translation'
4require 'action_view'
5include ActionView::Helpers::NumberHelper
6
7I18n.locale = :'en-US' # Need to set this, since I18n defaults to 'en'
8
9class StaticTest < ActiveSupport::TestCase
10 def setup
11 I18n.backend = Globalize::Backend::Static.new
12 translations = {:"en-US" => {:foo => "foo in en-US", :boz => 'boz', :buz => {:bum => 'bum'}},
13 :"en" => {:bar => "bar in en"},
14 :"de-DE" => {:baz => "baz in de-DE"},
15 :"de" => {:boo => "boo in de", :number => { :currency => { :format => { :unit => '€', :format => '%n %u'}}}}}
16 translations.each do |locale, data|
17 I18n.backend.store_translations locale, data
18 end
19 I18n.fallbacks.map :"de-DE" => :"en-US", :he => :en
20 end
21
22 test "returns an instance of Translation:Static" do
23 translation = I18n.translate :foo
24 assert_instance_of Globalize::Translation::Static, translation
25 end
26
27 test "returns the translation in en-US if present" do
28 assert_equal "foo in en-US", I18n.translate(:foo, :locale => :"en-US")
29 end
30
31 test "returns the translation in en if en-US is not present" do
32 assert_equal "bar in en", I18n.translate(:bar, :locale => :"en-US")
33 end
34
35 test "returns the translation in de-DE if present" do
36 assert_equal "baz in de-DE", I18n.translate(:baz, :locale => :"de-DE")
37 end
38
39 test "returns the translation in de if de-DE is not present" do
40 assert_equal "boo in de", I18n.translate(:boo, :locale => :"de-DE")
41 end
42
43 test "returns the translation in en-US if none of de-DE and de are present" do
44 assert_equal "foo in en-US", I18n.translate(:foo, :locale => :"de-DE")
45 end
46
47 test "returns the translation in en if none of de-DE, de and en-US are present" do
48 assert_equal "bar in en", I18n.translate(:bar, :locale => :"de-DE")
49 end
50
51 test "returns the translation in en if none in he is present" do
52 assert_equal "bar in en", I18n.translate(:bar, :locale => :he)
53 end
54
55 test "returns the given default String when the key is not present for any locale" do
56 assert_equal "default", I18n.translate(:missing, :default => "default")
57 end
58
59 test "returns the fallback translation for the key if present for a fallback locale" do
60 I18n.backend.store_translations :de, :non_default => "non_default in de"
61 assert_equal "non_default in de", I18n.translate(:non_default, :default => "default", :locale => :"de-DE")
62 end
63
64 test "returns an array of translations" do
65 assert_instance_of Array, I18n.translate([:foo, :boz])
66 end
67
68 test "returns an array of instances of Translation::Static" do
69 assert_equal [Globalize::Translation::Static], I18n.translate([:foo, :boz]).map(&:class).uniq
70 end
71
72 test "returns a hash of translations" do
73 assert_instance_of Hash, I18n.translate(:"buz")
74 end
75
76 test "returns an array of translations 2" do
77 assert_equal [Globalize::Translation::Static], I18n.translate(:"buz").values.map(&:class)
78 end
79
80 test "returns currency properly formated" do
81 currency = number_to_currency(10)
82 assert_equal "$10.00", currency
83 end
84
85 test "returns currency properly formated for locale" do
86 currency = number_to_currency(10, :locale => :'de')
87 assert_equal "10.000 €", currency
88 end
89
90 test "returns currency properly formated from parameters" do
91 currency = number_to_currency(10, :format => "%n %u", :unit => '€')
92 assert_equal "10.00 €", currency
93 end
94
95 test "makes sure interpolation does not break even with False as string" do
96 assert_equal "translation missing: en, support, array, skip_last_comma", I18n.translate(:"support.array.skip_last_comma")
97 end
98end
99
100class TranslationStaticTest < ActiveSupport::TestCase
101 def setup
102 I18n.backend = Globalize::Backend::Static.new
103 translations = {
104 :greeting => "Hi {{name}}",
105 :messages => { :one => "You have one message.", :other => "You have {{count}} messages."}
106 }
107 I18n.backend.store_translations :"en", translations
108 end
109
110 def greeting
111 I18n.translate :greeting, :locale => :"en-US", :name => "Joshua"
112 end
113
114 test "stores the actual locale" do
115 assert_equal :en, greeting.locale
116 end
117
118 test "stores the requested locale" do
119 assert_equal :'en-US', greeting.requested_locale
120 end
121
122 test "stores the requested key" do
123 assert_equal :greeting, greeting.key
124 end
125
126 test "stores the options given to #translate" do
127 assert_equal( {:name => "Joshua"}, greeting.options )
128 end
129
130 test "stores the original translation before test was interpolated" do
131 assert_equal "Hi {{name}}", greeting.original
132 end
133
134 test "stores the plural_key :one if pluralized as such" do
135 message = I18n.translate :messages, :locale => :"en-US", :count => 1
136 assert_equal :one, message.plural_key
137 end
138
139 test "stores the plural_key :other if pluralized as such" do
140 messages = I18n.translate :messages, :locale => :"en-US", :count => 2
141 assert_equal :other, messages.plural_key
142 end
143end
diff --git a/vendor/plugins/globalize2/test/data/locale/all.yml b/vendor/plugins/globalize2/test/data/locale/all.yml
deleted file mode 100644
index ee4fb4a..0000000
--- a/vendor/plugins/globalize2/test/data/locale/all.yml
+++ /dev/null
@@ -1,2 +0,0 @@
1en-US:
2 from-all-file: From the "all" file.
diff --git a/vendor/plugins/globalize2/test/data/locale/de-DE.yml b/vendor/plugins/globalize2/test/data/locale/de-DE.yml
deleted file mode 100644
index d4c468e..0000000
--- a/vendor/plugins/globalize2/test/data/locale/de-DE.yml
+++ /dev/null
@@ -1,2 +0,0 @@
1de-DE:
2 from-locale-file: Aus der Locale Datei. \ No newline at end of file
diff --git a/vendor/plugins/globalize2/test/data/locale/en-US.yml b/vendor/plugins/globalize2/test/data/locale/en-US.yml
deleted file mode 100644
index 866b171..0000000
--- a/vendor/plugins/globalize2/test/data/locale/en-US.yml
+++ /dev/null
@@ -1,2 +0,0 @@
1en-US:
2 from-locale-file: From the locale file.
diff --git a/vendor/plugins/globalize2/test/data/locale/en-US/module.yml b/vendor/plugins/globalize2/test/data/locale/en-US/module.yml
deleted file mode 100644
index 6655e9b..0000000
--- a/vendor/plugins/globalize2/test/data/locale/en-US/module.yml
+++ /dev/null
@@ -1,2 +0,0 @@
1en-US:
2 from-locale-dir: From the locale directory.
diff --git a/vendor/plugins/globalize2/test/data/locale/fi-FI/module.yml b/vendor/plugins/globalize2/test/data/locale/fi-FI/module.yml
deleted file mode 100644
index 62d00b1..0000000
--- a/vendor/plugins/globalize2/test/data/locale/fi-FI/module.yml
+++ /dev/null
@@ -1,2 +0,0 @@
1fi-FI:
2 from-locale-dir: Locale hakemistosta. \ No newline at end of file
diff --git a/vendor/plugins/globalize2/test/data/locale/root.yml b/vendor/plugins/globalize2/test/data/locale/root.yml
deleted file mode 100644
index e69de29..0000000
--- a/vendor/plugins/globalize2/test/data/locale/root.yml
+++ /dev/null
diff --git a/vendor/plugins/globalize2/test/data/models.rb b/vendor/plugins/globalize2/test/data/models.rb
index f6dab90..553fca3 100644
--- a/vendor/plugins/globalize2/test/data/models.rb
+++ b/vendor/plugins/globalize2/test/data/models.rb
@@ -1,6 +1,16 @@
1#require 'ruby2ruby'
2#require 'parse_tree'
3#require 'parse_tree_extensions'
4#require 'pp'
5
6class PostTranslation < ActiveRecord::Base
7 def existing_method ; end
8end
9
1class Post < ActiveRecord::Base 10class Post < ActiveRecord::Base
2 translates :subject, :content 11 translates :subject, :content
3 validates_presence_of :subject 12 validates_presence_of :subject
13 named_scope :foobar, :conditions => { :title => "foobar" }
4end 14end
5 15
6class Blog < ActiveRecord::Base 16class Blog < ActiveRecord::Base
@@ -18,7 +28,7 @@ class Comment < ActiveRecord::Base
18 validates_presence_of :content 28 validates_presence_of :content
19 belongs_to :post 29 belongs_to :post
20end 30end
21 31
22class TranslatedComment < Comment 32class TranslatedComment < Comment
23 translates :content 33 translates :content
24end 34end
@@ -26,4 +36,16 @@ end
26class UltraLongModelNameWithoutProper < ActiveRecord::Base 36class UltraLongModelNameWithoutProper < ActiveRecord::Base
27 translates :subject, :content 37 translates :subject, :content
28 validates_presence_of :subject 38 validates_presence_of :subject
29end \ No newline at end of file 39end
40
41class Reloader < Parent
42 after_create :do_reload
43
44 def do_reload
45 reload
46 end
47end
48
49class Validatee < ActiveRecord::Base
50 translates :string
51end
diff --git a/vendor/plugins/globalize2/test/i18n/missing_translations_test.rb b/vendor/plugins/globalize2/test/i18n/missing_translations_test.rb
index 8641d57..5d0ecd6 100644
--- a/vendor/plugins/globalize2/test/i18n/missing_translations_test.rb
+++ b/vendor/plugins/globalize2/test/i18n/missing_translations_test.rb
@@ -1,5 +1,5 @@
1require File.join( File.dirname(__FILE__), '..', 'test_helper' ) 1require File.dirname(__FILE__) + '/../test_helper'
2require 'globalize/i18n/missing_translations_log_handler' 2require 'i18n/missing_translations_log_handler'
3 3
4class MissingTranslationsTest < ActiveSupport::TestCase 4class MissingTranslationsTest < ActiveSupport::TestCase
5 test "defines I18n.missing_translations_logger accessor" do 5 test "defines I18n.missing_translations_logger accessor" do
@@ -19,18 +19,18 @@ class LogMissingTranslationsTest < ActiveSupport::TestCase
19 def setup 19 def setup
20 @locale, @key, @options = :en, :foo, {} 20 @locale, @key, @options = :en, :foo, {}
21 @exception = I18n::MissingTranslationData.new(@locale, @key, @options) 21 @exception = I18n::MissingTranslationData.new(@locale, @key, @options)
22 22
23 @logger = TestLogger.new 23 @logger = TestLogger.new
24 I18n.missing_translations_logger = @logger 24 I18n.missing_translations_logger = @logger
25 end 25 end
26 26
27 test "still returns the exception message for MissingTranslationData exceptions" do 27 test "still returns the exception message for MissingTranslationData exceptions" do
28 result = I18n.send(:missing_translations_log_handler, @exception, @locale, @key, @options) 28 result = I18n.send(:missing_translations_log_handler, @exception, @locale, @key, @options)
29 assert_equal 'translation missing: en, foo', result 29 assert_equal 'translation missing: en, foo', result
30 end 30 end
31 31
32 test "logs the missing translation to I18n.missing_translations_logger" do 32 test "logs the missing translation to I18n.missing_translations_logger" do
33 I18n.send(:missing_translations_log_handler, @exception, @locale, @key, @options) 33 I18n.send(:missing_translations_log_handler, @exception, @locale, @key, @options)
34 assert_equal 'translation missing: en, foo', @logger 34 assert_equal 'translation missing: en, foo', @logger
35 end 35 end
36end 36end
diff --git a/vendor/plugins/globalize2/test/load_path_test.rb b/vendor/plugins/globalize2/test/load_path_test.rb
deleted file mode 100644
index ff009b3..0000000
--- a/vendor/plugins/globalize2/test/load_path_test.rb
+++ /dev/null
@@ -1,49 +0,0 @@
1require File.join( File.dirname(__FILE__), 'test_helper' )
2require 'globalize/load_path'
3
4class LoadPathTest < ActiveSupport::TestCase
5 def setup
6 @plugin_dir = "#{File.dirname(__FILE__)}/.."
7 @locale_dir = "#{File.dirname(__FILE__)}/data/locale"
8 @load_path = Globalize::LoadPath.new
9 end
10
11 test "returns glob patterns for all locales and ruby + yaml files by default" do
12 patterns = %w(locales/all.rb
13 locales/*.rb
14 locales/*/**/*.rb
15 locales/all.yml
16 locales/*.yml
17 locales/*/**/*.yml)
18 assert_equal patterns, @load_path.send(:patterns, 'locales')
19 end
20
21 test "returns the glob patterns for registered locales and extensions" do
22 @load_path.locales = [:en, :de]
23 @load_path.extensions = [:sql]
24 patterns = %w(locales/all.sql
25 locales/en.sql
26 locales/en/**/*.sql
27 locales/de.sql
28 locales/de/**/*.sql)
29 assert_equal patterns, @load_path.send(:patterns, 'locales')
30 end
31
32 test "expands paths using yml as a default file extension" do
33 @load_path << @locale_dir
34 expected = %w(all.yml de-DE.yml en-US.yml en-US/module.yml fi-FI/module.yml root.yml)
35 assert_equal expected, @load_path.map{|path| path.sub("#{@locale_dir}\/", '')}
36 end
37
38 test "appends new paths to the collection so earlier collected paths preceed later collected ones" do
39 @load_path.locales = [:root]
40 @load_path << "#{@plugin_dir}/lib/locale"
41 @load_path << @locale_dir
42
43 expected = %W(#{@plugin_dir}/lib/locale/root.yml
44 #{@locale_dir}/all.yml
45 #{@locale_dir}/root.yml)
46 assert_equal expected, @load_path
47 end
48
49end
diff --git a/vendor/plugins/globalize2/test/locale/fallbacks_test.rb b/vendor/plugins/globalize2/test/locale/fallbacks_test.rb
deleted file mode 100644
index 304d3da..0000000
--- a/vendor/plugins/globalize2/test/locale/fallbacks_test.rb
+++ /dev/null
@@ -1,154 +0,0 @@
1require File.join( File.dirname(__FILE__), '..', 'test_helper' )
2require 'globalize/locale/fallbacks'
3
4include Globalize::Locale
5I18n.default_locale = :'en-US' # This has to be set explicitly, no longer default for I18n
6
7class FallbacksTest < ActiveSupport::TestCase
8 def setup
9 I18n.fallbacks = Fallbacks.new
10 end
11
12 def teardown
13 I18n.default_locale = :'en-US'
14 end
15
16 test "#[] caches computed results" do
17 I18n.fallbacks['en']
18 assert_equal( { :en => [:en, :"en-US", :root] }, I18n.fallbacks )
19 end
20
21 test "#defaults always reflect the I18n.default_locale if no default has been set manually" do
22 I18n.default_locale = :'en-US'
23 assert_equal( [:'en-US', :en, :root], I18n.fallbacks.defaults )
24 end
25
26 test "#defaults always reflect a manually passed default locale if any" do
27 I18n.fallbacks = Fallbacks.new(:'fi-FI')
28 assert_equal( [:'fi-FI', :fi, :root], I18n.fallbacks.defaults )
29 I18n.default_locale = :'de-DE'
30 assert_equal( [:'fi-FI', :fi, :root], I18n.fallbacks.defaults )
31 end
32
33 test "#defaults allows to set multiple defaults" do
34 I18n.fallbacks = Fallbacks.new(:'fi-FI', :'se-FI')
35 assert_equal( [:'fi-FI', :fi, :'se-FI', :se, :root], I18n.fallbacks.defaults )
36 end
37end
38
39class NoMappingFallbacksTest < ActiveSupport::TestCase
40 def setup
41 @fallbacks = Fallbacks.new(:'en-US')
42 end
43
44 test "returns [:es, :en-US, :root] for :es" do
45 assert_equal [:es, :"en-US", :en, :root], @fallbacks[:es]
46 end
47
48 test "returns [:es-ES, :es, :en-US, :root] for :es-ES" do
49 assert_equal [:"es-ES", :es, :"en-US", :en, :root], @fallbacks[:"es-ES"]
50 end
51
52 test "returns [:es-MX, :es, :en-US, :root] for :es-MX" do
53 assert_equal [:"es-MX", :es, :"en-US", :en, :root], @fallbacks[:"es-MX"]
54 end
55
56 test "returns [:es-Latn-ES, :es-Latn, :es, :en-US, :root] for :es-Latn-ES" do
57 assert_equal [:"es-Latn-ES", :"es-Latn", :es, :"en-US", :en, :root], @fallbacks[:'es-Latn-ES']
58 end
59
60 test "returns [:en, :en-US, :root] for :en" do
61 assert_equal [:en, :"en-US", :root], @fallbacks[:en]
62 end
63
64 test "returns [:en-US, :en, :root] for :en-US (special case: locale == default)" do
65 assert_equal [:"en-US", :en, :root], @fallbacks[:"en-US"]
66 end
67end
68
69class CaMappingFallbacksTest < ActiveSupport::TestCase
70 # Most people who speak Catalan also live in Spain, so test is safe to assume
71 # that they also speak Spanish as spoken in Spain.
72 def setup
73 @fallbacks = Fallbacks.new(:'en-US')
74 @fallbacks.map :ca => :"es-ES"
75 end
76
77 test "returns [:ca, :es-ES, :es, :en-US, :root] for :ca" do
78 assert_equal [:ca, :"es-ES", :es, :"en-US", :en, :root], @fallbacks[:ca]
79 end
80
81 test "returns [:ca-ES, :ca, :es-ES, :es, :en-US, :root] for :ca-ES" do
82 assert_equal [:"ca-ES", :ca, :"es-ES", :es, :"en-US", :en, :root], @fallbacks[:"ca-ES"]
83 end
84end
85
86class ArMappingFallbacksTest < ActiveSupport::TestCase
87 # People who speak Arabic as spoken in Palestine often times also speak
88 # Hebrew as spoken in Israel. However test is in no way safe to assume that
89 # everybody who speaks Arabic also speaks Hebrew.
90 def setup
91 @fallbacks = Fallbacks.new(:'en-US')
92 @fallbacks.map :"ar-PS" => :"he-IL"
93 end
94
95 test "returns [:ar, :en-US, :root] for :ar" do
96 assert_equal [:ar, :"en-US", :en, :root], @fallbacks[:ar]
97 end
98
99 test "returns [:ar-EG, :ar, :en-US, :root] for :ar-EG" do
100 assert_equal [:"ar-EG", :ar, :"en-US", :en, :root], @fallbacks[:"ar-EG"]
101 end
102
103 test "returns [:ar-PS, :ar, :he-IL, :he, :en-US, :root] for :ar-PS" do
104 assert_equal [:"ar-PS", :ar, :"he-IL", :he, :"en-US", :en, :root], @fallbacks[:"ar-PS"]
105 end
106end
107
108class SmsMappingFallbacksTest < ActiveSupport::TestCase
109 # Sami people live in several scandinavian countries. In Finnland many people
110 # know Swedish and Finnish. Thus, test can be assumed that Sami living in
111 # Finnland also speak Swedish and Finnish.
112 def setup
113 @fallbacks = Fallbacks.new(:'en-US')
114 @fallbacks.map :sms => [:"se-FI", :"fi-FI"]
115 end
116
117 test "returns [:sms-FI, :sms, :se-FI, :se, :fi-FI, :fi, :en-US, :root] for :sms-FI" do
118 assert_equal [:"sms-FI", :sms, :"se-FI", :se, :"fi-FI", :fi, :"en-US", :en, :root], @fallbacks[:"sms-FI"]
119 end
120end
121
122class DeAtMappingFallbacksTest < ActiveSupport::TestCase
123 def setup
124 @fallbacks = Fallbacks.new(:'en-US')
125 @fallbacks.map :"de-AT" => :"de-DE"
126 end
127
128 test "returns [:de, :en-US, :root] for de" do
129 assert_equal [:de, :"en-US", :en, :root], @fallbacks[:"de"]
130 end
131
132 test "returns [:de-DE, :de, :en-US, :root] for de-DE" do
133 assert_equal [:"de-DE", :de, :"en-US", :en, :root], @fallbacks[:"de-DE"]
134 end
135
136 test "returns [:de-AT, :de, :de-DE, :en-US, :root] for de-AT" do
137 assert_equal [:"de-AT", :de, :"de-DE", :"en-US", :en, :root], @fallbacks[:"de-AT"]
138 end
139end
140
141class DeMappingFallbacksTest < ActiveSupport::TestCase
142 def setup
143 @fallbacks = Fallbacks.new(:'en-US')
144 @fallbacks.map :de => :en, :he => :en
145 end
146
147 test "returns [:de, :en, :root] for :de" do
148 assert_equal [:de, :en, :"en-US", :root], @fallbacks[:de]
149 end
150
151 test "returns [:he, :en, :root] for :de" do
152 assert_equal [:he, :en, :"en-US", :root], @fallbacks[:he]
153 end
154end
diff --git a/vendor/plugins/globalize2/test/locale/language_tag_test.rb b/vendor/plugins/globalize2/test/locale/language_tag_test.rb
deleted file mode 100644
index 2448af1..0000000
--- a/vendor/plugins/globalize2/test/locale/language_tag_test.rb
+++ /dev/null
@@ -1,130 +0,0 @@
1require File.join( File.dirname(__FILE__), '..', 'test_helper' )
2require 'globalize/locale/language_tag'
3
4include Globalize::Locale
5
6class LanguageTagTest < ActiveSupport::TestCase
7 test "given a valid tag 'de' returns an LanguageTag from #tag" do
8 assert_instance_of LanguageTag, LanguageTag.tag('de')
9 end
10
11 test "given a valid tag 'de' returns an array of subtags" do
12 assert_equal ['de', nil, nil, nil, nil, nil, nil], LanguageTag::SimpleParser.match('de')
13 end
14
15 test "given a valid tag 'de-DE' returns an array of subtags" do
16 assert_equal ['de', nil, 'DE', nil, nil, nil, nil], LanguageTag::SimpleParser.match('de-DE')
17 end
18
19 test "given a valid lowercase tag 'de-latn-de-variant-x-phonebk' returns an array of subtags" do
20 assert_equal ['de', 'latn', 'de', 'variant', nil, 'x-phonebk', nil],
21 LanguageTag::SimpleParser.match('de-latn-de-variant-x-phonebk')
22 end
23
24 test "given a valid uppercase tag 'DE-LATN-DE-VARIANT-X-PHONEBK' returns an array of subtags" do
25 assert_equal ['DE', 'LATN', 'DE', 'VARIANT', nil, 'X-PHONEBK', nil],
26 LanguageTag::SimpleParser.match('DE-LATN-DE-VARIANT-X-PHONEBK')
27 end
28
29 test "given an invalid tag 'a-DE' test returns false" do
30 assert !LanguageTag::SimpleParser.match('a-DE')
31 end
32
33 test "given an invalid tag 'de-419-DE' test returns false" do
34 assert !LanguageTag::SimpleParser.match('de-419-DE')
35 end
36end
37
38class DeLatnLanguageTagTest < ActiveSupport::TestCase
39 def setup
40 subtags = %w(de Latn DE variant a-ext x-phonebk i-klingon)
41 @tag = LanguageTag.new *subtags
42 end
43
44 test "returns 'de' as the language subtag in lowercase" do
45 assert_equal 'de', @tag.language
46 end
47
48 test "returns 'Latn' as the script subtag in titlecase" do
49 assert_equal 'Latn', @tag.script
50 end
51
52 test "returns 'DE' as the region subtag in uppercase" do
53 assert_equal 'DE', @tag.region
54 end
55
56 test "returns 'variant' as the variant subtag in lowercase" do
57 assert_equal 'variant', @tag.variant
58 end
59
60 test "returns 'a-ext' as the extension subtag" do
61 assert_equal 'a-ext', @tag.extension
62 end
63
64 test "returns 'x-phonebk' as the privateuse subtag" do
65 assert_equal 'x-phonebk', @tag.privateuse
66 end
67
68 test "returns 'i-klingon' as the grandfathered subtag" do
69 assert_equal 'i-klingon', @tag.grandfathered
70 end
71
72 test "returns a formatted tag string from #to_s" do
73 assert_equal 'de-Latn-DE-variant-a-ext-x-phonebk-i-klingon', @tag.to_s
74 end
75
76 test "returns an array containing the formatted subtags from #to_a" do
77 assert_equal %w(de Latn DE variant a-ext x-phonebk i-klingon), @tag.to_a
78 end
79end
80
81class InheritanceLanguageTagTest < ActiveSupport::TestCase
82 test "returns 'de-Latn-DE-variant-a-ext-x-phonebk' as the parent of 'de-Latn-DE-variant-a-ext-x-phonebk-i-klingon'" do
83 tag = LanguageTag.new *%w(de Latn DE variant a-ext x-phonebk i-klingon)
84 assert_equal 'de-Latn-DE-variant-a-ext-x-phonebk', tag.parent.to_s
85 end
86
87 test "returns 'de-Latn-DE-variant-a-ext' as the parent of 'de-Latn-DE-variant-a-ext-x-phonebk'" do
88 tag = LanguageTag.new *%w(de Latn DE variant a-ext x-phonebk)
89 assert_equal 'de-Latn-DE-variant-a-ext', tag.parent.to_s
90 end
91
92 test "returns 'de-Latn-DE-variant' as the parent of 'de-Latn-DE-variant-a-ext'" do
93 tag = LanguageTag.new *%w(de Latn DE variant a-ext)
94 assert_equal 'de-Latn-DE-variant', tag.parent.to_s
95 end
96
97 test "returns 'de-Latn-DE' as the parent of 'de-Latn-DE-variant'" do
98 tag = LanguageTag.new *%w(de Latn DE variant)
99 assert_equal 'de-Latn-DE', tag.parent.to_s
100 end
101
102 test "returns 'de-Latn' as the parent of 'de-Latn-DE'" do
103 tag = LanguageTag.new *%w(de Latn DE)
104 assert_equal 'de-Latn', tag.parent.to_s
105 end
106
107 test "returns 'de' as the parent of 'de-Latn'" do
108 tag = LanguageTag.new *%w(de Latn)
109 assert_equal 'de', tag.parent.to_s
110 end
111
112 # TODO RFC4647 says: "If no language tag matches the request, the "default" value is returned."
113 # where should we set the default language?
114 # test "returns '' as the parent of 'de'" do
115 # tag = LanguageTag.new *%w(de)
116 # assert_equal '', tag.parent.to_s
117 # end
118
119 test "returns an array of 5 parents for 'de-Latn-DE-variant-a-ext-x-phonebk-i-klingon'" do
120 parents = %w(de-Latn-DE-variant-a-ext-x-phonebk-i-klingon
121 de-Latn-DE-variant-a-ext-x-phonebk
122 de-Latn-DE-variant-a-ext
123 de-Latn-DE-variant
124 de-Latn-DE
125 de-Latn
126 de)
127 tag = LanguageTag.new *%w(de Latn DE variant a-ext x-phonebk i-klingon)
128 assert_equal parents, tag.parents.map{|tag| tag.to_s}
129 end
130end
diff --git a/vendor/plugins/globalize2/test/model/active_record/translated_test.rb b/vendor/plugins/globalize2/test/model/active_record/translated_test.rb
deleted file mode 100644
index f54894e..0000000
--- a/vendor/plugins/globalize2/test/model/active_record/translated_test.rb
+++ /dev/null
@@ -1,478 +0,0 @@
1require File.join( File.dirname(__FILE__), '..', '..', 'test_helper' )
2require 'active_record'
3require 'globalize/model/active_record'
4
5# Hook up model translation
6ActiveRecord::Base.send(:include, Globalize::Model::ActiveRecord::Translated)
7
8# Load Post model
9require File.join( File.dirname(__FILE__), '..', '..', 'data', 'models' )
10
11class TranslatedTest < ActiveSupport::TestCase
12 def setup
13 I18n.locale = :'en-US'
14 I18n.fallbacks.clear
15 reset_db! File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'data', 'schema.rb'))
16 ActiveRecord::Base.locale = nil
17 end
18
19 def teardown
20 I18n.fallbacks.clear
21 end
22
23 test "modifiying translated fields" do
24 post = Post.create :subject => 'foo'
25 assert_equal 'foo', post.subject
26 post.subject = 'bar'
27 assert_equal 'bar', post.subject
28 end
29
30 test "modifiying translated fields while switching locales" do
31 post = Post.create :subject => 'foo'
32 assert_equal 'foo', post.subject
33 I18n.locale = :'de-DE'
34 post.subject = 'bar'
35 assert_equal 'bar', post.subject
36 I18n.locale = :'en-US'
37 assert_equal 'foo', post.subject
38 I18n.locale = :'de-DE'
39 post.subject = 'bar'
40 end
41
42 test "has post_translations" do
43 post = Post.create
44 assert_nothing_raised { post.globalize_translations }
45 end
46
47 test "has German post_translations" do
48 I18n.locale = :de
49 post = Post.create :subject => 'foo'
50 assert_equal 1, post.globalize_translations.size
51 I18n.locale = :en
52 assert_equal 1, post.globalize_translations.size
53 end
54
55 test "returns the value passed to :subject" do
56 post = Post.new
57 assert_equal 'foo', (post.subject = 'foo')
58 end
59
60 test "translates subject and content into en-US" do
61 post = Post.create :subject => 'foo', :content => 'bar'
62 assert_equal 'foo', post.subject
63 assert_equal 'bar', post.content
64 assert post.save
65 post.reload
66 assert_equal 'foo', post.subject
67 assert_equal 'bar', post.content
68 end
69
70 test "finds a German post" do
71 post = Post.create :subject => 'foo (en)', :content => 'bar'
72 I18n.locale = 'de-DE'
73 post = Post.first
74 post.subject = 'baz (de)'
75 post.save
76 assert_equal 'baz (de)', Post.first.subject
77 I18n.locale = :'en-US'
78 assert_equal 'foo (en)', Post.first.subject
79 end
80
81 test "saves an English post and loads test correctly" do
82 assert_nil Post.first
83 post = Post.create :subject => 'foo', :content => 'bar'
84 assert post.save
85 post = Post.first
86 assert_equal 'foo', post.subject
87 assert_equal 'bar', post.content
88 end
89
90 test "updates an attribute" do
91 post = Post.create :subject => 'foo', :content => 'bar'
92 post.update_attribute :subject, 'baz'
93 assert_equal 'baz', Post.first.subject
94 end
95
96 test "update_attributes failure" do
97 post = Post.create :subject => 'foo', :content => 'bar'
98 assert !post.update_attributes( { :subject => '' } )
99 assert_nil post.reload.attributes['subject']
100 assert_equal 'foo', post.subject
101 end
102
103 test "validates presence of :subject" do
104 post = Post.new
105 assert !post.save
106
107 post = Post.new :subject => 'foo'
108 assert post.save
109 end
110
111 test "returns the value for the correct locale, after locale switching" do
112 post = Post.create :subject => 'foo'
113 I18n.locale = 'de-DE'
114 post.subject = 'bar'
115 post.save
116 I18n.locale = 'en-US'
117 post = Post.first
118 assert_equal 'foo', post.subject
119 I18n.locale = 'de-DE'
120 assert_equal 'bar', post.subject
121 end
122
123 test "keeping one field in new locale when other field is changed" do
124 I18n.fallbacks.map 'de-DE' => [ 'en-US' ]
125 post = Post.create :subject => 'foo'
126 I18n.locale = 'de-DE'
127 post.content = 'bar'
128 assert_equal 'foo', post.subject
129 end
130
131 test "modifying non-required field in a new locale" do
132 I18n.fallbacks.map 'de-DE' => [ 'en-US' ]
133 post = Post.create :subject => 'foo'
134 I18n.locale = 'de-DE'
135 post.content = 'bar'
136 assert post.save
137 end
138
139 test "returns the value for the correct locale, after locale switching, without saving" do
140 post = Post.create :subject => 'foo'
141 I18n.locale = 'de-DE'
142 post.subject = 'bar'
143 I18n.locale = 'en-US'
144 assert_equal 'foo', post.subject
145 I18n.locale = 'de-DE'
146 assert_equal 'bar', post.subject
147 end
148
149 test "saves all locales, even after locale switching" do
150 post = Post.new :subject => 'foo'
151 I18n.locale = 'de-DE'
152 post.subject = 'bar'
153 I18n.locale = 'he-IL'
154 post.subject = 'baz'
155 post.save
156 I18n.locale = 'en-US'
157 post = Post.first
158 assert_equal 'foo', post.subject
159 I18n.locale = 'de-DE'
160 assert_equal 'bar', post.subject
161 I18n.locale = 'he-IL'
162 assert_equal 'baz', post.subject
163 end
164
165 test "resolves a simple fallback" do
166 I18n.locale = 'de-DE'
167 post = Post.create :subject => 'foo'
168 I18n.locale = 'de'
169 post.subject = 'baz'
170 post.content = 'bar'
171 post.save
172 I18n.locale = 'de-DE'
173 assert_equal 'foo', post.subject
174 assert_equal 'bar', post.content
175 end
176
177 test "resolves a simple fallback without reloading" do
178 I18n.locale = 'de-DE'
179 post = Post.new :subject => 'foo'
180 I18n.locale = 'de'
181 post.subject = 'baz'
182 post.content = 'bar'
183 I18n.locale = 'de-DE'
184 assert_equal 'foo', post.subject
185 assert_equal 'bar', post.content
186 end
187
188 test "resolves a complex fallback without reloading" do
189 I18n.fallbacks.map 'de' => %w(en he)
190 I18n.locale = 'de'
191 post = Post.new
192 I18n.locale = 'en'
193 post.subject = 'foo'
194 I18n.locale = 'he'
195 post.subject = 'baz'
196 post.content = 'bar'
197 I18n.locale = 'de'
198 assert_equal 'foo', post.subject
199 assert_equal 'bar', post.content
200 end
201
202 test "returns nil if no translations are found" do
203 post = Post.new :subject => 'foo'
204 assert_equal 'foo', post.subject
205 assert_nil post.content
206 end
207
208 test "returns nil if no translations are found; reloaded" do
209 post = Post.create :subject => 'foo'
210 post = Post.first
211 assert_equal 'foo', post.subject
212 assert_nil post.content
213 end
214
215 test "works with associations" do
216 blog = Blog.create
217 post1 = blog.posts.create :subject => 'foo'
218 I18n.locale = 'de-DE'
219 post2 = blog.posts.create :subject => 'bar'
220 assert_equal 2, blog.posts.size
221 I18n.locale = 'en-US'
222 assert_equal 'foo', blog.posts.first.subject
223 assert_nil blog.posts.last.subject
224 I18n.locale = 'de-DE'
225 assert_equal 'bar', blog.posts.last.subject
226 end
227
228 test "works with simple dynamic finders" do
229 foo = Post.create :subject => 'foo'
230 Post.create :subject => 'bar'
231 post = Post.find_by_subject('foo')
232 assert_equal foo, post
233 end
234
235 test 'change attribute on globalized model' do
236 post = Post.create :subject => 'foo', :content => 'bar'
237 assert_equal [], post.changed
238 post.subject = 'baz'
239 assert_equal [ 'subject' ], post.changed
240 post.content = 'quux'
241 assert_member 'subject', post.changed
242 assert_member 'content', post.changed
243 end
244
245 test 'change attribute on globalized model after locale switching' do
246 post = Post.create :subject => 'foo', :content => 'bar'
247 assert_equal [], post.changed
248 post.subject = 'baz'
249 I18n.locale = :de
250 assert_equal [ 'subject' ], post.changed
251 end
252
253 test 'fallbacks with lots of locale switching' do
254 I18n.fallbacks.map :'de-DE' => [ :'en-US' ]
255 post = Post.create :subject => 'foo'
256
257 I18n.locale = :'de-DE'
258 assert_equal 'foo', post.subject
259
260 I18n.locale = :'en-US'
261 post.update_attribute :subject, 'bar'
262
263 I18n.locale = :'de-DE'
264 assert_equal 'bar', post.subject
265 end
266
267 test 'reload' do
268 post = Post.create :subject => 'foo', :content => 'bar'
269 post.subject = 'baz'
270 assert_equal 'foo', post.reload.subject
271 end
272
273 test 'complex writing and stashing' do
274 post = Post.create :subject => 'foo', :content => 'bar'
275 post.subject = nil
276 assert_nil post.subject
277 assert !post.valid?
278 end
279
280 test 'translated class locale setting' do
281 assert ActiveRecord::Base.respond_to?(:locale)
282 assert_equal :'en-US', I18n.locale
283 assert_equal :'en-US', ActiveRecord::Base.locale
284 I18n.locale = :de
285 assert_equal :de, I18n.locale
286 assert_equal :de, ActiveRecord::Base.locale
287 ActiveRecord::Base.locale = :es
288 assert_equal :de, I18n.locale
289 assert_equal :es, ActiveRecord::Base.locale
290 I18n.locale = :fr
291 assert_equal :fr, I18n.locale
292 assert_equal :es, ActiveRecord::Base.locale
293 end
294
295 test "untranslated class responds to locale" do
296 assert Blog.respond_to?(:locale)
297 end
298
299 test "to ensure locales in different classes are the same" do
300 ActiveRecord::Base.locale = :de
301 assert_equal :de, ActiveRecord::Base.locale
302 assert_equal :de, Parent.locale
303 Parent.locale = :es
304 assert_equal :es, ActiveRecord::Base.locale
305 assert_equal :es, Parent.locale
306 end
307
308 test "attribute saving goes by content locale and not global locale" do
309 ActiveRecord::Base.locale = :de
310 assert_equal :'en-US', I18n.locale
311 Post.create :subject => 'foo'
312 assert_equal :de, Post.first.globalize_translations.first.locale
313 end
314
315 test "attribute loading goes by content locale and not global locale" do
316 post = Post.create :subject => 'foo'
317 assert_equal :'en-US', ActiveRecord::Base.locale
318 ActiveRecord::Base.locale = :de
319 assert_equal :'en-US', I18n.locale
320 post.update_attribute :subject, 'foo [de]'
321 assert_equal 'foo [de]', Post.first.subject
322 ActiveRecord::Base.locale = :'en-US'
323 assert_equal 'foo', Post.first.subject
324 end
325
326 test "access content locale before setting" do
327 Globalize::Model::ActiveRecord::Translated::ActMethods.class_eval "remove_class_variable(:@@locale)"
328 assert_nothing_raised { ActiveRecord::Base.locale }
329 end
330
331 test "translated_locales" do
332 Post.locale = :de
333 post = Post.create :subject => 'foo'
334 Post.locale = :es
335 post.update_attribute :subject, 'bar'
336 Post.locale = :fr
337 post.update_attribute :subject, 'baz'
338 assert_equal [ :de, :es, :fr ], post.translated_locales
339 assert_equal [ :de, :es, :fr ], Post.first.translated_locales
340 end
341
342 test "including globalize_translations" do
343 I18n.locale = :de
344 Post.create :subject => "Foo1", :content => "Bar1"
345 Post.create :subject => "Foo2", :content => "Bar2"
346
347 class << Post
348 def tranlsations_included
349 self.all(:include => :globalize_translations)
350 end
351 end
352
353 default = Post.all.map {|x| [x.subject, x.content]}
354 with_include = Post.tranlsations_included.map {|x| [x.subject, x.content]}
355 assert_equal default, with_include
356 end
357
358 test "setting multiple translations at once with options hash" do
359 Post.locale = :de
360 post = Post.create :subject => "foo1", :content => "foo1"
361 Post.locale = :en
362 post.update_attributes( :subject => "bar1", :content => "bar1" )
363
364 options = { :de => {:subject => "foo2", :content => "foo2"},
365 :en => {:subject => "bar2", :content => "bar2"} }
366 post.set_translations options
367 post.reload
368
369 assert ["bar2", "bar2"], [post.subject, post.content]
370 Post.locale = :de
371 assert ["foo2", "foo2"], [post.subject, post.content]
372 end
373
374 test "setting only one translation with set_translations" do
375 Post.locale = :de
376 post = Post.create :subject => "foo1", :content => "foo1"
377 Post.locale = :en
378 post.update_attributes( :subject => "bar1", :content => "bar1" )
379
380 options = { :en => {:subject => "bar2", :content => "bar2"} }
381 post.set_translations options
382 post.reload
383
384 assert ["bar2", "bar2"], [post.subject, post.content]
385 Post.locale = :de
386 assert ["foo1", "foo1"], [post.subject, post.content]
387 end
388
389 test "setting only selected attributes with set_translations" do
390 Post.locale = :de
391 post = Post.create :subject => "foo1", :content => "foo1"
392 Post.locale = :en
393 post.update_attributes( :subject => "bar1", :content => "bar1" )
394
395 options = { :de => {:content => "foo2"}, :en => {:subject => "bar2"} }
396 post.set_translations options
397 post.reload
398
399 assert ["bar2", "bar1"], [post.subject, post.content]
400 Post.locale = :de
401 assert ["foo1", "foo2"], [post.subject, post.content]
402 end
403
404 test "setting invalid attributes raises ArgumentError" do
405 Post.locale = :de
406 post = Post.create :subject => "foo1", :content => "foo1"
407 Post.locale = :en
408 post.update_attributes( :subject => "bar1", :content => "bar1" )
409
410 options = { :de => {:fake => "foo2"} }
411 exception = assert_raise(ActiveRecord::UnknownAttributeError) do
412 post.set_translations options
413 end
414 assert_equal "unknown attribute: fake", exception.message
415 end
416
417 test "reload accepting find options" do
418 p = Post.create :subject => "Foo", :content => "Bar"
419 assert p.reload(:readonly => true, :lock => true)
420 assert_raise(ArgumentError) { p.reload(:foo => :bar) }
421 end
422
423 test "dependent destroy of translation" do
424 p = Post.create :subject => "Foo", :content => "Bar"
425 assert_equal 1, PostTranslation.count
426 p.destroy
427 assert_equal 0, PostTranslation.count
428 end
429
430 test "translating subclass of untranslated comment model" do
431 translated_comment = TranslatedComment.create(:post => @post)
432 assert_nothing_raised { translated_comment.globalize_translations }
433 end
434
435 test "modifiying translated comments works as expected" do
436 I18n.locale = :en
437 translated_comment = TranslatedComment.create(:post => @post, :content => 'foo')
438 assert_equal 'foo', translated_comment.content
439
440 I18n.locale = :de
441 translated_comment.content = 'bar'
442 assert translated_comment.save
443 assert_equal 'bar', translated_comment.content
444
445 I18n.locale = :en
446 assert_equal 'foo', translated_comment.content
447
448 assert_equal 2, translated_comment.globalize_translations.size
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
470end
471
472# TODO should validate_presence_of take fallbacks into account? maybe we need
473# an extra validation call, or more options for validate_presence_of.
474# TODO error checking for fields that exist in main table, don't exist in
475# proxy table, aren't strings or text
476#
477# TODO allow finding by translated attributes in conditions?
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 3a1c8c4..195907d 100644
--- a/vendor/plugins/globalize2/test/test_helper.rb
+++ b/vendor/plugins/globalize2/test/test_helper.rb
@@ -1,28 +1,41 @@
1$LOAD_PATH << File.expand_path( File.dirname(__FILE__) + '/../lib' )
2
1require 'rubygems' 3require 'rubygems'
2require 'test/unit' 4require 'test/unit'
5require 'active_record'
3require 'active_support' 6require 'active_support'
4require 'active_support/test_case' 7require 'active_support/test_case'
5require 'mocha' 8require 'mocha'
9require 'globalize'
10require 'validation_reflection'
6 11
7$LOAD_PATH << File.expand_path( File.dirname(__FILE__) + '/../lib' ) 12config = { :adapter => 'sqlite3', :database => ':memory:' }
13ActiveRecord::Base.establish_connection(config)
8 14
9class ActiveSupport::TestCase 15class ActiveSupport::TestCase
10 def reset_db!( schema_path ) 16 def reset_db!(schema_path = nil)
11 ::ActiveRecord::Migration.verbose = false # Quiet down the migration engine 17 schema_path ||= File.expand_path(File.dirname(__FILE__) + '/data/schema.rb')
12 ::ActiveRecord::Base.establish_connection({ 18 ActiveRecord::Migration.verbose = false
13 :adapter => 'sqlite3', 19 ActiveRecord::Base.silence { load(schema_path) }
14 :database => ':memory:'
15 })
16 ::ActiveRecord::Base.silence do
17 load schema_path
18 end
19 end 20 end
20 21
21 def assert_member(item, arr) 22 def assert_member(item, array)
22 assert_block "Item #{item} is not in array #{arr}" do 23 assert_block "Item #{item} is not in array #{array}" do
23 arr.member? item 24 array.member?(item)
24 end 25 end
25 end 26 end
27
28 def assert_belongs_to(model, associated)
29 assert model.reflect_on_all_associations(:belongs_to).detect { |association|
30 association.name.to_s == associated.to_s
31 }
32 end
33
34 def assert_has_many(model, associated)
35 assert model.reflect_on_all_associations(:has_many).detect { |association|
36 association.name.to_s == associated.to_s
37 }
38 end
26end 39end
27 40
28module ActiveRecord 41module ActiveRecord
@@ -33,4 +46,31 @@ module ActiveRecord
33 end 46 end
34 end 47 end
35 end 48 end
36end \ No newline at end of file 49end
50
51# module ActiveRecord
52# class BaseWithoutTable < Base
53# self.abstract_class = true
54#
55# def create_or_update
56# errors.empty?
57# end
58#
59# class << self
60# def columns()
61# @columns ||= []
62# end
63#
64# def column(name, sql_type = nil, default = nil, null = true)
65# columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
66# reset_column_information
67# end
68#
69# # Do not reset @columns
70# def reset_column_information
71# read_methods.each { |name| undef_method(name) }
72# @column_names = @columns_hash = @content_columns = @dynamic_methods_hash = @read_methods = nil
73# end
74# end
75# end
76# end \ No newline at end of file
diff --git a/vendor/plugins/globalize2/test/translation_test.rb b/vendor/plugins/globalize2/test/translation_test.rb
deleted file mode 100644
index 4b52bb1..0000000
--- a/vendor/plugins/globalize2/test/translation_test.rb
+++ /dev/null
@@ -1,54 +0,0 @@
1require File.join( File.dirname(__FILE__), 'test_helper' )
2require 'globalize/translation'
3
4class TranslationTest < ActiveSupport::TestCase
5 include Globalize
6
7 def setup
8 @translation = Translation::Static.new 'foo', :locale => :'en-US'
9 end
10
11 test "responds to fallback?" do
12 assert @translation.respond_to?( :fallback? )
13 end
14
15 test "returns true when :locale and :requested_locale are not equal" do
16 @translation.requested_locale = :'de-DE'
17 assert @translation.fallback?
18 end
19
20 test "returns false when :locale and :requested_locale are equal" do
21 @translation.requested_locale = :'en-US'
22 assert !@translation.fallback?
23 end
24
25 test "has the attribute :locale" do
26 assert @translation.respond_to?( :locale )
27 end
28
29 test "has the attribute :requested_locale" do
30 assert @translation.respond_to?( :requested_locale )
31 end
32
33 test "has the attribute :options" do
34 assert @translation.respond_to?( :options )
35 end
36
37 test "has the attribute :plural_key" do
38 assert @translation.respond_to?( :plural_key )
39 end
40
41 test "has the attribute :original" do
42 assert @translation.respond_to?( :original )
43 end
44
45 test "Translation::Attribute has the attribute :locale" do
46 translation = Translation::Attribute.new 'foo'
47 assert translation.respond_to?( :locale )
48 end
49
50 test "Translation::Attribute has the attribute :requested_locale" do
51 translation = Translation::Attribute.new 'foo'
52 assert translation.respond_to?( :requested_locale )
53 end
54end