store_model 3.0.2 → 4.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -0
- data/lib/store_model/model.rb +1 -1
- data/lib/store_model/nested_attributes.rb +10 -9
- data/lib/store_model/types/enum_type.rb +1 -0
- data/lib/store_model/types/many.rb +1 -0
- data/lib/store_model/types/many_polymorphic.rb +1 -0
- data/lib/store_model/types/one.rb +12 -6
- data/lib/store_model/types/one_polymorphic.rb +8 -5
- data/lib/store_model/version.rb +1 -1
- metadata +11 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e3a1d792e0b67515ef121219d4568ef05a2589cb7bf1df33ac36af0f5e0b0efb
|
4
|
+
data.tar.gz: 98f11df54c5f5b6cd9d4604e889faf95e7b3f3dd85e97c37fe707c112565fc72
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 01a49de46f09b2bff6e25f760b2067e4984cf32851feea67bab4c95ff25bd1dd64f31346e286df2f532bbc6f4d37f9d6d8a3a16be1cacde32f64ccbc24fdc2bd
|
7
|
+
data.tar.gz: 73b1a450ba0f2eaca9f66c0761d30725af3403fa425bd8b7dca15b5d4d7c794948c9671ab6d6c78304a547db6401ba6388db0f703750483c47bc530995c6c1d5
|
data/README.md
CHANGED
@@ -22,6 +22,8 @@ class Product < ApplicationRecord
|
|
22
22
|
end
|
23
23
|
```
|
24
24
|
|
25
|
+
You can support my open–source work [here](https://boosty.to/dmitry_tsepelev).
|
26
|
+
|
25
27
|
## Why should I wrap my JSON columns?
|
26
28
|
|
27
29
|
Imagine that you have a model `Product` with a `jsonb` column called `configuration`. This is how you likely gonna work with this column:
|
data/lib/store_model/model.rb
CHANGED
@@ -228,7 +228,7 @@ module StoreModel
|
|
228
228
|
.attribute_types
|
229
229
|
.select { |_, type| type.is_a?(StoreModel::Types::EnumType) }
|
230
230
|
|
231
|
-
enum_mappings.
|
231
|
+
enum_mappings.each_key do |name|
|
232
232
|
next unless json.key?(name)
|
233
233
|
|
234
234
|
json[name] = public_send(name).as_json unless json[name].nil?
|
@@ -60,19 +60,20 @@ module StoreModel
|
|
60
60
|
# If schema loaded the attribute_types already populated and we can safely use it
|
61
61
|
# See ActiveRecord::ModelSchema#load_schema!
|
62
62
|
def nested_attribute_type(attribute)
|
63
|
-
|
64
|
-
attributes_to_define_after_schema_loads[attribute.to_s]&.first
|
65
|
-
else
|
66
|
-
attribute_types[attribute.to_s]
|
67
|
-
end
|
63
|
+
attribute_types[attribute.to_s]
|
68
64
|
end
|
69
65
|
|
70
|
-
def define_store_model_attr_accessors(attribute, options)
|
66
|
+
def define_store_model_attr_accessors(attribute, options) # rubocop:disable Metrics/MethodLength
|
71
67
|
case nested_attribute_type(attribute)
|
72
68
|
when Types::OneBase
|
73
69
|
define_association_setter_for_single(attribute, options)
|
74
70
|
define_method "#{attribute}_attributes=" do |*args, **kwargs|
|
75
|
-
send(
|
71
|
+
existing_model = send(attribute)
|
72
|
+
if existing_model && options[:update_only]
|
73
|
+
existing_model.assign_attributes(*args, **kwargs)
|
74
|
+
else
|
75
|
+
send("#{attribute}=", *args, **kwargs)
|
76
|
+
end
|
76
77
|
end
|
77
78
|
when Types::ManyBase
|
78
79
|
define_association_setter_for_many(attribute, options)
|
@@ -99,7 +100,7 @@ module StoreModel
|
|
99
100
|
return unless options&.dig(:allow_destroy)
|
100
101
|
|
101
102
|
define_method "#{association}=" do |attributes|
|
102
|
-
if ActiveRecord::Type::Boolean.new.cast(attributes.stringify_keys
|
103
|
+
if ActiveRecord::Type::Boolean.new.cast(attributes.stringify_keys["_destroy"])
|
103
104
|
super(nil)
|
104
105
|
else
|
105
106
|
super(attributes)
|
@@ -116,7 +117,7 @@ module StoreModel
|
|
116
117
|
|
117
118
|
if options&.dig(:allow_destroy)
|
118
119
|
attributes.reject! do |attribute|
|
119
|
-
ActiveRecord::Type::Boolean.new.cast(attribute.stringify_keys
|
120
|
+
ActiveRecord::Type::Boolean.new.cast(attribute.stringify_keys["_destroy"])
|
120
121
|
end
|
121
122
|
end
|
122
123
|
|
@@ -13,6 +13,7 @@ module StoreModel
|
|
13
13
|
# @return [StoreModel::Types::One]
|
14
14
|
def initialize(model_klass)
|
15
15
|
@model_klass = model_klass
|
16
|
+
super()
|
16
17
|
end
|
17
18
|
|
18
19
|
# Returns type
|
@@ -27,12 +28,17 @@ module StoreModel
|
|
27
28
|
# @param value [Object] a value to cast
|
28
29
|
#
|
29
30
|
# @return StoreModel::Model
|
30
|
-
def cast_value(value)
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
31
|
+
def cast_value(value) # rubocop:disable Metrics/MethodLength
|
32
|
+
return nil if value.nil?
|
33
|
+
|
34
|
+
if value.is_a?(String)
|
35
|
+
decode_and_initialize(value)
|
36
|
+
elsif value.is_a?(@model_klass)
|
37
|
+
value
|
38
|
+
elsif value.respond_to?(:to_h) # Hash itself included
|
39
|
+
model_instance(value.to_h)
|
40
|
+
else
|
41
|
+
raise_cast_error(value)
|
36
42
|
end
|
37
43
|
rescue ActiveModel::UnknownAttributeError => e
|
38
44
|
handle_unknown_attribute(value, e)
|
@@ -15,6 +15,7 @@ module StoreModel
|
|
15
15
|
# @return [StoreModel::Types::OnePolymorphic ]
|
16
16
|
def initialize(model_wrapper)
|
17
17
|
@model_wrapper = model_wrapper
|
18
|
+
super()
|
18
19
|
end
|
19
20
|
|
20
21
|
# Returns type
|
@@ -29,11 +30,13 @@ module StoreModel
|
|
29
30
|
# @param value [Object] a value to cast
|
30
31
|
#
|
31
32
|
# @return StoreModel::Model
|
32
|
-
def cast_value(value)
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
33
|
+
def cast_value(value) # rubocop:disable Metrics/MethodLength
|
34
|
+
return nil if value.nil?
|
35
|
+
|
36
|
+
if value.is_a?(String)
|
37
|
+
decode_and_initialize(value)
|
38
|
+
elsif value.respond_to?(:to_h) # Hash itself included
|
39
|
+
extract_model_klass(value).new(value.to_h)
|
37
40
|
else
|
38
41
|
raise_cast_error(value) unless value.class.ancestors.include?(StoreModel::Model)
|
39
42
|
|
data/lib/store_model/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: store_model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 4.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- DmitryTsepelev
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-06
|
11
|
+
date: 2024-09-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '7.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '7.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rspec
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -58,14 +58,14 @@ dependencies:
|
|
58
58
|
requirements:
|
59
59
|
- - '='
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
61
|
+
version: 1.65.1
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - '='
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version:
|
68
|
+
version: 1.65.1
|
69
69
|
description: Gem for working with JSON-backed attributes as ActiveRecord models
|
70
70
|
email:
|
71
71
|
- dmitry.a.tsepelev@gmail.com
|
@@ -107,7 +107,7 @@ homepage: https://github.com/DmitryTsepelev/store_model
|
|
107
107
|
licenses:
|
108
108
|
- MIT
|
109
109
|
metadata: {}
|
110
|
-
post_install_message:
|
110
|
+
post_install_message:
|
111
111
|
rdoc_options: []
|
112
112
|
require_paths:
|
113
113
|
- lib
|
@@ -115,15 +115,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
115
115
|
requirements:
|
116
116
|
- - ">="
|
117
117
|
- !ruby/object:Gem::Version
|
118
|
-
version: '
|
118
|
+
version: '3.0'
|
119
119
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
120
|
requirements:
|
121
121
|
- - ">="
|
122
122
|
- !ruby/object:Gem::Version
|
123
123
|
version: '0'
|
124
124
|
requirements: []
|
125
|
-
rubygems_version: 3.
|
126
|
-
signing_key:
|
125
|
+
rubygems_version: 3.5.11
|
126
|
+
signing_key:
|
127
127
|
specification_version: 4
|
128
128
|
summary: Gem for working with JSON-backed attributes as ActiveRecord models
|
129
129
|
test_files: []
|