buddy_translatable 1.0.2 → 1.1.0

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
2
  SHA256:
3
- metadata.gz: 4e189cdc05da2e42b02d5863d2b936003417be87ec79076a111c7c96278fab31
4
- data.tar.gz: 3cc54ac8f488cdb2318fcc624b1a2719c7db976b8b270d274788eae73ab25dd4
3
+ metadata.gz: 61db17a047508f80e5a5d1e0c149efcea45ee59a1a2f06c6624a16691d49c6da
4
+ data.tar.gz: 89c815eba978977f31f2404105075534a2311452a36f46a3c910f5362c491a69
5
5
  SHA512:
6
- metadata.gz: d83e39ffa2db864154bb58cbebef361034c20c9f7388b6191bbce7b0fcd030b3250f608e0ed8dd5cb66262fad941e5bc6b758b7358cb18e5b3207b8bd1124415
7
- data.tar.gz: 7b97f4bdce0772d5fd170bc195110b445eb211754e18fbd99edac3ad6a34795ca6632ffa892f10d594497a2aee99702ae8ae43c3bbae7e6bd625d8e9b26b2ea9
6
+ metadata.gz: eccbb1ed3369e12bf95a982153a40a4b018ebf2974c60c5464a910b108399b3902b6e9bba309d398fce303a8850f5a0ab1f6a0aa5f04d54669d99042e5a6ec63
7
+ data.tar.gz: e0f7595d85aa395ee44884f908fb3badba42f2bd8c77bdfddc143e8b3b823cf23687f715446f551f18c50d00179aa985570129183061d599dee2fe2a627c5004
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ # 1.0.2 (02-02-2023)
2
+ - Fix: fix postgres migration error "current transaction is aborted, commands ignored until end of transaction block"
3
+
1
4
  # 1.0.1 (18-06-2021)
2
5
  - Fix: fix error when running rails db:migrate
3
6
 
data/Dockerfile CHANGED
@@ -1,6 +1,6 @@
1
1
  FROM ruby:2.5
2
2
  RUN apt-get update -qq
3
- RUN gem install bundler
3
+ RUN gem install bundler -v 2.3.26
4
4
  WORKDIR /app
5
5
  COPY . /app
6
6
  RUN bundle install
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
@@ -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
- format = (try(:column_types) || try(:attribute_types))[attr.to_s].type rescue :text
16
- define_translatable_setters(attr, format)
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, format)
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] = %i[string text].include?(format) ? data.to_json : data
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
- res = self[attr] || {}
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BuddyTranslatable
4
- VERSION = '1.0.2'
4
+ VERSION = '1.1.0'
5
5
  end
@@ -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.2
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: 2021-08-03 00:00:00.000000000 Z
11
+ date: 2023-02-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler