Permalink
Browse files

Initial commit

  • Loading branch information...
Mik-die committed Jul 25, 2011
0 parents commit 413fa4531461a430eb32dd7b895ee05ef0fa3ba0
@@ -0,0 +1,2 @@
+.bundle/
+pkg/
1 .rspec
@@ -0,0 +1 @@
+--colour
1 .rvmrc
@@ -0,0 +1 @@
+rvm 1.9.2@mongoid-globalize --create
13 Gemfile
@@ -0,0 +1,13 @@
+source "http://rubygems.org"
+
+gem 'activemodel', '3.1.0.rc4'
+gem 'mongoid', :git => 'git://github.com/mongoid/mongoid.git'
+gem 'bson_ext'
+
+group :development, :test do
+ gem 'rspec'
+ gem 'mongoid-rspec'
+ gem 'database_cleaner'
+ gem 'ruby-debug19'
+ gem 'jeweler'
+end
@@ -0,0 +1,74 @@
+GIT
+ remote: git://github.com/mongoid/mongoid.git
+ revision: c4675a987925a831dc93ab5c525c99dedf46c841
+ specs:
+ mongoid (2.1.0)
+ activemodel (~> 3.0)
+ mongo (~> 1.3)
+ tzinfo (~> 0.3.22)
+
+GEM
+ remote: http://rubygems.org/
+ specs:
+ activemodel (3.1.0.rc4)
+ activesupport (= 3.1.0.rc4)
+ bcrypt-ruby (~> 2.1.4)
+ builder (~> 3.0.0)
+ i18n (~> 0.6)
+ activesupport (3.1.0.rc4)
+ multi_json (~> 1.0)
+ archive-tar-minitar (0.5.2)
+ bcrypt-ruby (2.1.4)
+ bson (1.3.1)
+ bson_ext (1.3.1)
+ builder (3.0.0)
+ columnize (0.3.4)
+ database_cleaner (0.6.7)
+ diff-lcs (1.1.2)
+ git (1.2.5)
+ i18n (0.6.0)
+ jeweler (1.6.4)
+ bundler (~> 1.0)
+ git (>= 1.2.5)
+ rake
+ linecache19 (0.5.12)
+ ruby_core_source (>= 0.1.4)
+ mongo (1.3.1)
+ bson (>= 1.3.1)
+ mongoid-rspec (1.4.4)
+ mongoid (~> 2.0)
+ rspec (~> 2)
+ multi_json (1.0.3)
+ rake (0.9.2)
+ rspec (2.6.0)
+ rspec-core (~> 2.6.0)
+ rspec-expectations (~> 2.6.0)
+ rspec-mocks (~> 2.6.0)
+ rspec-core (2.6.4)
+ rspec-expectations (2.6.0)
+ diff-lcs (~> 1.1.2)
+ rspec-mocks (2.6.0)
+ ruby-debug-base19 (0.11.25)
+ columnize (>= 0.3.1)
+ linecache19 (>= 0.5.11)
+ ruby_core_source (>= 0.1.4)
+ ruby-debug19 (0.11.6)
+ columnize (>= 0.3.1)
+ linecache19 (>= 0.5.11)
+ ruby-debug-base19 (>= 0.11.19)
+ ruby_core_source (0.1.5)
+ archive-tar-minitar (>= 0.5.2)
+ tzinfo (0.3.29)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ activemodel (= 3.1.0.rc4)
+ bson_ext
+ database_cleaner
+ jeweler
+ mongoid!
+ mongoid-rspec
+ rspec
+ ruby-debug19
@@ -0,0 +1,20 @@
+Copyright 2011 YOURNAME
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
No changes.
@@ -0,0 +1,39 @@
+# encoding: UTF-8
+require 'rubygems'
+begin
+ require 'bundler/setup'
+rescue LoadError
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
+end
+
+require 'rake'
+require 'rdoc/task'
+
+require 'rspec/core'
+require 'rspec/core/rake_task'
+
+RSpec::Core::RakeTask.new(:spec)
+
+task :default => :spec
+
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'TranslationPanel'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README.rdoc')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
+
+require 'jeweler'
+Jeweler::Tasks.new do |gem|
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
+ gem.name = "mongoid_globalize"
+ gem.homepage = "http://github.com/Mik-die/mongoid_globalize"
+ gem.license = "MIT"
+ gem.summary = %Q{Library for translating Mongoid documents}
+ gem.description = %Q{Library for translating Mongoid documents, based on Globalize3 principles}
+ gem.email = "MikDiet@gmail.com"
+ gem.authors = ["Mik-die"]
+ # dependencies defined in Gemfile
+end
+Jeweler::RubygemsDotOrgTasks.new
@@ -0,0 +1,69 @@
+require 'mongoid_globalize/adapter'
+require 'mongoid_globalize/act_macro'
+require 'mongoid_globalize/attributes'
+require 'mongoid_globalize/class_methods'
+require 'mongoid_globalize/document_translation'
+require 'mongoid_globalize/instance_methods'
+
+module Mongoid::Globalize
+ extend ActiveSupport::Concern
+ included do
+ class_attribute :translated_attribute_names, :fallbacks_for_empty_translations
+ self.translated_attribute_names = []
+ embeds_many :translations, :class_name => translation_class.name
+ #:extend => HasManyExtensions
+ before_create :unmerge_translations!
+ after_create :create_translations!
+ before_update :update_translations!
+ after_create :clear_translations!
+
+ extend Mongoid::Globalize::ActMacro
+ end
+
+ class << self
+ def locale
+ read_locale || I18n.locale
+ end
+
+ def locale=(locale)
+ set_locale(locale)
+ end
+
+ def with_locale(locale, &block)
+ previous_locale = read_locale
+ set_locale(locale)
+ result = yield(locale)
+ set_locale(previous_locale)
+ result
+ end
+
+ def with_locales(*locales, &block)
+ locales.flatten.map do |locale|
+ with_locale(locale, &block)
+ end
+ end
+
+ def fallbacks?
+ I18n.respond_to?(:fallbacks)
+ end
+
+ def fallbacks(locale = self.locale)
+ fallbacks? ? I18n.fallbacks[locale] : [locale.to_sym]
+ end
+
+ protected
+ def read_locale
+ Thread.current[:globalize_locale]
+ end
+
+ def set_locale(locale)
+ Thread.current[:globalize_locale] = locale.to_sym rescue nil
+ end
+ end
+ #
+ #module HasManyExtensions
+ # def find_or_initialize_by_locale(locale)
+ # with_locale(locale.to_s).first || build(:locale => locale.to_s)
+ # end
+ #end
+end
@@ -0,0 +1,23 @@
+module Mongoid::Globalize
+ module ActMacro
+ # TODO: other interface of +translates+. Like this:
+ # translates do
+ # field :title
+ # field :visible, type: Boolean
+ # end
+ def translates(*attr_names_or_hashes)
+ attr_hash = attr_names_or_hashes.inject({}) do |hash,attr|
+ hash.merge(attr.is_a?(Hash) ? attr : {attr => String})
+ end
+ attr_hash.each do |name, type|
+ self.translated_attribute_names.push name.to_sym
+ translated_attr_accessor(name)
+ translation_class.field name, type: type
+ end
+ end
+
+ def fallbacks_for_empty_translations!
+ self.fallbacks_for_empty_translations = true
+ end
+ end
+end
@@ -0,0 +1,73 @@
+module Mongoid::Globalize
+ class Adapter
+ attr_accessor :record, :stash, :translations
+ private :record=, :stash=
+
+ def initialize(record)
+ self.record = record
+ self.stash = Attributes.new
+ end
+
+ def fetch_stash(locale, name)
+ value = stash.read(locale, name)
+ return value if value
+ return nil
+ end
+
+ def fetch(locale, name)
+ Mongoid::Globalize.fallbacks(locale).each do |fallback|
+ value = fetch_stash(fallback, name) || fetch_attribute(fallback, name)
+ unless fallbacks_for?(value)
+ set_metadata(value, :locale => fallback, :requested_locale => locale)
+ return value
+ end
+ end
+ return nil
+ end
+
+ def write(locale, name, value)
+ stash.write(locale, name, value)
+ end
+
+ def save_translations!
+ stash.each do |locale, attrs|
+ translation = record.translations.find_or_initialize_by_locale(locale.to_s)
+ attrs.each{ |name, value| translation[name] = value }
+ translation.save!
+ end
+ # TODO: it's actual for mongoid?
+ #record.translations.reset
+ reset
+ end
+
+ def prepare_translations!
+ stash.each do |locale, attrs|
+ translation = record.translations.find_or_initialize_by_locale(locale.to_s)
+ attrs.each{ |name, value| translation[name] = value }
+ end
+ end
+
+ def reset
+ stash.clear
+ end
+
+ protected
+ def fetch_attribute(locale, name)
+ translation = record.translation_for(locale)
+ return translation && translation.send(name)
+ end
+
+ def set_metadata(object, metadata)
+ object.translation_metadata.merge!(meta_data) if object.respond_to?(:translation_metadata)
+ object
+ end
+
+ def fallbacks_for?(object)
+ object.nil? || (fallbacks_for_empty_translations? && object.blank?)
+ end
+
+ def fallbacks_for_empty_translations?
+ record.fallbacks_for_empty_translations
+ end
+ end
+end
@@ -0,0 +1,25 @@
+# Helper class for storing values per locale. Used by Mongoid::Globalize::Adapter
+# to stash and cache attribute values.
+
+module Mongoid::Globalize
+ class Attributes < Hash # TODO: Think about using HashWithIndifferentAccess ?
+ def [](locale)
+ locale = locale.to_sym
+ self[locale] = {} unless has_key?(locale)
+ self.fetch(locale)
+ end
+
+ def contains?(locale, name)
+ self[locale].has_key?(name.to_s)
+ end
+
+ def read(locale, name)
+ self[locale][name.to_s]
+ end
+
+ def write(locale, name, value)
+ #raise 'z' if value.nil? # TODO
+ self[locale][name.to_s] = value
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit 413fa45

Please sign in to comment.