buddy_translatable 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +3 -0
- data/Dockerfile +1 -1
- data/Gemfile.lock +3 -1
- data/README.md +4 -1
- data/lib/buddy_translatable/core.rb +30 -12
- data/lib/buddy_translatable/version.rb +1 -1
- data/lib/buddy_translatable.rb +16 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 61db17a047508f80e5a5d1e0c149efcea45ee59a1a2f06c6624a16691d49c6da
|
4
|
+
data.tar.gz: 89c815eba978977f31f2404105075534a2311452a36f46a3c910f5362c491a69
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eccbb1ed3369e12bf95a982153a40a4b018ebf2974c60c5464a910b108399b3902b6e9bba309d398fce303a8850f5a0ab1f6a0aa5f04d54669d99042e5a6ec63
|
7
|
+
data.tar.gz: e0f7595d85aa395ee44884f908fb3badba42f2bd8c77bdfddc143e8b3b823cf23687f715446f551f18c50d00179aa985570129183061d599dee2fe2a627c5004
|
data/CHANGELOG.md
CHANGED
data/Dockerfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
buddy_translatable (1.0.
|
4
|
+
buddy_translatable (1.0.2)
|
5
5
|
activerecord
|
6
6
|
activesupport
|
7
7
|
i18n
|
@@ -21,6 +21,7 @@ GEM
|
|
21
21
|
tzinfo (~> 2.0)
|
22
22
|
zeitwerk (~> 2.3)
|
23
23
|
ast (2.4.2)
|
24
|
+
byebug (11.1.3)
|
24
25
|
concurrent-ruby (1.1.9)
|
25
26
|
diff-lcs (1.3)
|
26
27
|
i18n (1.8.10)
|
@@ -70,6 +71,7 @@ PLATFORMS
|
|
70
71
|
DEPENDENCIES
|
71
72
|
buddy_translatable!
|
72
73
|
bundler
|
74
|
+
byebug
|
73
75
|
pg
|
74
76
|
rake
|
75
77
|
rspec
|
data/README.md
CHANGED
@@ -11,7 +11,10 @@ Allows you to store text data in multiple languages. Based on [translateable](ht
|
|
11
11
|
```ruby
|
12
12
|
class Post < ActiveRecord::Base
|
13
13
|
include BuddyTranslatable
|
14
|
-
translatable :title
|
14
|
+
translatable :title
|
15
|
+
# translatable :title, default_key: :de, available_locales: [:de, :en]
|
16
|
+
# default_key: by default use `I18n.default_locale`
|
17
|
+
# available_locales: by default `I18n.available_locales`
|
15
18
|
end
|
16
19
|
|
17
20
|
post = Post.create(title: { de: 'Hallo', en: 'Hello' })
|
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
module BuddyTranslatable
|
4
4
|
module Core
|
5
|
-
def translatable(*attrs, default_key:
|
5
|
+
def translatable(*attrs, default_key: I18n.default_locale, available_locales: nil)
|
6
6
|
available_locales ||= I18n.available_locales
|
7
7
|
attrs.each do |attr|
|
8
8
|
define_translatable_methods(attr, default_key.to_sym)
|
@@ -10,24 +10,23 @@ module BuddyTranslatable
|
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
|
-
# rubocop:disable Style/RescueModifier
|
14
13
|
def define_translatable_methods(attr, fallback_locale)
|
15
|
-
|
16
|
-
define_translatable_setters(attr,
|
14
|
+
is_json = BuddyTranslatable.translatable_attr_json?(self, attr)
|
15
|
+
define_translatable_setters(attr, is_json)
|
17
16
|
define_translatable_key_getters(attr, fallback_locale)
|
18
17
|
define_translatable_getters(attr)
|
18
|
+
define_translatable_finders(attr, is_json)
|
19
19
|
end
|
20
|
-
# rubocop:enable Style/RescueModifier
|
21
20
|
|
22
21
|
# Sample:
|
23
22
|
# model.title_data = { de: 'de val', en: 'en val' } # ==> replace value data
|
24
23
|
# model.title = { de: 'de val', en: 'en val' } # replace value data
|
25
24
|
# model.title = 'custom value' # sets new value for current locale
|
26
|
-
def define_translatable_setters(attr,
|
25
|
+
def define_translatable_setters(attr, is_json)
|
27
26
|
define_method("#{attr}_data=") do |arg|
|
28
27
|
data = send("#{attr}_data")
|
29
28
|
data = arg.is_a?(Hash) ? arg.symbolize_keys : data.merge(I18n.locale => arg)
|
30
|
-
self[attr] =
|
29
|
+
self[attr] = is_json ? data : data.to_json
|
31
30
|
end
|
32
31
|
|
33
32
|
define_method("#{attr}=") do |arg|
|
@@ -41,7 +40,7 @@ module BuddyTranslatable
|
|
41
40
|
def define_translatable_key_getters(attr, fallback_locale)
|
42
41
|
define_method("#{attr}_data_for") do |key|
|
43
42
|
value = send("#{attr}_data")
|
44
|
-
value[key] ||
|
43
|
+
value[key].presence ||
|
45
44
|
value[fallback_locale].presence ||
|
46
45
|
value.values.find(&:present?)
|
47
46
|
end
|
@@ -56,10 +55,7 @@ module BuddyTranslatable
|
|
56
55
|
# model.title # print value for current locale
|
57
56
|
def define_translatable_getters(attr)
|
58
57
|
define_method("#{attr}_data") do
|
59
|
-
|
60
|
-
res = new_record? ? { I18n.locale => '' } : {} unless res.present?
|
61
|
-
res = JSON.parse(res) if res.is_a?(String)
|
62
|
-
res.symbolize_keys
|
58
|
+
BuddyTranslatable.parse_translatable_data(self[attr])
|
63
59
|
end
|
64
60
|
|
65
61
|
define_method(attr) do |**_args|
|
@@ -82,5 +78,27 @@ module BuddyTranslatable
|
|
82
78
|
end
|
83
79
|
end
|
84
80
|
end
|
81
|
+
|
82
|
+
# Sample string value: {\"en\":\"key-1\"}
|
83
|
+
def define_translatable_finders(attr, is_json) # rubocop:disable Metrics/MethodLength Metrics/AbcSize
|
84
|
+
attr_query = sanitize_sql("#{table_name}.#{attr}")
|
85
|
+
scope :"where_#{attr}_with", (lambda do |value|
|
86
|
+
return where("#{attr_query} like ?", "%\":\"#{value}\"%") unless is_json
|
87
|
+
|
88
|
+
where("EXISTS (SELECT 1 FROM jsonb_each_text(#{attr_query}) j WHERE j.value = ?)", value)
|
89
|
+
end)
|
90
|
+
|
91
|
+
scope :"where_#{attr}_like", (lambda do |value|
|
92
|
+
return where("#{attr_query} like ?", "%#{value}%") unless is_json
|
93
|
+
|
94
|
+
where("EXISTS (SELECT 1 FROM jsonb_each_text(#{attr_query}) j WHERE j.value LIKE ?)", "%#{value}%")
|
95
|
+
end)
|
96
|
+
|
97
|
+
scope :"where_#{attr}_eq", (lambda do |value, locale = I18n.locale|
|
98
|
+
return where("#{attr_query} like ?", "%\"#{locale}\":\"#{value}\"%") unless is_json
|
99
|
+
|
100
|
+
where("#{attr}->>'#{locale}' = ?", value)
|
101
|
+
end)
|
102
|
+
end
|
85
103
|
end
|
86
104
|
end
|
data/lib/buddy_translatable.rb
CHANGED
@@ -6,4 +6,20 @@ module BuddyTranslatable
|
|
6
6
|
def self.included(base)
|
7
7
|
base.extend BuddyTranslatable::Core
|
8
8
|
end
|
9
|
+
|
10
|
+
def self.parse_translatable_data(data)
|
11
|
+
res = data || {}
|
12
|
+
res = {} unless res.present?
|
13
|
+
res = JSON.parse(res) if res.is_a?(String)
|
14
|
+
res.symbolize_keys
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.translatable_attr_json?(model_class, attr)
|
18
|
+
migrated = ActiveRecord::Base.connection.tables.include?(model_class.table_name)
|
19
|
+
return :text unless migrated
|
20
|
+
|
21
|
+
columns_data = model_class.try(:column_types) || model_class.try(:attribute_types)
|
22
|
+
format = (columns_data[attr.to_s].type rescue :text) # rubocop:disable Style/RescueModifier
|
23
|
+
%i[string text].exclude?(format)
|
24
|
+
end
|
9
25
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: buddy_translatable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Owen
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-02-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|