mongoid_i18n 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,23 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+ .bundle
21
+
22
+ ## PROJECT::SPECIFIC
23
+
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source :gemcutter
2
+
3
+ gem 'bson_ext', '0.20.1'
4
+ gem 'mongoid', '2.0.0.beta4'
5
+ gem 'rspec', '2.0.0.beta.8'
6
+ gem 'mocha'
7
+
8
+ gem 'jeweler'
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Rodrigo Álvarez
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,45 @@
1
+ = mongoid_i18n
2
+
3
+ Mongoid::I18n is a gem that makes localization almost transparent for the developer.
4
+
5
+ == Usage
6
+
7
+ class Entry
8
+ include Mongoid::Document
9
+ include Mongoid::I18n
10
+
11
+ localized_field :title # Just use localized_field instead of field
12
+ field :published, :type => Boolean
13
+ end
14
+
15
+ This will allow you to do stuff like this:
16
+
17
+ I18n.locale = :en
18
+ @entry = Entry.new(:title => 'Title') # will be stored in the 'en' translation
19
+
20
+ I18n.locale :es
21
+ @entry.title = 'Título' # will be stored in the 'es' translation
22
+
23
+ @entry.title_translations # this returns a hash: {'en' => 'Title', 'es' => 'Title'}
24
+ @entry.title_translations = {'en' => 'New title', 'es' => 'Nuevo título} # You can always mass-assign.
25
+
26
+ Criteria is handled for you:
27
+
28
+ I18n.locale = :en
29
+ Entry.where(:title => 'Title') # This will search automatically for the 'en' translation
30
+ Entry.find(:first, :title.in => ['Stuff', 'More stuff']) # find() and complex criteria also works
31
+
32
+
33
+ == Note on Patches/Pull Requests
34
+
35
+ * Fork the project.
36
+ * Make your feature addition or bug fix.
37
+ * Add tests for it. This is important so I don't break it in a
38
+ future version unintentionally.
39
+ * Commit, do not mess with rakefile, version, or history.
40
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
41
+ * Send me a pull request. Bonus points for topic branches.
42
+
43
+ == Copyright
44
+
45
+ Copyright (c) 2010 Rodrigo Álvarez. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,42 @@
1
+ # encoding: utf-8
2
+ require 'rubygems'
3
+ require 'rake'
4
+ require 'rspec'
5
+ require 'rspec/core/rake_task'
6
+
7
+ begin
8
+ require 'jeweler'
9
+ Jeweler::Tasks.new do |gem|
10
+ gem.name = "mongoid_i18n"
11
+ gem.summary = %Q{Mongoid plugin to deal with localizable fields}
12
+ gem.description = %Q{This gem aims to be a transparent way to deal with localizable fields.
13
+ Basically use localized_field() instead of field() and that's it.
14
+ It will take care of locales for you when using find.
15
+ }
16
+ gem.email = "papipo@gmail.com"
17
+ gem.homepage = "http://github.com/Papipo/mongoid_i18n"
18
+ gem.authors = ["Rodrigo Álvarez"]
19
+ gem.add_dependency "mongoid", '2.0.0.beta4'
20
+ gem.add_development_dependency "rspec", "2.0.0.beta.8"
21
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
22
+ end
23
+ Jeweler::GemcutterTasks.new
24
+ rescue LoadError
25
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
26
+ end
27
+
28
+
29
+ desc "Run all examples"
30
+ Rspec::Core::RakeTask.new(:spec)
31
+
32
+ task :default => :spec
33
+
34
+ require 'rake/rdoctask'
35
+ Rake::RDocTask.new do |rdoc|
36
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
37
+
38
+ rdoc.rdoc_dir = 'rdoc'
39
+ rdoc.title = "mongoid_i18n #{version}"
40
+ rdoc.rdoc_files.include('README*')
41
+ rdoc.rdoc_files.include('lib/**/*.rb')
42
+ end
@@ -0,0 +1,24 @@
1
+ module Mongoid
2
+ module I18n
3
+ class LocalizedCriteria < Mongoid::Criteria
4
+ def where(selector = nil)
5
+ case selector
6
+ when String
7
+ @selector.update("$where" => selector)
8
+ else
9
+ @selector.update(selector ? selector.expand_complex_criteria : {})
10
+ expand_localized_fields_in_selector if @selector.is_a?(Hash)
11
+ end
12
+ self
13
+ end
14
+
15
+ protected
16
+
17
+ def expand_localized_fields_in_selector
18
+ @klass.fields.select { |k,f| @selector.keys.include?(k.to_sym) && f.type == LocalizedField }.each_key do |k|
19
+ @selector["#{k}.#{::I18n.locale}"] = @selector.delete(k.to_sym)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,6 @@
1
+ module Mongoid
2
+ module I18n
3
+ class LocalizedField < Hash
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,17 @@
1
+ module Mongoid
2
+ class Criteria
3
+ def self.translate(*args)
4
+ klass = args[0]
5
+ params = args[1] || {}
6
+ unless params.is_a?(Hash)
7
+ return klass.criteria.id_criteria(params)
8
+ end
9
+ conditions = params.delete(:conditions) || {}
10
+ if conditions.include?(:id)
11
+ conditions[:_id] = conditions[:id]
12
+ conditions.delete(:id)
13
+ end
14
+ return klass.criteria.where(conditions).extras(params)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,35 @@
1
+ gem 'mongoid', '2.0.0.beta4'
2
+ require 'mongoid'
3
+ require 'mongoid/i18n/localized_field'
4
+ require 'mongoid/i18n/localized_criteria'
5
+ require 'mongoid/i18n/patches'
6
+
7
+ module Mongoid
8
+ module I18n
9
+ extend ActiveSupport::Concern
10
+
11
+ module ClassMethods
12
+ def localized_field(name, options = {})
13
+ field name, options.merge(:type => LocalizedField, :default => {})
14
+ end
15
+
16
+ def criteria
17
+ I18n::LocalizedCriteria.new(self)
18
+ end
19
+
20
+ protected
21
+ def create_accessors(name, meth, options = {})
22
+ if options[:type] == LocalizedField
23
+ define_method(meth) { read_attribute(name)[::I18n.locale.to_s] }
24
+ define_method("#{meth}=") do |value|
25
+ write_attribute(name, (@attributes[name] || {}).merge(::I18n.locale.to_s => value))
26
+ end
27
+ define_method("#{meth}_translations") { read_attribute(name) }
28
+ define_method("#{meth}_translations=") { |value| write_attribute(name, value) }
29
+ else
30
+ super
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
data/spec/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,108 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ class Entry
5
+ include Mongoid::Document
6
+ include Mongoid::I18n
7
+
8
+ localized_field :title
9
+ field :published, :type => Boolean
10
+ end
11
+
12
+ describe Mongoid::I18n, "localized_field" do
13
+ before do
14
+ I18n.locale = :en
15
+ end
16
+
17
+ describe "with an assigned value" do
18
+ before do
19
+ @entry = Entry.new(:title => 'Title')
20
+ end
21
+
22
+ describe "and persisted" do
23
+ before do
24
+ @entry.save
25
+ end
26
+
27
+ describe "where() criteria" do
28
+ it "should use the current locale value" do
29
+ Entry.where(:title => 'Title').first.should == @entry
30
+ end
31
+ end
32
+
33
+ describe "find(:first) with :conditions" do
34
+ it "should use the current locale value" do
35
+ Entry.find(:first, :conditions => {:title => 'Title'}).should == @entry
36
+ end
37
+ end
38
+ end
39
+
40
+ it "should return that value" do
41
+ @entry.title.should == 'Title'
42
+ end
43
+
44
+ describe "when the locale is changed" do
45
+ before do
46
+ I18n.locale = :es
47
+ end
48
+
49
+ it "should return a blank value" do
50
+ @entry.title.should be_blank
51
+ end
52
+
53
+ describe "a new value is assigned" do
54
+ before do
55
+ @entry.title = 'Título'
56
+ end
57
+
58
+ it "should return the new value" do
59
+ @entry.title.should == 'Título'
60
+ end
61
+
62
+ describe "persisted and retrieved from db" do
63
+ before do
64
+ @entry.save
65
+ @entry = Entry.find(:first, :conditions => {:title => 'Título'})
66
+ end
67
+
68
+ it "the localized field value should be ok" do
69
+ @entry.title.should == 'Título'
70
+ I18n.locale = :en
71
+ @entry.title.should == 'Title'
72
+ @entry.title_translations.should == {'en' => 'Title', 'es' => 'Título'}
73
+ end
74
+ end
75
+
76
+ describe "getter.translations" do
77
+ it "should return all translations" do
78
+ @entry.title_translations.should == {'en' => 'Title', 'es' => 'Título'}
79
+ end
80
+ end
81
+
82
+ describe "getter.translations=" do
83
+ before do
84
+ @entry.title_translations = {'en' => 'New title', 'es' => 'Nuevo título'}
85
+ end
86
+
87
+ it "should accept new translations" do
88
+ @entry.title_translations.should == {'en' => 'New title', 'es' => 'Nuevo título'}
89
+ end
90
+
91
+ it "the getter should return the new translation" do
92
+ @entry.title.should == 'Nuevo título'
93
+ end
94
+ end
95
+
96
+ describe "and we go back to the original locale" do
97
+ before do
98
+ I18n.locale = :en
99
+ end
100
+
101
+ it "should return the original value" do
102
+ @entry.title.should == 'Title'
103
+ end
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,18 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+
4
+ require 'mongoid/i18n'
5
+ require 'rspec'
6
+ require 'rspec/autorun'
7
+
8
+ Rspec.configure do |config|
9
+ config.mock_with :mocha
10
+ config.after :each do
11
+ Mongoid.master.collections.each(&:drop)
12
+ end
13
+ end
14
+
15
+ Mongoid.configure do |config|
16
+ config.master = Mongo::Connection.new.db('mongoid_i18n_test')
17
+ config.allow_dynamic_fields = false
18
+ end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::I18n::LocalizedCriteria do
4
+ describe "where" do
5
+ before do
6
+ klass = stub(:fields => {'title' => mock(:type => Mongoid::I18n::LocalizedField), 'published' => mock(:type => Boolean)})
7
+ @criteria = Mongoid::I18n::LocalizedCriteria.new(klass)
8
+ @criteria.where(:title.in => ['Title'], :published => true)
9
+ end
10
+
11
+ it "should expand fields that are LocalizedFields" do
12
+ @criteria.instance_variable_get("@selector").should == {'title.en' => {'$in' => ['Title']}, :published => true}
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe Mongoid::I18n do
5
+ before do
6
+ @class = Class.new do
7
+ include Mongoid::I18n
8
+ end
9
+ I18n.stubs(:locale).returns(:en)
10
+ end
11
+
12
+ describe "criteria" do
13
+ it "should return a LocalizedCriteria" do
14
+ @class.criteria.should be_a(Mongoid::I18n::LocalizedCriteria)
15
+ end
16
+ end
17
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid_i18n
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - "Rodrigo \xC3\x81lvarez"
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-05-03 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: mongoid
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 2
29
+ - 0
30
+ - 0
31
+ - beta4
32
+ version: 2.0.0.beta4
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: rspec
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - "="
41
+ - !ruby/object:Gem::Version
42
+ segments:
43
+ - 2
44
+ - 0
45
+ - 0
46
+ - beta
47
+ - 8
48
+ version: 2.0.0.beta.8
49
+ type: :development
50
+ version_requirements: *id002
51
+ 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.\n "
52
+ email: papipo@gmail.com
53
+ executables: []
54
+
55
+ extensions: []
56
+
57
+ extra_rdoc_files:
58
+ - LICENSE
59
+ - README.rdoc
60
+ files:
61
+ - .document
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE
65
+ - README.rdoc
66
+ - Rakefile
67
+ - lib/mongoid/i18n.rb
68
+ - lib/mongoid/i18n/localized_criteria.rb
69
+ - lib/mongoid/i18n/localized_field.rb
70
+ - lib/mongoid/i18n/patches.rb
71
+ - spec/.rspec
72
+ - spec/integration/mongoid/i18n_spec.rb
73
+ - spec/spec_helper.rb
74
+ - spec/unit/mongoid/i18n/localized_criteria_spec.rb
75
+ - spec/unit/mongoid/i18n_spec.rb
76
+ has_rdoc: true
77
+ homepage: http://github.com/Papipo/mongoid_i18n
78
+ licenses: []
79
+
80
+ post_install_message:
81
+ rdoc_options:
82
+ - --charset=UTF-8
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ segments:
90
+ - 0
91
+ version: "0"
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ segments:
97
+ - 0
98
+ version: "0"
99
+ requirements: []
100
+
101
+ rubyforge_project:
102
+ rubygems_version: 1.3.6
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: Mongoid plugin to deal with localizable fields
106
+ test_files:
107
+ - spec/integration/mongoid/i18n_spec.rb
108
+ - spec/spec_helper.rb
109
+ - spec/unit/mongoid/i18n/localized_criteria_spec.rb
110
+ - spec/unit/mongoid/i18n_spec.rb