buddy_translatable 1.0.2 → 1.1.1
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/CHANGELOG.md +3 -0
- data/Dockerfile +1 -1
- data/Gemfile.lock +2 -0
- data/README.md +13 -30
- data/lib/buddy_translatable/core.rb +28 -7
- data/lib/buddy_translatable/version.rb +1 -1
- data/lib/buddy_translatable.rb +9 -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: 14a66ca05f9355589a0a39d41e9432fc4f71bc52d7c6fbd686dfd941b1e867fb
|
4
|
+
data.tar.gz: e41a1b0f34843838a4dd6af3d6db262f721d97fdcf666f5419570b1c443f1829
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4e6176e0b6865bc9c0eea329c6585536b2d6ac7ff0790201dac09aab63d72c1527575790f0dd4e7a5cf630b6702f3db3f6edaf942b6199bc3f5332bd611d698f
|
7
|
+
data.tar.gz: abecbd30c7f980eeafe4325a8ff9994a4700da8f8d8a45ffeb66a627266ab5a59db83cbfbd3401c5a2bad008578970eccc8127ec496790ff3eec58f1720af8c7
|
data/CHANGELOG.md
CHANGED
data/Dockerfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -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
@@ -90,41 +90,24 @@ class AddTitleToPosts < ActiveRecord::Migration
|
|
90
90
|
end
|
91
91
|
```
|
92
92
|
|
93
|
-
###
|
93
|
+
### Filters
|
94
94
|
|
95
|
-
|
95
|
+
- Filter for an item with exact value in any locale
|
96
96
|
```ruby
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
included do
|
101
|
-
scope :where_jsonb_value, lambda { |attr, value|
|
102
|
-
attr_query = sanitize_sql("#{table_name}.#{attr}")
|
103
|
-
where("EXISTS (SELECT 1 FROM jsonb_each_text(#{attr_query}) j
|
104
|
-
WHERE j.value = ?)", value)
|
105
|
-
}
|
106
|
-
|
107
|
-
scope :where_jsonb_value_like, lambda { |attr, value, case_sens = false|
|
108
|
-
attr_query = sanitize_sql("#{table_name}.#{attr}")
|
109
|
-
condition = case_sens ? 'j.value LIKE ?' : 'lower(j.value) LIKE lower(?)'
|
110
|
-
where("EXISTS (SELECT 1
|
111
|
-
FROM jsonb_each_text(#{attr_query}) j
|
112
|
-
WHERE #{condition})", "%#{value}%")
|
113
|
-
}
|
114
|
-
end
|
115
|
-
end
|
116
|
-
|
117
|
-
class Post < ActiveRecord::Base
|
118
|
-
#...
|
119
|
-
include JsonbQuerable
|
120
|
-
#...
|
121
|
-
end
|
97
|
+
# where_<attr_name>_with(value: String)
|
98
|
+
Post.where_key_with('ebay')
|
99
|
+
```
|
122
100
|
|
123
|
-
|
101
|
+
- Filter for items with any locale that contains the provided value
|
102
|
+
```ruby
|
103
|
+
# where_<attr_name>_like(value: String)
|
104
|
+
Post.where_key_like('bay')
|
124
105
|
```
|
125
|
-
|
106
|
+
|
107
|
+
- Filter for items where current locale has the exact provided value
|
126
108
|
```ruby
|
127
|
-
|
109
|
+
# where_<attr_name>_eq(value: String, locale?: Symbol)
|
110
|
+
Post.where_key_eq('ebay')
|
128
111
|
```
|
129
112
|
|
130
113
|
## Development
|
@@ -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
|
@@ -82,5 +81,27 @@ module BuddyTranslatable
|
|
82
81
|
end
|
83
82
|
end
|
84
83
|
end
|
84
|
+
|
85
|
+
# Sample string value: {\"en\":\"key-1\"}
|
86
|
+
def define_translatable_finders(attr, is_json) # rubocop:disable Metrics/MethodLength Metrics/AbcSize
|
87
|
+
attr_query = sanitize_sql("#{table_name}.#{attr}")
|
88
|
+
scope :"where_#{attr}_with", (lambda do |value|
|
89
|
+
return where("#{attr_query} like ?", "%\":\"#{value}\"%") unless is_json
|
90
|
+
|
91
|
+
where("EXISTS (SELECT 1 FROM jsonb_each_text(#{attr_query}) j WHERE j.value = ?)", value)
|
92
|
+
end)
|
93
|
+
|
94
|
+
scope :"where_#{attr}_like", (lambda do |value|
|
95
|
+
return where("#{attr_query} like ?", "%#{value}%") unless is_json
|
96
|
+
|
97
|
+
where("EXISTS (SELECT 1 FROM jsonb_each_text(#{attr_query}) j WHERE j.value LIKE ?)", "%#{value}%")
|
98
|
+
end)
|
99
|
+
|
100
|
+
scope :"where_#{attr}_eq", (lambda do |value, locale = I18n.locale|
|
101
|
+
return where("#{attr_query} like ?", "%\"#{locale}\":\"#{value}\"%") unless is_json
|
102
|
+
|
103
|
+
where("#{attr}->>'#{locale}' = ?", value)
|
104
|
+
end)
|
105
|
+
end
|
85
106
|
end
|
86
107
|
end
|
data/lib/buddy_translatable.rb
CHANGED
@@ -6,4 +6,13 @@ module BuddyTranslatable
|
|
6
6
|
def self.included(base)
|
7
7
|
base.extend BuddyTranslatable::Core
|
8
8
|
end
|
9
|
+
|
10
|
+
def self.translatable_attr_json?(model_class, attr)
|
11
|
+
migrated = ActiveRecord::Base.connection.tables.include?(model_class.table_name) rescue nil
|
12
|
+
return :text unless migrated
|
13
|
+
|
14
|
+
columns_data = model_class.try(:column_types) || model_class.try(:attribute_types)
|
15
|
+
format = (columns_data[attr.to_s].type rescue :text) # rubocop:disable Style/RescueModifier
|
16
|
+
%i[string text].exclude?(format)
|
17
|
+
end
|
9
18
|
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.
|
4
|
+
version: 1.1.1
|
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-05-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|