Skip to content
This repository has been archived by the owner on Jul 19, 2023. It is now read-only.

Commit

Permalink
First approach to localized field
Browse files Browse the repository at this point in the history
  • Loading branch information
Papipo committed May 1, 2010
1 parent 5782aa9 commit 5109b39
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 27 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ tmtags
coverage
rdoc
pkg
.bundle

## PROJECT::SPECIFIC

8 changes: 8 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
source :gemcutter

gem 'bson_ext', '0.20.1'
gem 'mongoid', '2.0.0.beta4'
gem 'rspec', '2.0.0.beta.8'
gem 'mocha'

gem 'jeweler'
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2009 Rodrigo Álvarez
Copyright (c) 2010 Rodrigo Álvarez

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
25 changes: 11 additions & 14 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,36 +1,33 @@
# encoding: utf-8
require 'rubygems'
require 'rake'
require 'rspec'
require 'rspec/core/rake_task'

begin
require 'jeweler'
Jeweler::Tasks.new do |gem|
gem.name = "mongoid_i18n"
gem.summary = %Q{Mongoid plugin to deal with localizable fields}
gem.description = %Q{TODO: longer description of your gem}
gem.description = %Q{This gem aims to be a transparent way to deal with localizable fields.
Basically use localized_field() instead of field() and that's it.
It will take care of locales for you when using find.
}
gem.email = "[email protected]"
gem.homepage = "http://github.com/Papipo/mongoid_i18n"
gem.authors = ["Rodrigo Álvarez"]
gem.add_development_dependency "rspec", ">= 1.2.9"
gem.add_dependency "mongoid", '2.0.0.beta4'
gem.add_development_dependency "rspec", "2.0.0.beta.8"
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
end
Jeweler::GemcutterTasks.new
rescue LoadError
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
end

require 'spec/rake/spectask'
Spec::Rake::SpecTask.new(:spec) do |spec|
spec.libs << 'lib' << 'spec'
spec.spec_files = FileList['spec/**/*_spec.rb']
end

Spec::Rake::SpecTask.new(:rcov) do |spec|
spec.libs << 'lib' << 'spec'
spec.pattern = 'spec/**/*_spec.rb'
spec.rcov = true
end

task :spec => :check_dependencies
desc "Run all examples"
Rspec::Core::RakeTask.new(:spec)

task :default => :spec

Expand Down
29 changes: 29 additions & 0 deletions lib/mongoid/i18n.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
gem 'mongoid', '2.0.0.beta4'
require 'mongoid'
require 'mongoid/i18n/localized_field'

module Mongoid
module I18n
extend ActiveSupport::Concern

module ClassMethods
def localized_field(name, options = {})
field name, options.merge(:type => LocalizedField, :default => {})
end

protected
def create_accessors(name, meth, options = {})
if options[:type] == LocalizedField
define_method(meth) { read_attribute(name)[::I18n.locale] }
define_method("#{meth}=") do |value|
write_attribute(name, (@attributes[name] || {}).merge(::I18n.locale => value))
end
define_method("#{meth}_translations") { read_attribute(name) }
define_method("#{meth}_translations=") { |value| write_attribute(name, value) }
else
super
end
end
end
end
end
6 changes: 6 additions & 0 deletions lib/mongoid/i18n/localized_field.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Mongoid
module I18n
class LocalizedField < Hash
end
end
end
Empty file removed lib/mongoid_i18n.rb
Empty file.
File renamed without changes.
73 changes: 73 additions & 0 deletions spec/integration/mongoid/i18n_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# encoding: utf-8
require 'spec_helper'

class Entry
include Mongoid::Document
include Mongoid::I18n

localized_field :title
field :published, :type => Boolean
end

describe Mongoid::I18n, "localized_field" do
describe "with an assigned value" do
before do
I18n.locale = :en
@entry = Entry.new(:title => 'Title')
end

it "should return that value" do
@entry.title.should == 'Title'
end

describe "when the locale is changed" do
before do
I18n.locale = :es
end

it "should return a blank value value" do
@entry.title.should be_blank
end

describe "a new value is assigned" do
before do
@entry.title = 'Título'
end

it "should return the new value" do
@entry.title.should == 'Título'
end

describe "getter.translations" do
it "should return all translations" do
@entry.title_translations.should == {:en => 'Title', :es => 'Título'}
end
end

describe "getter.translations=" do
before do
@entry.title_translations = {:en => 'New title', :es => 'Nuevo título'}
end

it "should accept new translations" do
@entry.title_translations.should == {:en => 'New title', :es => 'Nuevo título'}
end

it "the setter should return the new translation" do
@entry.title.should == 'Nuevo título'
end
end

describe "and we go back to the original locale" do
before do
I18n.locale = :en
end

it "should return the original value" do
@entry.title.should == 'Title'
end
end
end
end
end
end
7 changes: 0 additions & 7 deletions spec/mongoid_i18n_spec.rb

This file was deleted.

19 changes: 14 additions & 5 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
$LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
require 'mongoid_i18n'
require 'spec'
require 'spec/autorun'

Spec::Runner.configure do |config|

require 'mongoid/i18n'
require 'rspec'
require 'rspec/autorun'

Rspec.configure do |config|
config.mock_with :mocha
config.after :each do
Mongoid.master.collections.each(&:drop)
end
end

Mongoid.configure do |config|
config.master = Mongo::Connection.new.db('mongoid_i18n_test')
config.allow_dynamic_fields = false
end

0 comments on commit 5109b39

Please sign in to comment.