mongoid_globalize 0.1.0 → 0.1.1
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/Gemfile +0 -1
- data/Gemfile.lock +7 -12
- data/README.textile +122 -0
- data/VERSION +1 -1
- data/lib/mongoid_globalize/act_macro.rb +7 -18
- data/lib/mongoid_globalize/adapter.rb +1 -11
- data/lib/mongoid_globalize/class_methods.rb +26 -21
- data/lib/mongoid_globalize/document_translation.rb +3 -2
- data/lib/mongoid_globalize/fields_builder.rb +17 -0
- data/lib/mongoid_globalize/instance_methods.rb +1 -24
- data/lib/mongoid_globalize.rb +3 -11
- data/mongoid_globalize.gemspec +5 -7
- data/spec/data/models.rb +22 -12
- data/spec/mongoid_globalize/translation_class_spec.rb +1 -1
- data/spec/mongoid_globalize_spec.rb +14 -6
- metadata +16 -26
- data/README.rdoc +0 -0
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -10,23 +10,20 @@ GIT
|
|
10
10
|
GEM
|
11
11
|
remote: http://rubygems.org/
|
12
12
|
specs:
|
13
|
-
activemodel (3.
|
14
|
-
activesupport (= 3.
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
activesupport (3.1.0.rc4)
|
19
|
-
multi_json (~> 1.0)
|
13
|
+
activemodel (3.0.9)
|
14
|
+
activesupport (= 3.0.9)
|
15
|
+
builder (~> 2.1.2)
|
16
|
+
i18n (~> 0.5.0)
|
17
|
+
activesupport (3.0.9)
|
20
18
|
archive-tar-minitar (0.5.2)
|
21
|
-
bcrypt-ruby (2.1.4)
|
22
19
|
bson (1.3.1)
|
23
20
|
bson_ext (1.3.1)
|
24
|
-
builder (
|
21
|
+
builder (2.1.2)
|
25
22
|
columnize (0.3.4)
|
26
23
|
database_cleaner (0.6.7)
|
27
24
|
diff-lcs (1.1.2)
|
28
25
|
git (1.2.5)
|
29
|
-
i18n (0.
|
26
|
+
i18n (0.5.0)
|
30
27
|
jeweler (1.6.4)
|
31
28
|
bundler (~> 1.0)
|
32
29
|
git (>= 1.2.5)
|
@@ -38,7 +35,6 @@ GEM
|
|
38
35
|
mongoid-rspec (1.4.4)
|
39
36
|
mongoid (~> 2.0)
|
40
37
|
rspec (~> 2)
|
41
|
-
multi_json (1.0.3)
|
42
38
|
rake (0.9.2)
|
43
39
|
rspec (2.6.0)
|
44
40
|
rspec-core (~> 2.6.0)
|
@@ -64,7 +60,6 @@ PLATFORMS
|
|
64
60
|
ruby
|
65
61
|
|
66
62
|
DEPENDENCIES
|
67
|
-
activemodel (= 3.1.0.rc4)
|
68
63
|
bson_ext
|
69
64
|
database_cleaner
|
70
65
|
jeweler
|
data/README.textile
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
h1. Mongoid::Globalize
|
2
|
+
|
3
|
+
Mongoid::Globalize is based on Globalize3, but targeted at Mongoid. As Globalize3, it 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 Mongoid::Document.
|
4
|
+
|
5
|
+
Mongoid::Globalize has to work with other frameworks, that supports Mongoid (Sinatra, Padrino, and others).
|
6
|
+
|
7
|
+
h2. Requirements
|
8
|
+
|
9
|
+
Mongoid > 2.0.2
|
10
|
+
|
11
|
+
Yes, at this moment Mongoid::Globalize doesn't support any published versions of Mongoid gem, so you need to clone master branch of this gem. I tested with "revision: e2540c818ebb1d37b3a8d1b7e6dd6836ce16d445":https://github.com/mongoid/mongoid/tree/e2540c818ebb1d37b3a8d1b7e6dd6836ce16d445, future revisions break some specs, so I wait for stable mongoid-2.1.0.
|
12
|
+
|
13
|
+
I also tested against ActiveModel-3.1.0.rc4, so Mongoid::Globalize has to work with Rails 3.1.
|
14
|
+
|
15
|
+
h2. Installation
|
16
|
+
|
17
|
+
To install Mongoid::Globalize just use:
|
18
|
+
|
19
|
+
<pre><code>
|
20
|
+
$ gem install mongoid_globalize
|
21
|
+
</code></pre>
|
22
|
+
|
23
|
+
or, with bundler, in your Gemfile
|
24
|
+
|
25
|
+
<pre><code>
|
26
|
+
gem 'mongoid_globalize'
|
27
|
+
</code></pre>
|
28
|
+
|
29
|
+
h2. Document translations
|
30
|
+
|
31
|
+
Document translations allow you to translate your document' field values. In order to make this work, you'll need to add +include Mongoid::Globalize+ into your document class and define translatable fields in block of +translates+ method, as you define usual fields. E.g.
|
32
|
+
|
33
|
+
<pre><code>
|
34
|
+
class Post
|
35
|
+
include Mongoid::Document
|
36
|
+
include Mongoid::Globalize
|
37
|
+
translates do
|
38
|
+
field :title
|
39
|
+
field :content
|
40
|
+
field :published, type: Boolean
|
41
|
+
end
|
42
|
+
end
|
43
|
+
</code></pre>
|
44
|
+
|
45
|
+
This allows you to translate the fields :title, :text and :published per locale:
|
46
|
+
|
47
|
+
<pre><code>
|
48
|
+
I18n.locale = :en
|
49
|
+
post.title # => Globalize rocks!
|
50
|
+
|
51
|
+
I18n.locale = :ru
|
52
|
+
post.title # => Глобалайз рулит!
|
53
|
+
</code></pre>
|
54
|
+
|
55
|
+
h2. I18n fallbacks for empty translations
|
56
|
+
|
57
|
+
It is possible to enable fallbacks for empty translations. It will depend on the configuration setting you have set for I18n translations in your config.
|
58
|
+
|
59
|
+
In Rails applications, for example, you can enable them by adding the next line to @config/application.rb@ (or only @config/environments/production.rb@ if you only want them in production)
|
60
|
+
|
61
|
+
<pre><code>config.i18n.fallbacks = true</code></pre>
|
62
|
+
|
63
|
+
By default, Mongoid::Globalize will only use fallbacks when your translation model does not exist or the translation value for the item you've requested is @nil@. However it is possible to also use fallbacks for @blank@ translations by adding @fallbacks_for_empty_translations!@ to the @translates@ block.
|
64
|
+
|
65
|
+
<pre><code>
|
66
|
+
class Post
|
67
|
+
include Mongoid::Document
|
68
|
+
include Mongoid::Globalize
|
69
|
+
translates do
|
70
|
+
field :title
|
71
|
+
field :content
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
puts post.translations.inspect
|
76
|
+
# => [#<Post::Translation id: 1, post_id: 1, locale: "en", title: "Globalize rocks!", name: "Globalize">, #<Post::Translation id: 2, post_id: 1, locale: "nl", title: '', name: nil>]
|
77
|
+
|
78
|
+
I18n.locale = :en
|
79
|
+
post.title # => 'Globalize rocks!'
|
80
|
+
post.name # => 'Globalize'
|
81
|
+
|
82
|
+
I18n.locale = :nl
|
83
|
+
post.title # => ''
|
84
|
+
post.name # => 'Globalize'
|
85
|
+
</code></pre>
|
86
|
+
<pre><code>
|
87
|
+
class Post
|
88
|
+
include Mongoid::Document
|
89
|
+
include Mongoid::Globalize
|
90
|
+
translates do
|
91
|
+
field :title
|
92
|
+
field :content
|
93
|
+
fallbacks_for_empty_translations!
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
puts post.translations.inspect
|
98
|
+
# => [#<Post::Translation id: 1, post_id: 1, locale: "en", title: "Globalize rocks!", name: "Globalize">, #<Post::Translation id: 2, post_id: 1, locale: "nl", title: '', name: nil>]
|
99
|
+
|
100
|
+
I18n.locale = :en
|
101
|
+
post.title # => 'Globalize rocks!'
|
102
|
+
post.name # => 'Globalize'
|
103
|
+
|
104
|
+
I18n.locale = :nl
|
105
|
+
post.title # => 'Globalize rocks!'
|
106
|
+
post.name # => 'Globalize'
|
107
|
+
</code></pre>
|
108
|
+
|
109
|
+
|
110
|
+
h2. Scoping objects by those with translations
|
111
|
+
|
112
|
+
To only return objects that have a translation for the given locale we can use the `with_translations` scope. This will only return records that have a translations for the passed in locale.
|
113
|
+
|
114
|
+
<pre><code>
|
115
|
+
|
116
|
+
Post.with_translations('en') # => [#<Post::Translation id: 1, post_id: 1, locale: "en", title: "Globalize rocks!", name: "Globalize">, #<Post::Translation id: 2, post_id: 1, locale: "nl", title: '', name: nil>]
|
117
|
+
|
118
|
+
Post.with_translations(I18n.locale) # => [#<Post::Translation id: 1, post_id: 1, locale: "en", title: "Globalize rocks!", name: "Globalize">, #<Post::Translation id: 2, post_id: 1, locale: "nl", title: '', name: nil>]
|
119
|
+
|
120
|
+
Post.with_translations('de') # => []
|
121
|
+
|
122
|
+
</code></pre>
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.1
|
@@ -1,23 +1,12 @@
|
|
1
1
|
module Mongoid::Globalize
|
2
2
|
module ActMacro
|
3
|
-
#
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
hash.merge(attr.is_a?(Hash) ? attr : {attr => String})
|
11
|
-
end
|
12
|
-
attr_hash.each do |name, type|
|
13
|
-
self.translated_attribute_names.push name.to_sym
|
14
|
-
translated_attr_accessor(name)
|
15
|
-
translation_class.field name, type: type
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
def fallbacks_for_empty_translations!
|
20
|
-
self.fallbacks_for_empty_translations = true
|
3
|
+
# translates do
|
4
|
+
# field :title
|
5
|
+
# field :visible, type: Boolean
|
6
|
+
# end
|
7
|
+
def translates(&block)
|
8
|
+
builder = FieldsBuilder.new(self)
|
9
|
+
builder.instance_exec(&block)
|
21
10
|
end
|
22
11
|
end
|
23
12
|
end
|
@@ -29,22 +29,12 @@ module Mongoid::Globalize
|
|
29
29
|
stash.write(locale, name, value)
|
30
30
|
end
|
31
31
|
|
32
|
-
def save_translations!
|
33
|
-
stash.each do |locale, attrs|
|
34
|
-
translation = record.translations.find_or_initialize_by_locale(locale.to_s)
|
35
|
-
attrs.each{ |name, value| translation[name] = value }
|
36
|
-
translation.save!
|
37
|
-
end
|
38
|
-
# TODO: it's actual for mongoid?
|
39
|
-
#record.translations.reset
|
40
|
-
reset
|
41
|
-
end
|
42
|
-
|
43
32
|
def prepare_translations!
|
44
33
|
stash.each do |locale, attrs|
|
45
34
|
translation = record.translations.find_or_initialize_by_locale(locale.to_s)
|
46
35
|
attrs.each{ |name, value| translation[name] = value }
|
47
36
|
end
|
37
|
+
reset
|
48
38
|
end
|
49
39
|
|
50
40
|
def reset
|
@@ -1,29 +1,34 @@
|
|
1
1
|
module Mongoid::Globalize
|
2
2
|
module ClassMethods
|
3
|
-
|
3
|
+
def translated_locales
|
4
|
+
all.distinct("translations.locale").sort.map &:to_sym
|
5
|
+
end
|
4
6
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
7
|
+
def with_locales(*locales)
|
8
|
+
where(translated_field_name(:locale).in => locales.flatten)
|
9
|
+
end
|
10
|
+
|
11
|
+
def with_translations(*locales)
|
12
|
+
locales = translated_locales if locales.empty?
|
13
|
+
with_locales(locales).with_required_attributes
|
14
|
+
end
|
15
|
+
|
16
|
+
def with_required_attributes
|
17
|
+
required_translated_attributes.inject(self) do |scope, name|
|
18
|
+
scope.where(translated_field_name(name).exists => true)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def translated_field_name(name)
|
23
|
+
"translations.#{name}".to_sym
|
24
|
+
end
|
25
|
+
|
26
|
+
# TODO:
|
22
27
|
#def with_translated_attribute(name, value, locales = nil)
|
23
28
|
# locales ||= Globalize.fallbacks
|
24
29
|
# with_translations.where(
|
25
|
-
#
|
26
|
-
#
|
30
|
+
# translated_field_name(name) => value,
|
31
|
+
# translated_field_name(:locale) => Array(locales).map(&:to_s)
|
27
32
|
# )
|
28
33
|
#end
|
29
34
|
|
@@ -46,11 +51,11 @@ module Mongoid::Globalize
|
|
46
51
|
klass = self.const_set(:Translation, Class.new(Mongoid::Globalize::DocumentTranslation))
|
47
52
|
end
|
48
53
|
klass.embedded_in name.underscore.gsub('/', '_')
|
54
|
+
klass.translated_klass = self
|
49
55
|
klass
|
50
56
|
end
|
51
57
|
end
|
52
58
|
|
53
|
-
protected
|
54
59
|
def translated_attr_accessor(name)
|
55
60
|
define_method(:"#{name}=") do |value|
|
56
61
|
write_attribute(name, value)
|
@@ -3,6 +3,8 @@ module Mongoid::Globalize
|
|
3
3
|
include Mongoid::Document
|
4
4
|
field :locale
|
5
5
|
class << self
|
6
|
+
attr_accessor :translated_klass
|
7
|
+
|
6
8
|
def with_locales(*locales)
|
7
9
|
locales = locales.flatten.map(&:to_s)
|
8
10
|
where(:locale.in => locales)
|
@@ -10,8 +12,7 @@ module Mongoid::Globalize
|
|
10
12
|
alias with_locale with_locales
|
11
13
|
|
12
14
|
def translated_locales
|
13
|
-
|
14
|
-
[]
|
15
|
+
all.distinct("locale").sort.map &:to_sym
|
15
16
|
end
|
16
17
|
|
17
18
|
def find_or_initialize_by_locale(locale)
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Mongoid::Globalize
|
2
|
+
class FieldsBuilder
|
3
|
+
def initialize(model)
|
4
|
+
@model = model
|
5
|
+
end
|
6
|
+
|
7
|
+
def field(name, *params)
|
8
|
+
@model.translated_attribute_names.push name.to_sym
|
9
|
+
@model.translated_attr_accessor(name)
|
10
|
+
@model.translation_class.field name, *params
|
11
|
+
end
|
12
|
+
|
13
|
+
def fallbacks_for_empty_translations!
|
14
|
+
@model.fallbacks_for_empty_translations = true
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -19,19 +19,6 @@ module Mongoid::Globalize
|
|
19
19
|
with_given_locale(attributes) { super }
|
20
20
|
end
|
21
21
|
|
22
|
-
# All of this replaced by #process.. may be :)
|
23
|
-
#def write_attributes(attributes, *args)
|
24
|
-
# with_given_locale(attributes) { super }
|
25
|
-
#end
|
26
|
-
|
27
|
-
#def update_attributes!(attributes, *args)
|
28
|
-
# with_given_locale(attributes) { super }
|
29
|
-
#end
|
30
|
-
#
|
31
|
-
#def update_attributes(attributes, *args)
|
32
|
-
# with_given_locale(attributes) { super }
|
33
|
-
#end
|
34
|
-
|
35
22
|
def write_attribute(name, value, options = {})
|
36
23
|
if translated?(name)
|
37
24
|
options = {:locale => nil}.merge(options)
|
@@ -138,23 +125,13 @@ module Mongoid::Globalize
|
|
138
125
|
locales
|
139
126
|
end
|
140
127
|
|
141
|
-
def
|
128
|
+
def prepare_translations!
|
142
129
|
@stop_merging_translated_attributes = true
|
143
130
|
translated_attribute_names.each do |name|
|
144
131
|
@attributes.delete name.to_s
|
145
132
|
@changed_attributes.delete name.to_s
|
146
133
|
end
|
147
|
-
end
|
148
|
-
|
149
|
-
def create_translations!
|
150
|
-
globalize.save_translations!
|
151
|
-
clear_translations!
|
152
|
-
end
|
153
|
-
|
154
|
-
def update_translations!
|
155
|
-
unmerge_translations!
|
156
134
|
globalize.prepare_translations!
|
157
|
-
globalize.reset
|
158
135
|
end
|
159
136
|
|
160
137
|
def clear_translations!
|
data/lib/mongoid_globalize.rb
CHANGED
@@ -3,6 +3,7 @@ require 'mongoid_globalize/act_macro'
|
|
3
3
|
require 'mongoid_globalize/attributes'
|
4
4
|
require 'mongoid_globalize/class_methods'
|
5
5
|
require 'mongoid_globalize/document_translation'
|
6
|
+
require 'mongoid_globalize/fields_builder'
|
6
7
|
require 'mongoid_globalize/instance_methods'
|
7
8
|
|
8
9
|
module Mongoid::Globalize
|
@@ -11,11 +12,8 @@ module Mongoid::Globalize
|
|
11
12
|
class_attribute :translated_attribute_names, :fallbacks_for_empty_translations
|
12
13
|
self.translated_attribute_names = []
|
13
14
|
embeds_many :translations, :class_name => translation_class.name
|
14
|
-
|
15
|
-
|
16
|
-
after_create :create_translations!
|
17
|
-
before_update :update_translations!
|
18
|
-
after_create :clear_translations!
|
15
|
+
before_save :prepare_translations!
|
16
|
+
after_save :clear_translations!
|
19
17
|
|
20
18
|
extend Mongoid::Globalize::ActMacro
|
21
19
|
end
|
@@ -60,10 +58,4 @@ module Mongoid::Globalize
|
|
60
58
|
Thread.current[:globalize_locale] = locale.to_sym rescue nil
|
61
59
|
end
|
62
60
|
end
|
63
|
-
#
|
64
|
-
#module HasManyExtensions
|
65
|
-
# def find_or_initialize_by_locale(locale)
|
66
|
-
# with_locale(locale.to_s).first || build(:locale => locale.to_s)
|
67
|
-
# end
|
68
|
-
#end
|
69
61
|
end
|
data/mongoid_globalize.gemspec
CHANGED
@@ -5,15 +5,15 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{mongoid_globalize}
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = [%q{Mik-die}]
|
12
|
-
s.date = %q{2011-07-
|
12
|
+
s.date = %q{2011-07-26}
|
13
13
|
s.description = %q{Library for translating Mongoid documents, based on Globalize3 principles}
|
14
14
|
s.email = %q{MikDiet@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
16
|
-
"README.
|
16
|
+
"README.textile"
|
17
17
|
]
|
18
18
|
s.files = [
|
19
19
|
".rspec",
|
@@ -21,7 +21,7 @@ Gem::Specification.new do |s|
|
|
21
21
|
"Gemfile",
|
22
22
|
"Gemfile.lock",
|
23
23
|
"MIT-LICENSE",
|
24
|
-
"README.
|
24
|
+
"README.textile",
|
25
25
|
"Rakefile",
|
26
26
|
"VERSION",
|
27
27
|
"lib/mongoid_globalize.rb",
|
@@ -30,6 +30,7 @@ Gem::Specification.new do |s|
|
|
30
30
|
"lib/mongoid_globalize/attributes.rb",
|
31
31
|
"lib/mongoid_globalize/class_methods.rb",
|
32
32
|
"lib/mongoid_globalize/document_translation.rb",
|
33
|
+
"lib/mongoid_globalize/fields_builder.rb",
|
33
34
|
"lib/mongoid_globalize/instance_methods.rb",
|
34
35
|
"mongoid_globalize.gemspec",
|
35
36
|
"spec/data/models.rb",
|
@@ -54,7 +55,6 @@ Gem::Specification.new do |s|
|
|
54
55
|
s.specification_version = 3
|
55
56
|
|
56
57
|
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
58
|
s.add_runtime_dependency(%q<mongoid>, [">= 0"])
|
59
59
|
s.add_runtime_dependency(%q<bson_ext>, [">= 0"])
|
60
60
|
s.add_development_dependency(%q<rspec>, [">= 0"])
|
@@ -63,7 +63,6 @@ Gem::Specification.new do |s|
|
|
63
63
|
s.add_development_dependency(%q<ruby-debug19>, [">= 0"])
|
64
64
|
s.add_development_dependency(%q<jeweler>, [">= 0"])
|
65
65
|
else
|
66
|
-
s.add_dependency(%q<activemodel>, ["= 3.1.0.rc4"])
|
67
66
|
s.add_dependency(%q<mongoid>, [">= 0"])
|
68
67
|
s.add_dependency(%q<bson_ext>, [">= 0"])
|
69
68
|
s.add_dependency(%q<rspec>, [">= 0"])
|
@@ -73,7 +72,6 @@ Gem::Specification.new do |s|
|
|
73
72
|
s.add_dependency(%q<jeweler>, [">= 0"])
|
74
73
|
end
|
75
74
|
else
|
76
|
-
s.add_dependency(%q<activemodel>, ["= 3.1.0.rc4"])
|
77
75
|
s.add_dependency(%q<mongoid>, [">= 0"])
|
78
76
|
s.add_dependency(%q<bson_ext>, [">= 0"])
|
79
77
|
s.add_dependency(%q<rspec>, [">= 0"])
|
data/spec/data/models.rb
CHANGED
@@ -2,10 +2,12 @@ class Post
|
|
2
2
|
include Mongoid::Document
|
3
3
|
include Mongoid::Globalize
|
4
4
|
belongs_to :blog
|
5
|
-
translates
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
translates do
|
6
|
+
field :title
|
7
|
+
field :content
|
8
|
+
field :published, type: Boolean
|
9
|
+
field :published_at, type: DateTime
|
10
|
+
end
|
9
11
|
validates_presence_of :title
|
10
12
|
scope :with_some_title, :conditions => { :title => 'some_title' }
|
11
13
|
end
|
@@ -15,8 +17,8 @@ class PostTranslation
|
|
15
17
|
field :locale
|
16
18
|
field :title
|
17
19
|
field :content
|
18
|
-
field :published
|
19
|
-
field :published_at
|
20
|
+
field :published, type: Boolean
|
21
|
+
field :published_at, type: DateTime
|
20
22
|
embedded_in :post
|
21
23
|
|
22
24
|
def existing_method
|
@@ -36,13 +38,16 @@ end
|
|
36
38
|
class Validatee
|
37
39
|
include Mongoid::Document
|
38
40
|
include Mongoid::Globalize
|
39
|
-
translates :string
|
41
|
+
translates{ field :string }
|
40
42
|
end
|
41
43
|
|
42
44
|
class Parent
|
43
45
|
include Mongoid::Document
|
44
46
|
include Mongoid::Globalize
|
45
|
-
translates
|
47
|
+
translates do
|
48
|
+
field :content
|
49
|
+
field :type
|
50
|
+
end
|
46
51
|
end
|
47
52
|
|
48
53
|
class Child < Parent
|
@@ -57,14 +62,17 @@ end
|
|
57
62
|
|
58
63
|
class TranslatedComment < Comment
|
59
64
|
include Mongoid::Globalize
|
60
|
-
translates
|
65
|
+
translates do
|
66
|
+
field :content
|
67
|
+
field :title
|
68
|
+
end
|
61
69
|
end
|
62
70
|
|
63
71
|
class User
|
64
72
|
include Mongoid::Document
|
65
73
|
include Mongoid::Timestamps
|
66
74
|
include Mongoid::Globalize
|
67
|
-
translates :name
|
75
|
+
translates{ field :name }
|
68
76
|
field :email
|
69
77
|
validates_presence_of :name, :email
|
70
78
|
end
|
@@ -72,6 +80,8 @@ end
|
|
72
80
|
class Task
|
73
81
|
include Mongoid::Document
|
74
82
|
include Mongoid::Globalize
|
75
|
-
|
76
|
-
|
83
|
+
translates do
|
84
|
+
fallbacks_for_empty_translations!
|
85
|
+
field :name
|
86
|
+
end
|
77
87
|
end
|
@@ -119,7 +119,6 @@ describe Mongoid::Globalize do
|
|
119
119
|
|
120
120
|
describe "#translated_locales" do
|
121
121
|
it "returns locales that have translations" do
|
122
|
-
pending "TODO"
|
123
122
|
first = Post.create!(:title => 'title', :locale => :en)
|
124
123
|
first.update_attributes(:title => 'Title', :locale => :de)
|
125
124
|
second = Post.create!(:title => 'title', :locale => :en)
|
@@ -144,12 +143,21 @@ describe Mongoid::Globalize do
|
|
144
143
|
end
|
145
144
|
|
146
145
|
describe "#with_translations" do
|
147
|
-
it "
|
148
|
-
pending "TODO"
|
146
|
+
it "loads only documents with translations" do
|
149
147
|
Post.create(:title => 'title 1')
|
150
|
-
Post.create(:title => 'title 2')
|
151
|
-
Post.with_translations.
|
152
|
-
Post.with_translations
|
148
|
+
Post.create(:title => 'title 2')
|
149
|
+
Post.with_translations.map(&:title).sort.should == ['title 1', 'title 2']
|
150
|
+
Post.with_translations(:de).should == []
|
151
|
+
end
|
152
|
+
|
153
|
+
it "doesn't load document with present locale, but absent required attributes" do
|
154
|
+
post = Post.create(:title => 'title 1')
|
155
|
+
post.set_translations(
|
156
|
+
:en => { :title => 'updated title' },
|
157
|
+
:ru => { :content => 'без заголовка' }
|
158
|
+
)
|
159
|
+
Post.with_translations(:en).first.should == post
|
160
|
+
Post.with_translations(:de).should == []
|
153
161
|
end
|
154
162
|
end
|
155
163
|
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: mongoid_globalize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.1.
|
5
|
+
version: 0.1.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Mik-die
|
@@ -10,21 +10,21 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-07-
|
13
|
+
date: 2011-07-26 00:00:00 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
|
-
name:
|
16
|
+
name: mongoid
|
17
17
|
requirement: &id001 !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
|
-
- - "
|
20
|
+
- - ">="
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version:
|
22
|
+
version: "0"
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: *id001
|
26
26
|
- !ruby/object:Gem::Dependency
|
27
|
-
name:
|
27
|
+
name: bson_ext
|
28
28
|
requirement: &id002 !ruby/object:Gem::Requirement
|
29
29
|
none: false
|
30
30
|
requirements:
|
@@ -35,18 +35,18 @@ dependencies:
|
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: *id002
|
37
37
|
- !ruby/object:Gem::Dependency
|
38
|
-
name:
|
38
|
+
name: rspec
|
39
39
|
requirement: &id003 !ruby/object:Gem::Requirement
|
40
40
|
none: false
|
41
41
|
requirements:
|
42
42
|
- - ">="
|
43
43
|
- !ruby/object:Gem::Version
|
44
44
|
version: "0"
|
45
|
-
type: :
|
45
|
+
type: :development
|
46
46
|
prerelease: false
|
47
47
|
version_requirements: *id003
|
48
48
|
- !ruby/object:Gem::Dependency
|
49
|
-
name: rspec
|
49
|
+
name: mongoid-rspec
|
50
50
|
requirement: &id004 !ruby/object:Gem::Requirement
|
51
51
|
none: false
|
52
52
|
requirements:
|
@@ -57,7 +57,7 @@ dependencies:
|
|
57
57
|
prerelease: false
|
58
58
|
version_requirements: *id004
|
59
59
|
- !ruby/object:Gem::Dependency
|
60
|
-
name:
|
60
|
+
name: database_cleaner
|
61
61
|
requirement: &id005 !ruby/object:Gem::Requirement
|
62
62
|
none: false
|
63
63
|
requirements:
|
@@ -68,7 +68,7 @@ dependencies:
|
|
68
68
|
prerelease: false
|
69
69
|
version_requirements: *id005
|
70
70
|
- !ruby/object:Gem::Dependency
|
71
|
-
name:
|
71
|
+
name: ruby-debug19
|
72
72
|
requirement: &id006 !ruby/object:Gem::Requirement
|
73
73
|
none: false
|
74
74
|
requirements:
|
@@ -79,7 +79,7 @@ dependencies:
|
|
79
79
|
prerelease: false
|
80
80
|
version_requirements: *id006
|
81
81
|
- !ruby/object:Gem::Dependency
|
82
|
-
name:
|
82
|
+
name: jeweler
|
83
83
|
requirement: &id007 !ruby/object:Gem::Requirement
|
84
84
|
none: false
|
85
85
|
requirements:
|
@@ -89,17 +89,6 @@ dependencies:
|
|
89
89
|
type: :development
|
90
90
|
prerelease: false
|
91
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
92
|
description: Library for translating Mongoid documents, based on Globalize3 principles
|
104
93
|
email: MikDiet@gmail.com
|
105
94
|
executables: []
|
@@ -107,14 +96,14 @@ executables: []
|
|
107
96
|
extensions: []
|
108
97
|
|
109
98
|
extra_rdoc_files:
|
110
|
-
- README.
|
99
|
+
- README.textile
|
111
100
|
files:
|
112
101
|
- .rspec
|
113
102
|
- .rvmrc
|
114
103
|
- Gemfile
|
115
104
|
- Gemfile.lock
|
116
105
|
- MIT-LICENSE
|
117
|
-
- README.
|
106
|
+
- README.textile
|
118
107
|
- Rakefile
|
119
108
|
- VERSION
|
120
109
|
- lib/mongoid_globalize.rb
|
@@ -123,6 +112,7 @@ files:
|
|
123
112
|
- lib/mongoid_globalize/attributes.rb
|
124
113
|
- lib/mongoid_globalize/class_methods.rb
|
125
114
|
- lib/mongoid_globalize/document_translation.rb
|
115
|
+
- lib/mongoid_globalize/fields_builder.rb
|
126
116
|
- lib/mongoid_globalize/instance_methods.rb
|
127
117
|
- mongoid_globalize.gemspec
|
128
118
|
- spec/data/models.rb
|
@@ -149,7 +139,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
149
139
|
requirements:
|
150
140
|
- - ">="
|
151
141
|
- !ruby/object:Gem::Version
|
152
|
-
hash: -
|
142
|
+
hash: -1007506339
|
153
143
|
segments:
|
154
144
|
- 0
|
155
145
|
version: "0"
|
data/README.rdoc
DELETED
File without changes
|