mongoid_i18n 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/.rvmrc CHANGED
@@ -1 +1 @@
1
- rvm 1.8.7-p334
1
+ rvm 1.8.7
data/Gemfile CHANGED
@@ -1,6 +1,6 @@
1
1
  source :gemcutter
2
2
 
3
- gem "mongoid", '>= 2.0.0'
3
+ gem "mongoid", '>= 2.1.0'
4
4
  gem "bson_ext"
5
5
 
6
6
  group :development, :test do
data/Gemfile.lock CHANGED
@@ -1,11 +1,11 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
- activemodel (3.0.9)
5
- activesupport (= 3.0.9)
4
+ activemodel (3.0.10)
5
+ activesupport (= 3.0.10)
6
6
  builder (~> 2.1.2)
7
7
  i18n (~> 0.5.0)
8
- activesupport (3.0.9)
8
+ activesupport (3.0.10)
9
9
  bson (1.3.1)
10
10
  bson_ext (1.3.1)
11
11
  builder (2.1.2)
@@ -19,7 +19,7 @@ GEM
19
19
  mocha (0.9.12)
20
20
  mongo (1.3.1)
21
21
  bson (>= 1.3.1)
22
- mongoid (2.0.2)
22
+ mongoid (2.1.8)
23
23
  activemodel (~> 3.0)
24
24
  mongo (~> 1.3)
25
25
  tzinfo (~> 0.3.22)
@@ -42,5 +42,5 @@ DEPENDENCIES
42
42
  bundler
43
43
  jeweler
44
44
  mocha
45
- mongoid (>= 2.0.0)
45
+ mongoid (>= 2.1.0)
46
46
  rspec
data/Rakefile CHANGED
@@ -30,7 +30,7 @@ RSpec::Core::RakeTask.new(:spec)
30
30
 
31
31
  task :default => :spec
32
32
 
33
- require 'rake/rdoctask'
33
+ require 'rdoc/task'
34
34
  Rake::RDocTask.new do |rdoc|
35
35
  version = File.exist?('VERSION') ? File.read('VERSION') : ""
36
36
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.1
1
+ 0.4.0
@@ -1,6 +1,49 @@
1
1
  module Mongoid
2
2
  module I18n
3
- class LocalizedField < Hash
3
+ class LocalizedField
4
+ include Mongoid::Fields::Serializable
5
+
6
+ # Return translated values of field, accoring to current locale.
7
+ # If :use_default_if_empty is set, then in case when there no
8
+ # translation available for current locale, if will try to
9
+ # get translation for defalt_locale.
10
+ def deserialize(object)
11
+ lookups = [self.locale]
12
+
13
+ # TODO: Add I18n.fallbacks support instead of :use_default_if_empty
14
+ if options[:use_default_if_empty]
15
+ lookups.push ::I18n.default_locale.to_s
16
+ end
17
+
18
+ # Find first localized value in lookup path and return corresponding value
19
+ object[lookups.find{|locale| object[locale]}]
20
+ end
21
+
22
+ # Return translations as keys. If :clear_empty_values is set to true
23
+ # pairs with empty values will be rejected
24
+ def to_hash(object)
25
+ if options[:clear_empty_values]
26
+ object.reject!{|k,v| v.blank?}
27
+ end
28
+
29
+ object
30
+ end
31
+
32
+ # Assing new translation to translation table.
33
+ def assign(object, value)
34
+ object.merge(locale => value)
35
+ end
36
+
37
+ # Replace translation hash with new one. If :clear_empty_values is set to
38
+ # pairs with empty values will be reject
39
+ def replace(object, values)
40
+ self.to_hash(values)
41
+ end
42
+
43
+ # Return current locale as string
44
+ def locale
45
+ ::I18n.locale.to_s
46
+ end
4
47
  end
5
48
  end
6
49
  end
data/lib/mongoid/i18n.rb CHANGED
@@ -11,7 +11,7 @@ module Mongoid
11
11
 
12
12
  module ClassMethods
13
13
  def localized_field(name, options = {})
14
- field name, options.merge(:type => LocalizedField, :default => LocalizedField.new)
14
+ field name, options.merge(:type => LocalizedField, :default => {})
15
15
  end
16
16
 
17
17
  def validates_default_locale(names, options = {})
@@ -28,30 +28,36 @@ module Mongoid
28
28
 
29
29
  protected
30
30
  def create_accessors(name, meth, options = {})
31
- if options[:type] == LocalizedField
32
- if options[:use_default_if_empty]
33
- define_method(meth) { read_attribute(name)[::I18n.locale.to_s] || read_attribute(name)[::I18n.default_locale.to_s] rescue ''}
34
- else
35
- define_method(meth) { read_attribute(name)[::I18n.locale.to_s] rescue '' }
36
- end
31
+ # Let Mongoid do all stuff
32
+ super
33
+
34
+ # Skip if create_accessors called on non LocalizedField field
35
+ return if LocalizedField != options[:type]
36
+
37
+ # Get field to retain incapsulation of LocalizedField class
38
+ field = fields[name]
39
+
40
+ generated_field_methods.module_eval do
41
+
42
+ # Redefine writer method, since it's impossible to correctly implement
43
+ # = method on field itself
37
44
  define_method("#{meth}=") do |value|
38
- value = if value.is_a?(Hash)
39
- (@attributes[name] || {}).merge(value)
40
- else
41
- (@attributes[name] || {}).merge(::I18n.locale.to_s => value)
42
- end
43
- value = value.delete_if { |key, value| value.blank? } if options[:clear_empty_values]
44
- write_attribute(name, value)
45
+ hash = field.assign(read_attribute(name), value)
46
+ write_attribute(name, hash)
45
47
  end
46
- define_method("#{meth}_translations") { read_attribute(name) }
47
- if options[:clear_empty_values]
48
- define_method("#{meth}_translations=") { |value| write_attribute(name, value.delete_if { |key, value| value.blank? }) }
49
- else
50
- define_method("#{meth}_translations=") { |value| write_attribute(name, value) }
48
+
49
+ # Return list of attribute translations
50
+ define_method("#{meth}_translations") do
51
+ field.to_hash(read_attribute(name))
52
+ end
53
+
54
+ # Mass-assign translations
55
+ define_method("#{meth}_translations=") do |values|
56
+ hash = field.replace(read_attribute(name), values)
57
+ write_attribute(name, hash)
51
58
  end
52
- else
53
- super
54
59
  end
60
+
55
61
  end
56
62
  end
57
63
  end
data/mongoid_i18n.gemspec CHANGED
@@ -4,17 +4,14 @@
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
- s.name = %q{mongoid_i18n}
8
- s.version = "0.3.1"
7
+ s.name = "mongoid_i18n"
8
+ s.version = "0.4.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Rodrigo \303\201lvarez"]
12
- s.date = %q{2011-07-08}
13
- s.description = %q{This gem aims to be a transparent way to deal with localizable fields.
14
- Basically use localized_field() instead of field() and that's it.
15
- It will take care of locales for you when using find or criteria.
16
- }
17
- s.email = %q{papipo@gmail.com}
12
+ s.date = "2011-10-06"
13
+ s.description = "This gem aims to be a transparent way to deal with localizable fields.\n Basically use localized_field() instead of field() and that's it.\n It will take care of locales for you when using find or criteria.\n "
14
+ s.email = "papipo@gmail.com"
18
15
  s.extra_rdoc_files = [
19
16
  "LICENSE",
20
17
  "README.rdoc"
@@ -38,23 +35,23 @@ Gem::Specification.new do |s|
38
35
  "spec/integration/mongoid/i18n_spec.rb",
39
36
  "spec/spec_helper.rb"
40
37
  ]
41
- s.homepage = %q{http://github.com/Papipo/mongoid_i18n}
38
+ s.homepage = "http://github.com/Papipo/mongoid_i18n"
42
39
  s.require_paths = ["lib"]
43
- s.rubygems_version = %q{1.6.2}
44
- s.summary = %q{Mongoid plugin to deal with localizable fields}
40
+ s.rubygems_version = "1.8.10"
41
+ s.summary = "Mongoid plugin to deal with localizable fields"
45
42
 
46
43
  if s.respond_to? :specification_version then
47
44
  s.specification_version = 3
48
45
 
49
46
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
50
- s.add_runtime_dependency(%q<mongoid>, [">= 2.0.0"])
47
+ s.add_runtime_dependency(%q<mongoid>, [">= 2.1.0"])
51
48
  s.add_runtime_dependency(%q<bson_ext>, [">= 0"])
52
49
  s.add_development_dependency(%q<rspec>, [">= 0"])
53
50
  s.add_development_dependency(%q<mocha>, [">= 0"])
54
51
  s.add_development_dependency(%q<jeweler>, [">= 0"])
55
52
  s.add_development_dependency(%q<bundler>, [">= 0"])
56
53
  else
57
- s.add_dependency(%q<mongoid>, [">= 2.0.0"])
54
+ s.add_dependency(%q<mongoid>, [">= 2.1.0"])
58
55
  s.add_dependency(%q<bson_ext>, [">= 0"])
59
56
  s.add_dependency(%q<rspec>, [">= 0"])
60
57
  s.add_dependency(%q<mocha>, [">= 0"])
@@ -62,7 +59,7 @@ Gem::Specification.new do |s|
62
59
  s.add_dependency(%q<bundler>, [">= 0"])
63
60
  end
64
61
  else
65
- s.add_dependency(%q<mongoid>, [">= 2.0.0"])
62
+ s.add_dependency(%q<mongoid>, [">= 2.1.0"])
66
63
  s.add_dependency(%q<bson_ext>, [">= 0"])
67
64
  s.add_dependency(%q<rspec>, [">= 0"])
68
65
  s.add_dependency(%q<mocha>, [">= 0"])
@@ -5,6 +5,8 @@ class Entry
5
5
  include Mongoid::Document
6
6
  include Mongoid::I18n
7
7
 
8
+ field :weight, :type => Integer, :default => 60
9
+
8
10
  localized_field :title
9
11
  localized_field :title_with_default, :use_default_if_empty => true
10
12
  localized_field :title_without_empty_values, :clear_empty_values => true
@@ -173,7 +175,7 @@ describe Mongoid::I18n, 'localized field in embedded document' do
173
175
  @entry = Entry.new
174
176
  @entry.create_sub_entry(:subtitle => 'Oxford Street')
175
177
  end
176
-
178
+
177
179
  it "should store the title in the right locale" do
178
180
  @entry.reload.sub_entry.subtitle.should == 'Oxford Street'
179
181
  end
@@ -364,3 +366,21 @@ describe Mongoid::I18n, "localized_field with validation 'validates_all_locales'
364
366
  end
365
367
  end
366
368
  end
369
+
370
+ describe Mongoid::I18n, "create_accessors" do
371
+ before do
372
+ I18n.locale = :en
373
+ @entry = Entry.new
374
+ end
375
+
376
+ it "should not affect other fields accessors" do
377
+ @entry.weight.should == 60
378
+
379
+ @entry.weight = 70
380
+ @entry.weight.should == 70
381
+ end
382
+
383
+ it "should not define own methods on for fields" do
384
+ @entry.should_not respond_to :weight_translations
385
+ end
386
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_i18n
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 15
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 3
9
- - 1
10
- version: 0.3.1
8
+ - 4
9
+ - 0
10
+ version: 0.4.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - "Rodrigo \xC3\x81lvarez"
@@ -15,28 +15,25 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-07-08 00:00:00 +02:00
19
- default_executable:
18
+ date: 2011-10-06 00:00:00 Z
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
22
- prerelease: false
23
- type: :runtime
24
21
  requirement: &id001 !ruby/object:Gem::Requirement
25
22
  none: false
26
23
  requirements:
27
24
  - - ">="
28
25
  - !ruby/object:Gem::Version
29
- hash: 15
26
+ hash: 11
30
27
  segments:
31
28
  - 2
29
+ - 1
32
30
  - 0
33
- - 0
34
- version: 2.0.0
35
- name: mongoid
31
+ version: 2.1.0
36
32
  version_requirements: *id001
37
- - !ruby/object:Gem::Dependency
33
+ name: mongoid
38
34
  prerelease: false
39
35
  type: :runtime
36
+ - !ruby/object:Gem::Dependency
40
37
  requirement: &id002 !ruby/object:Gem::Requirement
41
38
  none: false
42
39
  requirements:
@@ -46,11 +43,11 @@ dependencies:
46
43
  segments:
47
44
  - 0
48
45
  version: "0"
49
- name: bson_ext
50
46
  version_requirements: *id002
51
- - !ruby/object:Gem::Dependency
47
+ name: bson_ext
52
48
  prerelease: false
53
- type: :development
49
+ type: :runtime
50
+ - !ruby/object:Gem::Dependency
54
51
  requirement: &id003 !ruby/object:Gem::Requirement
55
52
  none: false
56
53
  requirements:
@@ -60,11 +57,11 @@ dependencies:
60
57
  segments:
61
58
  - 0
62
59
  version: "0"
63
- name: rspec
64
60
  version_requirements: *id003
65
- - !ruby/object:Gem::Dependency
61
+ name: rspec
66
62
  prerelease: false
67
63
  type: :development
64
+ - !ruby/object:Gem::Dependency
68
65
  requirement: &id004 !ruby/object:Gem::Requirement
69
66
  none: false
70
67
  requirements:
@@ -74,11 +71,11 @@ dependencies:
74
71
  segments:
75
72
  - 0
76
73
  version: "0"
77
- name: mocha
78
74
  version_requirements: *id004
79
- - !ruby/object:Gem::Dependency
75
+ name: mocha
80
76
  prerelease: false
81
77
  type: :development
78
+ - !ruby/object:Gem::Dependency
82
79
  requirement: &id005 !ruby/object:Gem::Requirement
83
80
  none: false
84
81
  requirements:
@@ -88,11 +85,11 @@ dependencies:
88
85
  segments:
89
86
  - 0
90
87
  version: "0"
91
- name: jeweler
92
88
  version_requirements: *id005
93
- - !ruby/object:Gem::Dependency
89
+ name: jeweler
94
90
  prerelease: false
95
91
  type: :development
92
+ - !ruby/object:Gem::Dependency
96
93
  requirement: &id006 !ruby/object:Gem::Requirement
97
94
  none: false
98
95
  requirements:
@@ -102,8 +99,10 @@ dependencies:
102
99
  segments:
103
100
  - 0
104
101
  version: "0"
105
- name: bundler
106
102
  version_requirements: *id006
103
+ name: bundler
104
+ prerelease: false
105
+ type: :development
107
106
  description: "This gem aims to be a transparent way to deal with localizable fields.\n Basically use localized_field() instead of field() and that's it.\n It will take care of locales for you when using find or criteria.\n "
108
107
  email: papipo@gmail.com
109
108
  executables: []
@@ -131,7 +130,6 @@ files:
131
130
  - spec/.rspec
132
131
  - spec/integration/mongoid/i18n_spec.rb
133
132
  - spec/spec_helper.rb
134
- has_rdoc: true
135
133
  homepage: http://github.com/Papipo/mongoid_i18n
136
134
  licenses: []
137
135
 
@@ -161,7 +159,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
161
159
  requirements: []
162
160
 
163
161
  rubyforge_project:
164
- rubygems_version: 1.6.2
162
+ rubygems_version: 1.8.10
165
163
  signing_key:
166
164
  specification_version: 3
167
165
  summary: Mongoid plugin to deal with localizable fields