Permalink
Browse files

drugi commit

  • Loading branch information...
1 parent c5bc8a8 commit 55aaaebd09f4f60bc2b535831da0fdda7268301c @pkurek pkurek committed Jul 26, 2011
Showing with 2,710 additions and 0 deletions.
  1. +9 −0 .gitignore
  2. +2 −0 Gemfile
  3. +21 −0 LICENSE
  4. +180 −0 README.textile
  5. +22 −0 Rakefile
  6. +36 −0 globalize3.gemspec
  7. +1 −0 init.rb
  8. +52 −0 lib/globalize.rb
  9. +12 −0 lib/globalize/active_record.rb
  10. +54 −0 lib/globalize/active_record/act_macro.rb
  11. +97 −0 lib/globalize/active_record/adapter.rb
  12. +27 −0 lib/globalize/active_record/attributes.rb
  13. +121 −0 lib/globalize/active_record/class_methods.rb
  14. +19 −0 lib/globalize/active_record/exceptions.rb
  15. +160 −0 lib/globalize/active_record/instance_methods.rb
  16. +125 −0 lib/globalize/active_record/migration.rb
  17. +35 −0 lib/globalize/active_record/translation.rb
  18. +5 −0 lib/globalize/versioning.rb
  19. +41 −0 lib/globalize/versioning/paper_trail.rb
  20. +1 −0 lib/globalize3.rb
  21. +3 −0 lib/globalize3/version.rb
  22. +41 −0 lib/i18n/missing_translations_log_handler.rb
  23. +25 −0 lib/i18n/missing_translations_raise_handler.rb
  24. +35 −0 lib/patches/active_record/query_method.rb
  25. +13 −0 lib/patches/active_record/xml_attribute_serializer.rb
  26. +1 −0 test/all.rb
  27. +68 −0 test/data/models.rb
  28. +108 −0 test/data/schema.rb
  29. +133 −0 test/globalize3/attributes_test.rb
  30. +58 −0 test/globalize3/clone_test.rb
  31. +61 −0 test/globalize3/dirty_tracking_test.rb
  32. +171 −0 test/globalize3/dynamic_finders_test.rb
  33. +146 −0 test/globalize3/fallbacks_test.rb
  34. +81 −0 test/globalize3/locale_test.rb
  35. +156 −0 test/globalize3/migration_test.rb
  36. +54 −0 test/globalize3/set_translations_test.rb
  37. +59 −0 test/globalize3/translation_class_test.rb
  38. +92 −0 test/globalize3/validations_test.rb
  39. +87 −0 test/globalize3/versioning_test.rb
  40. +159 −0 test/globalize3_test.rb
  41. +35 −0 test/i18n/missing_translations_test.rb
  42. +104 −0 test/test_helper.rb
View
@@ -0,0 +1,9 @@
+doc
+spec/spec/db/*
+vendor
+NOTES
+.DS_Store
+.bundle
+Gemfile.lock
+.rvmrc
+*.sw*
View
@@ -0,0 +1,2 @@
+source :rubygems
+gemspec
View
21 LICENSE
@@ -0,0 +1,21 @@
+The MIT License
+
+Copyright (c) 2008, 2009, 2010 Sven Fuchs, Joshua Harvey, Clemens Kofler, John-Paul Bader
+
+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.
View
@@ -0,0 +1,180 @@
+h1. Globalize3
+
+Globalize3 is the successor of Globalize for Rails. Globalize is targeted at ActiveRecord 3. It is compatible with and builds on the new "I18n API in Ruby on Rails":http://guides.rubyonrails.org/i18n.html and adds model translations to ActiveRecord.
+
+Globalize3 is much more lightweight and compatible than its predecessor Globalize for Rails was. Model translations in Globalize3 use default ActiveRecord features and do not limit any ActiveRecord functionality any more.
+
+h2. Requirements
+
+ActiveRecord > 3.0.0.rc
+I18n
+
+h2. Installation
+
+To install Globalize3 with its default setup just use:
+
+<pre><code>
+$ gem install globalize3
+</code></pre>
+
+h2. Model translations
+
+Model translations allow you to translate your models' attribute values. E.g.
+
+<pre><code>
+class Post < ActiveRecord::Base
+ translates :title, :text
+end
+</code></pre>
+
+Allows you to translate the attributes :title and :text per locale:
+
+<pre><code>
+I18n.locale = :en
+post.title # => Globalize3 rocks!
+
+I18n.locale = :he
+post.title # => גלובאלייז2 שולט!
+</code></pre>
+
+In order to make this work, you'll need to add the appropriate translation tables. Globalize3 comes with a handy helper method to help you do this. It's called @create_translation_table!@. Here's an example:
+
+<pre><code>
+class CreatePosts < ActiveRecord::Migration
+ def self.up
+ create_table :posts do |t|
+ t.timestamps
+ end
+ Post.create_translation_table! :title => :string, :text => :text
+ end
+ def self.down
+ drop_table :posts
+ Post.drop_translation_table!
+ end
+end
+</code></pre>
+
+Note that the ActiveRecord model @Post@ must already exist and have a @translates@ directive listing the translated fields.
+
+h2. Migrating existing data to and from the translated version
+
+As well as creating a translation table, you can also use @create_translation_table!@ to migrate across any
+existing data to the default locale. This can also operate in reverse to restore any translations from the default locale
+back to the model when you don't want to use a translation table anymore using @drop_translation_table!@
+
+This feature makes use of @untranslated_attributes@ which allows access to the model's attributes as they were before
+the translation was applied. Here's an example (which assumes you already have a model called @Post@ and its table exists):
+
+<pre><code>
+ class TranslatePosts < ActiveRecord::Migration
+ def self.up
+ Post.create_translation_table!({
+ :title => :string,
+ :text => :text
+ }, {
+ :migrate_data => true
+ })
+ end
+ def self.down
+ Post.drop_translation_table! :migrate_data => true
+ end
+ end
+</code></pre>
+
+h2. Versioning with Globalize3
+
+Globalize3 nicely integrates with "vestal_versions":http://github.com/laserlemon/vestal_versions:
+
+<pre><code>
+require 'globalize/versioning/vestal_versions'
+</code></pre>
+
+As of writing (2010-08-05) the original vestal_versions respository has not been updated to be compatible with Rails 3 though. You can use "this fork":http://github.com/svenfuchs/vestal_versions though. "Globalize3's Gemfile":http://github.com/svenfuchs/globalize3/blob/master/Gemfile#L10 is currently set up accordingly.
+
+Please also note that @update_attribute@ currently hides itself from dirty tracking in ActiveRecord >= 3.0.0.beta (which is considered a "regression":http://github.com/rails/rails/commit/01629d180468049d17a8be6900e27a4f0d2b18c4#commitcomment-123199). That means that you currently need to use attribute writers or @update_attributes@ in order to track changes/versions for your models.
+
+Also, please see the tests in test/globalize3/versioning_test.rb for some current gotchas.
+
+h2. I18n fallbacks for empty translations
+
+It is possible to enable fallbacks for empty translations. It will depend on the configuration setting you have set for I18n translations in your Rails config.
+
+You can enable them by adding the next line to @config/application.rb@ (or only @config/environments/production.rb@ if you only want them in production)
+
+<pre><code>config.i18n.fallbacks = true</code></pre>
+
+By default, globalize3 will only use fallbacks when your translation model does not exist or the translation value for the item you've requested is @nil@. However it is possible to also use fallbacks for @blank@ translations by adding @:fallbacks_for_empty_translations => true@ to the @translates@ method.
+
+<pre><code>
+ class Post < ActiveRecord::Base
+ translates :title, :name
+ end
+
+ puts post.translations.inspect
+ # => [#<Post::Translation id: 1, post_id: 1, locale: "en", title: "Globalize3 rocks!", name: "Globalize3">, #<Post::Translation id: 2, post_id: 1, locale: "nl", title: '', name: nil>]
+
+ I18n.locale = :en
+ post.title # => 'Globalize3 rocks!'
+ post.name # => 'Globalize3'
+
+ I18n.locale = :nl
+ post.title # => ''
+ post.name # => 'Globalize3'
+</code></pre>
+<pre><code>
+ class Post < ActiveRecord::Base
+ translates :title, :name, :fallbacks_for_empty_translations => true
+ end
+
+ puts post.translations.inspect
+ # => [#<Post::Translation id: 1, post_id: 1, locale: "en", title: "Globalize3 rocks!", name: "Globalize3">, #<Post::Translation id: 2, post_id: 1, locale: "nl", title: '', name: nil>]
+
+ I18n.locale = :en
+ post.title # => 'Globalize3 rocks!'
+ post.name # => 'Globalize3'
+
+ I18n.locale = :nl
+ post.title # => 'Globalize3 rocks!'
+ post.name # => 'Globalize3'
+</code></pre>
+
+
+h2. Scoping objects by those with translations
+
+To only return objects that have a translation for the given locale we can use the `with_translations` scope. This will only return records that have a translations for the passed in locale.
+
+<pre><code>
+
+Post.with_translations('en') # => [#<Post::Translation id: 1, post_id: 1, locale: "en", title: "Globalize3 rocks!", name: "Globalize3">, #<Post::Translation id: 2, post_id: 1, locale: "nl", title: '', name: nil>]
+
+Post.with_translations(I18n.locale) # => [#<Post::Translation id: 1, post_id: 1, locale: "en", title: "Globalize3 rocks!", name: "Globalize3">, #<Post::Translation id: 2, post_id: 1, locale: "nl", title: '', name: nil>]
+
+Post.with_translations('de') # => []
+
+</code></pre>
+
+h2. Changes since Globalize2
+
+* `translation_table_name` was renamed to `translations_table_name`
+* `available_locales` has been removed. please use `translated_locales`
+
+h2. Migration from Globalize for Rails (version 1)
+
+See this script by Tomasz Stachewicz: http://gist.github.com/120867
+
+h2. Alternative Solutions
+
+* "Veger's fork":http://github.com/veger/globalize2 - uses default AR schema for the default locale, delegates to the translations table for other locales only
+* "TranslatableColumns":http://github.com/iain/translatable_columns - have multiple languages of the same attribute in a model (Iain Hecker)
+* "localized_record":http://github.com/glennpow/localized_record - allows records to have localized attributes without any modifications to the database (Glenn Powell)
+* "model_translations":http://github.com/janne/model_translations - Minimal implementation of Globalize2 style model translations (Jan Andersson)
+
+h2. Related solutions
+
+* "globalize2_versioning":http://github.com/joshmh/globalize2_versioning - acts_as_versioned style versioning for globalize2 (Joshua Harvey)
+* "i18n_multi_locales_validations":http://github.com/ZenCocoon/i18n_multi_locales_validations - multi-locales attributes validations to validates attributes from globalize2 translations models (Sébastien Grosjean)
+* "globalize2 Demo App":http://github.com/svenfuchs/globalize2-demo - demo application for globalize2 (Sven Fuchs)</li>
+* "migrate_from_globalize1":http://gist.github.com/120867 - migrate model translations from Globalize1 to globalize2 (Tomasz Stachewicz)</li>
+* "easy_globalize2_accessors":http://github.com/astropanic/easy_globalize2_accessors - easily access (read and write) globalize2-translated fields (astropanic, Tomasz Stachewicz)</li>
+* "globalize2-easy-translate":http://github.com/bsamman/globalize2-easy-translate - adds methods to easily access or set translated attributes to your model (bsamman)</li>
+* "batch_translations":http://github.com/alvarezrilla/batch_translations - allow saving multiple globalize2 translations in the same request (Jose Alvarez Rilla)</li>
View
@@ -0,0 +1,22 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Run all tests.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'Globalize3'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
View
@@ -0,0 +1,36 @@
+$: << File.expand_path('../lib', __FILE__)
+
+require 'globalize3/version'
+
+Gem::Specification.new do |s|
+ s.name = 'globalize3'
+ s.version = Globalize3::VERSION
+ s.authors = ['Sven Fuchs', 'Joshua Harvey', 'Clemens Kofler', 'John-Paul Bader']
+ s.email = 'nobody@globalize-rails.org'
+ s.homepage = 'http://github.com/svenfuchs/globalize3'
+ s.summary = 'Rails I18n: de-facto standard library for ActiveRecord 3 model/data translation'
+ s.description = "#{s.summary}."
+
+ s.files = Dir['{lib/**/*,[A-Z]*}']
+ s.platform = Gem::Platform::RUBY
+ s.require_path = 'lib'
+ s.rubyforge_project = '[none]'
+
+ s.add_dependency 'activerecord', '>= 3.0.0'
+ s.add_dependency 'activemodel', '>= 3.0.0'
+ s.add_dependency 'paper_trail', '~> 2'
+
+ s.add_development_dependency 'database_cleaner', '0.5.2'
+ s.add_development_dependency 'mocha'
+ s.add_development_dependency 'pathname_local'
+ s.add_development_dependency 'test_declarative'
+
+ case ENV["RUBY_VERSION"]
+ when /1\.8\.\d.+$/
+ s.add_development_dependency 'ruby-debug'
+ when /1\.9\.\d.+$/
+ s.add_development_dependency 'ruby-debug19'
+ end
+
+ s.add_development_dependency 'sqlite3-ruby'
+end
View
@@ -0,0 +1 @@
+require 'globalize'
View
@@ -0,0 +1,52 @@
+require 'active_record'
+require 'patches/active_record/xml_attribute_serializer'
+require 'patches/active_record/query_method'
+
+module Globalize
+ autoload :ActiveRecord, 'globalize/active_record'
+ autoload :Versioning, 'globalize/versioning'
+
+ 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
+end
+
+ActiveRecord::Base.extend(Globalize::ActiveRecord::ActMacro)
@@ -0,0 +1,12 @@
+module Globalize
+ module ActiveRecord
+ autoload :ActMacro, 'globalize/active_record/act_macro'
+ autoload :Adapter, 'globalize/active_record/adapter'
+ autoload :Attributes, 'globalize/active_record/attributes'
+ autoload :ClassMethods, 'globalize/active_record/class_methods'
+ autoload :Exceptions, 'globalize/active_record/exceptions'
+ autoload :InstanceMethods, 'globalize/active_record/instance_methods'
+ autoload :Migration, 'globalize/active_record/migration'
+ autoload :Translation, 'globalize/active_record/translation'
+ end
+end
Oops, something went wrong.

0 comments on commit 55aaaeb

Please sign in to comment.