buddy_translatable 1.0.2 → 1.1.1

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: 14a66ca05f9355589a0a39d41e9432fc4f71bc52d7c6fbd686dfd941b1e867fb
4
+ data.tar.gz: e41a1b0f34843838a4dd6af3d6db262f721d97fdcf666f5419570b1c443f1829
5
5
  SHA512:
6
- metadata.gz: d83e39ffa2db864154bb58cbebef361034c20c9f7388b6191bbce7b0fcd030b3250f608e0ed8dd5cb66262fad941e5bc6b758b7358cb18e5b3207b8bd1124415
7
- data.tar.gz: 7b97f4bdce0772d5fd170bc195110b445eb211754e18fbd99edac3ad6a34795ca6632ffa892f10d594497a2aee99702ae8ae43c3bbae7e6bd625d8e9b26b2ea9
6
+ metadata.gz: 4e6176e0b6865bc9c0eea329c6585536b2d6ac7ff0790201dac09aab63d72c1527575790f0dd4e7a5cf630b6702f3db3f6edaf942b6199bc3f5332bd611d698f
7
+ data.tar.gz: abecbd30c7f980eeafe4325a8ff9994a4700da8f8d8a45ffeb66a627266ab5a59db83cbfbd3401c5a2bad008578970eccc8127ec496790ff3eec58f1720af8c7
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
data/README.md CHANGED
@@ -90,41 +90,24 @@ class AddTitleToPosts < ActiveRecord::Migration
90
90
  end
91
91
  ```
92
92
 
93
- ### Queries
93
+ ### Filters
94
94
 
95
- For searching you can use the following concern (Only `jsonb` format attributes):
95
+ - Filter for an item with exact value in any locale
96
96
  ```ruby
97
- module JsonbQuerable
98
- extend ActiveSupport::Concern
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
- Post.where_jsonb_value_like(:key, 'ebay')
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
- Simple queries:
106
+
107
+ - Filter for items where current locale has the exact provided value
126
108
  ```ruby
127
- Post.where("title->>'en' = ?", 'hello')
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
- 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
@@ -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
@@ -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.1'
5
5
  end
@@ -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.0.2
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: 2021-08-03 00:00:00.000000000 Z
11
+ date: 2023-05-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler