mongoid_globalize 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Translation class" do
4
+ it "returns by translation_class" do
5
+ Post.translation_class.should == Post::Translation
6
+ end
7
+
8
+ it "nested in the model class" do
9
+ Post.const_defined?(:Translation).should be_true
10
+ end
11
+
12
+ it "defines embeded_in relation" do
13
+ Post::Translation.should be_embedded_in(:post)
14
+ end
15
+
16
+ it "defines a reader for :locale that returns a symbol" do
17
+ post = Post::Translation.new
18
+ post.send(:write_attribute, 'locale', 'de')
19
+ post.locale.should == :de
20
+ end
21
+
22
+ it "defines a writer for :locale that writes a string" do
23
+ post = Post::Translation.new
24
+ post.locale = :de
25
+ post.read_attribute('locale').should == 'de'
26
+ end
27
+
28
+ it "creates for a namespaced model" do
29
+ lambda do
30
+ module Foo
31
+ module Bar
32
+ class Baz
33
+ include Mongoid::Document
34
+ include Mongoid::Globalize
35
+ translates :bumm
36
+ end
37
+ end
38
+ end
39
+ end.should_not raise_error
40
+ end
41
+
42
+ it "does not override existing translation class" do
43
+ PostTranslation.new.should respond_to(:existing_method)
44
+ end
45
+
46
+ describe "required_attributes" do
47
+ it "returns required attributes (i.e. validates_presence_of)" do
48
+ User.required_attributes.should == [:name, :email]
49
+ end
50
+ end
51
+
52
+ describe "required_translated_attributes" do
53
+ it "does not include non-translated attributes" do
54
+ User.required_translated_attributes.should == [:name]
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,93 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe "Validations" do
5
+ describe "update_attribute" do
6
+ it "succeeds with valid values" do
7
+ post = Post.create(:title => 'foo')
8
+ post.update_attributes(:title => 'baz')
9
+ post.should be_valid
10
+ Post.first.title.should == 'baz'
11
+ end
12
+
13
+ it "fails with invalid values" do
14
+ post = Post.create(:title => 'foo')
15
+ post.update_attributes(:title => '').should be_false
16
+ post.should_not be_valid
17
+ post.reload.attributes['title'].should_not be_nil
18
+ post.title.should == 'foo'
19
+ end
20
+ end
21
+
22
+ after :each do
23
+ Validatee.reset_callbacks(:validate)
24
+ end
25
+
26
+ describe "validates_presence_of" do
27
+ it "works" do
28
+ Validatee.class_eval{ validates_presence_of :string }
29
+ Validatee.new.should_not be_valid
30
+ Validatee.new(:string => 'foo').should be_valid
31
+ end
32
+ end
33
+
34
+ describe "validates_confirmation_of" do
35
+ it "works" do
36
+ Validatee.class_eval{ validates_confirmation_of :string }
37
+ Validatee.new(:string => 'foo', :string_confirmation => 'bar').should_not be_valid
38
+ Validatee.new(:string => 'foo', :string_confirmation => 'foo').should be_valid
39
+ end
40
+ end
41
+
42
+ describe "validates_acceptance_of" do
43
+ it "works" do
44
+ Validatee.class_eval{ validates_acceptance_of :string, :accept => '1' }
45
+ Validatee.new(:string => '0').should_not be_valid
46
+ Validatee.new(:string => '1').should be_valid
47
+ end
48
+ end
49
+
50
+ describe "validates_length_of (:is)" do
51
+ it "works" do
52
+ Validatee.class_eval{ validates_length_of :string, :is => 1 }
53
+ Validatee.new(:string => 'aa').should_not be_valid
54
+ Validatee.new(:string => 'a').should be_valid
55
+ end
56
+ end
57
+
58
+ describe "validates_format_of" do
59
+ it "works" do
60
+ Validatee.class_eval{ validates_format_of :string, :with => /^\d+$/ }
61
+ Validatee.new(:string => 'a').should_not be_valid
62
+ Validatee.new(:string => '1').should be_valid
63
+ end
64
+ end
65
+
66
+ describe "validates_inclusion_of" do
67
+ it "works" do
68
+ Validatee.class_eval{ validates_inclusion_of :string, :in => %(a) }
69
+ Validatee.new(:string => 'b').should_not be_valid
70
+ Validatee.new(:string => 'a').should be_valid
71
+ end
72
+ end
73
+
74
+ describe "validates_exclusion_of" do
75
+ it "works" do
76
+ Validatee.class_eval{ validates_exclusion_of :string, :in => %(b) }
77
+ Validatee.new(:string => 'b').should_not be_valid
78
+ Validatee.new(:string => 'a').should be_valid
79
+ end
80
+ end
81
+
82
+ describe "validates_numericality_of" do
83
+ it "works" do
84
+ Validatee.class_eval{ validates_numericality_of :string }
85
+ Validatee.new(:string => 'a').should_not be_valid
86
+ Validatee.new(:string => '1').should be_valid
87
+ end
88
+ end
89
+
90
+ pending "validates_uniqueness_of"
91
+ pending "validates_associated"
92
+ pending "a record with valid values on non-default locale validates"
93
+ end
@@ -0,0 +1,173 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe Mongoid::Globalize do
5
+ describe "a translated record" do
6
+ it "has many embed translations" do
7
+ Post.should embed_many(:translations)
8
+ end
9
+ end
10
+
11
+ describe "#translations" do
12
+ # It's different of G3. In G3 @translations are empty for this situation
13
+ its "have one empty with current locale for a new record" do
14
+ translations = Post.new.translations
15
+ translations.should == [translations.first]
16
+ translations.first.locale.should == :en
17
+ end
18
+ end
19
+
20
+ describe "#create" do
21
+ it "uses the given locale" do
22
+ post = Post.create(:title => 'Titel', :locale => :de)
23
+ post.should be_translated(:de).for(:title).as('Titel')
24
+ end
25
+ end
26
+
27
+ it "can translate boolean values" do
28
+ post = Post.create(:title => 'Titel', :published => true, :locale => :de)
29
+ post.should be_translated(:de).for(:published).as(true)
30
+ end
31
+
32
+ it "can translate datetime values" do
33
+ now = Time.now
34
+ post = Post.create(:title => 'Titel', :published_at => now, :locale => :de)
35
+ post.should be_translated(:de).for(:published_at).as(now)
36
+ end
37
+
38
+ describe "#attributes=" do
39
+ it "uses the given locale" do
40
+ post = Post.create(:title => 'title')
41
+ post.attributes = { :title => 'Titel', :locale => :de }
42
+ post.save
43
+ post.reload
44
+ post.translations.size.should == 2
45
+ post.should be_translated(:de).for(:title).as('Titel')
46
+ post.should be_translated(:en).for(:title).as('title')
47
+ end
48
+ end
49
+
50
+ describe "#create on associations" do
51
+ it "works" do
52
+ blog = Blog.create
53
+ blog.posts.create(:title => 'title')
54
+ blog.posts.create(:title => 'Titel', :locale => :de)
55
+ blog.posts.size.should == 2
56
+ blog.posts.first.should be_translated(:en).for(:title).as('title')
57
+ blog.posts.last.should be_translated(:de).for(:title).as('Titel')
58
+ end
59
+ end
60
+
61
+ describe "named scopes" do
62
+ its "work" do
63
+ post = Blog.create.posts.create(:title => 'some title')
64
+ post.reload
65
+ post.should be_translated(:en).for(:title).as('some title')
66
+ end
67
+ end
68
+
69
+ it "saves a translations document for each locale using a given locale" do
70
+ post = Post.create(:title => 'Titel', :locale => :de)
71
+ post.update_attributes(:title => 'title', :locale => :en)
72
+ post.translations.size.should == 2
73
+ post.should be_translated(:de).for(:title).as('Titel')
74
+ post.should be_translated(:en).for(:title).as('title')
75
+ end
76
+
77
+ it "saves a translations document for each locale using the current I18n locale" do
78
+ post = with_locale(:de) { Post.create(:title => 'Titel') }
79
+ with_locale(:en) { post.update_attributes(:title => 'title') }
80
+ post.translations.size.should == 2
81
+ post.should be_translated(:en).for(:title).as('title')
82
+ post.should be_translated(:de).for(:title).as('Titel')
83
+ end
84
+
85
+ describe "#reload" do
86
+ it "works with translated attributes" do
87
+ post = Post.create(:title => 'foo')
88
+ post.title = 'baz'
89
+ post.reload
90
+ post.title.should == 'foo'
91
+ end
92
+
93
+ it "accepts no options" do # because Mongoid's #reload doesn't accept ones
94
+ post = Post.create(:title => "title")
95
+ lambda{ post.reload(:readonly => true) }.should raise_error(ArgumentError)
96
+ end
97
+ end
98
+
99
+ describe "#destroy" do
100
+ it "destroys dependent translations" do
101
+ # it's true due to translations are embedded into document
102
+ end
103
+ end
104
+
105
+ describe "#to_xml" do
106
+ it "includes translated fields" do
107
+ post = Post.create(:title => "foo", :content => "bar")
108
+ post.reload
109
+ post.to_xml.should match(%r(<title>foo</title>))
110
+ post.to_xml.should match(%r(<content>bar</content>))
111
+ end
112
+
113
+ it " doesn't affect untranslated models" do
114
+ blog = Blog.create(:description => "my blog")
115
+ blog.reload
116
+ blog.to_xml.should match(%r(<description>my blog</description>))
117
+ end
118
+ end
119
+
120
+ describe "#translated_locales" do
121
+ it "returns locales that have translations" do
122
+ pending "TODO"
123
+ first = Post.create!(:title => 'title', :locale => :en)
124
+ first.update_attributes(:title => 'Title', :locale => :de)
125
+ second = Post.create!(:title => 'title', :locale => :en)
126
+ second.update_attributes(:title => 'titre', :locale => :fr)
127
+ Post.translated_locales.should == [:de, :en, :fr]
128
+ first.translated_locales.should == [:de, :en]
129
+ second.translated_locales.should == [:en, :fr]
130
+
131
+ first.reload
132
+ second.reload
133
+ first.translated_locales.should == [:de, :en]
134
+ second.translated_locales.should == [:en, :fr]
135
+ end
136
+ end
137
+
138
+ describe "a model with an after_save callback that reloads the model" do
139
+ it "still saves correctly" do
140
+ reloading_post = ReloadingPost.create!(:title => 'title')
141
+ reloading_post.title.should == 'title'
142
+ reloading_post.should be_translated(:en).for(:title).as('title')
143
+ end
144
+ end
145
+
146
+ describe "#with_translations" do
147
+ it "eager loads translations" do
148
+ pending "TODO"
149
+ Post.create(:title => 'title 1')
150
+ Post.create(:title => 'title 2')
151
+ Post.with_translations.first.translations.should be_loaded
152
+ Post.with_translations.map(&:title).should == ['title 1', 'title 2']
153
+ end
154
+ end
155
+
156
+ describe "a subclass of an untranslated model" do
157
+ it "can translate attributes" do
158
+ post = Post.create(:title => 'title')
159
+ translated_comment = TranslatedComment.create(:post => post, :content => 'content')
160
+ lambda{ translated_comment.translations }.should_not raise_error
161
+ translated_comment.should be_translated(:en).for(:content).as('content')
162
+ end
163
+
164
+ it "works when modifiying translated attributes" do
165
+ post = Post.create(:title => 'title')
166
+ translated_comment = TranslatedComment.create(:post => post, :content => 'content')
167
+ translated_comment.update_attributes(:content => 'Inhalt',
168
+ :locale => :de).should be_true
169
+ translated_comment.should be_translated(:en).for(:content).as('content')
170
+ translated_comment.should be_translated(:de).for(:content).as('Inhalt')
171
+ end
172
+ end
173
+ end
@@ -0,0 +1,77 @@
1
+ require 'rubygems'
2
+ require "bundler/setup"
3
+ require 'mongoid'
4
+
5
+ Mongoid.configure do |config|
6
+ name = "mongoid_globalize_test"
7
+ config.autocreate_indexes = true
8
+ config.master = Mongo::Connection.new.db(name)
9
+ config.logger = Logger.new($stdout, :warn)
10
+ end
11
+
12
+ require 'mongoid_globalize'
13
+ require File.expand_path('../data/models', __FILE__)
14
+
15
+ require 'rspec'
16
+ require 'database_cleaner'
17
+ require 'mongoid-rspec'
18
+ RSpec.configure do |config|
19
+ config.include Mongoid::Matchers
20
+ config.before :suite do
21
+ DatabaseCleaner.strategy = :truncation
22
+ end
23
+
24
+ config.before :each do
25
+ I18n.locale = :en
26
+ end
27
+
28
+ config.after :each do
29
+ DatabaseCleaner.clean
30
+ # because rspec run all specs in one thread
31
+ Thread.current[:globalize_locale] = nil
32
+ end
33
+ end
34
+
35
+ def with_locale(*args, &block)
36
+ Mongoid::Globalize.with_locale(*args, &block)
37
+ end
38
+
39
+ RSpec::Matchers.define :be_translated do |locale|
40
+ chain :for do |attributes|
41
+ @attributes = Array.wrap(attributes)
42
+ end
43
+
44
+ chain :as do |translations|
45
+ @translations = Array.wrap(translations)
46
+ end
47
+
48
+ match do |record|
49
+ @result = @attributes.map{|name| record.send(name, locale)}
50
+ @result == @translations
51
+ end
52
+
53
+ failure_message_for_should do |record|
54
+ "expected that attributes #{@attributes.inspect} for #{record} in " +
55
+ "#{locale.inspect} locale should be #{@translations.inspect}.\n" +
56
+ " Diff:\n -#{@translations.inspect}\n +#{@result.inspect}"
57
+ end
58
+ end
59
+
60
+ class BackendWithFallbacks < I18n::Backend::Simple
61
+ include I18n::Backend::Fallbacks
62
+ end
63
+
64
+ meta = class << I18n; self; end
65
+ meta.class_eval do
66
+ alias_method(:alternatives, :fallbacks)
67
+
68
+ def pretend_fallbacks
69
+ class << I18n; self; end.send(:alias_method, :fallbacks, :alternatives)
70
+ end
71
+
72
+ def hide_fallbacks
73
+ class << I18n; self; end.send(:remove_method, :fallbacks)
74
+ end
75
+ end
76
+
77
+ I18n.hide_fallbacks
metadata ADDED
@@ -0,0 +1,170 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid_globalize
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Mik-die
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-07-25 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activemodel
17
+ requirement: &id001 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - "="
21
+ - !ruby/object:Gem::Version
22
+ version: 3.1.0.rc4
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: mongoid
28
+ requirement: &id002 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: *id002
37
+ - !ruby/object:Gem::Dependency
38
+ name: bson_ext
39
+ requirement: &id003 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ type: :runtime
46
+ prerelease: false
47
+ version_requirements: *id003
48
+ - !ruby/object:Gem::Dependency
49
+ name: rspec
50
+ requirement: &id004 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *id004
59
+ - !ruby/object:Gem::Dependency
60
+ name: mongoid-rspec
61
+ requirement: &id005 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ type: :development
68
+ prerelease: false
69
+ version_requirements: *id005
70
+ - !ruby/object:Gem::Dependency
71
+ name: database_cleaner
72
+ requirement: &id006 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: "0"
78
+ type: :development
79
+ prerelease: false
80
+ version_requirements: *id006
81
+ - !ruby/object:Gem::Dependency
82
+ name: ruby-debug19
83
+ requirement: &id007 !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: "0"
89
+ type: :development
90
+ prerelease: false
91
+ version_requirements: *id007
92
+ - !ruby/object:Gem::Dependency
93
+ name: jeweler
94
+ requirement: &id008 !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: "0"
100
+ type: :development
101
+ prerelease: false
102
+ version_requirements: *id008
103
+ description: Library for translating Mongoid documents, based on Globalize3 principles
104
+ email: MikDiet@gmail.com
105
+ executables: []
106
+
107
+ extensions: []
108
+
109
+ extra_rdoc_files:
110
+ - README.rdoc
111
+ files:
112
+ - .rspec
113
+ - .rvmrc
114
+ - Gemfile
115
+ - Gemfile.lock
116
+ - MIT-LICENSE
117
+ - README.rdoc
118
+ - Rakefile
119
+ - VERSION
120
+ - lib/mongoid_globalize.rb
121
+ - lib/mongoid_globalize/act_macro.rb
122
+ - lib/mongoid_globalize/adapter.rb
123
+ - lib/mongoid_globalize/attributes.rb
124
+ - lib/mongoid_globalize/class_methods.rb
125
+ - lib/mongoid_globalize/document_translation.rb
126
+ - lib/mongoid_globalize/instance_methods.rb
127
+ - mongoid_globalize.gemspec
128
+ - spec/data/models.rb
129
+ - spec/mongoid_globalize/attributes_spec.rb
130
+ - spec/mongoid_globalize/clone_spec.rb
131
+ - spec/mongoid_globalize/dirty_tracking_spec.rb
132
+ - spec/mongoid_globalize/fallbacks_spec.rb
133
+ - spec/mongoid_globalize/locale_spec.rb
134
+ - spec/mongoid_globalize/set_translations_spec.rb
135
+ - spec/mongoid_globalize/translation_class_spec.rb
136
+ - spec/mongoid_globalize/validations_spec.rb
137
+ - spec/mongoid_globalize_spec.rb
138
+ - spec/spec_helper.rb
139
+ homepage: http://github.com/Mik-die/mongoid_globalize
140
+ licenses:
141
+ - MIT
142
+ post_install_message:
143
+ rdoc_options: []
144
+
145
+ require_paths:
146
+ - lib
147
+ required_ruby_version: !ruby/object:Gem::Requirement
148
+ none: false
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ hash: -528582867
153
+ segments:
154
+ - 0
155
+ version: "0"
156
+ required_rubygems_version: !ruby/object:Gem::Requirement
157
+ none: false
158
+ requirements:
159
+ - - ">="
160
+ - !ruby/object:Gem::Version
161
+ version: "0"
162
+ requirements: []
163
+
164
+ rubyforge_project:
165
+ rubygems_version: 1.8.5
166
+ signing_key:
167
+ specification_version: 3
168
+ summary: Library for translating Mongoid documents
169
+ test_files: []
170
+