i18n-active_record 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 41a58e172e0e92291488fed6cf32df0733b630a1
4
- data.tar.gz: 2faedc974053410c6a6b3d1d047e844e505f2e7f
3
+ metadata.gz: 6e3b0e3d5e5bfff8a2405a2aea617cf46a3b3b84
4
+ data.tar.gz: 80efc0144976cfc26a73bf2c75a627a6ee7d8ddc
5
5
  SHA512:
6
- metadata.gz: ffec88d68503d22c9ddf403f69240bdc57a30e7a27f21f7dcc37d3fafb9a302da95aa96ad30eec20c9e638bdb7708001afcf43eac5b55bd5adaad274b160b851
7
- data.tar.gz: f1bb77a80450ae7252e539d7d1146eef06c92a30f5f072c5b59dc871a0d7e90aefdbd3a1b7a9710cd4b9cbb340afdb03596bdbd8396fc2a8c63c5c46c133a64b
6
+ metadata.gz: a36a02163e4c98ec21995a567ee48a5bc27c9701590ae317d9e1d5deeefc7481b8190c36fce62a1314f42bdd174bb2561ebe1d64e043f9db3cb859e015840715
7
+ data.tar.gz: af48a542fc5d59c5c11e6885f994f60a0729253c48cd10a6a08fa7961c3f3c26f1a41eb65a9d06f8f713eed1bcb5d3169ce867f9227ff1efe39a63a008552548
@@ -1,15 +1,16 @@
1
1
  h1. I18n::Backend::ActiveRecord
2
2
 
3
+ !https://travis-ci.org/svenfuchs/i18n-active_record.svg?branch=master!:https://travis-ci.org/svenfuchs/i18n-active_record
4
+
3
5
  This repository contains the I18n ActiveRecord backend and support code that has been extracted from the "I18n":http://github.com/svenfuchs/i18n.
6
+ It is fully compatible with Rails 3, 4 and 5
4
7
 
5
8
  h2. Installation
6
9
 
7
10
  For Bundler put the following in your Gemfile:
8
11
 
9
12
  <pre>
10
- gem 'i18n-active_record',
11
- :git => 'git://github.com/svenfuchs/i18n-active_record.git',
12
- :require => 'i18n/active_record'
13
+ gem 'i18n-active_record', :require => 'i18n/active_record'
13
14
  </pre>
14
15
 
15
16
  Next create a active record model named @Translation@ with the Rails Generator.
@@ -51,14 +52,13 @@ A more advanced example (Thanks Moritz), which uses YAML files and ActiveRecord
51
52
 
52
53
  <pre>
53
54
  require 'i18n/backend/active_record'
54
-
55
+
55
56
  Translation = I18n::Backend::ActiveRecord::Translation
56
-
57
+
57
58
  if Translation.table_exists?
58
59
  I18n.backend = I18n::Backend::ActiveRecord.new
59
60
 
60
61
  I18n::Backend::ActiveRecord.send(:include, I18n::Backend::Memoize)
61
- I18n::Backend::ActiveRecord.send(:include, I18n::Backend::Flatten)
62
62
  I18n::Backend::Simple.send(:include, I18n::Backend::Memoize)
63
63
  I18n::Backend::Simple.send(:include, I18n::Backend::Pluralization)
64
64
 
@@ -66,12 +66,23 @@ A more advanced example (Thanks Moritz), which uses YAML files and ActiveRecord
66
66
  end
67
67
  </pre>
68
68
 
69
+ You may also configure whether the ActiveRecord backend should use @destroy@ or @delete@ when cleaning up internally.
70
+
71
+ <pre>
72
+ I18n::Backend::ActiveRecord.configure do |config|
73
+ config.cleanup_with_destroy = true # defaults to false
74
+ end
75
+ </pre>
76
+
69
77
  h2. Usage
70
78
 
71
79
  You can now use @I18n.t('Your String')@ to lookup translations in the database.
72
80
 
73
- h2. Maintainers
81
+ h2. Examples
74
82
 
75
- * Sven Fuchs
83
+ * http://collectiveidea.com/blog/archives/2016/05/31/beyond-yml-files-dynamic-translations/
76
84
 
85
+ h2. Maintainers
77
86
 
87
+ * Sven Fuchs
88
+ * Tim Maslyuchenko
data/Rakefile CHANGED
@@ -7,27 +7,42 @@ def execute(command)
7
7
  system command
8
8
  end
9
9
 
10
+ def bundle_options
11
+ opt = ''
12
+ opt += "--gemfile #{ENV['BUNDLE_GEMFILE']}" if ENV['BUNDLE_GEMFILE']
13
+ end
14
+
15
+ def each_database(&block)
16
+ ['sqlite', 'postgres', 'mysql'].each &block
17
+ end
18
+
10
19
  namespace :bundle do
11
20
  task :env do
12
- db = ENV['DB'].to_s
13
- db = 'sqlite' if db == ''
14
21
  ar = ENV['AR'].to_s
15
22
 
16
- next if db == 'sqlite' && ar.empty?
23
+ next if ar.empty?
17
24
 
18
- gemfile = 'gemfiles/Gemfile'
19
- gemfile += ".rails_#{ar}" unless ar.empty?
20
- gemfile += ".#{db}" unless db.empty?
25
+ gemfile = "gemfiles/Gemfile.rails_#{ar}"
21
26
  raise "Cannot find gemfile at #{gemfile}" unless File.exist?(gemfile)
22
27
 
23
28
  ENV['BUNDLE_GEMFILE'] = gemfile
29
+ puts "Using gemfile: #{gemfile}"
24
30
  end
25
31
 
26
32
  task install: :env do
27
- opt = ''
28
- opt += "--gemfile #{ENV['BUNDLE_GEMFILE']}" if ENV['BUNDLE_GEMFILE']
29
- execute "bundle install #{opt}"
33
+ execute "bundle install #{bundle_options}"
30
34
  end
35
+
36
+ task :install_all do
37
+ [nil, '3', '4', 'master'].each do |ar|
38
+ opt = ar && "AR=#{ar}"
39
+ execute "rake bundle:install #{opt}"
40
+ end
41
+ end
42
+ end
43
+
44
+ task :test do
45
+ each_database { |db| execute "rake #{db}:test" }
31
46
  end
32
47
 
33
48
  Rake::TestTask.new :_test do |t|
@@ -36,8 +51,11 @@ Rake::TestTask.new :_test do |t|
36
51
  t.verbose = false
37
52
  end
38
53
 
39
- task test: 'bundle:env' do
40
- Rake::Task['_test'].execute
54
+ each_database do |db|
55
+ namespace db do
56
+ task(:env) { ENV['DB'] = db }
57
+ task test: ['env', 'bundle:env', '_test']
58
+ end
41
59
  end
42
60
 
43
61
  task default: :test
@@ -1,5 +1,5 @@
1
1
  module I18n
2
2
  module ActiveRecord
3
- VERSION = '0.1.0'
3
+ VERSION = '0.1.1'
4
4
  end
5
5
  end
@@ -4,9 +4,20 @@ require 'i18n/backend/active_record/translation'
4
4
  module I18n
5
5
  module Backend
6
6
  class ActiveRecord
7
- autoload :Missing, 'i18n/backend/active_record/missing'
8
- autoload :StoreProcs, 'i18n/backend/active_record/store_procs'
9
- autoload :Translation, 'i18n/backend/active_record/translation'
7
+ autoload :Missing, 'i18n/backend/active_record/missing'
8
+ autoload :StoreProcs, 'i18n/backend/active_record/store_procs'
9
+ autoload :Translation, 'i18n/backend/active_record/translation'
10
+ autoload :Configuration, 'i18n/backend/active_record/configuration'
11
+
12
+ class << self
13
+ def configure
14
+ yield(config) if block_given?
15
+ end
16
+
17
+ def config
18
+ @config ||= Configuration.new
19
+ end
20
+ end
10
21
 
11
22
  module Implementation
12
23
  include Base, Flatten
@@ -22,7 +33,14 @@ module I18n
22
33
  def store_translations(locale, data, options = {})
23
34
  escape = options.fetch(:escape, true)
24
35
  flatten_translations(locale, data, escape, false).each do |key, value|
25
- Translation.locale(locale).lookup(expand_keys(key)).delete_all
36
+ translation = Translation.locale(locale).lookup(expand_keys(key))
37
+
38
+ if ActiveRecord.config.cleanup_with_destroy
39
+ translation.destroy_all
40
+ else
41
+ translation.delete_all
42
+ end
43
+
26
44
  Translation.create(:locale => locale.to_s, :key => key.to_s, :value => value)
27
45
  end
28
46
  end
@@ -59,4 +77,3 @@ module I18n
59
77
  end
60
78
  end
61
79
  end
62
-
@@ -0,0 +1,13 @@
1
+ module I18n
2
+ module Backend
3
+ class ActiveRecord
4
+ class Configuration
5
+ attr_accessor :cleanup_with_destroy
6
+
7
+ def initialize
8
+ @cleanup_with_destroy = false
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -49,6 +49,8 @@ module I18n
49
49
  TRUTHY_CHAR = "\001"
50
50
  FALSY_CHAR = "\002"
51
51
 
52
+ self.logger = nil
53
+
52
54
  self.table_name = 'translations'
53
55
 
54
56
  serialize :value
@@ -8,6 +8,7 @@ class I18nBackendActiveRecordTest < I18n::TestCase
8
8
 
9
9
  def teardown
10
10
  I18n::Backend::ActiveRecord::Translation.destroy_all
11
+ I18n::Backend::ActiveRecord.instance_variable_set :@config, I18n::Backend::ActiveRecord::Configuration.new
11
12
  super
12
13
  end
13
14
 
@@ -48,4 +49,31 @@ class I18nBackendActiveRecordTest < I18n::TestCase
48
49
  test "expand_keys" do
49
50
  assert_equal %w(foo foo.bar foo.bar.baz), I18n.backend.send(:expand_keys, :'foo.bar.baz')
50
51
  end
52
+
53
+ test "available_locales returns uniq locales" do
54
+ I18n::Backend::ActiveRecord::Translation.delete_all
55
+ I18n.backend.store_translations(:en, :foo => { :bar => 'bar' })
56
+ I18n.backend.store_translations(:en, :foo => { :baz => 'baz' })
57
+ I18n.backend.store_translations(:de, :foo1 => 'foo')
58
+ I18n.backend.store_translations(:de, :foo2 => 'foo')
59
+ I18n.backend.store_translations(:uk, :foo3 => 'foo')
60
+
61
+ available_locales = I18n::Backend::ActiveRecord::Translation.available_locales
62
+ assert_equal 3, available_locales.size
63
+ assert_includes available_locales, :en
64
+ assert_includes available_locales, :de
65
+ assert_includes available_locales, :uk
66
+ end
67
+
68
+ test "the default configuration has cleanup_with_destroy == false" do
69
+ refute I18n::Backend::ActiveRecord.config.cleanup_with_destroy
70
+ end
71
+
72
+ test "the configuration supports cleanup_with_destroy being set" do
73
+ I18n::Backend::ActiveRecord.configure do |config|
74
+ config.cleanup_with_destroy = true
75
+ end
76
+
77
+ assert I18n::Backend::ActiveRecord.config.cleanup_with_destroy
78
+ end
51
79
  end
@@ -8,8 +8,6 @@ require 'test_declarative'
8
8
  require 'i18n/active_record'
9
9
  require 'i18n/tests'
10
10
 
11
- gemfile = ENV['BUNDLE_GEMFILE'] || 'Gemfile'
12
-
13
11
  begin
14
12
  require 'active_record'
15
13
  ::ActiveRecord::Base.connection
@@ -17,9 +15,11 @@ rescue LoadError => e
17
15
  puts "can't use ActiveRecord backend because: #{e.message}"
18
16
  rescue ::ActiveRecord::ConnectionNotEstablished
19
17
  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'
18
+ case ENV['DB']
19
+ when 'postgres'
20
+ ::ActiveRecord::Base.establish_connection adapter: 'postgresql', database: 'i18n_unittest', username: ENV['PG_USER'] || 'i18n', password: '', host: 'localhost'
21
+ when 'mysql'
22
+ ::ActiveRecord::Base.establish_connection adapter: 'mysql', database: 'i18n_unittest', username: 'root', password: '', host: 'localhost'
23
23
  else
24
24
  ::ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
25
25
  end
@@ -48,6 +48,15 @@ class TEST_CASE
48
48
  end
49
49
 
50
50
  class I18n::TestCase < TEST_CASE
51
+ def setup
52
+ I18n.enforce_available_locales = false
53
+ I18n.available_locales = []
54
+ I18n.locale = :en
55
+ I18n.default_locale = :en
56
+ I18n.load_path = []
57
+ super
58
+ end
59
+
51
60
  def teardown
52
61
  I18n.enforce_available_locales = false
53
62
  I18n.available_locales = []
@@ -55,15 +64,14 @@ class I18n::TestCase < TEST_CASE
55
64
  I18n.default_locale = :en
56
65
  I18n.load_path = []
57
66
  I18n.backend = nil
67
+ super
58
68
  end
59
69
 
60
70
  def translations
61
71
  I18n.backend.instance_variable_get(:@translations)
62
72
  end
63
73
 
64
- def store_translations(*args)
65
- data = args.pop
66
- locale = args.pop || :en
74
+ def store_translations(locale, data)
67
75
  I18n.backend.store_translations(locale, data)
68
76
  end
69
77
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: i18n-active_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sven Fuchs
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-17 00:00:00.000000000 Z
11
+ date: 2016-08-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: i18n
@@ -51,6 +51,7 @@ files:
51
51
  - lib/i18n/active_record.rb
52
52
  - lib/i18n/active_record/version.rb
53
53
  - lib/i18n/backend/active_record.rb
54
+ - lib/i18n/backend/active_record/configuration.rb
54
55
  - lib/i18n/backend/active_record/missing.rb
55
56
  - lib/i18n/backend/active_record/store_procs.rb
56
57
  - lib/i18n/backend/active_record/translation.rb