mongoid_localization 0.0.2

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/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2011 YOURNAME
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,31 @@
1
+ = MongoidLocalization
2
+
3
+ This project rocks and uses MIT-LICENSE.
4
+
5
+ A simple gem for use with mongoid "~> 2.3.3", that adds attribute readers and writers to any Mongoid document, where a field has been set with :localize => true.
6
+
7
+ Usage:
8
+
9
+ Add to gemfile:
10
+
11
+ gem "mongoid_localization"
12
+
13
+ Add to Mongoid document class:
14
+ "include Mongoid::Localization"
15
+
16
+ Now any fields in a mongoid document that has localize set to true will have the following methods available appended with "_translations"
17
+
18
+ Example:
19
+
20
+ class Post
21
+ include Mongoid::Document
22
+ include Mongoid::Localization
23
+
24
+ field :name, :localize => true, :default => ""
25
+
26
+ end
27
+
28
+ The following methods will now be automatically available:
29
+
30
+ @post.name_translations
31
+ @post.name_translations =
@@ -0,0 +1,5 @@
1
+ module Mongoid #:nodoc:
2
+ module Localization
3
+ VERSION = "0.0.2"
4
+ end
5
+ end
@@ -0,0 +1,38 @@
1
+ module Mongoid
2
+
3
+ # this module adds attribute reader and writer methods for any mongoid document fields
4
+ # that have :localize set to true. This methods will access or write a hash of translations to any
5
+ # of these fields by using methods with "_translations" appended to them
6
+
7
+ module Localization
8
+ extend ActiveSupport::Concern
9
+
10
+ module ClassMethods
11
+
12
+ def create_accessors(name, meth, options ={})
13
+ self.fields.keys.each do |field|
14
+ if self.fields[field].options[:localize] == true
15
+ define_method("#{field}_translations") {read_attribute(field)}
16
+ define_method("#{field}_translations=") do |value|
17
+ if value.is_a?(Hash)
18
+ start_locale = I18n.locale
19
+ value.keys.each do |key|
20
+ if I18n.available_locales.include?(key.to_sym)
21
+ I18n.locale = key
22
+ write_attribute(field, value[key])
23
+ end
24
+ I18n.locale = start_locale
25
+ end
26
+ end
27
+ end
28
+ else
29
+ super
30
+ end
31
+ end
32
+ end
33
+
34
+ end
35
+
36
+ end
37
+
38
+ end
@@ -0,0 +1,127 @@
1
+ require "spec_helper"
2
+
3
+ class Post
4
+ include Mongoid::Document
5
+ include Mongoid::Localization
6
+
7
+ field :translations_available, :type => Array
8
+
9
+ field :name, :type => String, :localize => true, :default => ""
10
+
11
+ end
12
+
13
+ describe Mongoid::Localization, "fields with localize = true" do
14
+ before do
15
+ I18n.available_locales = [:en, :es]
16
+ I18n.locale =:en
17
+ end
18
+
19
+ describe "without an assigned value" do
20
+ before do
21
+ @post = Post.new
22
+ end
23
+
24
+ it "should return blank" do
25
+ @post.name.should be_blank
26
+ debugger
27
+ end
28
+ end
29
+
30
+ describe "with an assigned value" do
31
+ before do
32
+ @post = Post.new(:name => 'Title')
33
+ end
34
+
35
+ it "should return that value" do
36
+ @post.name.should == 'Title'
37
+ end
38
+
39
+ describe "and persisted" do
40
+ before do
41
+ @post.save
42
+ end
43
+
44
+ describe "find by id" do
45
+ it "should find the document" do
46
+ Post.find(@post.id).should == @post
47
+ end
48
+ end
49
+
50
+ describe "where() criteria" do
51
+ it "should use the current locale value" do
52
+ Post.where(:name => 'Title').first.should == @post
53
+ end
54
+ end
55
+
56
+ describe "find(:first) with :conditions" do
57
+ it "should use the current locale value" do
58
+ Post.find(:first, :conditions => {:name => 'Title'}).should == @post
59
+ end
60
+ end
61
+ end
62
+
63
+ describe "when the locale is changed" do
64
+ before do
65
+ I18n.locale = :es
66
+ end
67
+
68
+ it "should return a blank value" do
69
+ @post.name.should be_blank
70
+ end
71
+
72
+ describe "a new value is assigned" do
73
+ before do
74
+ @post.name = 'Titulo'
75
+ end
76
+
77
+ it "should return the new value" do
78
+ @post.name.should == 'Titulo'
79
+ end
80
+
81
+ describe "persisted and retrieved from db" do
82
+ before do
83
+ @post.save
84
+ @post.reload
85
+ end
86
+
87
+ it "the localized field value should be correct" do
88
+ @post.name.should == 'Titulo'
89
+ I18n.locale = :en
90
+ @post.name.should == 'Title'
91
+ @post.name_translations.should == {'en' => 'Title', 'es' => 'Titulo'}
92
+ end
93
+ end
94
+
95
+ describe "field_translations" do
96
+ it "should return all translations" do
97
+ @post.name_translations.should == {'en' => 'Title', 'es' => 'Titulo'}
98
+ end
99
+ end
100
+
101
+ describe "with mass-assigned translations" do
102
+ before do
103
+ @post.name_translations = {'en' => 'New title', 'es' => 'Nuevo Titulo'}
104
+ end
105
+
106
+ it "should set all translations" do
107
+ @post.name_translations.should == {'en' => 'New title', 'es' => 'Nuevo Titulo'}
108
+ end
109
+
110
+ it "the getter should return the new translation" do
111
+ @post.name.should == 'Nuevo Titulo'
112
+ end
113
+ end
114
+
115
+ describe "if we go back to the original locale" do
116
+ before do
117
+ I18n.locale = :en
118
+ end
119
+
120
+ it "should return the original value" do
121
+ @post.name.should == 'Title'
122
+ end
123
+ end
124
+ end
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,19 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'mongoid'
5
+ require 'mongoid/localization'
6
+ require 'rspec'
7
+ require 'rspec/autorun'
8
+
9
+
10
+ RSpec.configure do |config|
11
+ config.after :each do
12
+ Mongoid.master.collections.reject { |c| c.name =~ /^system\./ }.each(&:drop)
13
+ end
14
+ end
15
+
16
+ Mongoid.configure do |config|
17
+ config.master = Mongo::Connection.new.db('mongoid_localization_test')
18
+ config.allow_dynamic_fields = false
19
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid_localization
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - David Krett
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-11-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: &70340324546140 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.1.1
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70340324546140
25
+ - !ruby/object:Gem::Dependency
26
+ name: mongoid
27
+ requirement: &70340324544800 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 2.3.3
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70340324544800
36
+ - !ruby/object:Gem::Dependency
37
+ name: mongoid
38
+ requirement: &70340324543600 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 2.3.3
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70340324543600
47
+ - !ruby/object:Gem::Dependency
48
+ name: mongo
49
+ requirement: &70340324542760 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70340324542760
58
+ description: Simple gem that adds reader and writer methods to a mongoid document
59
+ to write and access translations when :localize has been set to true for a field
60
+ email:
61
+ - david@what2do.asia
62
+ executables: []
63
+ extensions: []
64
+ extra_rdoc_files: []
65
+ files:
66
+ - lib/mongoid/localization/version.rb
67
+ - lib/mongoid/localization.rb
68
+ - MIT-LICENSE
69
+ - README.rdoc
70
+ - spec/models/localized_model_spec.rb
71
+ - spec/spec_helper.rb
72
+ homepage: https://github.com/dbkbali/mongoid_localization
73
+ licenses: []
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 1.8.10
93
+ signing_key:
94
+ specification_version: 3
95
+ summary: Adds reader and writer methods to allow access to mongoid translations for
96
+ mongoid ~> 2.3.3
97
+ test_files:
98
+ - spec/models/localized_model_spec.rb
99
+ - spec/spec_helper.rb