├── VERSION ├── .rvmrc ├── spec ├── .rspec ├── spec_helper.rb └── integration │ └── mongoid │ └── i18n_spec.rb ├── .document ├── Gemfile ├── README.rdoc ├── config └── locales │ └── en.yml ├── .gitignore ├── lib └── mongoid │ ├── i18n │ ├── criterion │ │ └── selector.rb │ ├── localized_validator.rb │ └── localized_field.rb │ └── i18n.rb ├── LICENSE ├── Gemfile.lock ├── Rakefile └── mongoid_i18n.gemspec /VERSION: -------------------------------------------------------------------------------- 1 | 0.5.1 -------------------------------------------------------------------------------- /.rvmrc: -------------------------------------------------------------------------------- 1 | rvm 1.8.7 2 | -------------------------------------------------------------------------------- /spec/.rspec: -------------------------------------------------------------------------------- 1 | --color 2 | -------------------------------------------------------------------------------- /.document: -------------------------------------------------------------------------------- 1 | README.rdoc 2 | lib/**/*.rb 3 | bin/* 4 | features/**/*.feature 5 | LICENSE 6 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source :gemcutter 2 | 3 | gem "mongoid", '>= 2.1.0' 4 | 5 | group :development, :test do 6 | gem "bson_ext" 7 | gem "rspec" 8 | gem "mocha" 9 | gem "jeweler" 10 | gem "bundler" 11 | end -------------------------------------------------------------------------------- /README.rdoc: -------------------------------------------------------------------------------- 1 | This gem is no longer maintained. AFAIK, mongoid supports i18n by itself now: 2 | 3 | 3.X http://mongoid.org/en/mongoid/docs/documents.html#localized_fields 4 | 2.X http://two.mongoid.org/docs/documents/localized.html -------------------------------------------------------------------------------- /config/locales/en.yml: -------------------------------------------------------------------------------- 1 | en: 2 | locales: 3 | en: "English" 4 | activemodel: 5 | errors: 6 | messages: 7 | locale_blank: "%{cur_locale} can't be blank" 8 | all_locales_blank: "at least a translation should be inserted" -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 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 | 24 | -------------------------------------------------------------------------------- /lib/mongoid/i18n/criterion/selector.rb: -------------------------------------------------------------------------------- 1 | module Mongoid 2 | module Criterion 3 | class Selector< Hash 4 | def []=(key, value) 5 | key = "#{key}.#{::I18n.locale}" if fields[key.to_s].try(:type) == Mongoid::I18n::LocalizedField 6 | super 7 | end 8 | end 9 | end 10 | end -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | $LOAD_PATH.unshift(File.dirname(__FILE__)) 2 | $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) 3 | 4 | require 'mongoid' 5 | require 'mongoid/i18n' 6 | require 'rspec' 7 | require 'rspec/autorun' 8 | 9 | RSpec.configure do |config| 10 | config.mock_with :mocha 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_i18n_test') 18 | config.allow_dynamic_fields = false 19 | end -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 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. 21 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | GEM 2 | remote: http://rubygems.org/ 3 | specs: 4 | activemodel (3.1.1) 5 | activesupport (= 3.1.1) 6 | builder (~> 3.0.0) 7 | i18n (~> 0.6) 8 | activesupport (3.1.1) 9 | multi_json (~> 1.0) 10 | bson (1.4.0) 11 | bson_ext (1.4.0) 12 | builder (3.0.0) 13 | diff-lcs (1.1.3) 14 | git (1.2.5) 15 | i18n (0.6.0) 16 | jeweler (1.6.4) 17 | bundler (~> 1.0) 18 | git (>= 1.2.5) 19 | rake 20 | metaclass (0.0.1) 21 | mocha (0.10.0) 22 | metaclass (~> 0.0.1) 23 | mongo (1.4.0) 24 | bson (= 1.4.0) 25 | mongoid (2.3.2) 26 | activemodel (~> 3.1) 27 | mongo (~> 1.4) 28 | tzinfo (~> 0.3.22) 29 | multi_json (1.0.3) 30 | rake (0.9.2) 31 | rspec (2.7.0) 32 | rspec-core (~> 2.7.0) 33 | rspec-expectations (~> 2.7.0) 34 | rspec-mocks (~> 2.7.0) 35 | rspec-core (2.7.0) 36 | rspec-expectations (2.7.0) 37 | diff-lcs (~> 1.1.2) 38 | rspec-mocks (2.7.0) 39 | tzinfo (0.3.30) 40 | 41 | PLATFORMS 42 | ruby 43 | 44 | DEPENDENCIES 45 | bson_ext 46 | bundler 47 | jeweler 48 | mocha 49 | mongoid (>= 2.1.0) 50 | rspec 51 | -------------------------------------------------------------------------------- /lib/mongoid/i18n/localized_validator.rb: -------------------------------------------------------------------------------- 1 | module Mongoid 2 | module I18n 3 | class LocalizedValidator < ActiveModel::EachValidator 4 | def validate_each record, attribute, value 5 | if options[:mode] == :only_default 6 | if record.send("#{attribute}_translations")[::I18n.default_locale.to_s].blank? 7 | record.errors.add(attribute, :locale_blank, options.except(:mode).merge( 8 | :cur_locale => ::I18n.t(:"locales.#{::I18n.default_locale}", :default => ::I18n.default_locale.to_s) 9 | )) 10 | end 11 | elsif options[:mode] == :one_locale 12 | record.errors.add(attribute, :all_locales_blank, options.except(:mode)) if record.send("#{attribute}_translations").empty? 13 | elsif options[:mode] == :all_locales 14 | #difference between available locales and stored locales 15 | diffloc = ::I18n.available_locales - record.send("#{attribute}_translations").keys.collect { |key| key.to_sym } 16 | 17 | #print an error for each missing locale 18 | diffloc.each do |locale| 19 | record.errors.add(attribute, :locale_blank, options.except(:mode).merge( 20 | :cur_locale => ::I18n.t(:"locales.#{locale}", :default => locale.to_s) 21 | )) 22 | end 23 | end 24 | end 25 | end 26 | end 27 | end -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 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 or criteria. 15 | } 16 | gem.email = "papipo@gmail.com" 17 | gem.homepage = "http://github.com/Papipo/mongoid_i18n" 18 | gem.authors = ["Rodrigo Álvarez"] 19 | 20 | # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings 21 | end 22 | Jeweler::GemcutterTasks.new 23 | rescue LoadError 24 | puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler" 25 | end 26 | 27 | 28 | desc "Run all examples" 29 | RSpec::Core::RakeTask.new(:spec) 30 | 31 | task :default => :spec 32 | 33 | require 'rdoc/task' 34 | Rake::RDocTask.new do |rdoc| 35 | version = File.exist?('VERSION') ? File.read('VERSION') : "" 36 | 37 | rdoc.rdoc_dir = 'rdoc' 38 | rdoc.title = "mongoid_i18n #{version}" 39 | rdoc.rdoc_files.include('README*') 40 | rdoc.rdoc_files.include('lib/**/*.rb') 41 | end 42 | -------------------------------------------------------------------------------- /lib/mongoid/i18n/localized_field.rb: -------------------------------------------------------------------------------- 1 | module Mongoid 2 | module I18n 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 47 | end 48 | end 49 | end -------------------------------------------------------------------------------- /lib/mongoid/i18n.rb: -------------------------------------------------------------------------------- 1 | require 'mongoid/i18n/localized_field' 2 | require 'mongoid/i18n/criterion/selector' 3 | require 'mongoid/i18n/localized_validator' 4 | 5 | # add english load path by default 6 | I18n.load_path << File.join(File.dirname(__FILE__), "..", "..", "config", "locales", "en.yml") 7 | 8 | module Mongoid 9 | module I18n 10 | extend ActiveSupport::Concern 11 | 12 | module ClassMethods 13 | def localized_field(name, options = {}) 14 | field name, options.merge(:type => LocalizedField, :default => {}) 15 | end 16 | 17 | def validates_default_locale(names, options = {}) 18 | validates_with LocalizedValidator, options.merge(:mode => :only_default, :attributes => names) 19 | end 20 | 21 | def validates_one_locale(names, options = {}) 22 | validates_with LocalizedValidator, options.merge(:mode => :one_locale, :attributes => names) 23 | end 24 | 25 | def validates_all_locales(names, options = {}) 26 | validates_with LocalizedValidator, options.merge(:mode => :all_locales, :attributes => names) 27 | end 28 | 29 | protected 30 | def create_accessors(name, meth, options = {}) 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 44 | define_method("#{meth}=") do |value| 45 | hash = field.assign(read_attribute(name), value) 46 | write_attribute(name, hash) 47 | end 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) 58 | end 59 | end 60 | 61 | end 62 | end 63 | end 64 | end -------------------------------------------------------------------------------- /mongoid_i18n.gemspec: -------------------------------------------------------------------------------- 1 | # Generated by jeweler 2 | # DO NOT EDIT THIS FILE DIRECTLY 3 | # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec' 4 | # -*- encoding: utf-8 -*- 5 | 6 | Gem::Specification.new do |s| 7 | s.name = %q{mongoid_i18n} 8 | s.version = "0.5.1" 9 | 10 | s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= 11 | s.authors = [%q{Rodrigo Álvarez}] 12 | s.date = %q{2011-10-17} 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} 18 | s.extra_rdoc_files = [ 19 | "LICENSE", 20 | "README.rdoc" 21 | ] 22 | s.files = [ 23 | ".document", 24 | ".rvmrc", 25 | "Gemfile", 26 | "Gemfile.lock", 27 | "LICENSE", 28 | "README.rdoc", 29 | "Rakefile", 30 | "VERSION", 31 | "config/locales/en.yml", 32 | "lib/mongoid/i18n.rb", 33 | "lib/mongoid/i18n/criterion/selector.rb", 34 | "lib/mongoid/i18n/localized_field.rb", 35 | "lib/mongoid/i18n/localized_validator.rb", 36 | "mongoid_i18n.gemspec", 37 | "spec/.rspec", 38 | "spec/integration/mongoid/i18n_spec.rb", 39 | "spec/spec_helper.rb" 40 | ] 41 | s.homepage = %q{http://github.com/Papipo/mongoid_i18n} 42 | s.require_paths = [%q{lib}] 43 | s.rubygems_version = %q{1.8.6} 44 | s.summary = %q{Mongoid plugin to deal with localizable fields} 45 | 46 | if s.respond_to? :specification_version then 47 | s.specification_version = 3 48 | 49 | if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then 50 | s.add_runtime_dependency(%q, [">= 2.1.0"]) 51 | s.add_development_dependency(%q, [">= 0"]) 52 | s.add_development_dependency(%q, [">= 0"]) 53 | s.add_development_dependency(%q, [">= 0"]) 54 | s.add_development_dependency(%q, [">= 0"]) 55 | s.add_development_dependency(%q, [">= 0"]) 56 | else 57 | s.add_dependency(%q, [">= 2.1.0"]) 58 | s.add_dependency(%q, [">= 0"]) 59 | s.add_dependency(%q, [">= 0"]) 60 | s.add_dependency(%q, [">= 0"]) 61 | s.add_dependency(%q, [">= 0"]) 62 | s.add_dependency(%q, [">= 0"]) 63 | end 64 | else 65 | s.add_dependency(%q, [">= 2.1.0"]) 66 | s.add_dependency(%q, [">= 0"]) 67 | s.add_dependency(%q, [">= 0"]) 68 | s.add_dependency(%q, [">= 0"]) 69 | s.add_dependency(%q, [">= 0"]) 70 | s.add_dependency(%q, [">= 0"]) 71 | end 72 | end 73 | 74 | -------------------------------------------------------------------------------- /spec/integration/mongoid/i18n_spec.rb: -------------------------------------------------------------------------------- 1 | # encoding: utf-8 2 | require 'spec_helper' 3 | 4 | class Entry 5 | include Mongoid::Document 6 | include Mongoid::I18n 7 | 8 | field :weight, :type => Integer, :default => 60 9 | 10 | localized_field :title 11 | localized_field :title_with_default, :use_default_if_empty => true 12 | localized_field :title_without_empty_values, :clear_empty_values => true 13 | end 14 | 15 | class EntryWithValidations 16 | include Mongoid::Document 17 | include Mongoid::I18n 18 | 19 | localized_field :title_validated_with_default_locale 20 | localized_field :title_validated_with_one_locale 21 | localized_field :title_validated_with_all_locales 22 | 23 | validates_default_locale :title_validated_with_default_locale 24 | validates_one_locale :title_validated_with_one_locale 25 | validates_all_locales :title_validated_with_all_locales 26 | end 27 | 28 | describe Mongoid::I18n, "localized_field" do 29 | before do 30 | I18n.locale = :en 31 | end 32 | 33 | describe "without an assigned value" do 34 | before do 35 | @entry = Entry.new 36 | end 37 | 38 | it "should return blank" do 39 | @entry.title.should be_blank 40 | end 41 | end 42 | 43 | describe "with an assigned value" do 44 | before do 45 | @entry = Entry.new(:title => 'Title') 46 | end 47 | 48 | it "should return that value" do 49 | @entry.title.should == 'Title' 50 | end 51 | 52 | describe "and persisted" do 53 | before do 54 | @entry.save 55 | end 56 | 57 | describe "find by id" do 58 | it "should find the document" do 59 | Entry.find(@entry.id).should == @entry 60 | end 61 | end 62 | 63 | describe "where() criteria" do 64 | it "should use the current locale value" do 65 | Entry.where(:title => 'Title').first.should == @entry 66 | end 67 | end 68 | 69 | describe "find(:first) with :conditions" do 70 | it "should use the current locale value" do 71 | Entry.find(:first, :conditions => {:title => 'Title'}).should == @entry 72 | end 73 | end 74 | end 75 | 76 | describe "when the locale is changed" do 77 | before do 78 | I18n.locale = :es 79 | end 80 | 81 | it "should return a blank value" do 82 | @entry.title.should be_blank 83 | end 84 | 85 | describe "a new value is assigned" do 86 | before do 87 | @entry.title = 'Título' 88 | end 89 | 90 | it "should return the new value" do 91 | @entry.title.should == 'Título' 92 | end 93 | 94 | describe "persisted and retrieved from db" do 95 | before do 96 | @entry.save 97 | @entry.reload 98 | end 99 | 100 | it "the localized field value should be correct" do 101 | @entry.title.should == 'Título' 102 | I18n.locale = :en 103 | @entry.title.should == 'Title' 104 | @entry.title_translations.should == {'en' => 'Title', 'es' => 'Título'} 105 | end 106 | end 107 | 108 | describe "field_translations" do 109 | it "should return all translations" do 110 | @entry.title_translations.should == {'en' => 'Title', 'es' => 'Título'} 111 | end 112 | end 113 | 114 | describe "with mass-assigned translations" do 115 | before do 116 | @entry.title_translations = {'en' => 'New title', 'es' => 'Nuevo título'} 117 | end 118 | 119 | it "should set all translations" do 120 | @entry.title_translations.should == {'en' => 'New title', 'es' => 'Nuevo título'} 121 | end 122 | 123 | it "the getter should return the new translation" do 124 | @entry.title.should == 'Nuevo título' 125 | end 126 | end 127 | 128 | describe "if we go back to the original locale" do 129 | before do 130 | I18n.locale = :en 131 | end 132 | 133 | it "should return the original value" do 134 | @entry.title.should == 'Title' 135 | end 136 | end 137 | end 138 | end 139 | end 140 | end 141 | 142 | describe Mongoid::I18n, 'localized field in embedded association' do 143 | before do 144 | class Entry 145 | embeds_many :sub_entries 146 | end 147 | 148 | class SubEntry 149 | include Mongoid::Document 150 | include Mongoid::I18n 151 | localized_field :title 152 | embedded_in :entry, :inverse_of => :sub_entries 153 | end 154 | @entry = Entry.new 155 | @sub_entries = (0..2).map { @entry.sub_entries.build } 156 | end 157 | 158 | it "should contain the embedded documents" do 159 | @entry.sub_entries.criteria.instance_variable_get("@documents").should == @sub_entries 160 | end 161 | end 162 | 163 | describe Mongoid::I18n, 'localized field in embedded document' do 164 | before do 165 | class Entry 166 | embeds_one :sub_entry 167 | end 168 | 169 | class SubEntry 170 | include Mongoid::Document 171 | include Mongoid::I18n 172 | localized_field :subtitle 173 | embedded_in :entry, :inverse_of => :sub_entries 174 | end 175 | @entry = Entry.new 176 | @entry.create_sub_entry(:subtitle => 'Oxford Street') 177 | end 178 | 179 | it "should store the title in the right locale" do 180 | @entry.reload.sub_entry.subtitle.should == 'Oxford Street' 181 | end 182 | end 183 | 184 | describe Mongoid::I18n, "localized_field with :use_default_if_empty => true" do 185 | before do 186 | I18n.default_locale = :en 187 | I18n.locale = :en 188 | end 189 | 190 | describe "without an assigned value" do 191 | before do 192 | @entry = Entry.new 193 | end 194 | 195 | it "should return blank" do 196 | @entry.title_with_default.should be_blank 197 | end 198 | end 199 | 200 | describe "with an assigned value in the default locale" do 201 | before do 202 | @entry = Entry.new(:title_with_default => 'Title with default') 203 | end 204 | 205 | it "should return that value with the default locale" do 206 | @entry.title_with_default.should == 'Title with default' 207 | end 208 | 209 | describe "when the locale is changed" do 210 | before do 211 | I18n.locale = :it 212 | end 213 | 214 | it "should return the value of the default locale" do 215 | @entry.title_with_default.should == 'Title with default' 216 | end 217 | 218 | describe "when a new value is assigned" do 219 | before do 220 | @entry.title_with_default = 'Titolo con default' 221 | end 222 | 223 | it "should return the new value" do 224 | @entry.title_with_default.should == 'Titolo con default' 225 | end 226 | 227 | describe "if we go back to the original locale" do 228 | before do 229 | I18n.locale = :en 230 | end 231 | 232 | it "should return the original value" do 233 | @entry.title_with_default.should == 'Title with default' 234 | end 235 | end 236 | end 237 | end 238 | end 239 | end 240 | 241 | describe Mongoid::I18n, "localized_field with :clear_empty_values => true" do 242 | describe "when are assigned two translations" do 243 | before do 244 | I18n.locale = :en 245 | @entry = Entry.new 246 | @entry.title_without_empty_values_translations = {"en" => "Title en", "it" => "Title it"} 247 | end 248 | 249 | it "has those translations" do 250 | @entry.title_without_empty_values_translations.should == {"en" => "Title en", "it" => "Title it"} 251 | end 252 | 253 | describe "when is set to a blank value" do 254 | before do 255 | @entry.title_without_empty_values = '' 256 | end 257 | 258 | it "lose current locale translation" do 259 | @entry.title_without_empty_values_translations.should == {"it" => "Title it"} 260 | end 261 | end 262 | 263 | describe "when is assigned a blank translation" do 264 | before do 265 | @entry.title_without_empty_values_translations = {"it" => "", "en" => "Title en"} 266 | end 267 | 268 | it "lose that translation" do 269 | @entry.title_without_empty_values_translations.should == {"en" => "Title en"} 270 | end 271 | end 272 | end 273 | end 274 | 275 | describe Mongoid::I18n, "localized_field with validation 'validates_default_locale'" do 276 | before do 277 | I18n.default_locale = :en 278 | I18n.locale = :it 279 | @entry = EntryWithValidations.new 280 | end 281 | 282 | describe "when run entry validations and default locale translation wasn't set" do 283 | before do 284 | @entry.title_validated_with_default_locale="Titolo" 285 | @entry.valid? 286 | end 287 | 288 | it "is added a 'locale_blank' error for that field to entry errors list" do 289 | @entry.errors.include?(:title_validated_with_default_locale).should be_true 290 | @entry.errors[:title_validated_with_default_locale][0].split('.').last.should == 'locale_blank' 291 | end 292 | end 293 | 294 | describe "when run entry validations and default locale translation was set" do 295 | before do 296 | @entry.title_validated_with_default_locale_translations={'en'=>'Title'} 297 | @entry.valid? 298 | end 299 | 300 | it "no error for that field is added to entry errors list" do 301 | @entry.errors.include?(:title_validated_with_default_locale).should be_false 302 | end 303 | end 304 | end 305 | 306 | describe Mongoid::I18n, "localized_field with validation 'validates_one_locale'" do 307 | before do 308 | I18n.default_locale = :en 309 | I18n.locale = :it 310 | @entry = EntryWithValidations.new 311 | end 312 | 313 | describe "when run entry validations and no translation was set" do 314 | before do 315 | @entry.valid? 316 | end 317 | 318 | it "is added a 'locale_blank' error for that field to entry errors list" do 319 | @entry.errors.include?(:title_validated_with_one_locale).should be_true 320 | @entry.errors[:title_validated_with_one_locale][0].split('.').last.should == 'all_locales_blank' 321 | end 322 | end 323 | 324 | describe "when run entry validations and a locale translation was set" do 325 | before do 326 | @entry.title_validated_with_one_locale_translations={'it'=>'Titolo'} 327 | @entry.valid? 328 | end 329 | 330 | it "no error for that field is added to entry errors list" do 331 | @entry.errors.include?(:title_validated_with_one_locale).should be_false 332 | end 333 | end 334 | end 335 | 336 | describe Mongoid::I18n, "localized_field with validation 'validates_all_locales'" do 337 | before do 338 | I18n.default_locale = :en 339 | I18n.available_locales = [:en, :it, :de, :fr] 340 | I18n.locale = :it 341 | @entry = EntryWithValidations.new 342 | end 343 | 344 | describe "when run entry validations and not all translations were set" do 345 | before do 346 | @entry.title_validated_with_all_locales_translations={'it'=>'Titolo', 'en'=>'Title'} 347 | @entry.valid? 348 | end 349 | 350 | it "is added a 'locale_blank' error for that field for each missing locale" do 351 | @entry.errors.include?(:title_validated_with_all_locales).should be_true 352 | @entry.errors[:title_validated_with_all_locales].count.should == 2 353 | @entry.errors[:title_validated_with_all_locales][0].split('.').last.should == 'locale_blank' 354 | @entry.errors[:title_validated_with_all_locales][1].split('.').last.should == 'locale_blank' 355 | end 356 | end 357 | 358 | describe "when run entry validations and all available locales translation were set" do 359 | before do 360 | @entry.title_validated_with_all_locales_translations={'it'=>'Titolo', 'en'=>'Title', 'fr'=>'Titre', 'de'=>'Titel'} 361 | @entry.valid? 362 | end 363 | 364 | it "no error for that field is added to entry errors list" do 365 | @entry.errors.include?(:title_validated_with_all_locales).should be_false 366 | end 367 | end 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 387 | --------------------------------------------------------------------------------