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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 137d4c2784bfe06dbd6969ccb23c08230752a379d1c16cae7c86f2bd3a26c77c
4
- data.tar.gz: 41137ae24aa4c5ce86f6e418824e44fcd6d7754041d0c4129c02587153895bdc
3
+ metadata.gz: 61db17a047508f80e5a5d1e0c149efcea45ee59a1a2f06c6624a16691d49c6da
4
+ data.tar.gz: 89c815eba978977f31f2404105075534a2311452a36f46a3c910f5362c491a69
5
5
  SHA512:
6
- metadata.gz: 9428062d7b98175ab808858b9598e85bfbd19686862c72f7f1fc23e4d87e3b0323ff7b7606066cbee56b6c0a01e9432e2effc61598d5733fc8d1630e8c26ba8a
7
- data.tar.gz: 5de3c7ecc7ce1f3c0aebaa0579952584552d9a446b0a9e4c8d1a4b8e37927d3cb170181a66be3092504aac8bcb9fedc0074d924b5a0c4253346e398d02fe1090
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
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- buddy_translatable (1.0.1)
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, default_key: :de
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: :en, available_locales: nil)
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
- 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.1'
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.1
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-06-18 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