Permalink
Browse files

extract Backend::ActiveRecord to a separate gem

  • Loading branch information...
1 parent 2913ff9 commit 197dacebad356b910d69fa69a719c2ad10cf49e6 Sven Fuchs committed Nov 5, 2010
View
@@ -1,6 +1,5 @@
source :rubygems
-gem 'activerecord', '~> 3.0.0'
gem 'activesupport', '~> 3.0.0'
gem 'sqlite3-ruby'
gem 'mocha'
View
@@ -1,32 +1,16 @@
GEM
remote: http://rubygems.org/
specs:
- ParseTree (3.0.5)
+ ParseTree (3.0.6)
RubyInline (>= 3.7.0)
sexp_processor (>= 3.0.0)
- RubyInline (3.7.0)
- hoe (>= 1.5.3)
- activemodel (3.0.1)
- activesupport (= 3.0.1)
- builder (~> 2.1.2)
- i18n (~> 0.4.1)
- activerecord (3.0.1)
- activemodel (= 3.0.1)
- activesupport (= 3.0.1)
- arel (~> 1.0.0)
- tzinfo (~> 0.3.23)
+ RubyInline (3.8.6)
+ ZenTest (~> 4.3)
+ ZenTest (4.4.0)
activesupport (3.0.1)
- arel (1.0.1)
- activesupport (~> 3.0.0)
- builder (2.1.2)
ffi (0.6.3)
rake (>= 0.8.7)
- hoe (2.6.1)
- rake (>= 0.8.7)
- rubyforge (>= 2.0.4)
- i18n (0.4.2)
- json_pure (1.4.6)
- mocha (0.9.8)
+ mocha (0.9.9)
rake
rake (0.8.7)
ruby-cldr (0.0.1)
@@ -35,19 +19,15 @@ GEM
sexp_processor (~> 3.0)
ruby_parser (2.0.5)
sexp_processor (~> 3.0)
- rubyforge (2.0.4)
- json_pure (>= 1.1.7)
rufus-tokyo (1.0.7)
sexp_processor (3.0.5)
- sqlite3-ruby (1.3.1)
- tzinfo (0.3.23)
+ sqlite3-ruby (1.3.2)
PLATFORMS
ruby
DEPENDENCIES
ParseTree
- activerecord (~> 3.0.0)
activesupport (~> 3.0.0)
ffi
mocha
@@ -1,6 +1,5 @@
source :rubygems
-gem 'activerecord', '~> 2.3'
gem 'activesupport', '~> 2.3'
gem 'sqlite3-ruby'
gem 'mocha'
@@ -1,6 +1,5 @@
source :rubygems
-gem 'activerecord', '~> 3.0.0'
gem 'activesupport', '~> 3.0.0'
gem 'sqlite3-ruby'
gem 'mocha'
@@ -1,31 +1,16 @@
GEM
remote: http://rubygems.org/
specs:
- activemodel (3.0.1)
- activesupport (= 3.0.1)
- builder (~> 2.1.2)
- i18n (~> 0.4.1)
- activerecord (3.0.1)
- activemodel (= 3.0.1)
- activesupport (= 3.0.1)
- arel (~> 1.0.0)
- tzinfo (~> 0.3.23)
activesupport (3.0.1)
- arel (1.0.1)
- activesupport (~> 3.0.0)
- builder (2.1.2)
- i18n (0.4.1)
- mocha (0.9.8)
+ mocha (0.9.9)
rake
rake (0.8.7)
- sqlite3-ruby (1.3.1)
- tzinfo (0.3.23)
+ sqlite3-ruby (1.3.2)
PLATFORMS
ruby
DEPENDENCIES
- activerecord (~> 3.0.0)
activesupport (~> 3.0.0)
mocha
sqlite3-ruby
View
@@ -1,6 +1,5 @@
module I18n
module Backend
- autoload :ActiveRecord, 'i18n/backend/active_record'
autoload :Base, 'i18n/backend/base'
autoload :InterpolationCompiler, 'i18n/backend/interpolation_compiler'
autoload :Cache, 'i18n/backend/cache'
@@ -1,61 +0,0 @@
-require 'i18n/backend/base'
-require 'i18n/backend/active_record/translation'
-
-module I18n
- module Backend
- class ActiveRecord
- autoload :Missing, 'i18n/backend/active_record/missing'
- autoload :StoreProcs, 'i18n/backend/active_record/store_procs'
- autoload :Translation, 'i18n/backend/active_record/translation'
-
- module Implementation
- include Base, Flatten
-
- def available_locales
- begin
- Translation.available_locales
- rescue ::ActiveRecord::StatementInvalid
- []
- end
- end
-
- def store_translations(locale, data, options = {})
- escape = options.fetch(:escape, true)
- flatten_translations(locale, data, escape, false).each do |key, value|
- Translation.locale(locale).lookup(expand_keys(key)).delete_all
- Translation.create(:locale => locale.to_s, :key => key.to_s, :value => value)
- end
- end
-
- protected
-
- def lookup(locale, key, scope = [], options = {})
- key = normalize_flat_keys(locale, key, scope, options[:separator])
- result = Translation.locale(locale).lookup(key).all
-
- if result.empty?
- nil
- elsif result.first.key == key
- result.first.value
- else
- chop_range = (key.size + FLATTEN_SEPARATOR.size)..-1
- result = result.inject({}) do |hash, r|
- hash[r.key.slice(chop_range)] = r.value
- hash
- end
- result.deep_symbolize_keys
- end
- end
-
- # For a key :'foo.bar.baz' return ['foo', 'foo.bar', 'foo.bar.baz']
- def expand_keys(key)
- key.to_s.split(FLATTEN_SEPARATOR).inject([]) do |keys, key|
- keys << [keys.last, key].compact.join(FLATTEN_SEPARATOR)
- end
- end
- end
-
- include Implementation
- end
- end
-end
@@ -1,65 +0,0 @@
-# This extension stores translation stub records for missing translations to
-# the database.
-#
-# This is useful if you have a web based translation tool. It will populate
-# the database with untranslated keys as the application is being used. A
-# translator can then go through these and add missing translations.
-#
-# Example usage:
-#
-# I18n::Backend::Chain.send(:include, I18n::Backend::ActiveRecord::Missing)
-# I18n.backend = I18n::Backend::Chain.new(I18n::Backend::ActiveRecord.new, I18n::Backend::Simple.new)
-#
-# Stub records for pluralizations will also be created for each key defined
-# in i18n.plural.keys.
-#
-# For example:
-#
-# # en.yml
-# en:
-# i18n:
-# plural:
-# keys: [:zero, :one, :other]
-#
-# # pl.yml
-# pl:
-# i18n:
-# plural:
-# keys: [:zero, :one, :few, :other]
-#
-# It will also persist interpolation keys in Translation#interpolations so
-# translators will be able to review and use them.
-module I18n
- module Backend
- class ActiveRecord
- module Missing
- include Flatten
-
- def store_default_translations(locale, key, options = {})
- count, scope, default, separator = options.values_at(:count, :scope, :default, :separator)
- separator ||= I18n.default_separator
- key = normalize_flat_keys(locale, key, scope, separator)
-
- unless ActiveRecord::Translation.locale(locale).lookup(key).exists?
- interpolations = options.keys - Base::RESERVED_KEYS
- keys = count ? I18n.t('i18n.plural.keys', :locale => locale).map { |k| [key, k].join(FLATTEN_SEPARATOR) } : [key]
- keys.each { |key| store_default_translation(locale, key, interpolations) }
- end
- end
-
- def store_default_translation(locale, key, interpolations)
- translation = ActiveRecord::Translation.new :locale => locale.to_s, :key => key
- translation.interpolations = interpolations
- translation.save
- end
-
- def translate(locale, key, options = {})
- super
- rescue I18n::MissingTranslationData => e
- self.store_default_translations(locale, key, options)
- raise e
- end
- end
- end
- end
-end
@@ -1,38 +0,0 @@
-# This module is intended to be mixed into the ActiveRecord backend to allow
-# storing Ruby Procs as translation values in the database.
-#
-# I18n.backend = I18n::Backend::ActiveRecord.new
-# I18n::Backend::ActiveRecord::Translation.send(:include, I18n::Backend::ActiveRecord::StoreProcs)
-#
-# The StoreProcs module requires the ParseTree and ruby2ruby gems and therefor
-# was extracted from the original backend.
-#
-# ParseTree is not compatible with Ruby 1.9.
-
-begin
- require 'ruby2ruby'
- require 'parse_tree'
- require 'parse_tree_extensions'
-rescue LoadError => e
- puts "can't use StoreProcs because: #{e.message}"
-end
-
-module I18n
- module Backend
- class ActiveRecord
- module StoreProcs
- def value=(v)
- case v
- when Proc
- write_attribute(:value, v.to_ruby)
- write_attribute(:is_proc, true)
- else
- write_attribute(:value, v)
- end
- end
-
- Translation.send(:include, self) if method(:to_s).respond_to?(:to_ruby)
- end
- end
- end
-end
@@ -1,110 +0,0 @@
-require 'active_record'
-
-module I18n
- module Backend
- # ActiveRecord model used to store actual translations to the database.
- #
- # This model expects a table like the following to be already set up in
- # your the database:
- #
- # create_table :translations do |t|
- # t.string :locale
- # t.string :key
- # t.text :value
- # t.text :interpolations
- # t.boolean :is_proc, :default => false
- # end
- #
- # This model supports to named scopes :locale and :lookup. The :locale
- # scope simply adds a condition for a given locale:
- #
- # I18n::Backend::ActiveRecord::Translation.locale(:en).all
- # # => all translation records that belong to the :en locale
- #
- # The :lookup scope adds a condition for looking up all translations
- # that either start with the given keys (joined by an optionally given
- # separator or I18n.default_separator) or that exactly have this key.
- #
- # # with translations present for :"foo.bar" and :"foo.baz"
- # I18n::Backend::ActiveRecord::Translation.lookup(:foo)
- # # => an array with both translation records :"foo.bar" and :"foo.baz"
- #
- # I18n::Backend::ActiveRecord::Translation.lookup([:foo, :bar])
- # I18n::Backend::ActiveRecord::Translation.lookup(:"foo.bar")
- # # => an array with the translation record :"foo.bar"
- #
- # When the StoreProcs module was mixed into this model then Procs will
- # be stored to the database as Ruby code and evaluated when :value is
- # called.
- #
- # Translation = I18n::Backend::ActiveRecord::Translation
- # Translation.create \
- # :locale => 'en'
- # :key => 'foo'
- # :value => lambda { |key, options| 'FOO' }
- # Translation.find_by_locale_and_key('en', 'foo').value
- # # => 'FOO'
- class ActiveRecord
- class Translation < ::ActiveRecord::Base
- TRUTHY_CHAR = "\001"
- FALSY_CHAR = "\002"
-
- set_table_name 'translations'
- attr_protected :is_proc, :interpolations
-
- serialize :value
- serialize :interpolations, Array
-
- class << self
- def locale(locale)
- scoped(:conditions => { :locale => locale.to_s })
- end
-
- def lookup(keys, *separator)
- column_name = connection.quote_column_name('key')
- keys = Array(keys).map! { |key| key.to_s }
-
- unless separator.empty?
- warn "[DEPRECATION] Giving a separator to Translation.lookup is deprecated. " <<
- "You can change the internal separator by overwriting FLATTEN_SEPARATOR."
- end
-
- namespace = "#{keys.last}#{I18n::Backend::Flatten::FLATTEN_SEPARATOR}%"
- scoped(:conditions => ["#{column_name} IN (?) OR #{column_name} LIKE ?", keys, namespace])
- end
-
- def available_locales
- Translation.find(:all, :select => 'DISTINCT locale').map { |t| t.locale.to_sym }
- end
- end
-
- def interpolates?(key)
- self.interpolations.include?(key) if self.interpolations
- end
-
- def value
- value = read_attribute(:value)
- if is_proc
- Kernel.eval(value)
- elsif value == FALSY_CHAR
- false
- elsif value == TRUTHY_CHAR
- true
- else
- value
- end
- end
-
- def value=(value)
- if value === false
- value = FALSY_CHAR
- elsif value === true
- value = TRUTHY_CHAR
- end
-
- write_attribute(:value, value)
- end
- end
- end
- end
-end
Oops, something went wrong.

0 comments on commit 197dace

Please sign in to comment.