mongoid_globalize 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.rspec +1 -0
- data/.rvmrc +1 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +74 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +0 -0
- data/Rakefile +39 -0
- data/VERSION +1 -0
- data/lib/mongoid_globalize/act_macro.rb +23 -0
- data/lib/mongoid_globalize/adapter.rb +73 -0
- data/lib/mongoid_globalize/attributes.rb +25 -0
- data/lib/mongoid_globalize/class_methods.rb +64 -0
- data/lib/mongoid_globalize/document_translation.rb +30 -0
- data/lib/mongoid_globalize/instance_methods.rb +174 -0
- data/lib/mongoid_globalize.rb +69 -0
- data/mongoid_globalize.gemspec +86 -0
- data/spec/data/models.rb +77 -0
- data/spec/mongoid_globalize/attributes_spec.rb +138 -0
- data/spec/mongoid_globalize/clone_spec.rb +51 -0
- data/spec/mongoid_globalize/dirty_tracking_spec.rb +62 -0
- data/spec/mongoid_globalize/fallbacks_spec.rb +135 -0
- data/spec/mongoid_globalize/locale_spec.rb +89 -0
- data/spec/mongoid_globalize/set_translations_spec.rb +49 -0
- data/spec/mongoid_globalize/translation_class_spec.rb +57 -0
- data/spec/mongoid_globalize/validations_spec.rb +93 -0
- data/spec/mongoid_globalize_spec.rb +173 -0
- data/spec/spec_helper.rb +77 -0
- metadata +170 -0
@@ -0,0 +1,86 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{mongoid_globalize}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = [%q{Mik-die}]
|
12
|
+
s.date = %q{2011-07-25}
|
13
|
+
s.description = %q{Library for translating Mongoid documents, based on Globalize3 principles}
|
14
|
+
s.email = %q{MikDiet@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"README.rdoc"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
".rspec",
|
20
|
+
".rvmrc",
|
21
|
+
"Gemfile",
|
22
|
+
"Gemfile.lock",
|
23
|
+
"MIT-LICENSE",
|
24
|
+
"README.rdoc",
|
25
|
+
"Rakefile",
|
26
|
+
"VERSION",
|
27
|
+
"lib/mongoid_globalize.rb",
|
28
|
+
"lib/mongoid_globalize/act_macro.rb",
|
29
|
+
"lib/mongoid_globalize/adapter.rb",
|
30
|
+
"lib/mongoid_globalize/attributes.rb",
|
31
|
+
"lib/mongoid_globalize/class_methods.rb",
|
32
|
+
"lib/mongoid_globalize/document_translation.rb",
|
33
|
+
"lib/mongoid_globalize/instance_methods.rb",
|
34
|
+
"mongoid_globalize.gemspec",
|
35
|
+
"spec/data/models.rb",
|
36
|
+
"spec/mongoid_globalize/attributes_spec.rb",
|
37
|
+
"spec/mongoid_globalize/clone_spec.rb",
|
38
|
+
"spec/mongoid_globalize/dirty_tracking_spec.rb",
|
39
|
+
"spec/mongoid_globalize/fallbacks_spec.rb",
|
40
|
+
"spec/mongoid_globalize/locale_spec.rb",
|
41
|
+
"spec/mongoid_globalize/set_translations_spec.rb",
|
42
|
+
"spec/mongoid_globalize/translation_class_spec.rb",
|
43
|
+
"spec/mongoid_globalize/validations_spec.rb",
|
44
|
+
"spec/mongoid_globalize_spec.rb",
|
45
|
+
"spec/spec_helper.rb"
|
46
|
+
]
|
47
|
+
s.homepage = %q{http://github.com/Mik-die/mongoid_globalize}
|
48
|
+
s.licenses = [%q{MIT}]
|
49
|
+
s.require_paths = [%q{lib}]
|
50
|
+
s.rubygems_version = %q{1.8.5}
|
51
|
+
s.summary = %q{Library for translating Mongoid documents}
|
52
|
+
|
53
|
+
if s.respond_to? :specification_version then
|
54
|
+
s.specification_version = 3
|
55
|
+
|
56
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
57
|
+
s.add_runtime_dependency(%q<activemodel>, ["= 3.1.0.rc4"])
|
58
|
+
s.add_runtime_dependency(%q<mongoid>, [">= 0"])
|
59
|
+
s.add_runtime_dependency(%q<bson_ext>, [">= 0"])
|
60
|
+
s.add_development_dependency(%q<rspec>, [">= 0"])
|
61
|
+
s.add_development_dependency(%q<mongoid-rspec>, [">= 0"])
|
62
|
+
s.add_development_dependency(%q<database_cleaner>, [">= 0"])
|
63
|
+
s.add_development_dependency(%q<ruby-debug19>, [">= 0"])
|
64
|
+
s.add_development_dependency(%q<jeweler>, [">= 0"])
|
65
|
+
else
|
66
|
+
s.add_dependency(%q<activemodel>, ["= 3.1.0.rc4"])
|
67
|
+
s.add_dependency(%q<mongoid>, [">= 0"])
|
68
|
+
s.add_dependency(%q<bson_ext>, [">= 0"])
|
69
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
70
|
+
s.add_dependency(%q<mongoid-rspec>, [">= 0"])
|
71
|
+
s.add_dependency(%q<database_cleaner>, [">= 0"])
|
72
|
+
s.add_dependency(%q<ruby-debug19>, [">= 0"])
|
73
|
+
s.add_dependency(%q<jeweler>, [">= 0"])
|
74
|
+
end
|
75
|
+
else
|
76
|
+
s.add_dependency(%q<activemodel>, ["= 3.1.0.rc4"])
|
77
|
+
s.add_dependency(%q<mongoid>, [">= 0"])
|
78
|
+
s.add_dependency(%q<bson_ext>, [">= 0"])
|
79
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
80
|
+
s.add_dependency(%q<mongoid-rspec>, [">= 0"])
|
81
|
+
s.add_dependency(%q<database_cleaner>, [">= 0"])
|
82
|
+
s.add_dependency(%q<ruby-debug19>, [">= 0"])
|
83
|
+
s.add_dependency(%q<jeweler>, [">= 0"])
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
data/spec/data/models.rb
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
class Post
|
2
|
+
include Mongoid::Document
|
3
|
+
include Mongoid::Globalize
|
4
|
+
belongs_to :blog
|
5
|
+
translates :title, :content
|
6
|
+
|
7
|
+
translates :published => Boolean
|
8
|
+
translates :published_at => DateTime
|
9
|
+
validates_presence_of :title
|
10
|
+
scope :with_some_title, :conditions => { :title => 'some_title' }
|
11
|
+
end
|
12
|
+
|
13
|
+
class PostTranslation
|
14
|
+
include Mongoid::Document
|
15
|
+
field :locale
|
16
|
+
field :title
|
17
|
+
field :content
|
18
|
+
field :published => Boolean
|
19
|
+
field :published_at => DateTime
|
20
|
+
embedded_in :post
|
21
|
+
|
22
|
+
def existing_method
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class ReloadingPost < Post
|
27
|
+
after_create { reload }
|
28
|
+
end
|
29
|
+
|
30
|
+
class Blog
|
31
|
+
include Mongoid::Document
|
32
|
+
has_many :posts
|
33
|
+
field :name
|
34
|
+
end
|
35
|
+
|
36
|
+
class Validatee
|
37
|
+
include Mongoid::Document
|
38
|
+
include Mongoid::Globalize
|
39
|
+
translates :string
|
40
|
+
end
|
41
|
+
|
42
|
+
class Parent
|
43
|
+
include Mongoid::Document
|
44
|
+
include Mongoid::Globalize
|
45
|
+
translates :content, :type
|
46
|
+
end
|
47
|
+
|
48
|
+
class Child < Parent
|
49
|
+
end
|
50
|
+
|
51
|
+
class Comment
|
52
|
+
include Mongoid::Document
|
53
|
+
field :content
|
54
|
+
validates_presence_of :content
|
55
|
+
belongs_to :post
|
56
|
+
end
|
57
|
+
|
58
|
+
class TranslatedComment < Comment
|
59
|
+
include Mongoid::Globalize
|
60
|
+
translates :content, :title
|
61
|
+
end
|
62
|
+
|
63
|
+
class User
|
64
|
+
include Mongoid::Document
|
65
|
+
include Mongoid::Timestamps
|
66
|
+
include Mongoid::Globalize
|
67
|
+
translates :name
|
68
|
+
field :email
|
69
|
+
validates_presence_of :name, :email
|
70
|
+
end
|
71
|
+
|
72
|
+
class Task
|
73
|
+
include Mongoid::Document
|
74
|
+
include Mongoid::Globalize
|
75
|
+
fallbacks_for_empty_translations!
|
76
|
+
translates :name
|
77
|
+
end
|
@@ -0,0 +1,138 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe 'Attributes' do
|
5
|
+
it "defines accessors for the translated attributes" do
|
6
|
+
post = Post.new
|
7
|
+
post.should respond_to(:title)
|
8
|
+
post.should respond_to(:title=)
|
9
|
+
end
|
10
|
+
|
11
|
+
describe "attribute_names" do
|
12
|
+
it "returns translated and existing attribute names" do
|
13
|
+
Post.new(:blog_id => 5).attribute_names.should include('blog_id', 'content', 'title')
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
it "returns translated and regular attributes" do
|
18
|
+
post = Post.create(:title => 'foo', :blog_id => 5)
|
19
|
+
post.attributes.should include('blog_id' => 5, 'title' => 'foo', 'content' => nil)
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "write_attribute for non-translated attributes" do
|
23
|
+
it "should return the value" do
|
24
|
+
user = User.create(:name => 'Max Mustermann', :email => 'max@mustermann.de')
|
25
|
+
new_email = 'm.muster@mann.de'
|
26
|
+
user.write_attribute('email', new_email).should == new_email
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe "translated_attribute_names" do
|
31
|
+
it "returns translated attribute names" do
|
32
|
+
Post.translated_attribute_names.should include(:title, :content)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "a translated attribute" do
|
37
|
+
describe "writer" do
|
38
|
+
it "returns its argument" do
|
39
|
+
(Post.new.title = 'foo').should == 'foo'
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe "reader" do
|
44
|
+
it "returns the correct translation for a saved record after locale switching" do
|
45
|
+
post = Post.create(:title => 'title')
|
46
|
+
post.update_attributes(:title => 'Titel', :locale => :de)
|
47
|
+
post.reload
|
48
|
+
post.should be_translated(:en).for(:title).as('title')
|
49
|
+
post.should be_translated(:de).for(:title).as('Titel')
|
50
|
+
end
|
51
|
+
|
52
|
+
it "returns the correct translation for an unsaved record after locale switching" do
|
53
|
+
post = Post.create(:title => 'title')
|
54
|
+
with_locale(:de) { post.title = 'Titel' }
|
55
|
+
post.should be_translated(:en).for(:title).as('title')
|
56
|
+
post.should be_translated(:de).for(:title).as('Titel')
|
57
|
+
end
|
58
|
+
|
59
|
+
it "returns the correct translation for both saved/unsaved records while switching locales" do
|
60
|
+
post = Post.new(:title => 'title')
|
61
|
+
with_locale(:de) { post.title = 'Titel' }
|
62
|
+
with_locale(:he) { post.title = 'שם' }
|
63
|
+
post.should be_translated(:de).for(:title).as('Titel')
|
64
|
+
post.should be_translated(:he).for(:title).as('שם')
|
65
|
+
post.should be_translated(:en).for(:title).as('title')
|
66
|
+
post.should be_translated(:he).for(:title).as('שם')
|
67
|
+
post.should be_translated(:de).for(:title).as('Titel')
|
68
|
+
|
69
|
+
post.save
|
70
|
+
post.reload
|
71
|
+
post.should be_translated(:de).for(:title).as('Titel')
|
72
|
+
post.should be_translated(:he).for(:title).as('שם')
|
73
|
+
post.should be_translated(:en).for(:title).as('title')
|
74
|
+
post.should be_translated(:he).for(:title).as('שם')
|
75
|
+
post.should be_translated(:de).for(:title).as('Titel')
|
76
|
+
end
|
77
|
+
|
78
|
+
it "returns nil if no translations are found on an unsaved record" do
|
79
|
+
post = Post.new(:title => 'foo')
|
80
|
+
post.title.should == 'foo'
|
81
|
+
post.content.should be_nil
|
82
|
+
end
|
83
|
+
|
84
|
+
it "returns nil if no translations are found on a saved record" do
|
85
|
+
post = Post.create(:title => 'foo')
|
86
|
+
post.reload
|
87
|
+
post.title.should == 'foo'
|
88
|
+
post.content.should be_nil
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
describe "before_type_cast reader" do
|
94
|
+
it "works for translated attributes" do
|
95
|
+
post = Post.create(:title => 'title')
|
96
|
+
post.update_attributes(:title => "Titel", :locale => :de)
|
97
|
+
|
98
|
+
with_locale(:en) { post.title_before_type_cast.should == 'title' }
|
99
|
+
with_locale(:de) { post.title_before_type_cast.should == 'Titel' }
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
it "saves all translations on an sti model after locale switching" do
|
104
|
+
child = Child.new(:content => 'foo')
|
105
|
+
with_locale(:de) { child.content = 'bar' }
|
106
|
+
with_locale(:he) { child.content = 'baz' }
|
107
|
+
child.save
|
108
|
+
child.reload
|
109
|
+
child.should be_translated(:en).for(:content).as('foo')
|
110
|
+
child.should be_translated(:de).for(:content).as('bar')
|
111
|
+
child.should be_translated(:he).for(:content).as('baz')
|
112
|
+
end
|
113
|
+
|
114
|
+
describe "attribute reader" do
|
115
|
+
it "will use the current locale on Mongoid::Globalize or I18n without arguments" do
|
116
|
+
with_locale(:de){ Post.create!(:title => 'Titel', :content => 'Inhalt') }
|
117
|
+
I18n.locale = :de
|
118
|
+
Post.first.title.should == 'Titel'
|
119
|
+
I18n.locale = :en
|
120
|
+
Mongoid::Globalize.locale = :de
|
121
|
+
Post.first.title.should == 'Titel'
|
122
|
+
end
|
123
|
+
|
124
|
+
it "will use the given locale when passed a locale" do
|
125
|
+
post = with_locale(:de){ Post.create!(:title => 'Titel', :content => 'Inhalt') }
|
126
|
+
post.title(:de).should == 'Titel'
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
describe "modifying a translated attribute" do
|
131
|
+
it "does not change the untranslated value" do
|
132
|
+
post = Post.create(:title => 'title')
|
133
|
+
before = post.untranslated_attributes['title']
|
134
|
+
post.title = 'changed title'
|
135
|
+
post.untranslated_attributes['title'].should == before
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe "Clone" do
|
5
|
+
it 'stores translations from clonned new record' do
|
6
|
+
check_stored_translations(standard_post.clone)
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'stores translations from clonned created record' do
|
10
|
+
clonned = saved_post.clone
|
11
|
+
check_stored_translations(clonned)
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'stores translations from clonned found record' do
|
15
|
+
check_stored_translations( Post.find(saved_post.id).clone )
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'stores translations from clonned reloaded after creation record' do
|
19
|
+
check_stored_translations(saved_post.reload.clone)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def standard_post
|
24
|
+
post = Post.new({:title => 'title', :content => 'content'})
|
25
|
+
with_locale(:he) { post.title= 'שם' }
|
26
|
+
post
|
27
|
+
end
|
28
|
+
|
29
|
+
def saved_post
|
30
|
+
standard_post.tap{|p| p.save!}
|
31
|
+
end
|
32
|
+
|
33
|
+
def translations_modifications(clonned)
|
34
|
+
clonned.content = 'another content'
|
35
|
+
with_locale(:de) { clonned.title = 'Titel' }
|
36
|
+
end
|
37
|
+
|
38
|
+
def translations_specs(clonned)
|
39
|
+
clonned.should be_translated(:en).for(:title).as('title') # original
|
40
|
+
clonned.should be_translated(:en).for(:content).as('another content') # changed
|
41
|
+
clonned.should be_translated(:de).for(:title).as('Titel') # new
|
42
|
+
clonned.should be_translated(:he).for(:title).as('שם') # untouched language
|
43
|
+
end
|
44
|
+
|
45
|
+
def check_stored_translations(clonned)
|
46
|
+
translations_modifications(clonned)
|
47
|
+
translations_specs(clonned)
|
48
|
+
clonned.save!
|
49
|
+
clonned.reload
|
50
|
+
translations_specs(clonned)
|
51
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe "Dirty tracking" do
|
5
|
+
it "works" do
|
6
|
+
post = Post.create(:title => 'title', :content => 'content')
|
7
|
+
post.changed.should == []
|
8
|
+
|
9
|
+
post.title = 'title'
|
10
|
+
post.changed.should == []
|
11
|
+
|
12
|
+
post.title = 'changed title'
|
13
|
+
post.changed.should == ['title']
|
14
|
+
|
15
|
+
post.content = 'changed content'
|
16
|
+
post.changed.should include('title', 'content')
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'works per a locale' do
|
20
|
+
post = Post.create(:title => 'title', :content => 'content')
|
21
|
+
post.changed.should == []
|
22
|
+
|
23
|
+
post.title = 'changed title'
|
24
|
+
post.changes.should == { 'title' => ['title', 'changed title'] }
|
25
|
+
post.save
|
26
|
+
|
27
|
+
I18n.locale = :de
|
28
|
+
post.title = 'Alt Titel'
|
29
|
+
|
30
|
+
post.title = 'Titel'
|
31
|
+
post.changes.should == { 'title' => [nil, 'Titel'] }
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'works after locale switching' do
|
35
|
+
post = Post.create(:title => 'title', :content => 'content')
|
36
|
+
post.changed.should == []
|
37
|
+
|
38
|
+
post.title = 'changed title'
|
39
|
+
I18n.locale = :de
|
40
|
+
post.changed.should == ['title']
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'works on sti model' do
|
44
|
+
child = Child.create(:content => 'foo')
|
45
|
+
child.changed.should == []
|
46
|
+
|
47
|
+
child.content = 'bar'
|
48
|
+
child.changed.should == ['content']
|
49
|
+
|
50
|
+
child.content = 'baz'
|
51
|
+
child.changed.should include('content')
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'works on sti model after locale switching' do
|
55
|
+
child = Child.create(:content => 'foo')
|
56
|
+
child.changed.should == []
|
57
|
+
|
58
|
+
child.content = 'bar'
|
59
|
+
I18n.locale = :de
|
60
|
+
child.changed.should == ['content']
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,135 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe "Fallbacks" do
|
5
|
+
before :each do
|
6
|
+
@previous_backend = I18n.backend
|
7
|
+
I18n.pretend_fallbacks
|
8
|
+
I18n.backend = BackendWithFallbacks.new
|
9
|
+
|
10
|
+
I18n.locale = :'en-US'
|
11
|
+
I18n.fallbacks = ::I18n::Locale::Fallbacks.new
|
12
|
+
end
|
13
|
+
|
14
|
+
after :each do
|
15
|
+
I18n.fallbacks.clear
|
16
|
+
I18n.hide_fallbacks
|
17
|
+
I18n.backend = @previous_backend
|
18
|
+
end
|
19
|
+
|
20
|
+
it "keep one field in new locale when other field is changed" do
|
21
|
+
I18n.fallbacks.map('de-DE' => [ 'en-US' ])
|
22
|
+
post = Post.create :title => 'foo'
|
23
|
+
I18n.locale = 'de-DE'
|
24
|
+
post.content = 'bar'
|
25
|
+
post.title.should == 'foo'
|
26
|
+
end
|
27
|
+
|
28
|
+
it "modify non-required field in a new locale" do
|
29
|
+
I18n.fallbacks.map 'de-DE' => [ 'en-US' ]
|
30
|
+
post = Post.create :title => 'foo'
|
31
|
+
I18n.locale = 'de-DE'
|
32
|
+
post.content = 'bar'
|
33
|
+
post.save.should be_true
|
34
|
+
end
|
35
|
+
|
36
|
+
it "resolve a simple fallback" do
|
37
|
+
I18n.locale = 'de-DE'
|
38
|
+
post = Post.create :title => 'foo'
|
39
|
+
|
40
|
+
I18n.locale = 'de'
|
41
|
+
post.title = 'baz'
|
42
|
+
post.content = 'bar'
|
43
|
+
post.save!
|
44
|
+
|
45
|
+
I18n.locale = 'de-DE'
|
46
|
+
post.title.should == 'foo'
|
47
|
+
post.content.should == 'bar'
|
48
|
+
end
|
49
|
+
|
50
|
+
it "resolve a simple fallback without reloading" do
|
51
|
+
I18n.locale = 'de-DE'
|
52
|
+
post = Post.new :title => 'foo'
|
53
|
+
|
54
|
+
I18n.locale = 'de'
|
55
|
+
post.title = 'baz'
|
56
|
+
post.content = 'bar'
|
57
|
+
|
58
|
+
I18n.locale = 'de-DE'
|
59
|
+
post.title.should == 'foo'
|
60
|
+
post.content.should == 'bar'
|
61
|
+
end
|
62
|
+
|
63
|
+
it "resolve a complex fallback without reloading" do
|
64
|
+
I18n.fallbacks.map 'de' => %w(en he)
|
65
|
+
I18n.locale = 'de'
|
66
|
+
post = Post.new
|
67
|
+
I18n.locale = 'en'
|
68
|
+
post.title = 'foo'
|
69
|
+
I18n.locale = 'he'
|
70
|
+
post.title = 'baz'
|
71
|
+
post.content = 'bar'
|
72
|
+
I18n.locale = 'de'
|
73
|
+
post.title.should == 'foo'
|
74
|
+
post.content.should == 'bar'
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'work with lots of locale switching' do
|
78
|
+
I18n.fallbacks.map :'de-DE' => [ :'en-US' ]
|
79
|
+
post = Post.create :title => 'foo'
|
80
|
+
I18n.locale = :'de-DE'
|
81
|
+
post.title.should == 'foo'
|
82
|
+
|
83
|
+
I18n.locale = :'en-US'
|
84
|
+
post.update_attributes(:title => 'bar')
|
85
|
+
I18n.locale = :'de-DE'
|
86
|
+
post.title.should == 'bar'
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'work with lots of locale switching 2' do
|
90
|
+
I18n.fallbacks.map :'de-DE' => [ :'en-US' ]
|
91
|
+
child = Child.create :content => 'foo'
|
92
|
+
I18n.locale = :'de-DE'
|
93
|
+
child.content.should == 'foo'
|
94
|
+
|
95
|
+
I18n.locale = :'en-US'
|
96
|
+
child.update_attributes(:content => 'bar')
|
97
|
+
I18n.locale = :'de-DE'
|
98
|
+
child.content.should == 'bar'
|
99
|
+
end
|
100
|
+
|
101
|
+
it 'work with nil translations' do
|
102
|
+
I18n.fallbacks.map :'de-DE' => [ :'en-US' ]
|
103
|
+
post = Post.create :title => 'foo'
|
104
|
+
I18n.locale = :'de-DE'
|
105
|
+
post.title.should == 'foo'
|
106
|
+
|
107
|
+
post.update_attribute :title, nil
|
108
|
+
post.title.should == 'foo'
|
109
|
+
end
|
110
|
+
|
111
|
+
it 'work with empty translations' do
|
112
|
+
I18n.fallbacks.map :'de-DE' => [ :'en-US' ]
|
113
|
+
task = Task.create :name => 'foo'
|
114
|
+
I18n.locale = :'de-DE'
|
115
|
+
task.name.should == 'foo'
|
116
|
+
|
117
|
+
task.update_attribute :name, ''
|
118
|
+
task.name.should == 'foo'
|
119
|
+
end
|
120
|
+
|
121
|
+
it 'work with empty translations 2' do
|
122
|
+
I18n.fallbacks.map :'de-DE' => [ :'en-US' ]
|
123
|
+
task = Task.create :name => 'foo'
|
124
|
+
post = Post.create :title => 'foo'
|
125
|
+
I18n.locale = :'de-DE'
|
126
|
+
task.name.should == 'foo'
|
127
|
+
post.title.should == 'foo'
|
128
|
+
|
129
|
+
task.update_attribute :name, ''
|
130
|
+
task.name.should == 'foo'
|
131
|
+
|
132
|
+
post.update_attribute :title, ''
|
133
|
+
post.title.should == ''
|
134
|
+
end
|
135
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe Mongoid::Globalize do
|
5
|
+
it "has locale accessors" do
|
6
|
+
Mongoid::Globalize.should respond_to(:locale)
|
7
|
+
Mongoid::Globalize.should respond_to(:locale=)
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "#locale reader" do
|
11
|
+
it "can be called before a locale was set" do
|
12
|
+
Mongoid::Globalize.locale = nil
|
13
|
+
lambda{ Mongoid::Globalize.locale }.should_not raise_error
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe 'locale setting' do
|
18
|
+
it "works" do
|
19
|
+
I18n.locale.should == :en
|
20
|
+
Mongoid::Globalize.locale.should == :en
|
21
|
+
|
22
|
+
I18n.locale = :de
|
23
|
+
I18n.locale.should == :de
|
24
|
+
Mongoid::Globalize.locale.should == :de
|
25
|
+
|
26
|
+
Mongoid::Globalize.locale = :es
|
27
|
+
I18n.locale.should == :de
|
28
|
+
Mongoid::Globalize.locale.should == :es
|
29
|
+
|
30
|
+
I18n.locale = :fr
|
31
|
+
I18n.locale.should == :fr
|
32
|
+
Mongoid::Globalize.locale.should == :es
|
33
|
+
end
|
34
|
+
|
35
|
+
it "works with strings" do
|
36
|
+
I18n.locale = 'de'
|
37
|
+
Mongoid::Globalize.locale = 'de'
|
38
|
+
Mongoid::Globalize.locale.should == I18n.locale
|
39
|
+
|
40
|
+
I18n.locale = 'de'
|
41
|
+
Mongoid::Globalize.locale = :de
|
42
|
+
Mongoid::Globalize.locale.should == I18n.locale
|
43
|
+
|
44
|
+
I18n.locale = :de
|
45
|
+
Mongoid::Globalize.locale = 'de'
|
46
|
+
Mongoid::Globalize.locale.should == I18n.locale
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe "with_locale" do
|
51
|
+
it "temporarily sets the given locale and yields the block" do
|
52
|
+
Mongoid::Globalize.locale.should == :en
|
53
|
+
Mongoid::Globalize.with_locale :de do |locale|
|
54
|
+
Mongoid::Globalize.locale.should == :de
|
55
|
+
locale.should == :de
|
56
|
+
end
|
57
|
+
Mongoid::Globalize.locale.should == :en
|
58
|
+
end
|
59
|
+
|
60
|
+
it "calls block once with each locale given temporarily set" do
|
61
|
+
locales = Mongoid::Globalize.with_locales :en, [:de, :fr] do |locale|
|
62
|
+
Mongoid::Globalize.locale.should == locale
|
63
|
+
locale
|
64
|
+
end
|
65
|
+
locales.should == [:en, :de, :fr]
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe "attribute saving" do
|
70
|
+
it "goes by content locale and not global locale" do
|
71
|
+
Mongoid::Globalize.locale = :de
|
72
|
+
I18n.locale.should == :en
|
73
|
+
Post.create :title => 'foo'
|
74
|
+
Post.first.translations.first.locale.should == :de
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
describe "attribute loading" do
|
79
|
+
it "goes by content locale and not global locale" do
|
80
|
+
post = Post.create(:title => 'title')
|
81
|
+
Post.first.should be_translated(:en).for(:title).as('title')
|
82
|
+
|
83
|
+
Mongoid::Globalize.locale = :de
|
84
|
+
post.update_attributes(:title => 'Titel')
|
85
|
+
Post.first.should be_translated(:en).for(:title).as('title')
|
86
|
+
Post.first.should be_translated(:de).for(:title).as('Titel')
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe "#set_translations" do
|
5
|
+
it "sets multiple translations at once" do
|
6
|
+
post = Post.create(:title => 'title', :content => 'content', :locale => :en)
|
7
|
+
post.update_attributes(:title => 'Titel', :content => 'Inhalt', :locale => :de)
|
8
|
+
post.set_translations(
|
9
|
+
:en => { :title => 'updated title', :content => 'updated content' },
|
10
|
+
:de => { :title => 'geänderter Titel', :content => 'geänderter Inhalt' }
|
11
|
+
)
|
12
|
+
post.reload
|
13
|
+
post.should be_translated(:en).for([:title, :content])
|
14
|
+
.as(['updated title', 'updated content'])
|
15
|
+
post.should be_translated(:de).for([:title, :content])
|
16
|
+
.as(['geänderter Titel', 'geänderter Inhalt'])
|
17
|
+
end
|
18
|
+
|
19
|
+
it "does not touch existing translations for other locales" do
|
20
|
+
post = Post.create(:title => 'title', :content => 'content', :locale => :en)
|
21
|
+
post.update_attributes(:title => 'Titel', :content => 'Inhalt', :locale => :de)
|
22
|
+
post.set_translations(:en => { :title => 'updated title', :content => 'updated content' })
|
23
|
+
post.reload
|
24
|
+
post.should be_translated(:en).for([:title, :content])
|
25
|
+
.as(['updated title', 'updated content'])
|
26
|
+
post.should be_translated(:de).for([:title, :content]).as(['Titel', 'Inhalt'])
|
27
|
+
end
|
28
|
+
|
29
|
+
it "does not touch existing translations for other attributes" do
|
30
|
+
post = Post.create(:title => 'title', :content => 'content', :locale => :en)
|
31
|
+
post.update_attributes(:title => 'Titel', :content => 'Inhalt', :locale => :de)
|
32
|
+
post.set_translations(
|
33
|
+
:en => { :title => "updated title" },
|
34
|
+
:de => { :content => "geänderter Inhalt" }
|
35
|
+
)
|
36
|
+
post.reload
|
37
|
+
post.should be_translated(:en).for([:title, :content]).as(['updated title', 'content'])
|
38
|
+
post.should be_translated(:de).for([:title, :content]).as(['Titel', 'geänderter Inhalt'])
|
39
|
+
end
|
40
|
+
|
41
|
+
# in G3 error raises here. But Mongo it support, so why not.. :)
|
42
|
+
it "sets unknown attributes" do
|
43
|
+
post = Post.create(:title => 'title', :content => 'content', :locale => :en)
|
44
|
+
post.update_attributes(:title => 'Titel', :content => 'Inhalt', :locale => :de)
|
45
|
+
post.set_translations :de => {:does_not_exist => 'not exist'}
|
46
|
+
post.reload
|
47
|
+
post.translation_for(:de).does_not_exist.should == 'not exist'
|
48
|
+
end
|
49
|
+
end
|