attr_json 2.0.1 → 2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cd9c25a736bb3b1ce26527c555718efcff2967204d853962048ffc2471af243c
4
- data.tar.gz: dc022d69b43b4576d2939bb90f2e172b65b8dd45708b361758554561dc61f44f
3
+ metadata.gz: dadc04fb081fee0b326eaff03f569f15d745948119959e2a8eb5d91b690dec38
4
+ data.tar.gz: fb6a28e048e03822432e63a7205d0242d218ddace88dd0566624517c3a5454b5
5
5
  SHA512:
6
- metadata.gz: 6457f06b9aed14481b7dd1543929d7010b54d232473f64cde811452083279ab72bbeb92f7a972e903d8994f21f3e263c5163044aa363db4c35a7eadd42710f24
7
- data.tar.gz: 8dd0bad4eb89aad50ceb084ecb48a08d0f4b19d67f9421250fb808a2238c159fce891d1e07d37568b6ba6c65c284a9b2b600e1156cf32334a2c7d518d468994d
6
+ metadata.gz: 6048863f54826dc166200400a93cdfdb91055ddba960d6d2b8587077eafda5e3058e49d030f60c3eb05f74ba2ea99512c087c403f8eb0a18e4dd45c4f7929bee
7
+ data.tar.gz: afafb7cf57637cce5f99c3afacd38bc802aca8c5e60b9c0cd1adebc08a482b064ce5a7398c44507b612f94691fd8362bba9cb00d13f8d14603524e235e93b617
@@ -49,7 +49,15 @@ jobs:
49
49
 
50
50
  - gemfile: rails_7_0
51
51
  ruby: 3.2
52
+
53
+ - gemfile: rails_7_1
54
+ ruby: '3.0'
55
+
56
+ - gemfile: rails_7_1
57
+ ruby: 3.1
52
58
 
59
+ - gemfile: rails_7_1
60
+ ruby: 3.2
53
61
 
54
62
  name: ${{ matrix.gemfile }}, ruby ${{ matrix.ruby }}
55
63
 
data/Appraisals CHANGED
@@ -4,6 +4,11 @@ appraise "rails-6-0" do
4
4
  gem "rails", ">= 6.0.0", "< 6.1"
5
5
  gem "pg", "~> 1.0"
6
6
  gem "rspec-rails", "~> 5.0"
7
+
8
+
9
+ # Ruby 2.7 still needs webdrivers, since it can't use a new enough
10
+ # version of selenium-webdriver to download drivers itself
11
+ gem "webdrivers", ">= 5.3.1"
7
12
  end
8
13
 
9
14
  appraise "rails-6-1" do
@@ -25,6 +30,13 @@ appraise "rails-7-0" do
25
30
  gem "pg", "~> 1.0"
26
31
  end
27
32
 
33
+ appraise "rails-7-1" do
34
+ gem 'combustion', "~> 1.0"
35
+
36
+ gem "rails", "~> 7.1.0"
37
+ gem "pg", "~> 1.0"
38
+ end
39
+
28
40
  appraise "rails-edge" do
29
41
  # need combustion edge to work with rails edge, will no longer
30
42
  # be true on next combustion release, probably no later than Rails 7.1
data/CHANGELOG.md CHANGED
@@ -4,7 +4,7 @@ Notable changes to this project will be documented in this file.
4
4
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
5
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
- ## [Unreleased](https://github.com/jrochkind/attr_json/compare/v2.0.1...HEAD)
7
+ ## [Unreleased](https://github.com/jrochkind/attr_json/compare/v2.2.0...HEAD)
8
8
 
9
9
  ### Fixed
10
10
 
@@ -22,6 +22,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
22
22
 
23
23
  *
24
24
 
25
+ ## [2.2.0](https://github.com/jrochkind/attr_json/compare/v2.1.0...v2.2.0)
26
+
27
+ ### Added
28
+
29
+ * Test and allow Rails 7.1 https://github.com/jrochkind/attr_json/pull/208 Thanks @JoeSouthan
30
+
31
+
32
+ ## [2.1.0](https://github.com/jrochkind/attr_json/compare/v2.0.1...v2.1.0)
33
+
34
+ ### Added
35
+
36
+ * Support for `store_key` in models used with polymorphic model feature. https://github.com/jrochkind/attr_json/pull/206 . Thanks @rdubya
37
+
38
+
25
39
  ## [2.0.1](https://github.com/jrochkind/attr_json/compare/v2.0.0...v2.0.1)
26
40
 
27
41
 
data/Gemfile CHANGED
@@ -37,7 +37,6 @@ gem "sprockets-rails"
37
37
 
38
38
 
39
39
  gem 'capybara', "~> 3.0"
40
- gem 'webdrivers', '~> 5.0'
41
40
  gem "selenium-webdriver"
42
41
 
43
42
  gem "byebug"
data/README.md CHANGED
@@ -49,6 +49,9 @@ class MyModel < ActiveRecord::Base
49
49
  # You can have an _array_ of those things too. It will ordinarily default to empty array.
50
50
  attr_json :int_array, :integer, array: true
51
51
 
52
+ # The empty array default can be disabled with the following setting
53
+ attr_json :str_array, :string, array: true, default: AttrJson::AttributeDefinition::NO_DEFAULT_PROVIDED
54
+
52
55
  #and/or defaults
53
56
  attr_json :str_with_default, :string, default: "default value"
54
57
 
@@ -186,10 +189,8 @@ attribute name, it'll actually query on store_key. And properly handles any
186
189
  `container_attribute` -- it'll look in the proper jsonb column.
187
190
 
188
191
  Anything you can do with `jsonb_contains` should be handled
189
- by a [postgres `USING GIN` index](https://www.postgresql.org/docs/9.5/static/datatype-json.html#JSON-INDEXING)
190
- (I think! can anyone help confirm/deny?). To be sure, I recommend you
191
- investigate: Check out `to_sql` on any query to see what jsonb SQL it generates,
192
- and explore if you have the indexes you need.
192
+ by a [postgres `USING GIN` index](https://www.postgresql.org/docs/9.5/static/datatype-json.html#JSON-INDEXING). Figuring out how to use indexes for jsonb
193
+ queries can be confusing, [here is a good blog post](https://blog.kiprosh.com/postgres-gin-index-in-rails/).
193
194
 
194
195
  <a name="nested"></a>
195
196
  ## Nested models -- Structured/compound data
@@ -362,6 +363,9 @@ end
362
363
 
363
364
  class MyTable < ApplicationRecord
364
365
  serialize :some_json_column, MyModel.to_serialization_coder
366
+
367
+ # NOTE: In Rails 7.1+, write:
368
+ # serialize :some_json_column, coder: MyModel.to_serialization_coder
365
369
  end
366
370
 
367
371
  MyTable.create(some_json_column: MyModel.new(some_string: "string"))
@@ -456,7 +460,7 @@ Why might you want this?
456
460
 
457
461
  * Single-Table Inheritance, with sub-classes that have non-shared
458
462
  data fields. You rather not make all those columns, some of which will then also appear
459
- to inapplicable sub-classes.
463
+ to inapplicable sub-classes. (**note** you may have trouble with [ActiveRecord #becomes](https://api.rubyonrails.org/v7.0.4/classes/ActiveRecord/Persistence.html#method-i-becomes) in some versions of Rails due to Rails bug. See https://github.com/jrochkind/attr_json/issues/189 and https://github.com/rails/rails/issues/47538))
460
464
 
461
465
  * A "content management system" type project, where you need complex
462
466
  structured data of various types, maybe needs to be vary depending
data/attr_json.gemspec CHANGED
@@ -42,10 +42,12 @@ attributes use as much of the existing ActiveRecord architecture as we can.}
42
42
 
43
43
  spec.required_ruby_version = '>= 2.6.0'
44
44
 
45
- # Only to get CI to work on versions of Rails other than we release with,
46
- # should never release a gem with RAILS_GEM set!
45
+ # This conditional is only to get CI to work on versions of Rails other than
46
+ # we release with. The gem should never be released without the activerecord
47
+ # dependency included just as it is here, should never be released
48
+ # from an env tht has any of these variables set.
47
49
  unless ENV['APPRAISAL_INITIALIZED'] || ENV["TRAVIS"] || ENV['CI']
48
- spec.add_runtime_dependency "activerecord", ">= 6.0.0", "< 7.1"
50
+ spec.add_runtime_dependency "activerecord", ">= 6.0.0", "< 7.2"
49
51
  end
50
52
 
51
53
  spec.add_development_dependency "bundler"
@@ -12,8 +12,8 @@ gem "jquery-rails"
12
12
  gem "coffee-rails"
13
13
  gem "sprockets-rails"
14
14
  gem "capybara", "~> 3.0"
15
- gem "webdrivers", "~> 5.0"
16
15
  gem "selenium-webdriver"
17
16
  gem "byebug"
17
+ gem "webdrivers", ">= 5.3.1"
18
18
 
19
19
  gemspec path: "../"
@@ -12,7 +12,6 @@ gem "jquery-rails"
12
12
  gem "coffee-rails"
13
13
  gem "sprockets-rails"
14
14
  gem "capybara", "~> 3.0"
15
- gem "webdrivers", "~> 5.0"
16
15
  gem "selenium-webdriver"
17
16
  gem "byebug"
18
17
 
@@ -12,7 +12,6 @@ gem "jquery-rails"
12
12
  gem "coffee-rails"
13
13
  gem "sprockets-rails"
14
14
  gem "capybara", "~> 3.0"
15
- gem "webdrivers", "~> 5.0"
16
15
  gem "selenium-webdriver"
17
16
  gem "byebug"
18
17
 
@@ -0,0 +1,18 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "combustion", "~> 1.0"
6
+ gem "rails", "~> 7.1.0"
7
+ gem "pg", "~> 1.0"
8
+ gem "rspec-rails", "~> 6.0"
9
+ gem "simple_form", ">= 4.0"
10
+ gem "cocoon", ">= 1.2"
11
+ gem "jquery-rails"
12
+ gem "coffee-rails"
13
+ gem "sprockets-rails"
14
+ gem "capybara", "~> 3.0"
15
+ gem "selenium-webdriver"
16
+ gem "byebug"
17
+
18
+ gemspec path: "../"
@@ -12,7 +12,6 @@ gem "jquery-rails"
12
12
  gem "coffee-rails"
13
13
  gem "sprockets-rails"
14
14
  gem "capybara", "~> 3.0"
15
- gem "webdrivers", "~> 5.0"
16
15
  gem "selenium-webdriver"
17
16
  gem "byebug"
18
17
  gem "rack", "~> 2.0"
@@ -168,6 +168,9 @@ module AttrJson
168
168
  #
169
169
  # class MyTable < ApplicationRecord
170
170
  # serialize :some_json_column, MyModel.to_serialization_coder
171
+ #
172
+ # # In Rails 7.1+:
173
+ # # serialize :some_json_column, coder: MyModel.to_serialization_coder
171
174
  # end
172
175
  #
173
176
  def to_serialization_coder
@@ -100,17 +100,11 @@ module AttrJson
100
100
  end
101
101
 
102
102
  def cast(v)
103
- if v.nil?
104
- v
105
- elsif model_names.include?(v.class.name)
106
- v
107
- elsif v.respond_to?(:to_hash)
108
- cast_from_hash(v.to_hash)
109
- elsif v.respond_to?(:to_h)
110
- cast_from_hash(v.to_h)
111
- else
112
- raise_bad_model_name(v.class, v)
113
- end
103
+ cast_or_deserialize(v, :cast)
104
+ end
105
+
106
+ def deserialize(v)
107
+ cast_or_deserialize(v, :deserialize)
114
108
  end
115
109
 
116
110
  def serialize(v)
@@ -127,10 +121,6 @@ module AttrJson
127
121
  type.serialize(v).merge(type_key => model_name)
128
122
  end
129
123
 
130
- def deserialize(v)
131
- cast(v)
132
- end
133
-
134
124
  def type_for_model_name(model_name)
135
125
  model_type_lookup[model_name]
136
126
  end
@@ -153,15 +143,29 @@ module AttrJson
153
143
 
154
144
  protected
155
145
 
156
- def raise_missing_type_key(value)
157
- raise TypeError, "AttrJson::Type::Polymorphic can't cast without '#{type_key}' key: #{value}"
158
- end
159
-
160
- def raise_bad_model_name(name, value)
161
- raise TypeError, "This AttrJson::Type::PolymorphicType can only include {#{model_names.join(', ')}}, not '#{name}': #{value.inspect}"
146
+ # We need to make sure to call the correct operation on
147
+ # the model type, so that we get the same result as if
148
+ # we had called the type directly
149
+ #
150
+ # @param v [Object, nil] the value to cast or deserialize
151
+ # @param operation [Symbol] :cast or :deserialize
152
+ def cast_or_deserialize(v, operation)
153
+ if v.nil?
154
+ v
155
+ elsif model_names.include?(v.class.name)
156
+ v
157
+ elsif v.respond_to?(:to_hash)
158
+ model_from_hash(v.to_hash, operation)
159
+ elsif v.respond_to?(:to_h)
160
+ model_from_hash(v.to_h, operation)
161
+ else
162
+ raise_bad_model_name(v.class, v)
163
+ end
162
164
  end
163
165
 
164
- def cast_from_hash(hash)
166
+ # @param hash [Hash] the value to cast or deserialize
167
+ # @param operation [Symbol] :cast or :deserialize
168
+ def model_from_hash(hash, operation)
165
169
  new_hash = hash.stringify_keys
166
170
  model_name = new_hash.delete(type_key.to_s)
167
171
 
@@ -171,7 +175,21 @@ module AttrJson
171
175
 
172
176
  raise_bad_model_name(model_name, hash) if type.nil?
173
177
 
174
- type.cast(new_hash)
178
+ if operation == :deserialize
179
+ type.deserialize(new_hash)
180
+ elsif operation == :cast
181
+ type.cast(new_hash)
182
+ else
183
+ raise ArgumentError, "Unknown operation #{operation}"
184
+ end
185
+ end
186
+
187
+ def raise_missing_type_key(value)
188
+ raise TypeError, "AttrJson::Type::Polymorphic can't cast without '#{type_key}' key: #{value}"
189
+ end
190
+
191
+ def raise_bad_model_name(name, value)
192
+ raise TypeError, "This AttrJson::Type::PolymorphicType can only include {#{model_names.join(', ')}}, not '#{name}': #{value.inspect}"
175
193
  end
176
194
  end
177
195
  end
@@ -1,3 +1,3 @@
1
1
  module AttrJson
2
- VERSION = "2.0.1"
2
+ VERSION = "2.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: attr_json
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonathan Rochkind
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-02-28 00:00:00.000000000 Z
11
+ date: 2023-10-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -19,7 +19,7 @@ dependencies:
19
19
  version: 6.0.0
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '7.1'
22
+ version: '7.2'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -29,7 +29,7 @@ dependencies:
29
29
  version: 6.0.0
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '7.1'
32
+ version: '7.2'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: bundler
35
35
  requirement: !ruby/object:Gem::Requirement
@@ -164,6 +164,7 @@ files:
164
164
  - gemfiles/rails_6_0.gemfile
165
165
  - gemfiles/rails_6_1.gemfile
166
166
  - gemfiles/rails_7_0.gemfile
167
+ - gemfiles/rails_7_1.gemfile
167
168
  - gemfiles/rails_edge.gemfile
168
169
  - lib/attr_json.rb
169
170
  - lib/attr_json/attribute_definition.rb
@@ -206,7 +207,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
206
207
  - !ruby/object:Gem::Version
207
208
  version: '0'
208
209
  requirements: []
209
- rubygems_version: 3.3.26
210
+ rubygems_version: 3.4.10
210
211
  signing_key:
211
212
  specification_version: 4
212
213
  summary: ActiveRecord attributes stored serialized in a json column, super smooth.