hstore_translate 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -35,12 +35,23 @@ module HstoreTranslate
35
35
 
36
36
  protected
37
37
 
38
+ def hstore_translate_fallback_locales(locale)
39
+ return if !!@disable_fallback || !I18n.respond_to?(:fallbacks)
40
+ I18n.fallbacks[locale]
41
+ end
42
+
38
43
  def read_hstore_translation(attr_name, locale = I18n.locale)
39
44
  translations = send("#{attr_name}_translations") || {}
40
45
  translation = translations[locale.to_s]
41
46
 
42
- if (@disable_fallback == nil || @disable_fallback == false) && translation.nil? && I18n.respond_to?(:fallbacks) && (fallbacks = I18n.fallbacks[locale])
43
- fallbacks.find { |f| translation = translations[f.to_s] }
47
+ if fallback_locales = hstore_translate_fallback_locales(locale)
48
+ fallback_locales.each do |fallback_locale|
49
+ t = translations[fallback_locale.to_s]
50
+ if t && !t.empty? # differs from blank?
51
+ translation = t
52
+ break
53
+ end
54
+ end
44
55
  end
45
56
 
46
57
  translation
@@ -1,3 +1,3 @@
1
1
  module HstoreTranslate
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.9"
3
3
  end
@@ -1,6 +1,6 @@
1
- require "active_record/connection_adapters/postgresql_adapter"
2
- require "hstore_translate/translates"
3
- require "hstore_translate/version"
1
+ require 'active_record'
2
+ require 'active_record/connection_adapters/postgresql_adapter'
3
+ require 'hstore_translate/translates'
4
4
 
5
5
  module HstoreTranslate
6
6
  def self.native_hstore?
data/test/database.yml ADDED
@@ -0,0 +1,6 @@
1
+ test:
2
+ adapter: postgresql
3
+ host: localhost
4
+ database: hstore_translate_test
5
+ username: postgres
6
+ min_messages: warning
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+ gemspec :path => './../..'
3
+
4
+ gem 'activerecord', '~> 3.1.0'
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+ gemspec :path => './../..'
3
+
4
+ gem 'activerecord', '~> 3.2.0'
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+ gemspec :path => './../..'
3
+
4
+ gem 'activerecord', '~> 4.0.0'
@@ -0,0 +1,69 @@
1
+ require 'minitest/autorun'
2
+ require 'hstore_translate'
3
+
4
+ require 'database_cleaner'
5
+ DatabaseCleaner.strategy = :transaction
6
+
7
+ class Post < ActiveRecord::Base
8
+ translates :title
9
+ end
10
+
11
+ class HstoreTranslate::Test < MiniTest::Unit::TestCase
12
+ class << self
13
+ def prepare_database
14
+ create_database
15
+ create_table
16
+ end
17
+
18
+ private
19
+
20
+ def db_config
21
+ @db_config ||= begin
22
+ filepath = File.join('test', 'database.yml')
23
+ YAML.load_file(filepath)['test']
24
+ end
25
+ end
26
+
27
+ def establish_connection(config)
28
+ ActiveRecord::Base.establish_connection(config)
29
+ ActiveRecord::Base.connection
30
+ end
31
+
32
+ def create_database
33
+ system_config = db_config.merge('database' => 'postgres', 'schema_search_path' => 'public')
34
+ connection = establish_connection(system_config)
35
+ connection.create_database(db_config['database']) rescue nil
36
+ enable_extension
37
+ end
38
+
39
+ def enable_extension
40
+ connection = establish_connection(db_config)
41
+ unless connection.select_value("SELECT proname FROM pg_proc WHERE proname = 'akeys'")
42
+ if connection.send(:postgresql_version) < 90100
43
+ pg_sharedir = `pg_config --sharedir`.strip
44
+ hstore_script_path = File.join(pg_sharedir, 'contrib', 'hstore.sql')
45
+ connection.execute(File.read(hstore_script_path))
46
+ else
47
+ connection.execute('CREATE EXTENSION IF NOT EXISTS hstore')
48
+ end
49
+ end
50
+ end
51
+
52
+ def create_table
53
+ connection = establish_connection(db_config)
54
+ connection.create_table(:posts, :force => true) do |t|
55
+ t.column :title_translations, 'hstore'
56
+ end
57
+ end
58
+ end
59
+
60
+ prepare_database
61
+
62
+ def setup
63
+ DatabaseCleaner.start
64
+ end
65
+
66
+ def teardown
67
+ DatabaseCleaner.clean
68
+ end
69
+ end
@@ -0,0 +1,115 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'test_helper'
3
+
4
+ class TranslatesTest < HstoreTranslate::Test
5
+ def test_assigns_in_current_locale
6
+ I18n.with_locale(:en) do
7
+ p = Post.new(:title => "English Title")
8
+ assert_equal("English Title", p.title_translations['en'])
9
+ end
10
+ end
11
+
12
+ def test_retrieves_in_current_locale
13
+ p = Post.new(:title_translations => { "en" => "English Title", "fr" => "Titre français" })
14
+ I18n.with_locale(:fr) do
15
+ assert_equal("Titre français", p.title)
16
+ end
17
+ end
18
+
19
+ def test_retrieves_in_current_locale_with_fallbacks
20
+ I18n::Backend::Simple.include(I18n::Backend::Fallbacks)
21
+ I18n.default_locale = :"en-US"
22
+
23
+ p = Post.new(:title_translations => {"en" => "English Title"})
24
+ I18n.with_locale(:fr) do
25
+ assert_equal("English Title", p.title)
26
+ end
27
+ end
28
+
29
+ def test_assigns_in_specified_locale
30
+ I18n.with_locale(:en) do
31
+ p = Post.new(:title_translations => { "en" => "English Title" })
32
+ p.title_fr = "Titre français"
33
+ assert_equal("Titre français", p.title_translations["fr"])
34
+ end
35
+ end
36
+
37
+ def test_persists_changes_in_specified_locale
38
+ I18n.with_locale(:en) do
39
+ p = Post.create!(:title_translations => { "en" => "Original Text" })
40
+ p.title_en = "Updated Text"
41
+ p.save!
42
+ assert_equal("Updated Text", Post.last.title_en)
43
+ end
44
+ end
45
+
46
+ def test_retrieves_in_specified_locale
47
+ I18n.with_locale(:en) do
48
+ p = Post.new(:title_translations => { "en" => "English Title", "fr" => "Titre français" })
49
+ assert_equal("Titre français", p.title_fr)
50
+ end
51
+ end
52
+
53
+ def test_retrieves_in_specified_locale_with_fallbacks
54
+ I18n::Backend::Simple.include(I18n::Backend::Fallbacks)
55
+ I18n.default_locale = :"en-US"
56
+
57
+ p = Post.new(:title_translations => { "en" => "English Title" })
58
+ I18n.with_locale(:fr) do
59
+ assert_equal("English Title", p.title_fr)
60
+ end
61
+ end
62
+
63
+ def test_fallback_from_empty_string
64
+ I18n::Backend::Simple.include(I18n::Backend::Fallbacks)
65
+ I18n.default_locale = :"en-US"
66
+
67
+ p = Post.new(:title_translations => { "en" => "English Title", "fr" => "" })
68
+ I18n.with_locale(:fr) do
69
+ assert_equal("English Title", p.title_fr)
70
+ end
71
+ end
72
+
73
+ def test_retrieves_in_specified_locale_with_fallback_disabled
74
+ I18n::Backend::Simple.include(I18n::Backend::Fallbacks)
75
+ I18n.default_locale = :"en-US"
76
+
77
+ p = Post.new(:title_translations => { "en" => "English Title" })
78
+ p.disable_fallback
79
+ I18n.with_locale(:fr) do
80
+ assert_equal(nil, p.title_fr)
81
+ end
82
+ end
83
+
84
+ def test_retrieves_in_specified_locale_with_fallback_reenabled
85
+ I18n::Backend::Simple.include(I18n::Backend::Fallbacks)
86
+ I18n.default_locale = :"en-US"
87
+
88
+ p = Post.new(:title_translations => { "en" => "English Title" })
89
+ p.disable_fallback
90
+ p.enable_fallback
91
+ I18n.with_locale(:fr) do
92
+ assert_equal("English Title", p.title_fr)
93
+ end
94
+ end
95
+
96
+ def test_method_missing_delegates
97
+ assert_raises(NoMethodError) { Post.new.nonexistant_method }
98
+ end
99
+
100
+ def test_method_missing_delegates_non_translated_attributes
101
+ assert_raises(NoMethodError) { Post.new.other_fr }
102
+ end
103
+
104
+ def test_persists_translations_assigned_as_hash
105
+ p = Post.create!(:title_translations => { "en" => "English Title", "fr" => "Titre français" })
106
+ p.reload
107
+ assert_equal({"en" => "English Title", "fr" => "Titre français"}, p.title_translations)
108
+ end
109
+
110
+ def test_persists_translations_assigned_to_localized_accessors
111
+ p = Post.create!(:title_en => "English Title", :title_fr => "Titre français")
112
+ p.reload
113
+ assert_equal({"en" => "English Title", "fr" => "Titre français"}, p.title_translations)
114
+ end
115
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hstore_translate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,16 +9,16 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-15 00:00:00.000000000 Z
12
+ date: 2013-08-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: activerecord
15
+ name: pg
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
- version: 3.1.0
21
+ version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
@@ -26,15 +26,15 @@ dependencies:
26
26
  requirements:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
- version: 3.1.0
29
+ version: '0'
30
30
  - !ruby/object:Gem::Dependency
31
- name: pg
31
+ name: activerecord
32
32
  requirement: !ruby/object:Gem::Requirement
33
33
  none: false
34
34
  requirements:
35
35
  - - ! '>='
36
36
  - !ruby/object:Gem::Version
37
- version: '0'
37
+ version: 3.1.0
38
38
  type: :runtime
39
39
  prerelease: false
40
40
  version_requirements: !ruby/object:Gem::Requirement
@@ -42,7 +42,7 @@ dependencies:
42
42
  requirements:
43
43
  - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
- version: '0'
45
+ version: 3.1.0
46
46
  - !ruby/object:Gem::Dependency
47
47
  name: activerecord-postgres-hstore
48
48
  requirement: !ruby/object:Gem::Requirement
@@ -75,6 +75,38 @@ dependencies:
75
75
  - - ! '>='
76
76
  - !ruby/object:Gem::Version
77
77
  version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: minitest
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: '4.0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: '4.0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: database_cleaner
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
78
110
  description: Rails I18n library for ActiveRecord model/data translation using PostgreSQL's
79
111
  hstore datatype. Translations are stored directly in the model table rather than
80
112
  shadow tables.
@@ -86,11 +118,14 @@ files:
86
118
  - lib/hstore_translate/translates.rb
87
119
  - lib/hstore_translate/version.rb
88
120
  - lib/hstore_translate.rb
89
- - Gemfile
90
- - Gemfile.lock
91
- - MIT-LICENSE
92
- - Rakefile
93
121
  - README.md
122
+ - MIT-LICENSE
123
+ - test/database.yml
124
+ - test/gemfiles/Gemfile.rails-3.1.x
125
+ - test/gemfiles/Gemfile.rails-3.2.x
126
+ - test/gemfiles/Gemfile.rails-4.0.x
127
+ - test/test_helper.rb
128
+ - test/translates_test.rb
94
129
  homepage: https://github.com/robworley/hstore_translate
95
130
  licenses: []
96
131
  post_install_message:
@@ -110,11 +145,17 @@ required_rubygems_version: !ruby/object:Gem::Requirement
110
145
  - !ruby/object:Gem::Version
111
146
  version: '0'
112
147
  requirements: []
113
- rubyforge_project: ! '[none]'
148
+ rubyforge_project:
114
149
  rubygems_version: 1.8.24
115
150
  signing_key:
116
151
  specification_version: 3
117
152
  summary: Rails I18n library for ActiveRecord model/data translation using PostgreSQL's
118
153
  hstore datatype.
119
- test_files: []
154
+ test_files:
155
+ - test/database.yml
156
+ - test/gemfiles/Gemfile.rails-3.1.x
157
+ - test/gemfiles/Gemfile.rails-3.2.x
158
+ - test/gemfiles/Gemfile.rails-4.0.x
159
+ - test/test_helper.rb
160
+ - test/translates_test.rb
120
161
  has_rdoc:
data/Gemfile DELETED
@@ -1,8 +0,0 @@
1
- source "http://rubygems.org"
2
-
3
- if ENV["EDGE_RAILS"]
4
- gem 'pg'
5
- gem 'rails', :git => 'git://github.com/rails/rails'
6
- else
7
- gemspec
8
- end
data/Gemfile.lock DELETED
@@ -1,49 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- hstore_translate (0.0.8)
5
- activerecord (= 4.0.0.rc2)
6
- activerecord-postgres-hstore (~> 0.7.0)
7
- pg
8
-
9
- GEM
10
- remote: http://rubygems.org/
11
- specs:
12
- activemodel (4.0.0.rc2)
13
- activesupport (= 4.0.0.rc2)
14
- builder (~> 3.1.0)
15
- activerecord (4.0.0.rc2)
16
- activemodel (= 4.0.0.rc2)
17
- activerecord-deprecated_finders (~> 1.0.2)
18
- activesupport (= 4.0.0.rc2)
19
- arel (~> 4.0.0)
20
- activerecord-deprecated_finders (1.0.2)
21
- activerecord-postgres-hstore (0.7.6)
22
- activerecord (>= 3.1)
23
- pg-hstore (>= 1.1.5)
24
- rake
25
- activesupport (4.0.0.rc2)
26
- i18n (~> 0.6, >= 0.6.4)
27
- minitest (~> 4.2)
28
- multi_json (~> 1.3)
29
- thread_safe (~> 0.1)
30
- tzinfo (~> 0.3.37)
31
- arel (4.0.0)
32
- atomic (1.1.9)
33
- builder (3.1.4)
34
- i18n (0.6.4)
35
- minitest (4.7.4)
36
- multi_json (1.7.6)
37
- pg (0.15.1)
38
- pg-hstore (1.1.7)
39
- rake (10.0.3)
40
- thread_safe (0.1.0)
41
- atomic
42
- tzinfo (0.3.37)
43
-
44
- PLATFORMS
45
- ruby
46
-
47
- DEPENDENCIES
48
- hstore_translate!
49
- rake
data/Rakefile DELETED
@@ -1,12 +0,0 @@
1
- require 'rake'
2
- require 'rake/testtask'
3
-
4
- desc 'Default: run unit tests.'
5
- task :default => :test
6
-
7
- desc 'Test the ltree_hierarchy plugin.'
8
- Rake::TestTask.new(:test) do |t|
9
- t.libs << 'lib'
10
- t.pattern = 'test/**/*_test.rb'
11
- t.verbose = true
12
- end