awesome_jsonb_translate 0.1.2 → 0.2.0
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/README.md +36 -2
 - data/lib/awesome_jsonb_translate/active_record.rb +13 -4
 - data/lib/awesome_jsonb_translate/version.rb +1 -1
 - data/lib/awesome_jsonb_translate.rb +1 -1
 - metadata +4 -46
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA256:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: d81ced6cfae9bbcf7aaf22dd231827333f59947fbe614312940b1a58ed5ddbd9
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: e6ad24b4afff672e784204523050684eaabb8424d95f4cb7286e57bfd1483704
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: e1f1c95edd20c951ef43d669e4ba8f09b439ece16fab7f9c6d3f7626bf378ada3066690fa0084d5293a4029ffd876137644f014d012b723e1b6aea557a97344b
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: 11e2c1c76892384196f9aad8ad0fec6e8f53cfe3d340a29fd87fe822cd9b660a66a801f13455822e23ee1d3d964d08dece2d8b3390c3ffb43e48757f95e3a3d9
         
     | 
    
        data/README.md
    CHANGED
    
    | 
         @@ -1,4 +1,4 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            # Awesome JSONB Translate
         
     | 
| 
      
 1 
     | 
    
         
            +
            # Awesome JSONB Translate [](https://badge.fury.io/rb/awesome_jsonb_translate)
         
     | 
| 
       2 
2 
     | 
    
         | 
| 
       3 
3 
     | 
    
         
             
            This gem uses PostgreSQL's JSONB datatype and ActiveRecord models to translate model data.
         
     | 
| 
       4 
4 
     | 
    
         | 
| 
         @@ -10,7 +10,8 @@ This gem uses PostgreSQL's JSONB datatype and ActiveRecord models to translate m 
     | 
|
| 
       10 
10 
     | 
    
         | 
| 
       11 
11 
     | 
    
         
             
            ## Features
         
     | 
| 
       12 
12 
     | 
    
         | 
| 
       13 
     | 
    
         
            -
            - [x] `v0. 
     | 
| 
      
 13 
     | 
    
         
            +
            - [x] `v0.2.0` Support for raw JSONB syntax in find_by queries
         
     | 
| 
      
 14 
     | 
    
         
            +
            - [x] `v0.1.3` Fix redundant fallbacks when translation is nil
         
     | 
| 
       14 
15 
     | 
    
         | 
| 
       15 
16 
     | 
    
         
             
            ## Requirements
         
     | 
| 
       16 
17 
     | 
    
         | 
| 
         @@ -71,10 +72,12 @@ It always falls back to default locale 
     | 
|
| 
       71 
72 
     | 
    
         
             
            # Behavior with fallbacks enabled
         
     | 
| 
       72 
73 
     | 
    
         
             
            p = Page.new(title_en: 'English title')
         
     | 
| 
       73 
74 
     | 
    
         
             
            I18n.with_locale(:de) { p.title } # => 'English title' (falls back to English)
         
     | 
| 
      
 75 
     | 
    
         
            +
            p.title_de # => nil
         
     | 
| 
       74 
76 
     | 
    
         | 
| 
       75 
77 
     | 
    
         
             
            # Behavior with empty string
         
     | 
| 
       76 
78 
     | 
    
         
             
            p = Page.new(title_en: 'English title', title_de: '')
         
     | 
| 
       77 
79 
     | 
    
         
             
            I18n.with_locale(:de) { p.title } # => 'English title' (falls back since German is empty)
         
     | 
| 
      
 80 
     | 
    
         
            +
            p.title_de # => ''
         
     | 
| 
       78 
81 
     | 
    
         
             
            ```
         
     | 
| 
       79 
82 
     | 
    
         | 
| 
       80 
83 
     | 
    
         
             
            ### Assigning a Hash Directly
         
     | 
| 
         @@ -93,6 +96,8 @@ p.title_de = 'Aktualisierter Deutscher Titel' 
     | 
|
| 
       93 
96 
     | 
    
         | 
| 
       94 
97 
     | 
    
         
             
            ### Querying by Translated Value (JSONB-aware)
         
     | 
| 
       95 
98 
     | 
    
         | 
| 
      
 99 
     | 
    
         
            +
            **Hash-based queries (recommended for most use cases):**
         
     | 
| 
      
 100 
     | 
    
         
            +
             
     | 
| 
       96 
101 
     | 
    
         
             
            ```ruby
         
     | 
| 
       97 
102 
     | 
    
         
             
            # Find records by current locale value
         
     | 
| 
       98 
103 
     | 
    
         
             
            Page.find_by(title_en: 'English title')
         
     | 
| 
         @@ -107,6 +112,35 @@ Page.find_by(title_en: 'English title', author: 'John') 
     | 
|
| 
       107 
112 
     | 
    
         
             
            Page.where("title->>'en' = ?", 'English title').where(author: 'John')
         
     | 
| 
       108 
113 
     | 
    
         
             
            ```
         
     | 
| 
       109 
114 
     | 
    
         | 
| 
      
 115 
     | 
    
         
            +
            **Raw JSONB syntax queries (for advanced use cases):**
         
     | 
| 
      
 116 
     | 
    
         
            +
             
     | 
| 
      
 117 
     | 
    
         
            +
            You can also use raw PostgreSQL JSONB syntax directly with `find_by`:
         
     | 
| 
      
 118 
     | 
    
         
            +
             
     | 
| 
      
 119 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 120 
     | 
    
         
            +
            # Direct JSONB query syntax
         
     | 
| 
      
 121 
     | 
    
         
            +
            Page.find_by("title->>'en' = ?", 'English title')
         
     | 
| 
      
 122 
     | 
    
         
            +
             
     | 
| 
      
 123 
     | 
    
         
            +
            # Complex JSONB queries with operators
         
     | 
| 
      
 124 
     | 
    
         
            +
            Page.find_by("title->>'en' ILIKE ?", '%title%')
         
     | 
| 
      
 125 
     | 
    
         
            +
             
     | 
| 
      
 126 
     | 
    
         
            +
            # Case-insensitive search
         
     | 
| 
      
 127 
     | 
    
         
            +
            Page.find_by("LOWER(title->>'en') = ?", 'english title')
         
     | 
| 
      
 128 
     | 
    
         
            +
            ```
         
     | 
| 
      
 129 
     | 
    
         
            +
             
     | 
| 
      
 130 
     | 
    
         
            +
            **Important:** Raw SQL string syntax is only supported with `find_by`. The methods `find_or_initialize_by` and `find_or_create_by` require hash-based syntax because they need to know which attributes to set when creating/initializing new records.
         
     | 
| 
      
 131 
     | 
    
         
            +
             
     | 
| 
      
 132 
     | 
    
         
            +
            For these methods, use hash syntax with translated accessors:
         
     | 
| 
      
 133 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 134 
     | 
    
         
            +
            # Correct - uses hash syntax
         
     | 
| 
      
 135 
     | 
    
         
            +
            Page.find_or_initialize_by(title_en: 'English title')
         
     | 
| 
      
 136 
     | 
    
         
            +
            Page.find_or_create_by(title_de: 'Deutscher Titel')
         
     | 
| 
      
 137 
     | 
    
         
            +
             
     | 
| 
      
 138 
     | 
    
         
            +
            # Incorrect - string syntax not supported
         
     | 
| 
      
 139 
     | 
    
         
            +
            # Page.find_or_create_by("title->>'en' = ?", 'English title')  # Will not work
         
     | 
| 
      
 140 
     | 
    
         
            +
            ```
         
     | 
| 
      
 141 
     | 
    
         
            +
             
     | 
| 
      
 142 
     | 
    
         
            +
            When using raw JSONB syntax with `find_by`, the gem delegates to ActiveRecord's native query methods, giving you full access to PostgreSQL's JSONB operators and functions.
         
     | 
| 
      
 143 
     | 
    
         
            +
             
     | 
| 
       110 
144 
     | 
    
         
             
            ### Finding or Initializing Records
         
     | 
| 
       111 
145 
     | 
    
         | 
| 
       112 
146 
     | 
    
         
             
            ```ruby
         
     | 
| 
         @@ -1,6 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            # frozen_string_literal: true
         
     | 
| 
       2 
2 
     | 
    
         | 
| 
       3 
3 
     | 
    
         
             
            module AwesomeJsonbTranslate
         
     | 
| 
      
 4 
     | 
    
         
            +
              # Extends ActiveRecord models with translation capabilities using PostgreSQL JSONB columns
         
     | 
| 
       4 
5 
     | 
    
         
             
              module ActiveRecord
         
     | 
| 
       5 
6 
     | 
    
         
             
                def translates(*attrs)
         
     | 
| 
       6 
7 
     | 
    
         
             
                  include InstanceMethods
         
     | 
| 
         @@ -33,7 +34,7 @@ module AwesomeJsonbTranslate 
     | 
|
| 
       33 
34 
     | 
    
         | 
| 
       34 
35 
     | 
    
         
             
                def define_translation_reader_for_locale(attr_name, locale)
         
     | 
| 
       35 
36 
     | 
    
         
             
                  define_method("#{attr_name}_#{locale}") do
         
     | 
| 
       36 
     | 
    
         
            -
                     
     | 
| 
      
 37 
     | 
    
         
            +
                    read_translation_without_fallback(attr_name, locale)
         
     | 
| 
       37 
38 
     | 
    
         
             
                  end
         
     | 
| 
       38 
39 
     | 
    
         
             
                end
         
     | 
| 
       39 
40 
     | 
    
         | 
| 
         @@ -63,6 +64,7 @@ module AwesomeJsonbTranslate 
     | 
|
| 
       63 
64 
     | 
    
         
             
                  end
         
     | 
| 
       64 
65 
     | 
    
         
             
                end
         
     | 
| 
       65 
66 
     | 
    
         | 
| 
      
 67 
     | 
    
         
            +
                # Provides instance methods for reading and writing translations
         
     | 
| 
       66 
68 
     | 
    
         
             
                module InstanceMethods
         
     | 
| 
       67 
69 
     | 
    
         
             
                  def translated?(attr_name, locale = I18n.locale)
         
     | 
| 
       68 
70 
     | 
    
         
             
                    value = read_translation_without_fallback(attr_name, locale)
         
     | 
| 
         @@ -110,9 +112,12 @@ module AwesomeJsonbTranslate 
     | 
|
| 
       110 
112 
     | 
    
         
             
                  end
         
     | 
| 
       111 
113 
     | 
    
         
             
                end
         
     | 
| 
       112 
114 
     | 
    
         | 
| 
      
 115 
     | 
    
         
            +
                # Overrides ActiveRecord finder methods to support querying by translated attributes
         
     | 
| 
       113 
116 
     | 
    
         
             
                module FindByMethods
         
     | 
| 
       114 
117 
     | 
    
         
             
                  # Override find_by to handle translated attributes
         
     | 
| 
       115 
     | 
    
         
            -
                  def find_by(attributes)
         
     | 
| 
      
 118 
     | 
    
         
            +
                  def find_by(attributes, *args)
         
     | 
| 
      
 119 
     | 
    
         
            +
                    return super unless attributes.is_a?(Hash)
         
     | 
| 
      
 120 
     | 
    
         
            +
             
     | 
| 
       116 
121 
     | 
    
         
             
                    # Check if any of the keys represent translated attributes
         
     | 
| 
       117 
122 
     | 
    
         
             
                    has_translated_attrs = attributes.keys.any? do |key|
         
     | 
| 
       118 
123 
     | 
    
         
             
                      translated_accessors.include?(key.to_sym)
         
     | 
| 
         @@ -154,7 +159,9 @@ module AwesomeJsonbTranslate 
     | 
|
| 
       154 
159 
     | 
    
         
             
                  end
         
     | 
| 
       155 
160 
     | 
    
         | 
| 
       156 
161 
     | 
    
         
             
                  # Override find_or_initialize_by to handle translated attributes
         
     | 
| 
       157 
     | 
    
         
            -
                  def find_or_initialize_by(attributes)
         
     | 
| 
      
 162 
     | 
    
         
            +
                  def find_or_initialize_by(attributes, &block)
         
     | 
| 
      
 163 
     | 
    
         
            +
                    return super unless attributes.is_a?(Hash)
         
     | 
| 
      
 164 
     | 
    
         
            +
             
     | 
| 
       158 
165 
     | 
    
         
             
                    result = find_by(attributes)
         
     | 
| 
       159 
166 
     | 
    
         
             
                    return result if result
         
     | 
| 
       160 
167 
     | 
    
         | 
| 
         @@ -165,7 +172,9 @@ module AwesomeJsonbTranslate 
     | 
|
| 
       165 
172 
     | 
    
         
             
                  end
         
     | 
| 
       166 
173 
     | 
    
         | 
| 
       167 
174 
     | 
    
         
             
                  # Override find_or_create_by to handle translated attributes
         
     | 
| 
       168 
     | 
    
         
            -
                  def find_or_create_by(attributes)
         
     | 
| 
      
 175 
     | 
    
         
            +
                  def find_or_create_by(attributes, &block)
         
     | 
| 
      
 176 
     | 
    
         
            +
                    return super unless attributes.is_a?(Hash)
         
     | 
| 
      
 177 
     | 
    
         
            +
             
     | 
| 
       169 
178 
     | 
    
         
             
                    result = find_by(attributes)
         
     | 
| 
       170 
179 
     | 
    
         
             
                    return result if result
         
     | 
| 
       171 
180 
     | 
    
         | 
| 
         @@ -4,6 +4,6 @@ require 'active_record' 
     | 
|
| 
       4 
4 
     | 
    
         
             
            require 'awesome_jsonb_translate/version'
         
     | 
| 
       5 
5 
     | 
    
         
             
            require 'awesome_jsonb_translate/active_record'
         
     | 
| 
       6 
6 
     | 
    
         | 
| 
      
 7 
     | 
    
         
            +
            # Provides PostgreSQL JSONB-based translation functionality for ActiveRecord models
         
     | 
| 
       7 
8 
     | 
    
         
             
            module AwesomeJsonbTranslate
         
     | 
| 
       8 
     | 
    
         
            -
              # Just serve as a namespace
         
     | 
| 
       9 
9 
     | 
    
         
             
            end
         
     | 
    
        metadata
    CHANGED
    
    | 
         @@ -1,13 +1,13 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            --- !ruby/object:Gem::Specification
         
     | 
| 
       2 
2 
     | 
    
         
             
            name: awesome_jsonb_translate
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version
         
     | 
| 
       4 
     | 
    
         
            -
              version: 0. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.2.0
         
     | 
| 
       5 
5 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       6 
6 
     | 
    
         
             
            authors:
         
     | 
| 
       7 
7 
     | 
    
         
             
            - Nick Ostrovsky
         
     | 
| 
       8 
8 
     | 
    
         
             
            bindir: bin
         
     | 
| 
       9 
9 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       10 
     | 
    
         
            -
            date: 2025- 
     | 
| 
      
 10 
     | 
    
         
            +
            date: 2025-11-02 00:00:00.000000000 Z
         
     | 
| 
       11 
11 
     | 
    
         
             
            dependencies:
         
     | 
| 
       12 
12 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       13 
13 
     | 
    
         
             
              name: activerecord
         
     | 
| 
         @@ -37,48 +37,6 @@ dependencies: 
     | 
|
| 
       37 
37 
     | 
    
         
             
                - - ">="
         
     | 
| 
       38 
38 
     | 
    
         
             
                  - !ruby/object:Gem::Version
         
     | 
| 
       39 
39 
     | 
    
         
             
                    version: '0'
         
     | 
| 
       40 
     | 
    
         
            -
            - !ruby/object:Gem::Dependency
         
     | 
| 
       41 
     | 
    
         
            -
              name: pg
         
     | 
| 
       42 
     | 
    
         
            -
              requirement: !ruby/object:Gem::Requirement
         
     | 
| 
       43 
     | 
    
         
            -
                requirements:
         
     | 
| 
       44 
     | 
    
         
            -
                - - ">="
         
     | 
| 
       45 
     | 
    
         
            -
                  - !ruby/object:Gem::Version
         
     | 
| 
       46 
     | 
    
         
            -
                    version: '0'
         
     | 
| 
       47 
     | 
    
         
            -
              type: :development
         
     | 
| 
       48 
     | 
    
         
            -
              prerelease: false
         
     | 
| 
       49 
     | 
    
         
            -
              version_requirements: !ruby/object:Gem::Requirement
         
     | 
| 
       50 
     | 
    
         
            -
                requirements:
         
     | 
| 
       51 
     | 
    
         
            -
                - - ">="
         
     | 
| 
       52 
     | 
    
         
            -
                  - !ruby/object:Gem::Version
         
     | 
| 
       53 
     | 
    
         
            -
                    version: '0'
         
     | 
| 
       54 
     | 
    
         
            -
            - !ruby/object:Gem::Dependency
         
     | 
| 
       55 
     | 
    
         
            -
              name: rake
         
     | 
| 
       56 
     | 
    
         
            -
              requirement: !ruby/object:Gem::Requirement
         
     | 
| 
       57 
     | 
    
         
            -
                requirements:
         
     | 
| 
       58 
     | 
    
         
            -
                - - ">="
         
     | 
| 
       59 
     | 
    
         
            -
                  - !ruby/object:Gem::Version
         
     | 
| 
       60 
     | 
    
         
            -
                    version: '0'
         
     | 
| 
       61 
     | 
    
         
            -
              type: :development
         
     | 
| 
       62 
     | 
    
         
            -
              prerelease: false
         
     | 
| 
       63 
     | 
    
         
            -
              version_requirements: !ruby/object:Gem::Requirement
         
     | 
| 
       64 
     | 
    
         
            -
                requirements:
         
     | 
| 
       65 
     | 
    
         
            -
                - - ">="
         
     | 
| 
       66 
     | 
    
         
            -
                  - !ruby/object:Gem::Version
         
     | 
| 
       67 
     | 
    
         
            -
                    version: '0'
         
     | 
| 
       68 
     | 
    
         
            -
            - !ruby/object:Gem::Dependency
         
     | 
| 
       69 
     | 
    
         
            -
              name: rspec
         
     | 
| 
       70 
     | 
    
         
            -
              requirement: !ruby/object:Gem::Requirement
         
     | 
| 
       71 
     | 
    
         
            -
                requirements:
         
     | 
| 
       72 
     | 
    
         
            -
                - - ">="
         
     | 
| 
       73 
     | 
    
         
            -
                  - !ruby/object:Gem::Version
         
     | 
| 
       74 
     | 
    
         
            -
                    version: '0'
         
     | 
| 
       75 
     | 
    
         
            -
              type: :development
         
     | 
| 
       76 
     | 
    
         
            -
              prerelease: false
         
     | 
| 
       77 
     | 
    
         
            -
              version_requirements: !ruby/object:Gem::Requirement
         
     | 
| 
       78 
     | 
    
         
            -
                requirements:
         
     | 
| 
       79 
     | 
    
         
            -
                - - ">="
         
     | 
| 
       80 
     | 
    
         
            -
                  - !ruby/object:Gem::Version
         
     | 
| 
       81 
     | 
    
         
            -
                    version: '0'
         
     | 
| 
       82 
40 
     | 
    
         
             
            description: This gem uses PostgreSQL's JSONB datatype to store and retrieve translations
         
     | 
| 
       83 
41 
     | 
    
         
             
              for ActiveRecord models without extra columns or tables
         
     | 
| 
       84 
42 
     | 
    
         
             
            email:
         
     | 
| 
         @@ -108,14 +66,14 @@ required_ruby_version: !ruby/object:Gem::Requirement 
     | 
|
| 
       108 
66 
     | 
    
         
             
              requirements:
         
     | 
| 
       109 
67 
     | 
    
         
             
              - - ">="
         
     | 
| 
       110 
68 
     | 
    
         
             
                - !ruby/object:Gem::Version
         
     | 
| 
       111 
     | 
    
         
            -
                  version:  
     | 
| 
      
 69 
     | 
    
         
            +
                  version: 2.7.0
         
     | 
| 
       112 
70 
     | 
    
         
             
            required_rubygems_version: !ruby/object:Gem::Requirement
         
     | 
| 
       113 
71 
     | 
    
         
             
              requirements:
         
     | 
| 
       114 
72 
     | 
    
         
             
              - - ">="
         
     | 
| 
       115 
73 
     | 
    
         
             
                - !ruby/object:Gem::Version
         
     | 
| 
       116 
74 
     | 
    
         
             
                  version: '0'
         
     | 
| 
       117 
75 
     | 
    
         
             
            requirements: []
         
     | 
| 
       118 
     | 
    
         
            -
            rubygems_version: 3.6. 
     | 
| 
      
 76 
     | 
    
         
            +
            rubygems_version: 3.6.6
         
     | 
| 
       119 
77 
     | 
    
         
             
            specification_version: 4
         
     | 
| 
       120 
78 
     | 
    
         
             
            summary: ActiveRecord translations using PostgreSQL's JSONB data type
         
     | 
| 
       121 
79 
     | 
    
         
             
            test_files: []
         
     |