attr_json 1.3.0 → 1.4.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: 026a86cb550ed91faef1a0aa1eca9dd0fa1985d1d3ac3221da2a165d19145ef9
4
- data.tar.gz: be400202cc1016c053ffcdced03d17706601141e402d8998da7c0a3d3cea4815
3
+ metadata.gz: 3f2658693e8b0c4d99e412e33c8d2ef67a70bb71302f0d3d079326bef2380c44
4
+ data.tar.gz: 6acbf7ca9e743a2a22550731baf495306bc087fd3787c02ea19435b94f96a85d
5
5
  SHA512:
6
- metadata.gz: 98e164f55a903e7babe2a94cfb794d448391bfc75f080d9309f766764e15762f47f524aff7faa5933957302ad36af6f3b0e2ee0e323e0f32f20abbd8eb6e1e2f
7
- data.tar.gz: 17e789b630a3a131a57cb8f5805a53641c9802f5e711cad38af73440aa37beb33f1f9a636479fcc63a5b892251c09408a7478746ceeb2df960d207945bea2668
6
+ metadata.gz: 9a3cf5a739623ba998162b0072b351a2ae07316691dc013c2f88820dc36d5119833d64bc89335379dbd636f65fcf4253f818649f57538c753b0a5d98fc7c5b6c
7
+ data.tar.gz: 79a977eb57509ad7035bb5d20e6212568d51b8062de11d536bbeae58acab8bc8db48eba181adeecff97286fa59042b6eb143359b41a7c65d73955fb18c2edc75
@@ -53,10 +53,19 @@ jobs:
53
53
  - gemfile: rails_6_1
54
54
  ruby: 2.7
55
55
 
56
+ - gemfile: rails_6_1
57
+ ruby: '3.0'
58
+
59
+ - gemfile: rails_7_0
60
+ ruby: '3.0'
61
+
62
+ - gemfile: rails_7_0
63
+ ruby: 3.1
64
+
56
65
  name: ${{ matrix.gemfile }}, ruby ${{ matrix.ruby }}
57
66
 
58
67
  steps:
59
- - uses: actions/checkout@v2
68
+ - uses: actions/checkout@v3
60
69
 
61
70
  - name: Set up Ruby
62
71
  uses: ruby/setup-ruby@v1
@@ -45,7 +45,7 @@ jobs:
45
45
  include:
46
46
 
47
47
  - gemfile: rails_edge
48
- ruby: 2.7
48
+ ruby: '3.0'
49
49
 
50
50
  name: ${{ matrix.gemfile }}, ruby ${{ matrix.ruby }}
51
51
 
data/Appraisals CHANGED
@@ -35,20 +35,28 @@ end
35
35
  appraise "rails-6-1" do
36
36
  gem 'combustion', "~> 1.0"
37
37
 
38
- gem "rails", ">= 6.0.0.rc1", "< 6.2.0.a"
38
+ gem "rails", "~> 6.1.0"
39
39
  gem "pg", "~> 1.0"
40
+
41
+ # sprockets-rails is already a rails 6.1 dependency, but combustion is failing
42
+ # to require it, this is one way to get it required.
43
+ # https://github.com/pat/combustion/issues/128
44
+ gem "sprockets-rails"
40
45
  end
41
46
 
42
- appraise "rails-edge-6" do
43
- # Edge rails needs unreleased combustion
44
- # https://github.com/pat/combustion/issues/92
47
+ appraise "rails-7-0" do
45
48
  gem 'combustion', "~> 1.0"
46
49
 
47
- gem "rails", git: "https://github.com/rails/rails.git", branch: "master"
50
+ gem "rails", "~> 7.0.0"
48
51
  gem "pg", "~> 1.0"
52
+ end
49
53
 
50
- # We don't actually use coffeescript at all, we need coffee-rails as an explicit
51
- # dependency just for transitory edge weirdness using current sprockets release
52
- # with rails 6 edge.
53
- gem 'coffee-rails'
54
+ appraise "rails-edge" do
55
+ # need combustion edge to work with rails edge, will no longer
56
+ # be true on next combustion release, probably no later than Rails 7.1
57
+ # https://github.com/pat/combustion/pull/126
58
+ gem 'combustion', "~> 1.0", github: "pat/combustion"
59
+
60
+ gem "rails", git: "https://github.com/rails/rails.git", branch: "main"
61
+ gem "pg", "~> 1.0"
54
62
  end
data/CHANGELOG.md CHANGED
@@ -4,7 +4,28 @@ 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/v1.3.0...HEAD)
7
+ ## [Unreleased](https://github.com/jrochkind/attr_json/compare/v1.4.0...HEAD)
8
+
9
+
10
+ ## [1.4.1](https://github.com/jrochkind/attr_json/compare/v1.4.0...v1.4.1)
11
+
12
+ ### Fixed
13
+
14
+ * Fixed an obscure bug involving a conflict between attribute defaults and accepts_nested_attributes, in which defaults could overwrite assigned attributes. The `.fill_in_defaults` class method, which was never intended as public API and was commented accordingly, is gone. https://github.com/jrochkind/attr_json/pull/160
15
+
16
+ ## [1.4.0](https://github.com/jrochkind/attr_json/compare/v1.3.0...v1.4.0)
17
+
18
+ ### Changed
19
+
20
+ * When using store_key feature on an AttrJson::Model, you should not be able to pass in the store_key as a key in initializer or assign_attributes. It was really a bug that this ended up mapped to attribute this way, which could cause a problem in some cases; but calling it out in Changed section because if you were accidentally relying on it, it may appear as a backwards incompat to you. https://github.com/jrochkind/attr_json/pull/125
21
+
22
+ * Rails 7.0.0 allowed by gemspec and tested in CI
23
+
24
+ ### Fixed
25
+
26
+ * polymorphic single type can be set to nil https://github.com/jrochkind/attr_json/pull/115
27
+ * polymorphic models can be serialized from hash in container attribute. Thanks @machty. https://github.com/jrochkind/attr_json/pull/123
28
+ * fix bug with deserialization of nested attributes that have types that apply different serialization vs cast logic. Thanks @bradleesand. https://github.com/jrochkind/attr_json/pull/125
8
29
 
9
30
  ## [1.3.0](https://github.com/jrochkind/attr_json/compare/v1.2.0...v1.3.0)
10
31
 
data/Gemfile CHANGED
@@ -18,17 +18,24 @@ gem 'combustion', '~> 1.1'
18
18
  # be the way to do it.
19
19
  gem 'rails'
20
20
 
21
- # We should not really need to mention railties, it's already a dependency of
22
- # rails, but seems to be necessary to get around some mystery bug in bundler
23
- # dependency resolution.
24
- gem 'railties'
25
-
26
21
  gem "pg"
27
22
  gem "rspec-rails", "~> 4.0"
28
23
  gem "simple_form", ">= 4.0"
29
24
  gem 'cocoon', ">= 1.2"
30
25
  gem 'jquery-rails'
31
26
 
27
+ # Even though we don't use coffee-script, when running specs, some part of rails
28
+ # or other part of our stack is still insisting on requiring it, for reasons we
29
+ # don't understand, so we need to depend on it.
30
+ gem "coffee-rails"
31
+
32
+ # We do some tests using cocoon via sprockets, which needs sprockets-rails,
33
+ # which is not automatically available in Rails 7. We add it explicitly,
34
+ # which will duplciate dependences in rails pre-7, but add for rails 7. Not sure
35
+ # the future of cocoon in general. https://github.com/nathanvda/cocoon/issues/555
36
+ gem "sprockets-rails"
37
+
38
+
32
39
  gem 'capybara', "~> 3.0"
33
40
  gem 'webdrivers', '~> 4.0'
34
41
  gem "selenium-webdriver"
data/README.md CHANGED
@@ -398,27 +398,29 @@ Rails 5.1+ on `attr_json`s on your ActiveRecord classes that include
398
398
  `AttrJson::Record`, by including `AttrJson::Record::Dirty`.
399
399
  Change-tracking methods are available off the `attr_json_changes` method.
400
400
 
401
- class MyModel < ActiveRecord::Base
402
- include AttrJson::Record
403
- include AttrJson::Record::Dirty
404
-
405
- attr_json :str, :string
406
- end
407
-
408
- model = MyModel.new
409
- model.str = "old"
410
- model.save
411
- model.str = "new"
401
+ ```ruby
402
+ class MyModel < ActiveRecord::Base
403
+ include AttrJson::Record
404
+ include AttrJson::Record::Dirty
412
405
 
413
- # All and only "new" style dirty tracking methods (Raisl 5.1+)
414
- # are available:
406
+ attr_json :str, :string
407
+ end
415
408
 
416
- model.attr_json_changes.saved_changes
417
- model.attr_json_changes.changes_to_save
418
- model.attr_json_changes.saved_change_to_str?
419
- model.attr_json_changes.saved_change_to_str
420
- model.attr_json_changes.will_save_change_to_str?
421
- # etc
409
+ model = MyModel.new
410
+ model.str = "old"
411
+ model.save
412
+ model.str = "new"
413
+
414
+ # All and only "new" style dirty tracking methods (Raisl 5.1+)
415
+ # are available:
416
+
417
+ model.attr_json_changes.saved_changes
418
+ model.attr_json_changes.changes_to_save
419
+ model.attr_json_changes.saved_change_to_str?
420
+ model.attr_json_changes.saved_change_to_str
421
+ model.attr_json_changes.will_save_change_to_str?
422
+ # etc
423
+ ```
422
424
 
423
425
  More options are available, including merging changes from 'ordinary'
424
426
  ActiveRecord attributes in. See docs on [Dirty Tracking](./doc_src/dirty_tracking.md)
@@ -476,11 +478,15 @@ to prevent overwriting other updates from processes.
476
478
 
477
479
  ## State of Code, and To Be Done
478
480
 
479
- The functionality that is here seems pretty solid, and is being used by jrochkind in a production app.
481
+ This code is solid and stable and is being used in production by at least a handful of people, including the primary maintainer, jrochkind.
482
+
483
+ The project is currently getting very little maintainance -- and is still working reliably through Rails releases. It is tested on edge rails and ruby (and has needed very few if any changes with subsequent releases), and I endeavor to keep it working as Rails keeps releasing.
484
+
485
+ In order to keep the low-maintenace scenario sustainable, I am *very* cautious accepting new features, especially if they increase code complexity at all. Even if you have a working PR, I may be reluctant to accept it. I'm prioritizing sustainability and stability over new features, and so far this is working out well. However, discussion is always welcome! Especially when paired with code (failing tests for the bugfix or feature you want are super helpful on their own!).
480
486
 
481
487
  We are committed to [semantic versioning](https://semver.org/) and will endeavor to release no backwards breaking changes without a major version. We are also serious about minimizing backwards incompat releases altogether (ie minimiing major version releases).
482
488
 
483
- Feedback of any kind of _very welcome_, please feel free to use the issue tracker.
489
+ Feedback of any kind of _very welcome_, please feel free to use the issue tracker. It is hard to get a sense of how many people are actually using this, which is helpful both for my own sense of reward and for anyone to get a sense of the size of the userbase -- feel free to say hi and let us know how you are using it!
484
490
 
485
491
  Except for the jsonb_contains stuff using postgres jsonb contains operator, I don't believe any postgres-specific features are used. It ought to work with MySQL, testing and feedback welcome. (Or a PR to test on MySQL?). My own interest is postgres.
486
492
 
@@ -488,26 +494,30 @@ This is still mostly a single-maintainer operation, so has all the sustainabilit
488
494
 
489
495
  ### Possible future features:
490
496
 
491
- * partial updates for json hashes would be really nice: Using postgres jsonb merge operators to only overwrite what changed. In my initial attempts, AR doesn't make it easy to customize this.
497
+ * Make AttrJson::Model lean more heavily on ActiveModel::Attributes API that did not fully exist in first version of attr_json [hope for a future attr_json 2.0 release]
498
+
499
+ * Make AttrJson::Record insist on creating rails cover attributes (no longer optional) and integrating more fully into rails, including rails dirty tricking, eliminating need for custom dirty tracking implementation. Overall decrease in lines of code. Can use jsonb_accessor as guide for some aspects. [hope for inclusion in future attr_json 2.0 release]
500
+
501
+ * partial updates for json hashes would be really nice: Using postgres jsonb merge operators to only overwrite what changed. In my initial attempts, AR doesn't make it easy to customize this. [update: this is hard, probably not coming soon]
492
502
 
493
- * seamless compatibility with ransack
503
+ * seamless compatibility with ransack [update: not necessarily prioritized]
494
504
 
495
505
  * Should we give AttrJson::Model a before_serialize hook that you might
496
506
  want to use similar to AR before_save? Should AttrJson::Models
497
- raise on trying to serialize an invalid model?
507
+ raise on trying to serialize an invalid model? [update: eh, hasn't really come up]
498
508
 
499
509
  * There are limits to what you can do with just jsonb_contains
500
510
  queries. We could support operations like `>`, `<`, `<>`
501
511
  as [jsonb_accessor](https://github.com/devmynd/jsonb_accessor),
502
512
  even accross keypaths. (At present, you could use a
503
513
  before_savee to denormalize/renormalize copy your data into
504
- ordinary AR columns/associations for searching. Or perhaps a postgres ts_vector for text searching. Needs to be worked out.)
514
+ ordinary AR columns/associations for searching. Or perhaps a postgres ts_vector for text searching. Needs to be worked out.) [update: interested, but not necessarily prioritized. This one would be interesting for a third-party PR draft!]
505
515
 
506
516
  * We could/should probably support `jsonb_order` clauses, even
507
- accross key paths, like jsonb_accessor.
517
+ accross key paths, like jsonb_accessor. [update: interested but not necessarily prioritized]
508
518
 
509
519
  * Could we make these attributes work in ordinary AR where, same
510
- as they do in jsonb_contains? Maybe.
520
+ as they do in jsonb_contains? Maybe. [update: probably not]
511
521
 
512
522
  ## Development
513
523
 
data/attr_json.gemspec CHANGED
@@ -44,14 +44,30 @@ attributes use as much of the existing ActiveRecord architecture as we can.}
44
44
 
45
45
  # Only to get CI to work on versions of Rails other than we release with,
46
46
  # should never release a gem with RAILS_GEM set!
47
- unless ENV['APPRAISAL_INITIALIZED'] || ENV["TRAVIS"]
48
- spec.add_runtime_dependency "activerecord", ">= 5.0.0", "< 6.2"
47
+ unless ENV['APPRAISAL_INITIALIZED'] || ENV["TRAVIS"] || ENV['CI']
48
+ spec.add_runtime_dependency "activerecord", ">= 5.0.0", "< 7.1"
49
49
  end
50
50
 
51
51
  spec.add_development_dependency "bundler"
52
52
  spec.add_development_dependency "rake", ">= 10.0"
53
53
  spec.add_development_dependency "rspec", "~> 3.7"
54
- spec.add_development_dependency "database_cleaner", "~> 1.5"
55
54
  spec.add_development_dependency "yard-activesupport-concern"
56
55
  spec.add_development_dependency "appraisal", "~> 2.2"
56
+
57
+ # Working around annoying issue in selenium 3.x with ruby 3.0.
58
+ # we don't actually use rexml ourselves. selenium 3 is a dependency
59
+ # of webdrivers, and tries to use rexml without depending on it
60
+ # as is needed in ruby 3.
61
+ #
62
+ # https://github.com/SeleniumHQ/selenium/issues/9001
63
+ #
64
+ # if in the future you can remove this dependecy and still have tests pass
65
+ # under ruby 3.x, you're good.
66
+ spec.add_development_dependency "rexml"
67
+
68
+ # Used only for Capybara.server in our spec_helper.rb.
69
+ # webrick is no longer included in ruby 3.0, so has to
70
+ # be expressed as a dependecy, unless we switch
71
+ # capybara to use alternate webserver.
72
+ spec.add_development_dependency "webrick", "~> 1.0"
57
73
  end
@@ -4,12 +4,13 @@ source "https://rubygems.org"
4
4
 
5
5
  gem "combustion", "~> 0.9.0"
6
6
  gem "rails", "~> 5.0.0"
7
- gem "railties"
8
7
  gem "pg", "~> 0.18"
9
8
  gem "rspec-rails", "~> 4.0"
10
9
  gem "simple_form", ">= 4.0"
11
10
  gem "cocoon", ">= 1.2"
12
11
  gem "jquery-rails"
12
+ gem "coffee-rails"
13
+ gem "sprockets-rails"
13
14
  gem "capybara", "~> 3.0"
14
15
  gem "webdrivers", "~> 4.0"
15
16
  gem "selenium-webdriver"
@@ -4,12 +4,13 @@ source "https://rubygems.org"
4
4
 
5
5
  gem "combustion", "~> 0.9.0"
6
6
  gem "rails", "~> 5.1.0"
7
- gem "railties"
8
7
  gem "pg", "~> 1.0"
9
8
  gem "rspec-rails", "~> 4.0"
10
9
  gem "simple_form", ">= 4.0"
11
10
  gem "cocoon", ">= 1.2"
12
11
  gem "jquery-rails"
12
+ gem "coffee-rails"
13
+ gem "sprockets-rails"
13
14
  gem "capybara", "~> 3.0"
14
15
  gem "webdrivers", "~> 4.0"
15
16
  gem "selenium-webdriver"
@@ -4,12 +4,13 @@ source "https://rubygems.org"
4
4
 
5
5
  gem "combustion", "~> 0.9.0"
6
6
  gem "rails", "~> 5.2.0"
7
- gem "railties"
8
7
  gem "pg", "~> 1.0"
9
8
  gem "rspec-rails", "~> 4.0"
10
9
  gem "simple_form", ">= 4.0"
11
10
  gem "cocoon", ">= 1.2"
12
11
  gem "jquery-rails"
12
+ gem "coffee-rails"
13
+ gem "sprockets-rails"
13
14
  gem "capybara", "~> 3.0"
14
15
  gem "webdrivers", "~> 4.0"
15
16
  gem "selenium-webdriver"
@@ -4,12 +4,13 @@ source "https://rubygems.org"
4
4
 
5
5
  gem "combustion", "~> 1.0"
6
6
  gem "rails", ">= 6.0.0", "< 6.1"
7
- gem "railties"
8
7
  gem "pg", "~> 1.0"
9
8
  gem "rspec-rails", "~> 4.0"
10
9
  gem "simple_form", ">= 4.0"
11
10
  gem "cocoon", ">= 1.2"
12
11
  gem "jquery-rails"
12
+ gem "coffee-rails"
13
+ gem "sprockets-rails"
13
14
  gem "capybara", "~> 3.0"
14
15
  gem "webdrivers", "~> 4.0"
15
16
  gem "selenium-webdriver"
@@ -3,13 +3,14 @@
3
3
  source "https://rubygems.org"
4
4
 
5
5
  gem "combustion", "~> 1.0"
6
- gem "rails", "~> 6.1"
7
- gem "railties"
6
+ gem "rails", "~> 6.1.0"
8
7
  gem "pg", "~> 1.0"
9
8
  gem "rspec-rails", "~> 4.0"
10
9
  gem "simple_form", ">= 4.0"
11
10
  gem "cocoon", ">= 1.2"
12
11
  gem "jquery-rails"
12
+ gem "coffee-rails"
13
+ gem "sprockets-rails"
13
14
  gem "capybara", "~> 3.0"
14
15
  gem "webdrivers", "~> 4.0"
15
16
  gem "selenium-webdriver"
@@ -3,17 +3,17 @@
3
3
  source "https://rubygems.org"
4
4
 
5
5
  gem "combustion", "~> 1.0"
6
- gem "rails", git: "https://github.com/rails/rails.git", branch: "master"
7
- gem "railties"
6
+ gem "rails", "~> 7.0.0"
8
7
  gem "pg", "~> 1.0"
9
8
  gem "rspec-rails", "~> 4.0"
10
9
  gem "simple_form", ">= 4.0"
11
10
  gem "cocoon", ">= 1.2"
12
11
  gem "jquery-rails"
12
+ gem "coffee-rails"
13
+ gem "sprockets-rails"
13
14
  gem "capybara", "~> 3.0"
14
15
  gem "webdrivers", "~> 4.0"
15
16
  gem "selenium-webdriver"
16
17
  gem "byebug"
17
- gem "coffee-rails"
18
18
 
19
19
  gemspec path: "../"
@@ -2,18 +2,18 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "combustion", "~> 1.0"
6
- gem "rails", git: "https://github.com/rails/rails.git", branch: "master"
7
- gem "railties"
5
+ gem "combustion", "~> 1.0", github: "pat/combustion"
6
+ gem "rails", git: "https://github.com/rails/rails.git", branch: "main"
8
7
  gem "pg", "~> 1.0"
9
- gem "rspec-rails", "~> 3.7"
8
+ gem "rspec-rails", "~> 4.0"
10
9
  gem "simple_form", ">= 4.0"
11
10
  gem "cocoon", ">= 1.2"
12
11
  gem "jquery-rails"
12
+ gem "coffee-rails"
13
+ gem "sprockets-rails"
13
14
  gem "capybara", "~> 3.0"
14
15
  gem "webdrivers", "~> 4.0"
15
16
  gem "selenium-webdriver"
16
17
  gem "byebug"
17
- gem "coffee-rails"
18
18
 
19
19
  gemspec path: "../"
@@ -103,16 +103,27 @@ module AttrJson
103
103
  end
104
104
 
105
105
 
106
- # Like `.new`, but translate store keys in hash
106
+ # The inverse of model#serializable_hash -- re-hydrates a serialized hash to a model.
107
+ #
108
+ # Similar to `.new`, but translates things that need to be translated in deserialization,
109
+ # like store_keys, and properly calling deserialize on the underlying types.
110
+ #
111
+ # @example Model.new_from_serializable(hash)
107
112
  def new_from_serializable(attributes = {})
108
- attributes = attributes.transform_keys do |key|
113
+ attributes = attributes.collect do |key, value|
109
114
  # store keys in arguments get translated to attribute names on initialize.
110
115
  if attribute_def = self.attr_json_registry.store_key_lookup("", key.to_s)
111
- attribute_def.name.to_s
112
- else
113
- key
116
+ key = attribute_def.name.to_s
114
117
  end
115
- end
118
+
119
+ attr_type = self.attr_json_registry.has_attribute?(key) && self.attr_json_registry.type_for_attribute(key)
120
+ if attr_type
121
+ value = attr_type.deserialize(value)
122
+ end
123
+
124
+ [key, value]
125
+ end.to_h
126
+
116
127
  self.new(attributes)
117
128
  end
118
129
 
@@ -166,27 +177,6 @@ module AttrJson
166
177
  end
167
178
  end
168
179
 
169
- # This should kind of be considered 'protected', but the semantics
170
- # of how we want to call it don't give us a visibility modifier that works.
171
- # Prob means refactoring called for. TODO?
172
- def fill_in_defaults(hash)
173
- # Only if we need to mutate it to add defaults, we'll dup it first. deep_dup not neccesary
174
- # since we're only modifying top-level here.
175
- duped = false
176
- attr_json_registry.definitions.each do |definition|
177
- if definition.has_default? && ! (hash.has_key?(definition.store_key.to_s) || hash.has_key?(definition.store_key.to_sym))
178
- unless duped
179
- hash = hash.dup
180
- duped = true
181
- end
182
-
183
- hash[definition.store_key] = definition.provide_default!
184
- end
185
- end
186
-
187
- hash
188
- end
189
-
190
180
  private
191
181
 
192
182
  # Define an anonymous module and include it, so can still be easily
@@ -202,11 +192,9 @@ module AttrJson
202
192
  end
203
193
 
204
194
  def initialize(attributes = {})
205
- if !attributes.respond_to?(:transform_keys)
206
- raise ArgumentError, "When assigning attributes, you must pass a hash as an argument."
207
- end
195
+ super
208
196
 
209
- super(self.class.fill_in_defaults(attributes))
197
+ fill_in_defaults!
210
198
  end
211
199
 
212
200
  def attributes
@@ -292,6 +280,14 @@ module AttrJson
292
280
 
293
281
  private
294
282
 
283
+ def fill_in_defaults!
284
+ self.class.attr_json_registry.definitions.each do |definition|
285
+ if definition.has_default? && !attributes.has_key?(definition.name.to_s)
286
+ self.send("#{definition.name.to_s}=", definition.provide_default!)
287
+ end
288
+ end
289
+ end
290
+
295
291
  def _attr_json_write(key, value)
296
292
  if attribute_def = self.class.attr_json_registry[key.to_sym]
297
293
  attributes[key.to_s] = attribute_def.cast(value)
@@ -270,7 +270,13 @@ module AttrJson
270
270
  # find it from currently declared attributes.
271
271
  # https://github.com/rails/rails/blob/6aa5cf03ea8232180ffbbae4c130b051f813c670/activemodel/lib/active_model/attribute_methods.rb#L463-L468
272
272
  def matched_attribute_method(method_name)
273
- matches = self.class.send(:attribute_method_matchers_matching, method_name)
273
+ if self.class.respond_to?(:attribute_method_patterns_matching, true)
274
+ # Rails 7.1+
275
+ matches = self.class.send(:attribute_method_patterns_matching, method_name)
276
+ else
277
+ matches = self.class.send(:attribute_method_matchers_matching, method_name)
278
+ end
279
+
274
280
  matches.detect do |match|
275
281
  registry.has_attribute?(match.attr_name)
276
282
  end
@@ -32,10 +32,10 @@ module AttrJson
32
32
  # to_hash is actually the 'implicit' conversion, it really is a hash
33
33
  # even though it isn't is_a?(Hash), try to_hash first before to_h,
34
34
  # the explicit conversion.
35
- model.new_from_serializable(v.to_hash)
35
+ model.new(v.to_hash)
36
36
  elsif v.respond_to?(:to_h)
37
37
  # TODO Maybe we ought not to do this on #to_h?
38
- model.new_from_serializable(v.to_h)
38
+ model.new(v.to_h)
39
39
  elsif model.attr_json_config.bad_cast == :as_nil
40
40
  # This was originally default behavior, to be like existing ActiveRecord
41
41
  # which kind of silently does this for non-castable basic values. That
@@ -58,7 +58,31 @@ module AttrJson
58
58
  end
59
59
 
60
60
  def deserialize(v)
61
- cast(v)
61
+ if v.nil?
62
+ # important to stay nil instead of empty object, because they
63
+ # are different things.
64
+ v
65
+ elsif v.kind_of? model
66
+ v
67
+ elsif v.respond_to?(:to_hash)
68
+ # to_hash is actually the 'implicit' conversion, it really is a hash
69
+ # even though it isn't is_a?(Hash), try to_hash first before to_h,
70
+ # the explicit conversion.
71
+ model.new_from_serializable(v.to_hash)
72
+ elsif v.respond_to?(:to_h)
73
+ # TODO Maybe we ought not to do this on #to_h? especially here in deserialize?
74
+ model.new_from_serializable(v.to_h)
75
+ elsif model.attr_json_config.bad_cast == :as_nil
76
+ # TODO should we have different config value for bad_deserialize vs bad_cast?
77
+
78
+ # This was originally default behavior, to be like existing ActiveRecord
79
+ # which kind of silently does this for non-castable basic values. That
80
+ # ended up being confusing in the basic case, so now we raise by default,
81
+ # but this is still configurable.
82
+ nil
83
+ else
84
+ raise BadCast.new("Can not cast from #{v.inspect} to #{self.type}")
85
+ end
62
86
  end
63
87
 
64
88
  # these guys are definitely mutable, so we need this.
@@ -114,6 +114,11 @@ module AttrJson
114
114
  end
115
115
 
116
116
  def serialize(v)
117
+ return nil if v.nil?
118
+
119
+ # if it's not already a model cast it to a model if possible (eg it's a hash)
120
+ v = cast(v)
121
+
117
122
  model_name = v.class.name
118
123
  type = type_for_model_name(model_name)
119
124
 
@@ -1,3 +1,3 @@
1
1
  module AttrJson
2
- VERSION = "1.3.0"
2
+ VERSION = "1.4.1"
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: 1.3.0
4
+ version: 1.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonathan Rochkind
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-12-13 00:00:00.000000000 Z
11
+ date: 2022-12-20 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: 5.0.0
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '6.2'
22
+ version: '7.1'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -29,7 +29,7 @@ dependencies:
29
29
  version: 5.0.0
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '6.2'
32
+ version: '7.1'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: bundler
35
35
  requirement: !ruby/object:Gem::Requirement
@@ -73,21 +73,35 @@ dependencies:
73
73
  - !ruby/object:Gem::Version
74
74
  version: '3.7'
75
75
  - !ruby/object:Gem::Dependency
76
- name: database_cleaner
76
+ name: yard-activesupport-concern
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: appraisal
77
91
  requirement: !ruby/object:Gem::Requirement
78
92
  requirements:
79
93
  - - "~>"
80
94
  - !ruby/object:Gem::Version
81
- version: '1.5'
95
+ version: '2.2'
82
96
  type: :development
83
97
  prerelease: false
84
98
  version_requirements: !ruby/object:Gem::Requirement
85
99
  requirements:
86
100
  - - "~>"
87
101
  - !ruby/object:Gem::Version
88
- version: '1.5'
102
+ version: '2.2'
89
103
  - !ruby/object:Gem::Dependency
90
- name: yard-activesupport-concern
104
+ name: rexml
91
105
  requirement: !ruby/object:Gem::Requirement
92
106
  requirements:
93
107
  - - ">="
@@ -101,19 +115,19 @@ dependencies:
101
115
  - !ruby/object:Gem::Version
102
116
  version: '0'
103
117
  - !ruby/object:Gem::Dependency
104
- name: appraisal
118
+ name: webrick
105
119
  requirement: !ruby/object:Gem::Requirement
106
120
  requirements:
107
121
  - - "~>"
108
122
  - !ruby/object:Gem::Version
109
- version: '2.2'
123
+ version: '1.0'
110
124
  type: :development
111
125
  prerelease: false
112
126
  version_requirements: !ruby/object:Gem::Requirement
113
127
  requirements:
114
128
  - - "~>"
115
129
  - !ruby/object:Gem::Version
116
- version: '2.2'
130
+ version: '1.0'
117
131
  description: |-
118
132
  ActiveRecord attributes stored serialized in a json column, super smooth.
119
133
  For Rails 5.0, 5.1, or 5.2. Typed and cast like Active Record. Supporting nested models,
@@ -153,8 +167,8 @@ files:
153
167
  - gemfiles/rails_5_2.gemfile
154
168
  - gemfiles/rails_6_0.gemfile
155
169
  - gemfiles/rails_6_1.gemfile
170
+ - gemfiles/rails_7_0.gemfile
156
171
  - gemfiles/rails_edge.gemfile
157
- - gemfiles/rails_edge_6.gemfile
158
172
  - lib/attr_json.rb
159
173
  - lib/attr_json/attribute_definition.rb
160
174
  - lib/attr_json/attribute_definition/registry.rb
@@ -182,7 +196,7 @@ licenses:
182
196
  metadata:
183
197
  homepage_uri: https://github.com/jrochkind/attr_json
184
198
  source_code_uri: https://github.com/jrochkind/attr_json
185
- post_install_message:
199
+ post_install_message:
186
200
  rdoc_options: []
187
201
  require_paths:
188
202
  - lib
@@ -197,8 +211,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
197
211
  - !ruby/object:Gem::Version
198
212
  version: '0'
199
213
  requirements: []
200
- rubygems_version: 3.0.3
201
- signing_key:
214
+ rubygems_version: 3.2.33
215
+ signing_key:
202
216
  specification_version: 4
203
217
  summary: ActiveRecord attributes stored serialized in a json column, super smooth.
204
218
  test_files: []