json_translate 4.0.0 → 4.0.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
- SHA1:
3
- metadata.gz: 18109ecd716fe83a685fba1c452278e24c292d88
4
- data.tar.gz: 23188ebe9a7a00efc712a3ee7945911eae0137bb
2
+ SHA256:
3
+ metadata.gz: 98c9caf9ca99da1bac3fdecdfa8c5249536b9f23fa7f16071efeddd7ad9cd97d
4
+ data.tar.gz: 13eb7ef35a9f5f322d5798c07cad3373bd7f403980a9edaeb0072e2a70350db2
5
5
  SHA512:
6
- metadata.gz: b38e84f20f8c74bd68cd8cf18149fdc54c55b5b514d491056b177c1cacc9372289235d11f4bc954395e25dbf7aa1d6eb533fe23d26fd01fa616c2e0724147c07
7
- data.tar.gz: bb93cce1c20f30bcc460e59c53f80fda46e6f5232ef4f7c43c7344dafc618a26b424bb60ac7320d9be1d5849e7bf1a13f007d23d869181408d35145f7d8d72ee
6
+ metadata.gz: cf87995f6e6295943f366be9e486f9c8abb2334c238d89d5cc7796e2e58a61e0f28b8b2e18172602de3dd312a0eda99adef29aa51f8a2bb172f6db3d638a358c
7
+ data.tar.gz: 2c0b99f0e3c2b02c5db293a6e8831fcea11a2afe74d0c8b3e0048f1ffc48962424db88c1e81320382845b73de4b90099c09baa2c9a6bb0d9cfb0a64f1b2a9db4
data/README.md CHANGED
@@ -145,3 +145,34 @@ post.enable_fallback do
145
145
  post.title_nl # => This database rocks!
146
146
  end
147
147
  ```
148
+
149
+ ## Enable blank value translations
150
+
151
+ By default, empty String values are not stored, instead the locale is deleted.
152
+
153
+ ```ruby
154
+ class Post < ActiveRecord::Base
155
+ translates :title
156
+ end
157
+
158
+ post.title_translations # => { en: 'Hello', fr: 'Bonjour' }
159
+ post.title_en = ""
160
+ post.title_translations # => { fr: 'Bonjour' }
161
+ ```
162
+
163
+ Activating `allow_blank: true` enables to store empty String values.
164
+ ```ruby
165
+ class Post < ActiveRecord::Base
166
+ translates :title, allow_blank: true
167
+ end
168
+
169
+ post.title_translations # => { en: 'Hello', fr: 'Bonjour' }
170
+ post.title_en = ""
171
+ post.title_translations # => { en: '', fr: 'Bonjour' }
172
+ ```
173
+
174
+ `nil` value delete the locale key/value anyway.
175
+ ```ruby
176
+ post.title_en = nil
177
+ post.title_translations # => { fr: 'Bonjour' }
178
+ ```
@@ -3,7 +3,7 @@ module JSONTranslate
3
3
  SUFFIX = "_translations".freeze
4
4
  MYSQL_ADAPTERS = %w[MySQL Mysql2 Mysql2Spatial]
5
5
 
6
- def translates(*attrs)
6
+ def translates(*attrs, allow_blank: false)
7
7
  include InstanceMethods
8
8
 
9
9
  class_attribute :translated_attribute_names, :permitted_translated_attributes
@@ -19,22 +19,22 @@ module JSONTranslate
19
19
 
20
20
  attrs.each do |attr_name|
21
21
  define_method attr_name do |**params|
22
- read_json_translation(attr_name, params)
22
+ read_json_translation(attr_name, **params)
23
23
  end
24
24
 
25
25
  define_method "#{attr_name}=" do |value|
26
- write_json_translation(attr_name, value)
26
+ write_json_translation(attr_name, value, allow_blank: allow_blank)
27
27
  end
28
28
 
29
29
  I18n.available_locales.each do |locale|
30
30
  normalized_locale = locale.to_s.downcase.gsub(/[^a-z]/, '')
31
31
 
32
32
  define_method :"#{attr_name}_#{normalized_locale}" do |**params|
33
- read_json_translation(attr_name, locale, false, params)
33
+ read_json_translation(attr_name, locale: locale, fallback: false, **params)
34
34
  end
35
35
 
36
36
  define_method "#{attr_name}_#{normalized_locale}=" do |value|
37
- write_json_translation(attr_name, value, locale)
37
+ write_json_translation(attr_name, value, locale: locale, allow_blank: allow_blank)
38
38
  end
39
39
  end
40
40
 
@@ -23,7 +23,7 @@ module JSONTranslate
23
23
  end
24
24
  end
25
25
 
26
- def read_json_translation(attr_name, locale = I18n.locale, fallback = true, **params)
26
+ def read_json_translation(attr_name, locale: I18n.locale, fallback: true, **params)
27
27
  translations = public_send("#{attr_name}#{SUFFIX}") || {}
28
28
 
29
29
  selected_locale = locale
@@ -45,11 +45,11 @@ module JSONTranslate
45
45
  translation
46
46
  end
47
47
 
48
- def write_json_translation(attr_name, value, locale = I18n.locale)
49
- value = value.presence
48
+ def write_json_translation(attr_name, value, locale: I18n.locale, allow_blank:)
49
+ value = allow_blank ? value : value.presence
50
50
  translation_store = "#{attr_name}#{SUFFIX}"
51
51
  translations = public_send(translation_store) || {}
52
- public_send("#{translation_store}_will_change!") unless translations[locale.to_s] == value
52
+ translation_store_will_change!(translation_store) unless translations[locale.to_s] == value
53
53
  if value
54
54
  translations[locale.to_s] = value
55
55
  else
@@ -59,6 +59,10 @@ module JSONTranslate
59
59
  value
60
60
  end
61
61
 
62
+ def translation_store_will_change!(translation_store)
63
+ public_send("#{translation_store}_will_change!")
64
+ end
65
+
62
66
  def toggle_fallback(enabled)
63
67
  if block_given?
64
68
  old_value = @enabled_fallback
@@ -1,3 +1,3 @@
1
1
  module JSONTranslate
2
- VERSION = "4.0.0"
2
+ VERSION = "4.0.1"
3
3
  end
@@ -1,6 +1,6 @@
1
1
  source 'https://rubygems.org'
2
2
  gemspec :path => './../..'
3
3
 
4
- gem 'pg', '< 1.0.0'
4
+ gem 'pg', '~> 1.0.0'
5
5
  gem 'mysql2'
6
6
  gem 'activerecord', '~> 5.0.0'
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+ gemspec :path => './../..'
3
+
4
+ gem 'pg'
5
+ gem 'mysql2'
6
+ gem 'activerecord'
data/test/test_helper.rb CHANGED
@@ -11,7 +11,7 @@ class Post < ActiveRecord::Base
11
11
  end
12
12
 
13
13
  class PostDetailed < Post
14
- translates :comment
14
+ translates :comment, allow_blank: true
15
15
  end
16
16
 
17
17
  class JSONTranslate::Test < Minitest::Test
@@ -40,8 +40,7 @@ class JSONTranslate::Test < Minitest::Test
40
40
  end
41
41
 
42
42
  def create_database
43
- system_config = db_config
44
- connection = establish_connection(system_config)
43
+ connection = establish_connection(db_config)
45
44
  connection.create_database(db_config['database']) rescue nil
46
45
  end
47
46
 
@@ -61,6 +60,8 @@ class JSONTranslate::Test < Minitest::Test
61
60
  def setup
62
61
  I18n.available_locales = ['en', 'en-US', 'fr']
63
62
  I18n.config.enforce_available_locales = true
63
+ I18n::Backend::Simple.include(I18n::Backend::Fallbacks)
64
+ I18n.fallbacks = I18n.available_locales
64
65
  DatabaseCleaner.start
65
66
  end
66
67
 
@@ -13,18 +13,15 @@ class TranslatesTest < JSONTranslate::Test
13
13
  def test_retrieves_in_current_locale
14
14
  p = Post.new(
15
15
  :title_translations => { "en" => "English Title", "fr" => "Titre français" },
16
- :body_1_translations => { "en" => "English Body", "fr" => "Corps anglais" }
16
+ :body_1_translations => { "en" => "English Body", "fr" => "Corps français" }
17
17
  )
18
18
  I18n.with_locale(:fr) do
19
19
  assert_equal("Titre français", p.title)
20
- assert_equal("Corps anglais", p.body_1)
20
+ assert_equal("Corps français", p.body_1)
21
21
  end
22
22
  end
23
23
 
24
24
  def test_retrieves_in_current_locale_with_fallbacks
25
- I18n::Backend::Simple.include(I18n::Backend::Fallbacks)
26
- I18n.default_locale = :"en-US"
27
-
28
25
  p = Post.new(:title_translations => {"en" => "English Title"}, :body_1_translations => { "en" => "English Body" })
29
26
  I18n.with_locale(:fr) do
30
27
  assert_equal("English Title", p.title)
@@ -42,6 +39,31 @@ class TranslatesTest < JSONTranslate::Test
42
39
  end
43
40
  end
44
41
 
42
+ def test_assigns_nil_value_as_standard
43
+ I18n.with_locale(:en) do
44
+ p = Post.new(:title_translations => { "en" => "English Title" })
45
+ p.title_fr = ""
46
+ p.title = ""
47
+ assert_nil(p.title_translations["fr"])
48
+ assert_nil(p.title_translations["en"])
49
+ end
50
+ end
51
+
52
+ def test_assigns_blank_value_when_active
53
+ # when allow_blank: true option is active
54
+ I18n.with_locale(:en) do
55
+ p = PostDetailed.new(:comment_translations => { "en" => "English Comment" }, :title_translations => { "en" => "English Comment" })
56
+ p.comment_fr = ""
57
+ p.comment = ""
58
+ p.title_en = nil
59
+ p.title = nil
60
+ assert_equal("", p.comment_translations["fr"])
61
+ assert_equal("", p.comment_translations["en"])
62
+ assert_nil(p.title_translations["en"])
63
+ assert_nil(p.title_translations["en"])
64
+ end
65
+ end
66
+
45
67
  def test_persists_changes_in_specified_locale
46
68
  I18n.with_locale(:en) do
47
69
  p = Post.create!(:title_translations => { "en" => "Original Text" }, :body_1_translations => { "en" => "Original Body" })
@@ -64,9 +86,6 @@ class TranslatesTest < JSONTranslate::Test
64
86
  end
65
87
 
66
88
  def test_retrieves_in_specified_locale_with_fallbacks
67
- I18n::Backend::Simple.include(I18n::Backend::Fallbacks)
68
- I18n.default_locale = :"en-US"
69
-
70
89
  p = Post.new(:title_translations => { "en" => "English Title" }, :body_1_translations => { "en" => "English Body" })
71
90
  I18n.with_locale(:fr) do
72
91
  assert_equal("English Title", p.title)
@@ -79,9 +98,6 @@ class TranslatesTest < JSONTranslate::Test
79
98
  end
80
99
 
81
100
  def test_fallback_from_empty_string
82
- I18n::Backend::Simple.include(I18n::Backend::Fallbacks)
83
- I18n.default_locale = :"en-US"
84
-
85
101
  p = Post.new(:title_translations => { "en" => "English Title", "fr" => "" }, :body_1_translations => { "en" => "English Body", "fr" => "" })
86
102
  I18n.with_locale(:fr) do
87
103
  assert_equal("English Title", p.title)
@@ -94,9 +110,6 @@ class TranslatesTest < JSONTranslate::Test
94
110
  end
95
111
 
96
112
  def test_retrieves_in_specified_locale_with_fallback_disabled
97
- I18n::Backend::Simple.include(I18n::Backend::Fallbacks)
98
- I18n.default_locale = :"en-US"
99
-
100
113
  p = Post.new(:title_translations => { "en" => "English Title" }, :body_1_translations => { "en" => "English Body" })
101
114
  p.disable_fallback
102
115
  I18n.with_locale(:fr) do
@@ -106,9 +119,6 @@ class TranslatesTest < JSONTranslate::Test
106
119
  end
107
120
 
108
121
  def test_retrieves_in_specified_locale_with_fallback_disabled_using_a_block
109
- I18n::Backend::Simple.include(I18n::Backend::Fallbacks)
110
- I18n.default_locale = :"en-US"
111
-
112
122
  p = Post.new(:title_translations => { "en" => "English Title" }, :body_1_translations => { "en" => "English Body" })
113
123
  p.enable_fallback
114
124
 
@@ -133,9 +143,6 @@ class TranslatesTest < JSONTranslate::Test
133
143
  end
134
144
 
135
145
  def test_retrieves_in_specified_locale_with_fallback_reenabled
136
- I18n::Backend::Simple.include(I18n::Backend::Fallbacks)
137
- I18n.default_locale = :"en-US"
138
-
139
146
  p = Post.new(:title_translations => { "en" => "English Title" }, :body_1_translations => { "en" => "English Body" })
140
147
  p.disable_fallback
141
148
  p.enable_fallback
@@ -150,9 +157,6 @@ class TranslatesTest < JSONTranslate::Test
150
157
  end
151
158
 
152
159
  def test_retrieves_in_specified_locale_with_fallback_reenabled_using_a_block
153
- I18n::Backend::Simple.include(I18n::Backend::Fallbacks)
154
- I18n.default_locale = :"en-US"
155
-
156
160
  p = Post.new(:title_translations => { "en" => "English Title" }, :body_1_translations => { "en" => "English Body" })
157
161
  p.disable_fallback
158
162
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_translate
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.0
4
+ version: 4.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rob Worley
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2018-01-26 00:00:00.000000000 Z
12
+ date: 2021-04-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -84,6 +84,7 @@ files:
84
84
  - test/database.yml
85
85
  - test/gemfiles/Gemfile.rails-4.2.x
86
86
  - test/gemfiles/Gemfile.rails-5.0.x
87
+ - test/gemfiles/Gemfile.rails-6.0.x
87
88
  - test/test_helper.rb
88
89
  - test/translates_test.rb
89
90
  homepage: https://github.com/cfabianski/json_translate
@@ -105,13 +106,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
105
106
  - !ruby/object:Gem::Version
106
107
  version: '0'
107
108
  requirements: []
108
- rubyforge_project:
109
- rubygems_version: 2.6.14
109
+ rubygems_version: 3.1.2
110
110
  signing_key:
111
111
  specification_version: 4
112
112
  summary: Rails I18n library for ActiveRecord model/data translation using PostgreSQL's
113
113
  JSONB datatype.
114
114
  test_files:
115
+ - test/gemfiles/Gemfile.rails-6.0.x
115
116
  - test/gemfiles/Gemfile.rails-5.0.x
116
117
  - test/gemfiles/Gemfile.rails-4.2.x
117
118
  - test/database.yml