i18n-active_record 0.0.2 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 41a58e172e0e92291488fed6cf32df0733b630a1
4
+ data.tar.gz: 2faedc974053410c6a6b3d1d047e844e505f2e7f
5
+ SHA512:
6
+ metadata.gz: ffec88d68503d22c9ddf403f69240bdc57a30e7a27f21f7dcc37d3fafb9a302da95aa96ad30eec20c9e638bdb7708001afcf43eac5b55bd5adaad274b160b851
7
+ data.tar.gz: f1bb77a80450ae7252e539d7d1146eef06c92a30f5f072c5b59dc871a0d7e90aefdbd3a1b7a9710cd4b9cbb340afdb03596bdbd8396fc2a8c63c5c46c133a64b
@@ -43,20 +43,27 @@ To load @I18n::Backend::ActiveRecord@ into your Rails application, create a new
43
43
  A simple configuration for your locale.rb could look like this:
44
44
 
45
45
  <pre>
46
- I18n.backend = I18n::Backend::Database.new
46
+ require 'i18n/backend/active_record'
47
+ I18n.backend = I18n::Backend::ActiveRecord.new
47
48
  </pre>
48
49
 
49
- A more adavanced example (Thanks Moritz), which uses YAML files and ActiveRecord for lookups:
50
+ A more advanced example (Thanks Moritz), which uses YAML files and ActiveRecord for lookups:
50
51
 
51
52
  <pre>
52
- I18n.backend = I18n::Backend::ActiveRecord.new
53
-
54
- I18n::Backend::ActiveRecord.send(:include, I18n::Backend::Memoize)
55
- I18n::Backend::ActiveRecord.send(:include, I18n::Backend::Flatten)
56
- I18n::Backend::Simple.send(:include, I18n::Backend::Memoize)
57
- I18n::Backend::Simple.send(:include, I18n::Backend::Pluralization)
58
-
59
- I18n.backend = I18n::Backend::Chain.new(I18n::Backend::Simple.new, I18n.backend)
53
+ require 'i18n/backend/active_record'
54
+
55
+ Translation = I18n::Backend::ActiveRecord::Translation
56
+
57
+ if Translation.table_exists?
58
+ I18n.backend = I18n::Backend::ActiveRecord.new
59
+
60
+ I18n::Backend::ActiveRecord.send(:include, I18n::Backend::Memoize)
61
+ I18n::Backend::ActiveRecord.send(:include, I18n::Backend::Flatten)
62
+ I18n::Backend::Simple.send(:include, I18n::Backend::Memoize)
63
+ I18n::Backend::Simple.send(:include, I18n::Backend::Pluralization)
64
+
65
+ I18n.backend = I18n::Backend::Chain.new(I18n::Backend::Simple.new, I18n.backend)
66
+ end
60
67
  </pre>
61
68
 
62
69
  h2. Usage
data/Rakefile CHANGED
@@ -1,10 +1,43 @@
1
1
  require 'rake'
2
2
  require 'rake/testtask'
3
+ require 'bundler/gem_tasks'
3
4
 
4
- Rake::TestTask.new do |t|
5
- t.libs << 'lib'
5
+ def execute(command)
6
+ puts command
7
+ system command
8
+ end
9
+
10
+ namespace :bundle do
11
+ task :env do
12
+ db = ENV['DB'].to_s
13
+ db = 'sqlite' if db == ''
14
+ ar = ENV['AR'].to_s
15
+
16
+ next if db == 'sqlite' && ar.empty?
17
+
18
+ gemfile = 'gemfiles/Gemfile'
19
+ gemfile += ".rails_#{ar}" unless ar.empty?
20
+ gemfile += ".#{db}" unless db.empty?
21
+ raise "Cannot find gemfile at #{gemfile}" unless File.exist?(gemfile)
22
+
23
+ ENV['BUNDLE_GEMFILE'] = gemfile
24
+ end
25
+
26
+ task install: :env do
27
+ opt = ''
28
+ opt += "--gemfile #{ENV['BUNDLE_GEMFILE']}" if ENV['BUNDLE_GEMFILE']
29
+ execute "bundle install #{opt}"
30
+ end
31
+ end
32
+
33
+ Rake::TestTask.new :_test do |t|
34
+ t.libs << 'test'
6
35
  t.pattern = 'test/**/*_test.rb'
7
36
  t.verbose = false
8
37
  end
9
38
 
10
- task :default => :test
39
+ task test: 'bundle:env' do
40
+ Rake::Task['_test'].execute
41
+ end
42
+
43
+ task default: :test
@@ -1,3 +1 @@
1
1
  require 'i18n'
2
- require 'i18n/backend/active_record'
3
-
@@ -1,6 +1,5 @@
1
1
  module I18n
2
2
  module ActiveRecord
3
- VERSION = "0.0.2"
3
+ VERSION = '0.1.0'
4
4
  end
5
5
  end
6
-
@@ -31,7 +31,7 @@ module I18n
31
31
 
32
32
  def lookup(locale, key, scope = [], options = {})
33
33
  key = normalize_flat_keys(locale, key, scope, options[:separator])
34
- result = Translation.locale(locale).lookup(key).all
34
+ result = Translation.locale(locale).lookup(key)
35
35
 
36
36
  if result.empty?
37
37
  nil
@@ -41,7 +41,7 @@ module I18n
41
41
  key = normalize_flat_keys(locale, key, scope, separator)
42
42
 
43
43
  unless ActiveRecord::Translation.locale(locale).lookup(key).exists?
44
- interpolations = options.keys - Base::RESERVED_KEYS
44
+ interpolations = options.keys - I18n::RESERVED_KEYS
45
45
  keys = count ? I18n.t('i18n.plural.keys', :locale => locale).map { |k| [key, k].join(FLATTEN_SEPARATOR) } : [key]
46
46
  keys.each { |key| store_default_translation(locale, key, interpolations) }
47
47
  end
@@ -54,13 +54,15 @@ module I18n
54
54
  end
55
55
 
56
56
  def translate(locale, key, options = {})
57
- super
58
- rescue I18n::MissingTranslationData => e
59
- self.store_default_translations(locale, key, options)
60
- raise e
57
+ result = catch(:exception) { super }
58
+ if result.is_a?(I18n::MissingTranslation)
59
+ self.store_default_translations(locale, key, options)
60
+ throw(:exception, result)
61
+ else
62
+ result
63
+ end
61
64
  end
62
65
  end
63
66
  end
64
67
  end
65
68
  end
66
-
@@ -49,15 +49,14 @@ module I18n
49
49
  TRUTHY_CHAR = "\001"
50
50
  FALSY_CHAR = "\002"
51
51
 
52
- set_table_name 'translations'
53
- attr_protected :is_proc, :interpolations
52
+ self.table_name = 'translations'
54
53
 
55
54
  serialize :value
56
55
  serialize :interpolations, Array
57
56
 
58
57
  class << self
59
58
  def locale(locale)
60
- scoped(:conditions => { :locale => locale.to_s })
59
+ where(:locale => locale.to_s)
61
60
  end
62
61
 
63
62
  def lookup(keys, *separator)
@@ -70,11 +69,11 @@ module I18n
70
69
  end
71
70
 
72
71
  namespace = "#{keys.last}#{I18n::Backend::Flatten::FLATTEN_SEPARATOR}%"
73
- scoped(:conditions => ["#{column_name} IN (?) OR #{column_name} LIKE ?", keys, namespace])
72
+ where("#{column_name} IN (?) OR #{column_name} LIKE ?", keys, namespace)
74
73
  end
75
74
 
76
75
  def available_locales
77
- Translation.find(:all, :select => 'DISTINCT locale').map { |t| t.locale.to_sym }
76
+ Translation.select('DISTINCT locale').to_a.map { |t| t.locale.to_sym }
78
77
  end
79
78
  end
80
79
 
@@ -108,4 +107,3 @@ module I18n
108
107
  end
109
108
  end
110
109
  end
111
-
@@ -1,6 +1,6 @@
1
- require File.expand_path('../test_helper', __FILE__)
1
+ require 'test_helper'
2
2
 
3
- class I18nBackendActiveRecordTest < Test::Unit::TestCase
3
+ class I18nBackendActiveRecordTest < I18n::TestCase
4
4
  def setup
5
5
  I18n.backend = I18n::Backend::ActiveRecord.new
6
6
  store_translations(:en, :foo => { :bar => 'bar', :baz => 'baz' })
@@ -40,15 +40,12 @@ class I18nBackendActiveRecordTest < Test::Unit::TestCase
40
40
  assert_equal "Pagina's", I18n.t(:"Pagina's", :locale => :es)
41
41
  end
42
42
 
43
- with_mocha do
44
- test "missing translations table does not cause an error in #available_locales" do
45
- I18n::Backend::ActiveRecord::Translation.expects(:available_locales).raises(::ActiveRecord::StatementInvalid)
46
- assert_equal [], I18n.backend.available_locales
47
- end
43
+ test "missing translations table does not cause an error in #available_locales" do
44
+ I18n::Backend::ActiveRecord::Translation.expects(:available_locales).raises(::ActiveRecord::StatementInvalid, 'msg')
45
+ assert_equal [], I18n.backend.available_locales
48
46
  end
49
47
 
50
- def test_expand_keys
48
+ test "expand_keys" do
51
49
  assert_equal %w(foo foo.bar foo.bar.baz), I18n.backend.send(:expand_keys, :'foo.bar.baz')
52
50
  end
53
- end if defined?(ActiveRecord)
54
-
51
+ end
@@ -1,6 +1,6 @@
1
- require File.expand_path('../test_helper', __FILE__)
1
+ require 'test_helper'
2
2
 
3
- class I18nActiveRecordApiTest < Test::Unit::TestCase
3
+ class I18nActiveRecordApiTest < I18n::TestCase
4
4
  def setup
5
5
  I18n.backend = I18n::Backend::ActiveRecord.new
6
6
  super
@@ -26,5 +26,4 @@ class I18nActiveRecordApiTest < Test::Unit::TestCase
26
26
  test "make sure we use an ActiveRecord backend" do
27
27
  assert_equal I18n::Backend::ActiveRecord, I18n.backend.class
28
28
  end
29
- end if defined?(ActiveRecord)
30
-
29
+ end
@@ -1,6 +1,6 @@
1
- require File.expand_path('../test_helper', __FILE__)
1
+ require 'test_helper'
2
2
 
3
- class I18nActiveRecordMissingTest < Test::Unit::TestCase
3
+ class I18nActiveRecordMissingTest < I18n::TestCase
4
4
  class Backend < I18n::Backend::ActiveRecord
5
5
  include I18n::Backend::ActiveRecord::Missing
6
6
  end
@@ -20,6 +20,7 @@ class I18nActiveRecordMissingTest < Test::Unit::TestCase
20
20
 
21
21
  test "lookup persists the key" do
22
22
  I18n.t('foo.bar.baz')
23
+
23
24
  assert_equal 1, I18n::Backend::ActiveRecord::Translation.count
24
25
  assert I18n::Backend::ActiveRecord::Translation.locale(:en).find_by_key('foo.bar.baz')
25
26
  end
@@ -38,7 +39,7 @@ class I18nActiveRecordMissingTest < Test::Unit::TestCase
38
39
 
39
40
  test "creates one stub per pluralization" do
40
41
  I18n.t('foo', :count => 999)
41
- translations = I18n::Backend::ActiveRecord::Translation.locale(:en).find_all_by_key %w{ foo.zero foo.one foo.other }
42
+ translations = I18n::Backend::ActiveRecord::Translation.locale(:en).where key: %w{ foo.zero foo.one foo.other }
42
43
  assert_equal 3, translations.length
43
44
  end
44
45
 
@@ -50,22 +51,20 @@ class I18nActiveRecordMissingTest < Test::Unit::TestCase
50
51
 
51
52
  test "creates a stub when a custom separator is used" do
52
53
  I18n.t('foo|baz', :separator => '|')
53
- I18n::Backend::ActiveRecord::Translation.locale(:en).lookup("foo.baz").first.update_attributes!(:value => 'baz!')
54
+ I18n::Backend::ActiveRecord::Translation.locale(:en).lookup("foo.baz").first.update_attributes(:value => 'baz!')
54
55
  assert_equal 'baz!', I18n.t('foo|baz', :separator => '|')
55
56
  end
56
57
 
57
58
  test "creates a stub per pluralization when a custom separator is used" do
58
59
  I18n.t('foo|bar', :count => 999, :separator => '|')
59
- translations = I18n::Backend::ActiveRecord::Translation.locale(:en).find_all_by_key %w{ foo.bar.zero foo.bar.one foo.bar.other }
60
+ translations = I18n::Backend::ActiveRecord::Translation.locale(:en).where key: %w{ foo.bar.zero foo.bar.one foo.bar.other }
60
61
  assert_equal 3, translations.length
61
62
  end
62
63
 
63
64
  test "creates a stub when a custom separator is used and the key contains the flatten separator (a dot character)" do
64
65
  key = 'foo|baz.zab'
65
66
  I18n.t(key, :separator => '|')
66
- I18n::Backend::ActiveRecord::Translation.locale(:en).lookup("foo.baz\001zab").first.update_attributes!(:value => 'baz!')
67
+ I18n::Backend::ActiveRecord::Translation.locale(:en).lookup("foo.baz\001zab").first.update_attributes(:value => 'baz!')
67
68
  assert_equal 'baz!', I18n.t(key, :separator => '|')
68
69
  end
69
-
70
- end if defined?(ActiveRecord)
71
-
70
+ end
@@ -1,36 +1,59 @@
1
- require 'test_setup'
1
+ $KCODE = 'u' if RUBY_VERSION <= '1.9'
2
2
 
3
- I18n::Tests.parse_options!
4
3
  require 'bundler/setup'
5
- $:.unshift File.expand_path("../lib", File.dirname(__FILE__))
4
+ require 'minitest/autorun'
5
+ require 'mocha/setup'
6
+ require 'test_declarative'
7
+
6
8
  require 'i18n/active_record'
7
9
  require 'i18n/tests'
8
- require 'mocha'
9
- I18n::Tests.setup_active_record
10
-
11
- class Test::Unit::TestCase
12
- def self.test(name, &block)
13
- test_name = "test_#{name.gsub(/\s+/,'_')}".to_sym
14
- defined = instance_method(test_name) rescue false
15
- raise "#{test_name} is already defined in #{self}" if defined
16
- if block_given?
17
- define_method(test_name, &block)
18
- else
19
- define_method(test_name) do
20
- flunk "No implementation provided for #{name}"
21
- end
10
+
11
+ gemfile = ENV['BUNDLE_GEMFILE'] || 'Gemfile'
12
+
13
+ begin
14
+ require 'active_record'
15
+ ::ActiveRecord::Base.connection
16
+ rescue LoadError => e
17
+ puts "can't use ActiveRecord backend because: #{e.message}"
18
+ rescue ::ActiveRecord::ConnectionNotEstablished
19
+ require 'i18n/backend/active_record'
20
+ if gemfile.end_with? 'postgres'
21
+ ::ActiveRecord::Base.establish_connection adapter: 'postgresql', database: 'i18n_unittest', username: 'i18n', password: '', host: 'localhost'
22
+ elsif gemfile.end_with? 'mysql'
23
+ else
24
+ ::ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
25
+ end
26
+ ::ActiveRecord::Migration.verbose = false
27
+ ::ActiveRecord::Schema.define(:version => 1) do
28
+ create_table :translations, :force => true do |t|
29
+ t.string :locale
30
+ t.string :key
31
+ t.text :value
32
+ t.text :interpolations
33
+ t.boolean :is_proc, :default => false
22
34
  end
35
+ add_index :translations, [:locale, :key], :unique => true
23
36
  end
37
+ end
24
38
 
25
- def self.with_mocha
26
- yield if Object.respond_to?(:expects)
39
+ TEST_CASE = defined?(Minitest::Test) ? Minitest::Test : MiniTest::Unit::TestCase
40
+
41
+ class TEST_CASE
42
+ alias :assert_raise :assert_raises
43
+ alias :assert_not_equal :refute_equal
44
+
45
+ def assert_nothing_raised(*args)
46
+ yield
27
47
  end
48
+ end
28
49
 
50
+ class I18n::TestCase < TEST_CASE
29
51
  def teardown
30
- I18n.locale = nil
52
+ I18n.enforce_available_locales = false
53
+ I18n.available_locales = []
54
+ I18n.locale = :en
31
55
  I18n.default_locale = :en
32
56
  I18n.load_path = []
33
- I18n.available_locales = nil
34
57
  I18n.backend = nil
35
58
  end
36
59
 
@@ -48,9 +71,3 @@ class Test::Unit::TestCase
48
71
  File.dirname(__FILE__) + '/test_data/locales'
49
72
  end
50
73
  end
51
-
52
- Object.class_eval do
53
- def meta_class
54
- class << self; self; end
55
- end
56
- end unless Object.method_defined?(:meta_class)
metadata CHANGED
@@ -1,98 +1,85 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: i18n-active_record
3
- version: !ruby/object:Gem::Version
4
- hash: 27
5
- prerelease: false
6
- segments:
7
- - 0
8
- - 0
9
- - 2
10
- version: 0.0.2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
11
5
  platform: ruby
12
- authors:
6
+ authors:
13
7
  - Sven Fuchs
14
8
  autorequire:
15
9
  bindir: bin
16
10
  cert_chain: []
17
-
18
- date: 2010-12-27 00:00:00 +01:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
11
+ date: 2016-03-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
22
14
  name: i18n
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
27
17
  - - ">="
28
- - !ruby/object:Gem::Version
29
- hash: 11
30
- segments:
31
- - 0
32
- - 5
33
- - 0
18
+ - !ruby/object:Gem::Version
34
19
  version: 0.5.0
35
20
  type: :runtime
36
- version_requirements: *id001
37
- description: "[description]"
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.5.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: I18n ActiveRecord backend. Allows to store translations in a database
42
+ using ActiveRecord, e.g. for providing a web-interface for managing translations.
38
43
  email: svenfuchs@artweb-design.de
39
44
  executables: []
40
-
41
45
  extensions: []
42
-
43
46
  extra_rdoc_files: []
44
-
45
- files:
46
- - ci/Gemfile.rails-3.x
47
- - ci/Gemfile.rails-3.x.lock
48
- - lib/i18n/active_record/version.rb
47
+ files:
48
+ - MIT-LICENSE
49
+ - README.textile
50
+ - Rakefile
49
51
  - lib/i18n/active_record.rb
52
+ - lib/i18n/active_record/version.rb
53
+ - lib/i18n/backend/active_record.rb
50
54
  - lib/i18n/backend/active_record/missing.rb
51
55
  - lib/i18n/backend/active_record/store_procs.rb
52
56
  - lib/i18n/backend/active_record/translation.rb
53
- - lib/i18n/backend/active_record.rb
54
57
  - test/active_record_test.rb
55
- - test/all.rb
56
58
  - test/api_test.rb
57
59
  - test/missing_test.rb
58
60
  - test/test_helper.rb
59
- - test/test_setup.rb
60
- - MIT-LICENSE
61
- - README.textile
62
- - Rakefile
63
- has_rdoc: true
64
61
  homepage: http://github.com/svenfuchs/i18n-active_record
65
- licenses: []
66
-
62
+ licenses:
63
+ - MIT
64
+ metadata: {}
67
65
  post_install_message:
68
66
  rdoc_options: []
69
-
70
- require_paths:
67
+ require_paths:
71
68
  - lib
72
- required_ruby_version: !ruby/object:Gem::Requirement
73
- none: false
74
- requirements:
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
75
71
  - - ">="
76
- - !ruby/object:Gem::Version
77
- hash: 3
78
- segments:
79
- - 0
80
- version: "0"
81
- required_rubygems_version: !ruby/object:Gem::Requirement
82
- none: false
83
- requirements:
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
84
76
  - - ">="
85
- - !ruby/object:Gem::Version
86
- hash: 3
87
- segments:
88
- - 0
89
- version: "0"
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
90
79
  requirements: []
91
-
92
80
  rubyforge_project: "[none]"
93
- rubygems_version: 1.3.7
81
+ rubygems_version: 2.5.2
94
82
  signing_key:
95
- specification_version: 3
96
- summary: "[summary]"
83
+ specification_version: 4
84
+ summary: I18n ActiveRecord backend
97
85
  test_files: []
98
-
@@ -1,10 +0,0 @@
1
- source :rubygems
2
-
3
- # gem 'i18n', '~> 0.5.0'
4
- gem 'i18n', :path => '~/Development/projects/i18n/i18n'
5
-
6
- gem 'activerecord', '~> 3.0.0'
7
- gem 'activesupport', '~> 3.0.0'
8
- gem 'sqlite3-ruby'
9
- gem 'mocha'
10
-
@@ -1,36 +0,0 @@
1
- PATH
2
- remote: ~/Development/projects/i18n/i18n
3
- specs:
4
- i18n (0.4.2)
5
-
6
- GEM
7
- remote: http://rubygems.org/
8
- specs:
9
- activemodel (3.0.1)
10
- activesupport (= 3.0.1)
11
- builder (~> 2.1.2)
12
- i18n (~> 0.4.1)
13
- activerecord (3.0.1)
14
- activemodel (= 3.0.1)
15
- activesupport (= 3.0.1)
16
- arel (~> 1.0.0)
17
- tzinfo (~> 0.3.23)
18
- activesupport (3.0.1)
19
- arel (1.0.1)
20
- activesupport (~> 3.0.0)
21
- builder (2.1.2)
22
- mocha (0.9.9)
23
- rake
24
- rake (0.8.7)
25
- sqlite3-ruby (1.3.1)
26
- tzinfo (0.3.23)
27
-
28
- PLATFORMS
29
- ruby
30
-
31
- DEPENDENCIES
32
- activerecord (~> 3.0.0)
33
- activesupport (~> 3.0.0)
34
- i18n!
35
- mocha
36
- sqlite3-ruby
@@ -1,7 +0,0 @@
1
- dir = File.dirname(__FILE__)
2
- $:.unshift(dir)
3
-
4
- Dir["#{dir}/**/*_test.rb"].sort.each do |file|
5
- require file.sub(/^#{dir}\/(.*)\.rb$/, '\1')
6
- end
7
-
@@ -1,92 +0,0 @@
1
- $KCODE = 'u' if RUBY_VERSION <= '1.9'
2
-
3
- require 'rubygems'
4
- require 'test/unit'
5
- require 'optparse'
6
-
7
- # Do not load the i18n gem from libraries like active_support.
8
- #
9
- # This is required for testing against Rails 2.3 because active_support/vendor.rb#24 tries
10
- # to load I18n using the gem method. Instead, we want to test the local library of course.
11
- alias :gem_for_ruby_19 :gem # for 1.9. gives a super ugly seg fault otherwise
12
- def gem(gem_name, *version_requirements)
13
- puts("skipping loading the i18n gem ...") && return if gem_name =='i18n'
14
- super(gem_name, *version_requirements)
15
- end
16
-
17
- module I18n
18
- module Tests
19
- class << self
20
- def options
21
- @options ||= { :with => [], :adapter => 'sqlite3' }
22
- end
23
-
24
- def parse_options!
25
- OptionParser.new do |o|
26
- o.on('-w', '--with DEPENDENCIES', 'Define dependencies') do |dep|
27
- options[:with] = dep.split(',').map { |group| group.to_sym }
28
- end
29
- end.parse!
30
-
31
- options[:with].each do |dep|
32
- case dep
33
- when :sqlite3, :mysql, :postgres
34
- @options[:adapter] = dep
35
- when :r23, :'rails-2.3.x'
36
- ENV['BUNDLE_GEMFILE'] = 'ci/Gemfile.rails-2.3.x'
37
- when :r3, :'rails-3.0.x'
38
- ENV['BUNDLE_GEMFILE'] = 'ci/Gemfile.rails-3.x'
39
- when :'no-rails'
40
- ENV['BUNDLE_GEMFILE'] = 'ci/Gemfile.no-rails'
41
- end
42
- end
43
-
44
- ENV['BUNDLE_GEMFILE'] ||= 'ci/Gemfile.all'
45
- end
46
-
47
- def setup_active_record
48
- begin
49
- require 'active_record'
50
- ActiveRecord::Base.connection
51
- true
52
- rescue LoadError => e
53
- puts "can't use ActiveRecord backend because: #{e.message}"
54
- rescue ActiveRecord::ConnectionNotEstablished
55
- require 'i18n/backend/active_record'
56
- require 'i18n/backend/active_record/store_procs'
57
- connect_active_record
58
- true
59
- end
60
- end
61
-
62
- def connect_active_record
63
- connect_adapter
64
- ActiveRecord::Migration.verbose = false
65
- ActiveRecord::Schema.define(:version => 1) do
66
- create_table :translations, :force => true do |t|
67
- t.string :locale
68
- t.string :key
69
- t.text :value
70
- t.text :interpolations
71
- t.boolean :is_proc, :default => false
72
- end
73
- add_index :translations, [:locale, :key], :unique => true
74
- end
75
- end
76
-
77
- def connect_adapter
78
- case options[:adapter].to_sym
79
- when :sqlite3
80
- ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
81
- when :mysql
82
- # CREATE DATABASE i18n_unittest;
83
- # CREATE USER 'i18n'@'localhost' IDENTIFIED BY '';
84
- # GRANT ALL PRIVILEGES ON i18n_unittest.* to 'i18n'@'localhost';
85
- ActiveRecord::Base.establish_connection(:adapter => "mysql", :database => "i18n_unittest", :username => "i18n", :password => "", :host => "localhost")
86
- end
87
- end
88
- end
89
- end
90
- end
91
-
92
-