store_attribute 2.0.0 → 2.1.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: 8a7e7f09f536e3ca33ac3ade96579b1e21d44dc4d40ffa24d9b2450a6c91e9e1
4
- data.tar.gz: e8fd0c4de5b2ecfdd0f2f3154ffa37d5ff5bf11cbf4c225c7681ec5294de3b78
3
+ metadata.gz: b1846642dbb892be6b0a2428ee9687b90cff7edb89c0f16e5c583b6b841eee00
4
+ data.tar.gz: 4a7d7b77654182e2152e855b140d2d72c6e38c244974f6937cdd6ed3cf6d8d71
5
5
  SHA512:
6
- metadata.gz: f3ed249e25a049a2a8cbe728f45e586c4504f5604e26c40914e1bfc5c6512b4929cae2a59141e2b198e81627ad4fc0504f6104bd9b58fc6c526699358c3f04d5
7
- data.tar.gz: 906e39d1c5304a09688650afb5365b53363972cdd684cb1bfd58fe6ebf588cfd406309d3907b7766144510bb9325ba1102e10ede0c9e29e3291cde4988f902ad
6
+ metadata.gz: 8497c232c1f4d26dfa75df390fc0cb44b8c2ee5d375a7cb382a62e64fcb5ccc2d5cb08f632118fd84565af9d330b977df514b1073380aec9cca6e249bbadac62
7
+ data.tar.gz: b3f8793814b0cefaad1325faa8fe799c19e4261947bdd5d637ba3515dc0b6dbfbf144d02ef092675e8465eff3b4528869609fdc543819be377d4238200889ddd
data/CHANGELOG.md CHANGED
@@ -2,6 +2,26 @@
2
2
 
3
3
  ## master
4
4
 
5
+ ## 2.1.0 (2026-01-12)
6
+
7
+ - **Require Ruby 3.2+ and Rails 7.2+.**
8
+
9
+ - Fix type lookup issue when registering store_attributes as attributes. ([@n-ivan][])
10
+
11
+ ## 2.0.1 (2025-05-09) 🎇
12
+
13
+ - Register store_attributes as attributes. ([@rickcsong](https://github.com/rickcsong))
14
+
15
+ ```ruby
16
+ class User < ActiveRecord::Base
17
+ self.store_attribute_register_attributes = true
18
+
19
+ store_attribute :extra, :color, :string, default: "grey"
20
+ end
21
+
22
+ User.attribute_types.keys.include?("color") #=> true
23
+ ```
24
+
5
25
  ## 2.0.0 (2024-12-12)
6
26
 
7
27
  - **Breaking:** The `store_attribute_unset_values_fallback_to_default` option is now true by default, meaning that the default value will be returned when the attribute key is not present in the serialized value.
@@ -132,3 +152,4 @@ See [PR#22](https://github.com/palkan/store_attribute/pull/22).
132
152
  [@glaszig]: https://github.com/glaszig
133
153
  [@ioki-klaus]: https://github.com/ioki-klaus
134
154
  [@markedmondson]: https://github.com/markedmondson
155
+ [@n-ivan]: https://github.com/n-ivan
data/README.md CHANGED
@@ -4,16 +4,16 @@
4
4
 
5
5
  ## Store Attribute
6
6
 
7
- ActiveRecord extension which adds typecasting to store accessors.
7
+ Active Record extension which adds type casting support to store accessors.
8
8
 
9
- Originally extracted from not merged PR to Rails: [rails/rails#18942](https://github.com/rails/rails/pull/18942).
9
+ Originally extracted from the (unmerged) PR to Ruby on Rails: [rails/rails#18942](https://github.com/rails/rails/pull/18942).
10
10
 
11
11
  ### Install
12
12
 
13
13
  In your Gemfile:
14
14
 
15
15
  ```ruby
16
- # for Rails 6.1+ (7 is supported)
16
+ # for Rails 6.1+
17
17
  gem "store_attribute", "~> 1.0"
18
18
 
19
19
  # for Rails 5+ (6 is supported)
@@ -172,3 +172,11 @@ You can also configure the global default for this option in an initializer or a
172
172
  # config/application.rb
173
173
  StoreAttribute.store_attribute_unset_values_fallback_to_default = false
174
174
  ```
175
+
176
+ ## Contributing
177
+
178
+ Bug reports and pull requests are welcome on GitHub at [https://github.com/palkan/store_attribute](https://github.com/palkan/store_attribute).
179
+
180
+ ## License
181
+
182
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -14,7 +14,7 @@ module StoreAttribute
14
14
  prev_store, new_store = orig_changes.map(&:dup)
15
15
 
16
16
  prev_store&.each do |key, value|
17
- if new_store[key] == value
17
+ if new_store&.dig(key) == value
18
18
  prev_store.except!(key)
19
19
  new_store&.except!(key)
20
20
  end
@@ -10,6 +10,7 @@ module ActiveRecord
10
10
  alias_method :_orig_store_without_types, :store
11
11
  alias_method :_orig_store_accessor_without_types, :store_accessor
12
12
 
13
+ attr_writer :store_attribute_register_attributes
13
14
  attr_writer :store_attribute_unset_values_fallback_to_default
14
15
 
15
16
  # Defines store on this model.
@@ -133,6 +134,30 @@ module ActiveRecord
133
134
 
134
135
  _local_typed_stored_attributes[store_name][:owner] = self if options.key?(:default) || !_local_typed_stored_attributes?
135
136
  _local_typed_stored_attributes[store_name][:types][name] = [type, options]
137
+
138
+ if store_attribute_register_attributes
139
+ cast_type =
140
+ if type == :value
141
+ ActiveModel::Type::Value.new(**options.except(:default))
142
+ elsif type.is_a?(Symbol)
143
+ ActiveRecord::Type.lookup(type, **options.except(:default))
144
+ else
145
+ type
146
+ end
147
+
148
+ attribute(name, cast_type, **options)
149
+ end
150
+ end
151
+
152
+ def store_attribute_register_attributes
153
+ return @store_attribute_register_attributes if instance_variable_defined?(:@store_attribute_register_attributes)
154
+
155
+ @store_attribute_register_attributes =
156
+ if superclass.respond_to?(:store_attribute_register_attributes)
157
+ superclass.store_attribute_register_attributes
158
+ else
159
+ StoreAttribute.store_attribute_register_attributes
160
+ end
136
161
  end
137
162
 
138
163
  def store_attribute_unset_values_fallback_to_default
@@ -92,12 +92,16 @@ module ActiveRecord
92
92
  self
93
93
  end
94
94
 
95
+ def mutable?
96
+ true
97
+ end
98
+
95
99
  def write(object, attribute, key, value)
96
100
  value = type_for(key).cast(value) if typed?(key)
97
101
  store_accessor.write(object, attribute, key, value)
98
102
  end
99
103
 
100
- delegate :read, :prepare, to: :store_accessor
104
+ delegate :get, :read, :prepare, to: :store_accessor
101
105
 
102
106
  def build_defaults
103
107
  defaults.transform_values do |val|
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StoreAttribute # :nodoc:
4
- VERSION = "2.0.0"
4
+ VERSION = "2.1.0"
5
5
  end
@@ -8,7 +8,9 @@ module StoreAttribute
8
8
  # Global default value for `store_attribute_unset_values_fallback_to_default` option.
9
9
  # Must be set before any model is loaded
10
10
  attr_accessor :store_attribute_unset_values_fallback_to_default
11
+ attr_accessor :store_attribute_register_attributes
11
12
  end
12
13
 
13
14
  self.store_attribute_unset_values_fallback_to_default = true
15
+ self.store_attribute_register_attributes = false
14
16
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: store_attribute
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - palkan
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2024-12-13 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: activerecord
@@ -16,28 +15,28 @@ dependencies:
16
15
  requirements:
17
16
  - - ">="
18
17
  - !ruby/object:Gem::Version
19
- version: '6.1'
18
+ version: '7.2'
20
19
  type: :runtime
21
20
  prerelease: false
22
21
  version_requirements: !ruby/object:Gem::Requirement
23
22
  requirements:
24
23
  - - ">="
25
24
  - !ruby/object:Gem::Version
26
- version: '6.1'
25
+ version: '7.2'
27
26
  - !ruby/object:Gem::Dependency
28
- name: pg
27
+ name: pglite
29
28
  requirement: !ruby/object:Gem::Requirement
30
29
  requirements:
31
30
  - - ">="
32
31
  - !ruby/object:Gem::Version
33
- version: '1.0'
32
+ version: 0.0.5
34
33
  type: :development
35
34
  prerelease: false
36
35
  version_requirements: !ruby/object:Gem::Requirement
37
36
  requirements:
38
37
  - - ">="
39
38
  - !ruby/object:Gem::Version
40
- version: '1.0'
39
+ version: 0.0.5
41
40
  - !ruby/object:Gem::Dependency
42
41
  name: rake
43
42
  requirement: !ruby/object:Gem::Requirement
@@ -91,7 +90,6 @@ metadata:
91
90
  documentation_uri: http://github.com/palkan/store_attribute
92
91
  homepage_uri: http://github.com/palkan/store_attribute
93
92
  source_code_uri: http://github.com/palkan/store_attribute
94
- post_install_message:
95
93
  rdoc_options: []
96
94
  require_paths:
97
95
  - lib
@@ -99,15 +97,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
99
97
  requirements:
100
98
  - - ">="
101
99
  - !ruby/object:Gem::Version
102
- version: 3.0.0
100
+ version: 3.2.0
103
101
  required_rubygems_version: !ruby/object:Gem::Requirement
104
102
  requirements:
105
103
  - - ">="
106
104
  - !ruby/object:Gem::Version
107
105
  version: '0'
108
106
  requirements: []
109
- rubygems_version: 3.4.19
110
- signing_key:
107
+ rubygems_version: 3.6.9
111
108
  specification_version: 4
112
109
  summary: ActiveRecord extension which adds typecasting to store accessors
113
110
  test_files: []