jsonb_accessor 1.4 → 1.4.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/.github/dependabot.yml +6 -0
- data/.github/workflows/ci.yml +40 -62
- data/.github/workflows/push_gem.yml +43 -0
- data/.rubocop.yml +1 -1
- data/.ruby-version +1 -1
- data/CHANGELOG.md +4 -0
- data/Gemfile +3 -0
- data/README.md +61 -17
- data/jsonb_accessor.gemspec +7 -6
- data/lib/jsonb_accessor/helpers.rb +23 -0
- data/lib/jsonb_accessor/macro.rb +40 -27
- data/lib/jsonb_accessor/version.rb +1 -1
- metadata +36 -29
- data/Appraisals +0 -13
- data/Dockerfile +0 -12
- data/docker-compose.yml +0 -30
- data/gemfiles/activerecord_6.1.gemfile +0 -7
- data/gemfiles/activerecord_7.0.gemfile +0 -7
- data/gemfiles/activerecord_7.1.gemfile +0 -7
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ddb7db7a9ab5930ae31cac0fa871289638b0c5a3ba2f3486b4731a39e5d8f3aa
|
|
4
|
+
data.tar.gz: 8c279fcfd16e6c5d490f31a45b0734f06fe70ee500c2a72cae1204d4d6a93e29
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8a9b59f77d798b4e4134ccf5305a107bd220c89f1a377b7c6899273ba62c0c75ad697cb527c60f9a7a6e81dff52087c286cd8518859f00da4a2e8c37e3094531
|
|
7
|
+
data.tar.gz: 733ee774842a1f4c2b734913fd1af2c02b43a892b27a955e64f44c46ca6bff93466436fd5e9d651e0ad081a09dc7d2a704302e6f5039b4db49e00d38544030f6
|
data/.github/workflows/ci.yml
CHANGED
|
@@ -4,89 +4,67 @@ on:
|
|
|
4
4
|
push:
|
|
5
5
|
branches: [master]
|
|
6
6
|
pull_request:
|
|
7
|
-
branches: [
|
|
7
|
+
branches: [master]
|
|
8
8
|
|
|
9
9
|
jobs:
|
|
10
10
|
lint:
|
|
11
|
-
runs-on: ubuntu-
|
|
11
|
+
runs-on: ubuntu-24.04
|
|
12
12
|
steps:
|
|
13
|
-
- uses: actions/checkout@
|
|
14
|
-
|
|
15
|
-
-
|
|
16
|
-
uses: ruby/setup-ruby@v1
|
|
13
|
+
- uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
|
|
14
|
+
|
|
15
|
+
- uses: ruby/setup-ruby@80740b3b13bf9857e28854481ca95a84e78a2bdf # v1.284.0
|
|
17
16
|
with:
|
|
17
|
+
ruby-version: "3.4"
|
|
18
18
|
bundler-cache: true
|
|
19
|
-
|
|
20
|
-
-
|
|
21
|
-
run: bundle exec rubocop
|
|
19
|
+
|
|
20
|
+
- run: bundle exec rubocop
|
|
22
21
|
|
|
23
22
|
tests:
|
|
24
23
|
needs: lint
|
|
24
|
+
runs-on: ubuntu-24.04
|
|
25
|
+
strategy:
|
|
26
|
+
fail-fast: false
|
|
27
|
+
matrix:
|
|
28
|
+
ruby: ["ruby-3.2", "ruby-3.3", "ruby-3.4", "ruby-4.0", "jruby-9.4", "jruby-10.0"]
|
|
29
|
+
activerecord: ["6.1", "7.0", "7.1", "7.2", "8.0", "8.1"]
|
|
30
|
+
postgresql: ["13", "14", "15", "16", "17", "18"]
|
|
31
|
+
exclude:
|
|
32
|
+
# fails due to "ArgumentError: when initializing an Active Record adapter with a config hash, that should be the only argument" in db:schema:load
|
|
33
|
+
- ruby: "jruby-9.4"
|
|
34
|
+
activerecord: "7.2"
|
|
35
|
+
- ruby: "jruby-9.4"
|
|
36
|
+
activerecord: "8.0"
|
|
37
|
+
# fails due to "Because activerecord >= 8.0.0.beta1 depends on Ruby >= 3.2.0 and Gemfile depends on activerecord ~> 8.1, Ruby >= 3.2.0 is required. So, because current Ruby version is = 3.1.7, version solving has failed."
|
|
38
|
+
- ruby: "jruby-9.4"
|
|
39
|
+
activerecord: "8.1"
|
|
40
|
+
# https://github.com/jruby/activerecord-jdbc-adapter/issues/1173
|
|
41
|
+
- ruby: "jruby-10.0"
|
|
42
|
+
activerecord: "8.0"
|
|
43
|
+
# https://github.com/jruby/activerecord-jdbc-adapter/issues/1184
|
|
44
|
+
- ruby: "jruby-10.0"
|
|
45
|
+
activerecord: "8.1"
|
|
46
|
+
name: "Active Record ${{ matrix.activerecord }} with PostgreSQL ${{ matrix.postgresql }} on ${{ matrix.ruby }}"
|
|
25
47
|
services:
|
|
26
48
|
db:
|
|
27
|
-
image: postgres
|
|
49
|
+
image: postgres:${{ matrix.postgresql }}
|
|
28
50
|
env:
|
|
29
51
|
POSTGRES_HOST_AUTH_METHOD: trust
|
|
30
52
|
POSTGRES_DB: jsonb_accessor
|
|
31
53
|
ports: ['5432:5432']
|
|
32
54
|
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
|
|
55
|
+
steps:
|
|
56
|
+
- uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
|
|
33
57
|
|
|
34
|
-
|
|
35
|
-
strategy:
|
|
36
|
-
fail-fast: false
|
|
37
|
-
matrix:
|
|
38
|
-
include:
|
|
39
|
-
- gemfile: activerecord_6.1
|
|
40
|
-
ruby: '3.0'
|
|
41
|
-
|
|
42
|
-
- gemfile: activerecord_6.1
|
|
43
|
-
ruby: '3.1'
|
|
44
|
-
|
|
45
|
-
- gemfile: activerecord_6.1
|
|
46
|
-
ruby: '3.2'
|
|
47
|
-
|
|
48
|
-
- gemfile: activerecord_7.0
|
|
49
|
-
ruby: '3.0'
|
|
50
|
-
|
|
51
|
-
- gemfile: activerecord_7.0
|
|
52
|
-
ruby: '3.1'
|
|
53
|
-
|
|
54
|
-
- gemfile: activerecord_7.0
|
|
55
|
-
ruby: '3.2'
|
|
56
|
-
|
|
57
|
-
- gemfile: activerecord_7.1
|
|
58
|
-
ruby: '3.0'
|
|
59
|
-
|
|
60
|
-
- gemfile: activerecord_7.1
|
|
61
|
-
ruby: '3.1'
|
|
62
|
-
|
|
63
|
-
- gemfile: activerecord_7.1
|
|
64
|
-
ruby: '3.2'
|
|
65
|
-
|
|
66
|
-
- gemfile: activerecord_7.0
|
|
67
|
-
ruby: 'jruby-9.4'
|
|
58
|
+
- run: echo "gem 'activerecord', '~> ${{ matrix.activerecord }}.0'" > Gemfile.local
|
|
68
59
|
|
|
69
|
-
|
|
60
|
+
- if: matrix.activerecord == '6.1' # see https://github.com/rails/rails/pull/54264#issuecomment-2596149819 and https://www.ruby-lang.org/en/news/2024/12/25/ruby-3-4-0-released/#standard-library-updates and https://www.ruby-lang.org/en/news/2025/12/25/ruby-4-0-0-released/#stdlib-updates
|
|
61
|
+
run: printf "gem 'concurrent-ruby', '< 1.3.5'\ngem 'mutex_m'\ngem 'base64'\ngem 'bigdecimal'\ngem 'logger'\n" >> Gemfile.local
|
|
70
62
|
|
|
71
|
-
|
|
72
|
-
BUNDLE_GEMFILE: ${{ github.workspace }}/gemfiles/${{ matrix.gemfile }}.gemfile
|
|
73
|
-
steps:
|
|
74
|
-
- uses: actions/checkout@v3
|
|
75
|
-
|
|
76
|
-
- name: Set up Ruby
|
|
77
|
-
uses: ruby/setup-ruby@v1
|
|
63
|
+
- uses: ruby/setup-ruby@80740b3b13bf9857e28854481ca95a84e78a2bdf # v1.284.0
|
|
78
64
|
with:
|
|
79
65
|
ruby-version: ${{ matrix.ruby }}
|
|
80
66
|
bundler-cache: true
|
|
81
67
|
|
|
82
|
-
-
|
|
83
|
-
run: |
|
|
84
|
-
bundle install
|
|
85
|
-
|
|
86
|
-
- name: Setup DB
|
|
87
|
-
run: |
|
|
88
|
-
bundle exec rake db:schema:load
|
|
68
|
+
- run: bundle exec rake db:schema:load
|
|
89
69
|
|
|
90
|
-
-
|
|
91
|
-
run: |
|
|
92
|
-
bundle exec rake spec
|
|
70
|
+
- run: bundle exec rake spec
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
name: Publish gem to rubygems.org
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
tags:
|
|
6
|
+
- 'v*'
|
|
7
|
+
|
|
8
|
+
permissions:
|
|
9
|
+
contents: read
|
|
10
|
+
|
|
11
|
+
jobs:
|
|
12
|
+
push:
|
|
13
|
+
if: github.repository == 'madeintandem/jsonb_accessor'
|
|
14
|
+
runs-on: ubuntu-24.04
|
|
15
|
+
|
|
16
|
+
permissions:
|
|
17
|
+
contents: write
|
|
18
|
+
id-token: write
|
|
19
|
+
|
|
20
|
+
strategy:
|
|
21
|
+
matrix:
|
|
22
|
+
ruby: ["ruby-3.4", "jruby-9.4"]
|
|
23
|
+
|
|
24
|
+
steps:
|
|
25
|
+
- uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
|
|
26
|
+
with:
|
|
27
|
+
egress-policy: audit
|
|
28
|
+
|
|
29
|
+
- uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
|
|
30
|
+
|
|
31
|
+
- if: matrix.ruby == 'jruby-9.4' # cribbed from https://github.com/ruby/psych/blob/v5.2.4/.github/workflows/push_gem.yml
|
|
32
|
+
run: |
|
|
33
|
+
sudo apt install default-jdk maven
|
|
34
|
+
gem update --system
|
|
35
|
+
gem install ruby-maven rake-compiler --no-document
|
|
36
|
+
rake compile
|
|
37
|
+
|
|
38
|
+
- uses: ruby/setup-ruby@80740b3b13bf9857e28854481ca95a84e78a2bdf # v1.284.0
|
|
39
|
+
with:
|
|
40
|
+
bundler-cache: true
|
|
41
|
+
ruby-version: ${{ matrix.ruby }}
|
|
42
|
+
|
|
43
|
+
- uses: rubygems/release-gem@1c162a739e8b4cb21a676e97b087e8268d8fc40b # v1.1.2
|
data/.rubocop.yml
CHANGED
data/.ruby-version
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
3.
|
|
1
|
+
3.4.3
|
data/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
## [Unreleased]
|
|
3
3
|
|
|
4
|
+
## [1.4.1] - 2026-01-22
|
|
5
|
+
### Added
|
|
6
|
+
- Support for `prefix` and `suffix` options to customize attribute accessor names while preserving original keys in the JSONB column. [#173](https://github.com/madeintandem/jsonb_accessor/issues/173)
|
|
7
|
+
|
|
4
8
|
## [1.4] - 2023-10-15
|
|
5
9
|
### Breaking change
|
|
6
10
|
- `jsonb_accessor` dropped support for Ruby 2 and Rails versions lower than 6.1. Support for ActiveRecord::Enum was also
|
data/Gemfile
CHANGED
data/README.md
CHANGED
|
@@ -8,6 +8,10 @@ Adds typed `jsonb` backed fields as first class citizens to your `ActiveRecord`
|
|
|
8
8
|
|
|
9
9
|
It also adds generic scopes for querying `jsonb` columns.
|
|
10
10
|
|
|
11
|
+
## ⚠️ Status
|
|
12
|
+
|
|
13
|
+
This gem is in maintenance mode and no active development of new features is planned. The major focus is to keep it working with new Ruby/Rails versions and fix any bugs reported. Any PRs for feature requests or enhancements will be reviewed and merged -- so contributions are encouraged!
|
|
14
|
+
|
|
11
15
|
## Table of Contents
|
|
12
16
|
|
|
13
17
|
- [Installation](#installation)
|
|
@@ -87,6 +91,58 @@ product.title #=> "Foo"
|
|
|
87
91
|
product.data #=> { "t" => "Foo" }
|
|
88
92
|
```
|
|
89
93
|
|
|
94
|
+
You can also pass in a `prefix` or `suffix` option.
|
|
95
|
+
|
|
96
|
+
```ruby
|
|
97
|
+
class Product < ActiveRecord::Base
|
|
98
|
+
jsonb_accessor :data,
|
|
99
|
+
title: [:string, prefix: :data],
|
|
100
|
+
external_id: [:integer, suffix: :attr]
|
|
101
|
+
end
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
This allows you to use `data_title` and `external_id_attr` for your getters and setters, but use `title` and `external_id` as the key in the `jsonb`.
|
|
105
|
+
Also, you can pass `true` as a value for `prefix` or `suffix` to use the json_accessor name.
|
|
106
|
+
|
|
107
|
+
```ruby
|
|
108
|
+
product = Product.new(data_title: "Foo", external_id_attr: 12314122)
|
|
109
|
+
product.data_title #=> "Foo"
|
|
110
|
+
product.external_id_attr #=> 12314122
|
|
111
|
+
product.data #=> { "title" => "Foo", "external_id" => 12314122 }
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### Global Options
|
|
115
|
+
|
|
116
|
+
You can apply options to all fields by passing an options hash as the second parameter:
|
|
117
|
+
|
|
118
|
+
```ruby
|
|
119
|
+
class Product < ActiveRecord::Base
|
|
120
|
+
jsonb_accessor :data, { prefix: true },
|
|
121
|
+
title: :string,
|
|
122
|
+
external_id: :integer,
|
|
123
|
+
price: :decimal
|
|
124
|
+
end
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
This applies the `prefix` to all fields. You can still override it for individual fields:
|
|
128
|
+
|
|
129
|
+
```ruby
|
|
130
|
+
class Product < ActiveRecord::Base
|
|
131
|
+
jsonb_accessor :data, { prefix: :product },
|
|
132
|
+
title: :string,
|
|
133
|
+
external_id: [:integer, prefix: :custom],
|
|
134
|
+
price: :decimal
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
product = Product.new(product_title: "Widget", custom_external_id: 123, product_price: 19.99)
|
|
138
|
+
product.product_title #=> "Widget"
|
|
139
|
+
product.custom_external_id #=> 123
|
|
140
|
+
product.product_price #=> 19.99
|
|
141
|
+
product.data #=> { "title" => "Widget", "external_id" => 123, "price" => 19.99 }
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
Global options currently support `:prefix` and `:suffix`.
|
|
145
|
+
|
|
90
146
|
## Scopes
|
|
91
147
|
|
|
92
148
|
Jsonb Accessor provides several scopes to make it easier to query `jsonb` columns. `jsonb_contains`, `jsonb_number_where`, `jsonb_time_where`, and `jsonb_where` are available on all `ActiveRecord::Base` subclasses and don't require that you make use of the `jsonb_accessor` declaration.
|
|
@@ -318,34 +374,22 @@ Because this gem promotes attributes nested into the JSON column to first level
|
|
|
318
374
|
|
|
319
375
|
## Dependencies
|
|
320
376
|
|
|
321
|
-
|
|
322
|
-
- ActiveRecord >= 6.1
|
|
323
|
-
- Postgres >= 9.4 (in order to use the [jsonb column type](http://www.postgresql.org/docs/9.4/static/datatype-json.html)).
|
|
377
|
+
We actively test the following in CI, older versions may work but are **not** supported:
|
|
324
378
|
|
|
325
|
-
|
|
379
|
+
- Ruby >= 3.2 or JRuby >= 9.4
|
|
380
|
+
- Rails >= 6.1
|
|
381
|
+
- PostgreSQL >= 13
|
|
326
382
|
|
|
327
|
-
|
|
383
|
+
JRuby isn't fully supported due to errors, see the CI matrix -- contributions to improve support are welcome!
|
|
328
384
|
|
|
329
385
|
## Development
|
|
330
386
|
|
|
331
|
-
### On your local machine
|
|
332
|
-
|
|
333
387
|
After checking out the repo, run `bin/setup` to install dependencies (make sure postgres is running first).
|
|
334
388
|
|
|
335
389
|
Run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
336
390
|
|
|
337
391
|
`rake` will run Rubocop and the specs.
|
|
338
392
|
|
|
339
|
-
### With Docker
|
|
340
|
-
|
|
341
|
-
```
|
|
342
|
-
# setup
|
|
343
|
-
docker-compose build
|
|
344
|
-
docker-compose run ruby rake db:migrate
|
|
345
|
-
# run test suite
|
|
346
|
-
docker-compose run ruby rake spec
|
|
347
|
-
```
|
|
348
|
-
|
|
349
393
|
## Contributing
|
|
350
394
|
|
|
351
395
|
1. [Fork it](https://github.com/madeintandem/jsonb_accessor/fork)
|
data/jsonb_accessor.gemspec
CHANGED
|
@@ -15,7 +15,7 @@ Gem::Specification.new do |spec|
|
|
|
15
15
|
|
|
16
16
|
spec.summary = "Adds typed jsonb backed fields to your ActiveRecord models."
|
|
17
17
|
spec.description = "Adds typed jsonb backed fields to your ActiveRecord models."
|
|
18
|
-
spec.homepage = "https://github.com/
|
|
18
|
+
spec.homepage = "https://github.com/madeintandem/jsonb_accessor"
|
|
19
19
|
spec.license = "MIT"
|
|
20
20
|
spec.required_ruby_version = ">= 3"
|
|
21
21
|
|
|
@@ -32,14 +32,15 @@ Gem::Specification.new do |spec|
|
|
|
32
32
|
spec.add_dependency "pg", ">= 0.18.1"
|
|
33
33
|
end
|
|
34
34
|
|
|
35
|
-
spec.add_development_dependency "appraisal", "~> 2.5"
|
|
36
35
|
spec.add_development_dependency "awesome_print"
|
|
37
|
-
spec.add_development_dependency "
|
|
36
|
+
spec.add_development_dependency "benchmark" # for Ruby 4.0
|
|
37
|
+
spec.add_development_dependency "database_cleaner-active_record", ">= 2.1"
|
|
38
|
+
spec.add_development_dependency "ostruct" # for Ruby 4.0
|
|
38
39
|
spec.add_development_dependency "pry"
|
|
39
40
|
spec.add_development_dependency "pry-doc"
|
|
40
41
|
spec.add_development_dependency "pry-nav"
|
|
41
|
-
spec.add_development_dependency "psych", "
|
|
42
|
+
spec.add_development_dependency "psych", ">= 3"
|
|
42
43
|
spec.add_development_dependency "rake", ">= 12.3.3"
|
|
43
|
-
spec.add_development_dependency "rspec", "
|
|
44
|
-
spec.add_development_dependency "rubocop", "
|
|
44
|
+
spec.add_development_dependency "rspec", ">= 3.6.0"
|
|
45
|
+
spec.add_development_dependency "rubocop", ">= 1.0"
|
|
45
46
|
end
|
|
@@ -42,5 +42,28 @@ module JsonbAccessor
|
|
|
42
42
|
Time.zone.parse(datetime)
|
|
43
43
|
end
|
|
44
44
|
end
|
|
45
|
+
|
|
46
|
+
def define_attribute_name(json_attribute, name, prefix, suffix)
|
|
47
|
+
accessor_prefix =
|
|
48
|
+
case prefix
|
|
49
|
+
when String, Symbol
|
|
50
|
+
"#{prefix}_"
|
|
51
|
+
when TrueClass
|
|
52
|
+
"#{json_attribute}_"
|
|
53
|
+
else
|
|
54
|
+
""
|
|
55
|
+
end
|
|
56
|
+
accessor_suffix =
|
|
57
|
+
case suffix
|
|
58
|
+
when String, Symbol
|
|
59
|
+
"_#{suffix}"
|
|
60
|
+
when TrueClass
|
|
61
|
+
"_#{json_attribute}"
|
|
62
|
+
else
|
|
63
|
+
""
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
"#{accessor_prefix}#{name}#{accessor_suffix}"
|
|
67
|
+
end
|
|
45
68
|
end
|
|
46
69
|
end
|
data/lib/jsonb_accessor/macro.rb
CHANGED
|
@@ -3,22 +3,30 @@
|
|
|
3
3
|
module JsonbAccessor
|
|
4
4
|
module Macro
|
|
5
5
|
module ClassMethods
|
|
6
|
-
def jsonb_accessor(jsonb_attribute,
|
|
7
|
-
names_and_store_keys =
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
6
|
+
def jsonb_accessor(jsonb_attribute, global_options = {}, **definitions)
|
|
7
|
+
names_and_store_keys = {}
|
|
8
|
+
names_and_defaults = {}
|
|
9
|
+
names_and_attribute_names = {}
|
|
10
|
+
|
|
11
|
+
definitions.each do |name, value|
|
|
12
|
+
args = Array(value)
|
|
13
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
|
14
|
+
|
|
15
|
+
# Determine store keys and default values for each field
|
|
16
|
+
names_and_store_keys[name.to_s] = (options.delete(:store_key) || name).to_s
|
|
17
|
+
names_and_defaults[name.to_s] = options.delete(:default) unless options[:default].nil?
|
|
18
|
+
|
|
19
|
+
prefix = options.delete(:prefix) || global_options[:prefix]
|
|
20
|
+
suffix = options.delete(:suffix) || global_options[:suffix]
|
|
21
|
+
attribute_name = JsonbAccessor::Helpers.define_attribute_name(jsonb_attribute, name, prefix, suffix)
|
|
22
|
+
|
|
23
|
+
# Define virtual attributes for each field
|
|
24
|
+
names_and_attribute_names[name.to_s] = attribute_name
|
|
25
|
+
attribute attribute_name, *args, **options
|
|
19
26
|
end
|
|
20
27
|
|
|
21
28
|
store_key_mapping_method_name = "jsonb_store_key_mapping_for_#{jsonb_attribute}"
|
|
29
|
+
attribute_name_mapping_method_name = "jsonb_attribute_name_mapping_for_#{jsonb_attribute}"
|
|
22
30
|
# Defines methods on the model class
|
|
23
31
|
class_methods = Module.new do
|
|
24
32
|
# Allows us to get a mapping of field names to store keys scoped to the column
|
|
@@ -26,17 +34,16 @@ module JsonbAccessor
|
|
|
26
34
|
superclass_mapping = superclass.try(store_key_mapping_method_name) || {}
|
|
27
35
|
superclass_mapping.merge(names_and_store_keys)
|
|
28
36
|
end
|
|
37
|
+
|
|
38
|
+
# Allows us to get a mapping of field names to attribute names scoped to the column
|
|
39
|
+
define_method(attribute_name_mapping_method_name) do
|
|
40
|
+
superclass_mapping = superclass.try(attribute_name_mapping_method_name) || {}
|
|
41
|
+
superclass_mapping.merge(names_and_attribute_names)
|
|
42
|
+
end
|
|
29
43
|
end
|
|
30
44
|
# We extend with class methods here so we can use the results of methods it defines to define more useful methods later
|
|
31
45
|
extend class_methods
|
|
32
46
|
|
|
33
|
-
# Get field names to default values mapping
|
|
34
|
-
names_and_defaults = field_types.each_with_object({}) do |(name, type), mapping|
|
|
35
|
-
_type, options = Array(type)
|
|
36
|
-
field_default = options.try(:delete, :default)
|
|
37
|
-
mapping[name.to_s] = field_default unless field_default.nil?
|
|
38
|
-
end
|
|
39
|
-
|
|
40
47
|
# Get store keys to default values mapping
|
|
41
48
|
store_keys_and_defaults = JsonbAccessor::Helpers.convert_keys_to_store_keys(names_and_defaults, public_send(store_key_mapping_method_name))
|
|
42
49
|
|
|
@@ -63,11 +70,13 @@ module JsonbAccessor
|
|
|
63
70
|
setters = Module.new do
|
|
64
71
|
# Overrides the setter created by `attribute` above to make sure the jsonb attribute is kept in sync.
|
|
65
72
|
names_and_store_keys.each do |name, store_key|
|
|
66
|
-
|
|
73
|
+
attribute_name = names_and_attribute_names[name]
|
|
74
|
+
|
|
75
|
+
define_method("#{attribute_name}=") do |value|
|
|
67
76
|
super(value)
|
|
68
77
|
|
|
69
78
|
# If enum was defined, take the value from the enum and not what comes out directly from the getter
|
|
70
|
-
attribute_value = defined_enums[
|
|
79
|
+
attribute_value = defined_enums[attribute_name].present? ? defined_enums[attribute_name][value] : public_send(attribute_name)
|
|
71
80
|
|
|
72
81
|
# Rails always saves time based on `default_timezone`. Since #as_json considers timezone, manual conversion is needed
|
|
73
82
|
if attribute_value.acts_like?(:time)
|
|
@@ -83,6 +92,7 @@ module JsonbAccessor
|
|
|
83
92
|
define_method("#{jsonb_attribute}=") do |value|
|
|
84
93
|
value ||= {}
|
|
85
94
|
names_to_store_keys = self.class.public_send(store_key_mapping_method_name)
|
|
95
|
+
names_to_attribute_names = self.class.public_send(attribute_name_mapping_method_name)
|
|
86
96
|
|
|
87
97
|
# this is the raw hash we want to save in the jsonb_attribute
|
|
88
98
|
value_with_store_keys = JsonbAccessor::Helpers.convert_keys_to_store_keys(value, names_to_store_keys)
|
|
@@ -96,7 +106,8 @@ module JsonbAccessor
|
|
|
96
106
|
# Only proceed if this attribute has been defined using `jsonb_accessor`.
|
|
97
107
|
next unless names_to_store_keys.key?(name)
|
|
98
108
|
|
|
99
|
-
|
|
109
|
+
attribute_name = names_to_attribute_names[name]
|
|
110
|
+
write_attribute(attribute_name, attribute_value)
|
|
100
111
|
end
|
|
101
112
|
end
|
|
102
113
|
end
|
|
@@ -109,13 +120,15 @@ module JsonbAccessor
|
|
|
109
120
|
jsonb_values = public_send(jsonb_attribute) || {}
|
|
110
121
|
jsonb_values.each do |store_key, value|
|
|
111
122
|
name = names_and_store_keys.key(store_key)
|
|
112
|
-
|
|
123
|
+
attribute_name = names_and_attribute_names[name]
|
|
124
|
+
|
|
125
|
+
next unless attribute_name
|
|
113
126
|
|
|
114
127
|
write_attribute(
|
|
115
|
-
|
|
116
|
-
JsonbAccessor::Helpers.deserialize_value(value, self.class.type_for_attribute(
|
|
128
|
+
attribute_name,
|
|
129
|
+
JsonbAccessor::Helpers.deserialize_value(value, self.class.type_for_attribute(attribute_name).type)
|
|
117
130
|
)
|
|
118
|
-
clear_attribute_change(
|
|
131
|
+
clear_attribute_change(attribute_name) if persisted?
|
|
119
132
|
end
|
|
120
133
|
end
|
|
121
134
|
|
metadata
CHANGED
|
@@ -1,16 +1,15 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: jsonb_accessor
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version:
|
|
4
|
+
version: 1.4.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Michael Crismali
|
|
8
8
|
- Joe Hirn
|
|
9
9
|
- Jason Haruska
|
|
10
|
-
autorequire:
|
|
11
10
|
bindir: exe
|
|
12
11
|
cert_chain: []
|
|
13
|
-
date:
|
|
12
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
14
13
|
dependencies:
|
|
15
14
|
- !ruby/object:Gem::Dependency
|
|
16
15
|
name: activerecord
|
|
@@ -55,21 +54,21 @@ dependencies:
|
|
|
55
54
|
- !ruby/object:Gem::Version
|
|
56
55
|
version: 0.18.1
|
|
57
56
|
- !ruby/object:Gem::Dependency
|
|
58
|
-
name:
|
|
57
|
+
name: awesome_print
|
|
59
58
|
requirement: !ruby/object:Gem::Requirement
|
|
60
59
|
requirements:
|
|
61
|
-
- - "
|
|
60
|
+
- - ">="
|
|
62
61
|
- !ruby/object:Gem::Version
|
|
63
|
-
version: '
|
|
62
|
+
version: '0'
|
|
64
63
|
type: :development
|
|
65
64
|
prerelease: false
|
|
66
65
|
version_requirements: !ruby/object:Gem::Requirement
|
|
67
66
|
requirements:
|
|
68
|
-
- - "
|
|
67
|
+
- - ">="
|
|
69
68
|
- !ruby/object:Gem::Version
|
|
70
|
-
version: '
|
|
69
|
+
version: '0'
|
|
71
70
|
- !ruby/object:Gem::Dependency
|
|
72
|
-
name:
|
|
71
|
+
name: benchmark
|
|
73
72
|
requirement: !ruby/object:Gem::Requirement
|
|
74
73
|
requirements:
|
|
75
74
|
- - ">="
|
|
@@ -86,16 +85,30 @@ dependencies:
|
|
|
86
85
|
name: database_cleaner-active_record
|
|
87
86
|
requirement: !ruby/object:Gem::Requirement
|
|
88
87
|
requirements:
|
|
89
|
-
- - "
|
|
88
|
+
- - ">="
|
|
90
89
|
- !ruby/object:Gem::Version
|
|
91
90
|
version: '2.1'
|
|
92
91
|
type: :development
|
|
93
92
|
prerelease: false
|
|
94
93
|
version_requirements: !ruby/object:Gem::Requirement
|
|
95
94
|
requirements:
|
|
96
|
-
- - "
|
|
95
|
+
- - ">="
|
|
97
96
|
- !ruby/object:Gem::Version
|
|
98
97
|
version: '2.1'
|
|
98
|
+
- !ruby/object:Gem::Dependency
|
|
99
|
+
name: ostruct
|
|
100
|
+
requirement: !ruby/object:Gem::Requirement
|
|
101
|
+
requirements:
|
|
102
|
+
- - ">="
|
|
103
|
+
- !ruby/object:Gem::Version
|
|
104
|
+
version: '0'
|
|
105
|
+
type: :development
|
|
106
|
+
prerelease: false
|
|
107
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
108
|
+
requirements:
|
|
109
|
+
- - ">="
|
|
110
|
+
- !ruby/object:Gem::Version
|
|
111
|
+
version: '0'
|
|
99
112
|
- !ruby/object:Gem::Dependency
|
|
100
113
|
name: pry
|
|
101
114
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -142,14 +155,14 @@ dependencies:
|
|
|
142
155
|
name: psych
|
|
143
156
|
requirement: !ruby/object:Gem::Requirement
|
|
144
157
|
requirements:
|
|
145
|
-
- - "
|
|
158
|
+
- - ">="
|
|
146
159
|
- !ruby/object:Gem::Version
|
|
147
160
|
version: '3'
|
|
148
161
|
type: :development
|
|
149
162
|
prerelease: false
|
|
150
163
|
version_requirements: !ruby/object:Gem::Requirement
|
|
151
164
|
requirements:
|
|
152
|
-
- - "
|
|
165
|
+
- - ">="
|
|
153
166
|
- !ruby/object:Gem::Version
|
|
154
167
|
version: '3'
|
|
155
168
|
- !ruby/object:Gem::Dependency
|
|
@@ -170,30 +183,30 @@ dependencies:
|
|
|
170
183
|
name: rspec
|
|
171
184
|
requirement: !ruby/object:Gem::Requirement
|
|
172
185
|
requirements:
|
|
173
|
-
- - "
|
|
186
|
+
- - ">="
|
|
174
187
|
- !ruby/object:Gem::Version
|
|
175
188
|
version: 3.6.0
|
|
176
189
|
type: :development
|
|
177
190
|
prerelease: false
|
|
178
191
|
version_requirements: !ruby/object:Gem::Requirement
|
|
179
192
|
requirements:
|
|
180
|
-
- - "
|
|
193
|
+
- - ">="
|
|
181
194
|
- !ruby/object:Gem::Version
|
|
182
195
|
version: 3.6.0
|
|
183
196
|
- !ruby/object:Gem::Dependency
|
|
184
197
|
name: rubocop
|
|
185
198
|
requirement: !ruby/object:Gem::Requirement
|
|
186
199
|
requirements:
|
|
187
|
-
- - "
|
|
200
|
+
- - ">="
|
|
188
201
|
- !ruby/object:Gem::Version
|
|
189
|
-
version: '1'
|
|
202
|
+
version: '1.0'
|
|
190
203
|
type: :development
|
|
191
204
|
prerelease: false
|
|
192
205
|
version_requirements: !ruby/object:Gem::Requirement
|
|
193
206
|
requirements:
|
|
194
|
-
- - "
|
|
207
|
+
- - ">="
|
|
195
208
|
- !ruby/object:Gem::Version
|
|
196
|
-
version: '1'
|
|
209
|
+
version: '1.0'
|
|
197
210
|
description: Adds typed jsonb backed fields to your ActiveRecord models.
|
|
198
211
|
email:
|
|
199
212
|
- michael@crismali.com
|
|
@@ -203,15 +216,15 @@ executables: []
|
|
|
203
216
|
extensions: []
|
|
204
217
|
extra_rdoc_files: []
|
|
205
218
|
files:
|
|
219
|
+
- ".github/dependabot.yml"
|
|
206
220
|
- ".github/workflows/ci.yml"
|
|
221
|
+
- ".github/workflows/push_gem.yml"
|
|
207
222
|
- ".gitignore"
|
|
208
223
|
- ".rspec"
|
|
209
224
|
- ".rubocop.yml"
|
|
210
225
|
- ".ruby-version"
|
|
211
|
-
- Appraisals
|
|
212
226
|
- CHANGELOG.md
|
|
213
227
|
- CODE_OF_CONDUCT.md
|
|
214
|
-
- Dockerfile
|
|
215
228
|
- Gemfile
|
|
216
229
|
- LICENSE.txt
|
|
217
230
|
- Makefile
|
|
@@ -223,10 +236,6 @@ files:
|
|
|
223
236
|
- db/config.yml
|
|
224
237
|
- db/migrate/20150407031737_set_up_testing_db.rb
|
|
225
238
|
- db/schema.rb
|
|
226
|
-
- docker-compose.yml
|
|
227
|
-
- gemfiles/activerecord_6.1.gemfile
|
|
228
|
-
- gemfiles/activerecord_7.0.gemfile
|
|
229
|
-
- gemfiles/activerecord_7.1.gemfile
|
|
230
239
|
- jsonb_accessor.gemspec
|
|
231
240
|
- lib/jsonb_accessor.rb
|
|
232
241
|
- lib/jsonb_accessor/attribute_query_methods.rb
|
|
@@ -235,11 +244,10 @@ files:
|
|
|
235
244
|
- lib/jsonb_accessor/query_builder.rb
|
|
236
245
|
- lib/jsonb_accessor/query_helper.rb
|
|
237
246
|
- lib/jsonb_accessor/version.rb
|
|
238
|
-
homepage: https://github.com/
|
|
247
|
+
homepage: https://github.com/madeintandem/jsonb_accessor
|
|
239
248
|
licenses:
|
|
240
249
|
- MIT
|
|
241
250
|
metadata: {}
|
|
242
|
-
post_install_message:
|
|
243
251
|
rdoc_options: []
|
|
244
252
|
require_paths:
|
|
245
253
|
- lib
|
|
@@ -254,8 +262,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
254
262
|
- !ruby/object:Gem::Version
|
|
255
263
|
version: '0'
|
|
256
264
|
requirements: []
|
|
257
|
-
rubygems_version: 3.
|
|
258
|
-
signing_key:
|
|
265
|
+
rubygems_version: 3.6.7
|
|
259
266
|
specification_version: 4
|
|
260
267
|
summary: Adds typed jsonb backed fields to your ActiveRecord models.
|
|
261
268
|
test_files: []
|
data/Appraisals
DELETED
data/Dockerfile
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
ARG RUBY_VERSION
|
|
2
|
-
ARG RUBY_PLATFORM
|
|
3
|
-
FROM ${RUBY_PLATFORM}:${RUBY_VERSION}
|
|
4
|
-
|
|
5
|
-
RUN apt-get update && apt-get install -y --no-install-recommends git
|
|
6
|
-
|
|
7
|
-
WORKDIR /usr/src/app
|
|
8
|
-
|
|
9
|
-
COPY lib/jsonb_accessor/version.rb ./lib/jsonb_accessor/version.rb
|
|
10
|
-
COPY jsonb_accessor.gemspec Gemfile ./
|
|
11
|
-
RUN bundle install
|
|
12
|
-
COPY . ./
|
data/docker-compose.yml
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
version: '3'
|
|
2
|
-
|
|
3
|
-
services:
|
|
4
|
-
ruby:
|
|
5
|
-
environment:
|
|
6
|
-
- DATABASE_HOST=postgres
|
|
7
|
-
build:
|
|
8
|
-
args:
|
|
9
|
-
- RUBY_VERSION=${RUBY_VERSION:-3.2.2}
|
|
10
|
-
- RUBY_PLATFORM=${RUBY_PLATFORM:-ruby}
|
|
11
|
-
context: .
|
|
12
|
-
volumes:
|
|
13
|
-
- '.:/usr/src/app'
|
|
14
|
-
depends_on:
|
|
15
|
-
- postgres
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
postgres:
|
|
19
|
-
image: postgres
|
|
20
|
-
environment:
|
|
21
|
-
- POSTGRES_HOST_AUTH_METHOD=trust
|
|
22
|
-
- POSTGRES_DB=jsonb_accessor
|
|
23
|
-
- PGDATA=/var/lib/postgresql/data/pgdata
|
|
24
|
-
volumes:
|
|
25
|
-
- pg_data:/var/lib/postgresql/data/pgdata
|
|
26
|
-
ports:
|
|
27
|
-
- 5432:5432
|
|
28
|
-
|
|
29
|
-
volumes:
|
|
30
|
-
pg_data:
|