store_model 0.12.0 → 1.1.0

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: 59ca0d09ab27d1e2fb7d83c2463d6467879fc915343bef013cc45759c2c1d508
4
- data.tar.gz: 58cf03e6d692d62715ee0564f79913a8212e73b95defae7572ef88314c244265
3
+ metadata.gz: df54ca03d99d1173d2e94d32c6a8cc4fa4a503c8f6d7a26b77ff4275e9cf7420
4
+ data.tar.gz: 011b0d5f4fe3532998bca81043895f0882a5f709245c4d688a0226ba17eb2cbb
5
5
  SHA512:
6
- metadata.gz: 22e05c3bf0438d9add8abfed9950b3a8fba77766b90e5105ef766bf8bc64e233758d85ec841aefa026d6dbe075334999e969c90fbdf5057bd45cd38355a240ac
7
- data.tar.gz: 508b2bddff9deac36022342f8069adfe66857301436347d9d7fd01fdf594207c5e83670581f2fefc50567c80d927b3344cb5ff62e5b9ae5e7269f0f86db833df
6
+ metadata.gz: c4dad930177bc577c03a7f12959f57ac897dd1caa17c2817786e15e4e120dee2b1563778575c5eebcbaf9639abca7b8a8ea7bf9ba4f6ee93e2624267d4a06798
7
+ data.tar.gz: dc0a1200e73a465832e3abb9b6778991395a8c70b19b0e1480b19f15b723d64bc7e259fdbeaef6be837c286767675bf44f1828173a8004edfa0f058f7db1888f
@@ -10,5 +10,13 @@ module StoreModel
10
10
  # Controls usage of MergeArrayErrorStrategy
11
11
  # @return [Boolean]
12
12
  attr_accessor :merge_array_errors
13
+
14
+ # Controls if the result of `as_json` will contain the unknown attributes of the model
15
+ # @return [Boolean]
16
+ attr_accessor :serialize_unknown_attributes
17
+
18
+ def initialize
19
+ @serialize_unknown_attributes = true
20
+ end
13
21
  end
14
22
  end
@@ -15,9 +15,17 @@ module StoreModel
15
15
  end
16
16
  end
17
17
 
18
- def write_attribute(*)
19
- super.tap do |value|
20
- assign_parent_to_store_model_relation(value)
18
+ if Rails::VERSION::MAJOR >= 7
19
+ def _write_attribute(*)
20
+ super.tap do |value|
21
+ assign_parent_to_store_model_relation(value)
22
+ end
23
+ end
24
+ else
25
+ def write_attribute(*)
26
+ super.tap do |value|
27
+ assign_parent_to_store_model_relation(value)
28
+ end
21
29
  end
22
30
  end
23
31
  end
@@ -11,6 +11,7 @@ module StoreModel
11
11
  def self.included(base) # :nodoc:
12
12
  base.include ActiveModel::Model
13
13
  base.include ActiveModel::Attributes
14
+ base.include ActiveRecord::AttributeMethods::BeforeTypeCast
14
15
  base.include ActiveModel::AttributeMethods
15
16
  base.include StoreModel::NestedAttributes
16
17
 
@@ -29,7 +30,15 @@ module StoreModel
29
30
  #
30
31
  # @return [Hash]
31
32
  def as_json(options = {})
32
- attributes.with_indifferent_access.as_json(options)
33
+ serialize_unknown_attributes = if options.key?(:serialize_unknown_attributes)
34
+ options[:serialize_unknown_attributes]
35
+ else
36
+ StoreModel.config.serialize_unknown_attributes
37
+ end
38
+
39
+ result = attributes.with_indifferent_access
40
+ result.merge!(unknown_attributes) if serialize_unknown_attributes
41
+ result.as_json(options)
33
42
  end
34
43
 
35
44
  # Compares two StoreModel::Model instances
@@ -42,6 +51,7 @@ module StoreModel
42
51
 
43
52
  attributes.all? { |name, value| value == other.attributes[name] }
44
53
  end
54
+ alias eql? ==
45
55
 
46
56
  # Returns hash for a StoreModel::Model instance based on attributes hash
47
57
  #
@@ -38,7 +38,7 @@ module StoreModel
38
38
  def serialize(value)
39
39
  case value
40
40
  when Array
41
- ActiveSupport::JSON.encode(value)
41
+ ActiveSupport::JSON.encode(value, serialize_unknown_attributes: true)
42
42
  else
43
43
  super
44
44
  end
@@ -47,7 +47,7 @@ module StoreModel
47
47
  def serialize(value)
48
48
  case value
49
49
  when Hash, @model_klass
50
- ActiveSupport::JSON.encode(value)
50
+ ActiveSupport::JSON.encode(value, serialize_unknown_attributes: true)
51
51
  else
52
52
  super
53
53
  end
@@ -52,9 +52,11 @@ module StoreModel
52
52
  def serialize(value)
53
53
  case value
54
54
  when Hash
55
- ActiveSupport::JSON.encode(value)
55
+ ActiveSupport::JSON.encode(value, serialize_unknown_attributes: true)
56
56
  else
57
- return ActiveSupport::JSON.encode(value) if implements_model?(value.class)
57
+ if implements_model?(value.class)
58
+ return ActiveSupport::JSON.encode(value, serialize_unknown_attributes: true)
59
+ end
58
60
 
59
61
  super
60
62
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StoreModel # :nodoc:
4
- VERSION = "0.12.0"
4
+ VERSION = "1.1.0"
5
5
  end
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: 0.12.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - DmitryTsepelev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-10-03 00:00:00.000000000 Z
11
+ date: 2022-07-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -128,14 +128,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
128
128
  requirements:
129
129
  - - ">="
130
130
  - !ruby/object:Gem::Version
131
- version: '2.3'
131
+ version: '2.6'
132
132
  required_rubygems_version: !ruby/object:Gem::Requirement
133
133
  requirements:
134
134
  - - ">="
135
135
  - !ruby/object:Gem::Version
136
136
  version: '0'
137
137
  requirements: []
138
- rubygems_version: 3.1.2
138
+ rubygems_version: 3.3.3
139
139
  signing_key:
140
140
  specification_version: 4
141
141
  summary: Gem for working with JSON-backed attributes as ActiveRecord models