json_translate 3.0.1 → 4.0.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.
- checksums.yaml +4 -4
- data/README.md +17 -8
- data/lib/json_translate.rb +0 -1
- data/lib/json_translate/translates.rb +21 -6
- data/lib/json_translate/translates/instance_methods.rb +29 -50
- data/lib/json_translate/version.rb +1 -1
- data/test/database.yml +8 -1
- data/test/gemfiles/Gemfile.rails-4.2.x +2 -1
- data/test/gemfiles/Gemfile.rails-5.0.x +2 -1
- data/test/test_helper.rb +11 -5
- data/test/translates_test.rb +107 -24
- metadata +7 -8
- data/lib/json_translate/translates/active_record_with_json_translates.rb +0 -22
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 18109ecd716fe83a685fba1c452278e24c292d88
|
|
4
|
+
data.tar.gz: 23188ebe9a7a00efc712a3ee7945911eae0137bb
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b38e84f20f8c74bd68cd8cf18149fdc54c55b5b514d491056b177c1cacc9372289235d11f4bc954395e25dbf7aa1d6eb533fe23d26fd01fa616c2e0724147c07
|
|
7
|
+
data.tar.gz: bb93cce1c20f30bcc460e59c53f80fda46e6f5232ef4f7c43c7344dafc618a26b424bb60ac7320d9be1d5849e7bf1a13f007d23d869181408d35145f7d8d72ee
|
data/README.md
CHANGED
|
@@ -1,18 +1,20 @@
|
|
|
1
|
+
[](https://badge.fury.io/rb/json_translate)
|
|
2
|
+
[](https://travis-ci.org/cfabianski/json_translate)
|
|
3
|
+
[](COPYRIGHT)
|
|
4
|
+
[](https://codeclimate.com/github/cfabianski/json_translate)
|
|
5
|
+
|
|
1
6
|
# JSON Translate
|
|
2
7
|
|
|
3
8
|
Rails I18n library for ActiveRecord model/data translation using PostgreSQL's
|
|
4
|
-
JSONB datatype. It provides an interface inspired by
|
|
9
|
+
JSONB datatype or MySQL's JSON datatype. It provides an interface inspired by
|
|
5
10
|
[Globalize3](https://github.com/svenfuchs/globalize3) but removes the need to
|
|
6
11
|
maintain separate translation tables.
|
|
7
12
|
|
|
8
|
-
[](https://travis-ci.org/cfabianski/json_translate)
|
|
9
|
-
[](COPYRIGHT)
|
|
10
|
-
[](https://codeclimate.com/github/cfabianski/json_translate)
|
|
11
|
-
|
|
12
13
|
## Requirements
|
|
13
14
|
|
|
14
15
|
* ActiveRecord >= 4.2.0
|
|
15
16
|
* I18n
|
|
17
|
+
* MySQL support requires ActiveRecord >= 5 and MySQL >= 5.7.8.
|
|
16
18
|
|
|
17
19
|
## Installation
|
|
18
20
|
|
|
@@ -24,8 +26,15 @@ When using bundler, put it in your Gemfile:
|
|
|
24
26
|
source 'https://rubygems.org'
|
|
25
27
|
|
|
26
28
|
gem 'activerecord'
|
|
29
|
+
|
|
30
|
+
# PostgreSQL
|
|
27
31
|
gem 'pg', :platform => :ruby
|
|
28
32
|
gem 'activerecord-jdbcpostgresql-adapter', :platform => :jruby
|
|
33
|
+
|
|
34
|
+
# or MySQL
|
|
35
|
+
gem 'mysql2', :platform => :ruby
|
|
36
|
+
gem 'activerecord-jdbcmysql-adapter', :platform => :jruby
|
|
37
|
+
|
|
29
38
|
gem 'json_translate'
|
|
30
39
|
```
|
|
31
40
|
|
|
@@ -57,21 +66,21 @@ post.title # => This database rocks!
|
|
|
57
66
|
post.title_he # => אתר זה טוב
|
|
58
67
|
```
|
|
59
68
|
|
|
60
|
-
To find records using translations without constructing
|
|
69
|
+
To find records using translations without constructing JSON queries by hand:
|
|
61
70
|
|
|
62
71
|
```ruby
|
|
63
72
|
Post.with_title_translation("This database rocks!") # => #<ActiveRecord::Relation ...>
|
|
64
73
|
Post.with_title_translation("אתר זה טוב", :he) # => #<ActiveRecord::Relation ...>
|
|
65
74
|
```
|
|
66
75
|
|
|
67
|
-
In order to make this work, you'll need to define an JSONB column for each of
|
|
76
|
+
In order to make this work, you'll need to define an JSON or JSONB column for each of
|
|
68
77
|
your translated attributes, using the suffix "_translations":
|
|
69
78
|
|
|
70
79
|
```ruby
|
|
71
80
|
class CreatePosts < ActiveRecord::Migration
|
|
72
81
|
def up
|
|
73
82
|
create_table :posts do |t|
|
|
74
|
-
t.column :title_translations, 'jsonb'
|
|
83
|
+
t.column :title_translations, 'jsonb' # or 'json' for MySQL
|
|
75
84
|
t.column :body_translations, 'jsonb'
|
|
76
85
|
t.timestamps
|
|
77
86
|
end
|
data/lib/json_translate.rb
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
module JSONTranslate
|
|
2
2
|
module Translates
|
|
3
3
|
SUFFIX = "_translations".freeze
|
|
4
|
-
|
|
4
|
+
MYSQL_ADAPTERS = %w[MySQL Mysql2 Mysql2Spatial]
|
|
5
5
|
|
|
6
6
|
def translates(*attrs)
|
|
7
7
|
include InstanceMethods
|
|
@@ -18,22 +18,37 @@ module JSONTranslate
|
|
|
18
18
|
].flatten.compact
|
|
19
19
|
|
|
20
20
|
attrs.each do |attr_name|
|
|
21
|
-
define_method attr_name do
|
|
22
|
-
read_json_translation(attr_name)
|
|
21
|
+
define_method attr_name do |**params|
|
|
22
|
+
read_json_translation(attr_name, params)
|
|
23
23
|
end
|
|
24
24
|
|
|
25
25
|
define_method "#{attr_name}=" do |value|
|
|
26
26
|
write_json_translation(attr_name, value)
|
|
27
27
|
end
|
|
28
28
|
|
|
29
|
+
I18n.available_locales.each do |locale|
|
|
30
|
+
normalized_locale = locale.to_s.downcase.gsub(/[^a-z]/, '')
|
|
31
|
+
|
|
32
|
+
define_method :"#{attr_name}_#{normalized_locale}" do |**params|
|
|
33
|
+
read_json_translation(attr_name, locale, false, params)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
define_method "#{attr_name}_#{normalized_locale}=" do |value|
|
|
37
|
+
write_json_translation(attr_name, value, locale)
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
29
41
|
define_singleton_method "with_#{attr_name}_translation" do |value, locale = I18n.locale|
|
|
30
42
|
quoted_translation_store = connection.quote_column_name("#{attr_name}#{SUFFIX}")
|
|
31
43
|
translation_hash = { "#{locale}" => value }
|
|
32
|
-
|
|
44
|
+
|
|
45
|
+
if MYSQL_ADAPTERS.include?(connection.adapter_name)
|
|
46
|
+
where("JSON_CONTAINS(#{quoted_translation_store}, :translation, '$')", translation: translation_hash.to_json)
|
|
47
|
+
else
|
|
48
|
+
where("#{quoted_translation_store} @> :translation::jsonb", translation: translation_hash.to_json)
|
|
49
|
+
end
|
|
33
50
|
end
|
|
34
51
|
end
|
|
35
|
-
|
|
36
|
-
send(:prepend, ActiveRecordWithJSONTranslate)
|
|
37
52
|
end
|
|
38
53
|
|
|
39
54
|
def translates?
|
|
@@ -3,10 +3,12 @@ module JSONTranslate
|
|
|
3
3
|
module InstanceMethods
|
|
4
4
|
def disable_fallback
|
|
5
5
|
toggle_fallback(false)
|
|
6
|
+
yield if block_given?
|
|
6
7
|
end
|
|
7
8
|
|
|
8
9
|
def enable_fallback
|
|
9
10
|
toggle_fallback(true)
|
|
11
|
+
yield if block_given?
|
|
10
12
|
end
|
|
11
13
|
|
|
12
14
|
protected
|
|
@@ -14,70 +16,47 @@ module JSONTranslate
|
|
|
14
16
|
attr_reader :enabled_fallback
|
|
15
17
|
|
|
16
18
|
def json_translate_fallback_locales(locale)
|
|
17
|
-
|
|
18
|
-
|
|
19
|
+
if enabled_fallback != false && I18n.respond_to?(:fallbacks)
|
|
20
|
+
Array(I18n.fallbacks[locale])
|
|
21
|
+
else
|
|
22
|
+
Array(locale)
|
|
23
|
+
end
|
|
19
24
|
end
|
|
20
25
|
|
|
21
|
-
def read_json_translation(attr_name, locale = I18n.locale)
|
|
26
|
+
def read_json_translation(attr_name, locale = I18n.locale, fallback = true, **params)
|
|
22
27
|
translations = public_send("#{attr_name}#{SUFFIX}") || {}
|
|
23
28
|
|
|
24
|
-
|
|
25
|
-
|
|
29
|
+
selected_locale = locale
|
|
30
|
+
if fallback
|
|
31
|
+
selected_locale = json_translate_fallback_locales(locale).detect do |available_locale|
|
|
32
|
+
translations[available_locale.to_s].present?
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
translation = translations[selected_locale.to_s]
|
|
37
|
+
|
|
38
|
+
if translation && params.present?
|
|
39
|
+
begin
|
|
40
|
+
translation = I18n.interpolate(translation, params)
|
|
41
|
+
rescue I18n::MissingInterpolationArgument
|
|
42
|
+
end
|
|
26
43
|
end
|
|
27
44
|
|
|
28
|
-
|
|
45
|
+
translation
|
|
29
46
|
end
|
|
30
47
|
|
|
31
48
|
def write_json_translation(attr_name, value, locale = I18n.locale)
|
|
49
|
+
value = value.presence
|
|
32
50
|
translation_store = "#{attr_name}#{SUFFIX}"
|
|
33
51
|
translations = public_send(translation_store) || {}
|
|
34
52
|
public_send("#{translation_store}_will_change!") unless translations[locale.to_s] == value
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
value
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
def respond_to_with_translates?(symbol, include_all = false)
|
|
41
|
-
return true if parse_translated_attribute_accessor(symbol)
|
|
42
|
-
respond_to_without_translates?(symbol, include_all)
|
|
43
|
-
end
|
|
44
|
-
|
|
45
|
-
def method_missing_with_translates(method_name, *args)
|
|
46
|
-
translated_attr_name, locale, assigning = parse_translated_attribute_accessor(method_name)
|
|
47
|
-
|
|
48
|
-
return method_missing_without_translates(method_name, *args) unless translated_attr_name
|
|
49
|
-
|
|
50
|
-
if assigning
|
|
51
|
-
write_json_translation(translated_attr_name, args.first, locale)
|
|
53
|
+
if value
|
|
54
|
+
translations[locale.to_s] = value
|
|
52
55
|
else
|
|
53
|
-
|
|
56
|
+
translations.delete(locale.to_s)
|
|
54
57
|
end
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
# Internal: Parse a translated convenience accessor name.
|
|
58
|
-
#
|
|
59
|
-
# method_name - The accessor name.
|
|
60
|
-
#
|
|
61
|
-
# Examples
|
|
62
|
-
#
|
|
63
|
-
# parse_translated_attribute_accessor("title_en=")
|
|
64
|
-
# # => [:title, :en, true]
|
|
65
|
-
#
|
|
66
|
-
# parse_translated_attribute_accessor("title_fr")
|
|
67
|
-
# # => [:title, :fr, false]
|
|
68
|
-
#
|
|
69
|
-
# Returns the attribute name Symbol, locale Symbol, and a Boolean
|
|
70
|
-
# indicating whether or not the caller is attempting to assign a value.
|
|
71
|
-
def parse_translated_attribute_accessor(method_name)
|
|
72
|
-
return unless /\A(?<attribute>[a-z_]+)_(?<locale>[a-z]{2})(?<assignment>=?)\z/ =~ method_name
|
|
73
|
-
|
|
74
|
-
translated_attr_name = attribute.to_sym
|
|
75
|
-
return unless translated_attribute_names.include?(translated_attr_name)
|
|
76
|
-
|
|
77
|
-
locale = locale.to_sym
|
|
78
|
-
assigning = assignment.present?
|
|
79
|
-
|
|
80
|
-
[translated_attr_name, locale, assigning]
|
|
58
|
+
public_send("#{translation_store}=", translations)
|
|
59
|
+
value
|
|
81
60
|
end
|
|
82
61
|
|
|
83
62
|
def toggle_fallback(enabled)
|
data/test/database.yml
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
|
-
|
|
1
|
+
postgres:
|
|
2
2
|
adapter: postgresql
|
|
3
3
|
host: localhost
|
|
4
4
|
database: json_translate_test
|
|
5
5
|
username: postgres
|
|
6
|
+
schema_search_path: public
|
|
6
7
|
min_messages: warning
|
|
8
|
+
|
|
9
|
+
mysql:
|
|
10
|
+
adapter: mysql2
|
|
11
|
+
host: localhost
|
|
12
|
+
database: json_translate_test
|
|
13
|
+
username: root
|
data/test/test_helper.rb
CHANGED
|
@@ -7,7 +7,7 @@ DatabaseCleaner.strategy = :transaction
|
|
|
7
7
|
I18n.available_locales = [:en, :fr]
|
|
8
8
|
|
|
9
9
|
class Post < ActiveRecord::Base
|
|
10
|
-
translates :title
|
|
10
|
+
translates :title, :body_1
|
|
11
11
|
end
|
|
12
12
|
|
|
13
13
|
class PostDetailed < Post
|
|
@@ -23,10 +23,14 @@ class JSONTranslate::Test < Minitest::Test
|
|
|
23
23
|
|
|
24
24
|
private
|
|
25
25
|
|
|
26
|
+
def adapter
|
|
27
|
+
@adapter ||= ENV['DB'] || 'postgres'
|
|
28
|
+
end
|
|
29
|
+
|
|
26
30
|
def db_config
|
|
27
31
|
@db_config ||= begin
|
|
28
32
|
filepath = File.join('test', 'database.yml')
|
|
29
|
-
YAML.load_file(filepath)[
|
|
33
|
+
YAML.load_file(filepath)[adapter]
|
|
30
34
|
end
|
|
31
35
|
end
|
|
32
36
|
|
|
@@ -36,16 +40,18 @@ class JSONTranslate::Test < Minitest::Test
|
|
|
36
40
|
end
|
|
37
41
|
|
|
38
42
|
def create_database
|
|
39
|
-
system_config = db_config
|
|
43
|
+
system_config = db_config
|
|
40
44
|
connection = establish_connection(system_config)
|
|
41
45
|
connection.create_database(db_config['database']) rescue nil
|
|
42
46
|
end
|
|
43
47
|
|
|
44
48
|
def create_table
|
|
49
|
+
column_type = adapter == 'mysql' ? 'json' : 'jsonb'
|
|
45
50
|
connection = establish_connection(db_config)
|
|
46
51
|
connection.create_table(:posts, :force => true) do |t|
|
|
47
|
-
t.column :title_translations,
|
|
48
|
-
t.column :
|
|
52
|
+
t.column :title_translations, column_type
|
|
53
|
+
t.column :body_1_translations, column_type
|
|
54
|
+
t.column :comment_translations, column_type
|
|
49
55
|
end
|
|
50
56
|
end
|
|
51
57
|
end
|
data/test/translates_test.rb
CHANGED
|
@@ -4,15 +4,20 @@ require 'test_helper'
|
|
|
4
4
|
class TranslatesTest < JSONTranslate::Test
|
|
5
5
|
def test_assigns_in_current_locale
|
|
6
6
|
I18n.with_locale(:en) do
|
|
7
|
-
p = Post.new(:title => "English Title")
|
|
7
|
+
p = Post.new(:title => "English Title", :body_1 => "English Body")
|
|
8
8
|
assert_equal("English Title", p.title_translations['en'])
|
|
9
|
+
assert_equal("English Body", p.body_1_translations['en'])
|
|
9
10
|
end
|
|
10
11
|
end
|
|
11
12
|
|
|
12
13
|
def test_retrieves_in_current_locale
|
|
13
|
-
p = Post.new(
|
|
14
|
+
p = Post.new(
|
|
15
|
+
:title_translations => { "en" => "English Title", "fr" => "Titre français" },
|
|
16
|
+
:body_1_translations => { "en" => "English Body", "fr" => "Corps anglais" }
|
|
17
|
+
)
|
|
14
18
|
I18n.with_locale(:fr) do
|
|
15
19
|
assert_equal("Titre français", p.title)
|
|
20
|
+
assert_equal("Corps anglais", p.body_1)
|
|
16
21
|
end
|
|
17
22
|
end
|
|
18
23
|
|
|
@@ -20,33 +25,41 @@ class TranslatesTest < JSONTranslate::Test
|
|
|
20
25
|
I18n::Backend::Simple.include(I18n::Backend::Fallbacks)
|
|
21
26
|
I18n.default_locale = :"en-US"
|
|
22
27
|
|
|
23
|
-
p = Post.new(:title_translations => {"en" => "English Title"})
|
|
28
|
+
p = Post.new(:title_translations => {"en" => "English Title"}, :body_1_translations => { "en" => "English Body" })
|
|
24
29
|
I18n.with_locale(:fr) do
|
|
25
30
|
assert_equal("English Title", p.title)
|
|
31
|
+
assert_equal("English Body", p.body_1)
|
|
26
32
|
end
|
|
27
33
|
end
|
|
28
34
|
|
|
29
35
|
def test_assigns_in_specified_locale
|
|
30
36
|
I18n.with_locale(:en) do
|
|
31
|
-
p = Post.new(:title_translations => { "en" => "English Title" })
|
|
37
|
+
p = Post.new(:title_translations => { "en" => "English Title" }, :body_1_translations => { "en" => "English Body" })
|
|
32
38
|
p.title_fr = "Titre français"
|
|
39
|
+
p.body_1_fr = "Corps anglais"
|
|
33
40
|
assert_equal("Titre français", p.title_translations["fr"])
|
|
41
|
+
assert_equal("Corps anglais", p.body_1_translations["fr"])
|
|
34
42
|
end
|
|
35
43
|
end
|
|
36
44
|
|
|
37
45
|
def test_persists_changes_in_specified_locale
|
|
38
46
|
I18n.with_locale(:en) do
|
|
39
|
-
p = Post.create!(:title_translations => { "en" => "Original Text" })
|
|
47
|
+
p = Post.create!(:title_translations => { "en" => "Original Text" }, :body_1_translations => { "en" => "Original Body" })
|
|
40
48
|
p.title_en = "Updated Text"
|
|
49
|
+
p.body_1_en = "Updated Body"
|
|
41
50
|
p.save!
|
|
42
51
|
assert_equal("Updated Text", Post.last.title_en)
|
|
52
|
+
assert_equal("Updated Body", Post.last.body_1_en)
|
|
43
53
|
end
|
|
44
54
|
end
|
|
45
55
|
|
|
46
56
|
def test_retrieves_in_specified_locale
|
|
47
57
|
I18n.with_locale(:en) do
|
|
48
|
-
p = Post.new(:title_translations => { "en" => "English Title", "fr" => "Titre français" })
|
|
58
|
+
p = Post.new(:title_translations => { "en" => "English Title", "fr" => "Titre français" }, :body_1_translations => { "en" => "English Body", "fr" => "Corps anglais" })
|
|
49
59
|
assert_equal("Titre français", p.title_fr)
|
|
60
|
+
assert_equal("Corps anglais", p.body_1_fr)
|
|
61
|
+
assert_equal("English Title", p.title_en)
|
|
62
|
+
assert_equal("English Body", p.body_1_en)
|
|
50
63
|
end
|
|
51
64
|
end
|
|
52
65
|
|
|
@@ -54,9 +67,14 @@ class TranslatesTest < JSONTranslate::Test
|
|
|
54
67
|
I18n::Backend::Simple.include(I18n::Backend::Fallbacks)
|
|
55
68
|
I18n.default_locale = :"en-US"
|
|
56
69
|
|
|
57
|
-
p = Post.new(:title_translations => { "en" => "English Title" })
|
|
70
|
+
p = Post.new(:title_translations => { "en" => "English Title" }, :body_1_translations => { "en" => "English Body" })
|
|
58
71
|
I18n.with_locale(:fr) do
|
|
59
|
-
assert_equal("English Title", p.
|
|
72
|
+
assert_equal("English Title", p.title)
|
|
73
|
+
assert_equal("English Body", p.body_1)
|
|
74
|
+
assert_nil(p.title_fr)
|
|
75
|
+
assert_nil(p.body_1_fr)
|
|
76
|
+
assert_equal("English Title", p.title_en)
|
|
77
|
+
assert_equal("English Body", p.body_1_en)
|
|
60
78
|
end
|
|
61
79
|
end
|
|
62
80
|
|
|
@@ -64,9 +82,14 @@ class TranslatesTest < JSONTranslate::Test
|
|
|
64
82
|
I18n::Backend::Simple.include(I18n::Backend::Fallbacks)
|
|
65
83
|
I18n.default_locale = :"en-US"
|
|
66
84
|
|
|
67
|
-
p = Post.new(:title_translations => { "en" => "English Title", "fr" => "" })
|
|
85
|
+
p = Post.new(:title_translations => { "en" => "English Title", "fr" => "" }, :body_1_translations => { "en" => "English Body", "fr" => "" })
|
|
68
86
|
I18n.with_locale(:fr) do
|
|
69
|
-
assert_equal("English Title", p.
|
|
87
|
+
assert_equal("English Title", p.title)
|
|
88
|
+
assert_equal("English Body", p.body_1)
|
|
89
|
+
assert_equal("", p.title_fr)
|
|
90
|
+
assert_equal("", p.body_1_fr)
|
|
91
|
+
assert_equal("English Title", p.title_en)
|
|
92
|
+
assert_equal("English Body", p.body_1_en)
|
|
70
93
|
end
|
|
71
94
|
end
|
|
72
95
|
|
|
@@ -74,10 +97,11 @@ class TranslatesTest < JSONTranslate::Test
|
|
|
74
97
|
I18n::Backend::Simple.include(I18n::Backend::Fallbacks)
|
|
75
98
|
I18n.default_locale = :"en-US"
|
|
76
99
|
|
|
77
|
-
p = Post.new(:title_translations => { "en" => "English Title" })
|
|
100
|
+
p = Post.new(:title_translations => { "en" => "English Title" }, :body_1_translations => { "en" => "English Body" })
|
|
78
101
|
p.disable_fallback
|
|
79
102
|
I18n.with_locale(:fr) do
|
|
80
|
-
|
|
103
|
+
assert_nil(p.title)
|
|
104
|
+
assert_nil(p.body_1)
|
|
81
105
|
end
|
|
82
106
|
end
|
|
83
107
|
|
|
@@ -85,22 +109,43 @@ class TranslatesTest < JSONTranslate::Test
|
|
|
85
109
|
I18n::Backend::Simple.include(I18n::Backend::Fallbacks)
|
|
86
110
|
I18n.default_locale = :"en-US"
|
|
87
111
|
|
|
88
|
-
p = Post.new(:title_translations => { "en" => "English Title" })
|
|
112
|
+
p = Post.new(:title_translations => { "en" => "English Title" }, :body_1_translations => { "en" => "English Body" })
|
|
89
113
|
p.enable_fallback
|
|
90
114
|
|
|
91
|
-
|
|
92
|
-
|
|
115
|
+
I18n.with_locale(:fr) do
|
|
116
|
+
assert_equal("English Title", p.title)
|
|
117
|
+
assert_equal("English Body", p.body_1)
|
|
118
|
+
assert_nil(p.title_fr)
|
|
119
|
+
assert_nil(p.body_1_fr)
|
|
120
|
+
assert_equal("English Title", p.title_en)
|
|
121
|
+
assert_equal("English Body", p.body_1_en)
|
|
122
|
+
yielded = p.disable_fallback do
|
|
123
|
+
assert_nil(p.title)
|
|
124
|
+
assert_nil(p.body_1)
|
|
125
|
+
assert_nil(p.title_fr)
|
|
126
|
+
assert_nil(p.body_1_fr)
|
|
127
|
+
assert_equal("English Title", p.title_en)
|
|
128
|
+
assert_equal("English Body", p.body_1_en)
|
|
129
|
+
:block_return_value
|
|
130
|
+
end
|
|
131
|
+
assert_equal(:block_return_value, yielded)
|
|
132
|
+
end
|
|
93
133
|
end
|
|
94
134
|
|
|
95
135
|
def test_retrieves_in_specified_locale_with_fallback_reenabled
|
|
96
136
|
I18n::Backend::Simple.include(I18n::Backend::Fallbacks)
|
|
97
137
|
I18n.default_locale = :"en-US"
|
|
98
138
|
|
|
99
|
-
p = Post.new(:title_translations => { "en" => "English Title" })
|
|
139
|
+
p = Post.new(:title_translations => { "en" => "English Title" }, :body_1_translations => { "en" => "English Body" })
|
|
100
140
|
p.disable_fallback
|
|
101
141
|
p.enable_fallback
|
|
102
142
|
I18n.with_locale(:fr) do
|
|
103
|
-
assert_equal("English Title", p.
|
|
143
|
+
assert_equal("English Title", p.title)
|
|
144
|
+
assert_equal("English Body", p.body_1)
|
|
145
|
+
assert_nil(p.title_fr)
|
|
146
|
+
assert_nil(p.body_1_fr)
|
|
147
|
+
assert_equal("English Title", p.title_en)
|
|
148
|
+
assert_equal("English Body", p.body_1_en)
|
|
104
149
|
end
|
|
105
150
|
end
|
|
106
151
|
|
|
@@ -108,11 +153,27 @@ class TranslatesTest < JSONTranslate::Test
|
|
|
108
153
|
I18n::Backend::Simple.include(I18n::Backend::Fallbacks)
|
|
109
154
|
I18n.default_locale = :"en-US"
|
|
110
155
|
|
|
111
|
-
p = Post.new(:title_translations => { "en" => "English Title" })
|
|
156
|
+
p = Post.new(:title_translations => { "en" => "English Title" }, :body_1_translations => { "en" => "English Body" })
|
|
112
157
|
p.disable_fallback
|
|
113
158
|
|
|
114
|
-
|
|
115
|
-
|
|
159
|
+
I18n.with_locale(:fr) do
|
|
160
|
+
assert_nil(p.title)
|
|
161
|
+
assert_nil(p.body_1)
|
|
162
|
+
assert_nil(p.title_fr)
|
|
163
|
+
assert_nil(p.body_1_fr)
|
|
164
|
+
assert_equal("English Title", p.title_en)
|
|
165
|
+
assert_equal("English Body", p.body_1_en)
|
|
166
|
+
yielded = p.enable_fallback do
|
|
167
|
+
assert_equal("English Title", p.title)
|
|
168
|
+
assert_equal("English Body", p.body_1)
|
|
169
|
+
assert_nil(p.title_fr)
|
|
170
|
+
assert_nil(p.body_1_fr)
|
|
171
|
+
assert_equal("English Title", p.title_en)
|
|
172
|
+
assert_equal("English Body", p.body_1_en)
|
|
173
|
+
:block_return_value
|
|
174
|
+
end
|
|
175
|
+
assert_equal(:block_return_value, yielded)
|
|
176
|
+
end
|
|
116
177
|
end
|
|
117
178
|
|
|
118
179
|
def test_method_missing_delegates
|
|
@@ -124,22 +185,38 @@ class TranslatesTest < JSONTranslate::Test
|
|
|
124
185
|
end
|
|
125
186
|
|
|
126
187
|
def test_persists_translations_assigned_as_hash
|
|
127
|
-
p = Post.create!(:title_translations => { "en" => "English Title", "fr" => "Titre français" })
|
|
188
|
+
p = Post.create!(:title_translations => { "en" => "English Title", "fr" => "Titre français" }, :body_1_translations => { "en" => "English Body", "fr" => "Corps anglais" })
|
|
128
189
|
p.reload
|
|
129
190
|
assert_equal({"en" => "English Title", "fr" => "Titre français"}, p.title_translations)
|
|
191
|
+
assert_equal({"en" => "English Body", "fr" => "Corps anglais"}, p.body_1_translations)
|
|
130
192
|
end
|
|
131
193
|
|
|
132
194
|
def test_persists_translations_assigned_to_localized_accessors
|
|
133
|
-
p = Post.create!(:title_en => "English Title", :title_fr => "Titre français")
|
|
195
|
+
p = Post.create!(:title_en => "English Title", :title_fr => "Titre français", :body_1_en => "English Body", :body_1_fr => "Corps anglais")
|
|
134
196
|
p.reload
|
|
135
197
|
assert_equal({"en" => "English Title", "fr" => "Titre français"}, p.title_translations)
|
|
198
|
+
assert_equal({"en" => "English Body", "fr" => "Corps anglais"}, p.body_1_translations)
|
|
136
199
|
end
|
|
137
200
|
|
|
138
201
|
def test_with_translation_relation
|
|
139
|
-
p = Post.create!(:title_translations => { "en" => "Alice in Wonderland", "fr" => "Alice au pays des merveilles" })
|
|
202
|
+
p = Post.create!(:title_translations => { "en" => "Alice in Wonderland", "fr" => "Alice au pays des merveilles" }, :body_1_translations => { "en" => "English Body", "fr" => "Corps anglais" })
|
|
140
203
|
I18n.with_locale(:en) do
|
|
141
204
|
assert_equal p.title_en, Post.with_title_translation("Alice in Wonderland").first.try(:title)
|
|
205
|
+
assert_equal p.body_1_en, Post.with_body_1_translation("English Body").first.try(:body_1)
|
|
206
|
+
end
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
def test_with_interpolation_arguments
|
|
210
|
+
p = Post.create!(:title_translations => { "en" => "Alice in %{where}" })
|
|
211
|
+
I18n.with_locale(:en) do
|
|
212
|
+
assert_equal p.title(where: "Wonderland"), "Alice in Wonderland"
|
|
142
213
|
end
|
|
214
|
+
assert_equal p.title_en(where: "Wonderland"), "Alice in Wonderland"
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
def test_for_missing_interpolation_arguments
|
|
218
|
+
p = Post.create!(:title_translations => { "en" => "Alice in %{where}" })
|
|
219
|
+
assert_equal p.title_en, "Alice in %{where}"
|
|
143
220
|
end
|
|
144
221
|
|
|
145
222
|
def test_class_method_translates?
|
|
@@ -153,6 +230,10 @@ class TranslatesTest < JSONTranslate::Test
|
|
|
153
230
|
"en" => "Alice in Wonderland",
|
|
154
231
|
"fr" => "Alice au pays des merveilles"
|
|
155
232
|
},
|
|
233
|
+
:body_1_translations => {
|
|
234
|
+
"en" => "English Body",
|
|
235
|
+
"fr" => "Corps anglais"
|
|
236
|
+
},
|
|
156
237
|
:comment_translations => {
|
|
157
238
|
"en" => "Awesome book",
|
|
158
239
|
"fr" => "Un livre unique"
|
|
@@ -161,11 +242,13 @@ class TranslatesTest < JSONTranslate::Test
|
|
|
161
242
|
|
|
162
243
|
I18n.with_locale(:en) { assert_equal "Awesome book", p.comment }
|
|
163
244
|
I18n.with_locale(:en) { assert_equal "Alice in Wonderland", p.title }
|
|
245
|
+
I18n.with_locale(:en) { assert_equal "English Body", p.body_1 }
|
|
164
246
|
I18n.with_locale(:fr) { assert_equal "Un livre unique", p.comment }
|
|
165
247
|
I18n.with_locale(:fr) { assert_equal "Alice au pays des merveilles", p.title }
|
|
248
|
+
I18n.with_locale(:fr) { assert_equal "Corps anglais", p.body_1 }
|
|
166
249
|
end
|
|
167
250
|
|
|
168
251
|
def test_permitted_translated_attributes
|
|
169
|
-
assert_equal [:title_en, :title_fr, :comment_en, :comment_fr], PostDetailed.permitted_translated_attributes
|
|
252
|
+
assert_equal [:title_en, :title_fr, :body_1_en, :body_1_fr, :comment_en, :comment_fr], PostDetailed.permitted_translated_attributes
|
|
170
253
|
end
|
|
171
254
|
end
|
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
|
+
version: 4.0.0
|
|
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:
|
|
12
|
+
date: 2018-01-26 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
|
14
14
|
- !ruby/object:Gem::Dependency
|
|
15
15
|
name: activerecord
|
|
@@ -79,7 +79,6 @@ files:
|
|
|
79
79
|
- README.md
|
|
80
80
|
- lib/json_translate.rb
|
|
81
81
|
- lib/json_translate/translates.rb
|
|
82
|
-
- lib/json_translate/translates/active_record_with_json_translates.rb
|
|
83
82
|
- lib/json_translate/translates/instance_methods.rb
|
|
84
83
|
- lib/json_translate/version.rb
|
|
85
84
|
- test/database.yml
|
|
@@ -87,7 +86,7 @@ files:
|
|
|
87
86
|
- test/gemfiles/Gemfile.rails-5.0.x
|
|
88
87
|
- test/test_helper.rb
|
|
89
88
|
- test/translates_test.rb
|
|
90
|
-
homepage: https://github.com/
|
|
89
|
+
homepage: https://github.com/cfabianski/json_translate
|
|
91
90
|
licenses:
|
|
92
91
|
- MIT
|
|
93
92
|
metadata: {}
|
|
@@ -107,14 +106,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
107
106
|
version: '0'
|
|
108
107
|
requirements: []
|
|
109
108
|
rubyforge_project:
|
|
110
|
-
rubygems_version: 2.6.
|
|
109
|
+
rubygems_version: 2.6.14
|
|
111
110
|
signing_key:
|
|
112
111
|
specification_version: 4
|
|
113
112
|
summary: Rails I18n library for ActiveRecord model/data translation using PostgreSQL's
|
|
114
113
|
JSONB datatype.
|
|
115
114
|
test_files:
|
|
116
|
-
- test/database.yml
|
|
117
|
-
- test/gemfiles/Gemfile.rails-4.2.x
|
|
118
115
|
- test/gemfiles/Gemfile.rails-5.0.x
|
|
119
|
-
- test/
|
|
116
|
+
- test/gemfiles/Gemfile.rails-4.2.x
|
|
117
|
+
- test/database.yml
|
|
120
118
|
- test/translates_test.rb
|
|
119
|
+
- test/test_helper.rb
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
module JSONTranslate
|
|
2
|
-
module Translates
|
|
3
|
-
module ActiveRecordWithJSONTranslate
|
|
4
|
-
def respond_to?(symbol, include_all = false)
|
|
5
|
-
return true if parse_translated_attribute_accessor(symbol)
|
|
6
|
-
super(symbol, include_all)
|
|
7
|
-
end
|
|
8
|
-
|
|
9
|
-
def method_missing(method_name, *args)
|
|
10
|
-
translated_attr_name, locale, assigning = parse_translated_attribute_accessor(method_name)
|
|
11
|
-
|
|
12
|
-
return super(method_name, *args) unless translated_attr_name
|
|
13
|
-
|
|
14
|
-
if assigning
|
|
15
|
-
write_json_translation(translated_attr_name, args.first, locale)
|
|
16
|
-
else
|
|
17
|
-
read_json_translation(translated_attr_name, locale)
|
|
18
|
-
end
|
|
19
|
-
end
|
|
20
|
-
end
|
|
21
|
-
end
|
|
22
|
-
end
|