store_attribute 2.0.0 → 2.0.1

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: 5530282b5ea4488c94d5f90ba1474c9a8b6dd1b153c412cce62a6dca0c69c3d3
4
+ data.tar.gz: 67f65c84644842ebc7c6cdbcc455f034854d5ada9dd469180cf94a0a4cf02d38
5
5
  SHA512:
6
- metadata.gz: f3ed249e25a049a2a8cbe728f45e586c4504f5604e26c40914e1bfc5c6512b4929cae2a59141e2b198e81627ad4fc0504f6104bd9b58fc6c526699358c3f04d5
7
- data.tar.gz: 906e39d1c5304a09688650afb5365b53363972cdd684cb1bfd58fe6ebf588cfd406309d3907b7766144510bb9325ba1102e10ede0c9e29e3291cde4988f902ad
6
+ metadata.gz: f00159fa7de150635efa6c2d623ab8aacac3b166582fa5b48d623621f38ee79a123903e538faddd8b42d9cca955a9c18ea0fc4ebd32cfaba7de51b233b203891
7
+ data.tar.gz: f7ee58675bc257f21343054e131b3708ee3d8f5c1f504e814e80e3eb95eb0e506bf69361815736cf50732f8846b1b73671533802854f402bf72881ceaada3de9
data/CHANGELOG.md CHANGED
@@ -2,6 +2,20 @@
2
2
 
3
3
  ## master
4
4
 
5
+ ## 2.0.1 (2025-05-09) 🎇
6
+
7
+ - Register store_attributes as attributes. ([@rickcsong](https://github.com/rickcsong))
8
+
9
+ ```ruby
10
+ class User < ActiveRecord::Base
11
+ self.store_attribute_register_attributes = true
12
+
13
+ store_attribute :extra, :color, :string, default: "grey"
14
+ end
15
+
16
+ User.attribute_types.keys.include?("color") #=> true
17
+ ```
18
+
5
19
  ## 2.0.0 (2024-12-12)
6
20
 
7
21
  - **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.
data/README.md CHANGED
@@ -172,3 +172,18 @@ 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
+ For local development, you'll need PostgreSQL up and running. You can either install it on your host machine or run via Docker as follows:
181
+
182
+ ```bash
183
+ docker run --name store_attribute_postgres -e POSTGRES_HOST_AUTH_METHOD=trust -e POSTGRES_USER=$USER -e POSTGRES_PASSWORD=postgres -p 5432:5432 -d postgres
184
+ docker exec -it store_attribute_postgres createdb -U $USER store_attribute_test
185
+ ```
186
+
187
+ ## License
188
+
189
+ 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,28 @@ 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
+ else
143
+ ActiveRecord::Type.lookup(type, **options.except(:default))
144
+ end
145
+
146
+ attribute(name, cast_type, **options)
147
+ end
148
+ end
149
+
150
+ def store_attribute_register_attributes
151
+ return @store_attribute_register_attributes if instance_variable_defined?(:@store_attribute_register_attributes)
152
+
153
+ @store_attribute_register_attributes =
154
+ if superclass.respond_to?(:store_attribute_register_attributes)
155
+ superclass.store_attribute_register_attributes
156
+ else
157
+ StoreAttribute.store_attribute_register_attributes
158
+ end
136
159
  end
137
160
 
138
161
  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.0.1"
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,14 @@
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.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - palkan
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-12-13 00:00:00.000000000 Z
11
+ date: 2025-05-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -91,7 +91,7 @@ metadata:
91
91
  documentation_uri: http://github.com/palkan/store_attribute
92
92
  homepage_uri: http://github.com/palkan/store_attribute
93
93
  source_code_uri: http://github.com/palkan/store_attribute
94
- post_install_message:
94
+ post_install_message:
95
95
  rdoc_options: []
96
96
  require_paths:
97
97
  - lib
@@ -107,7 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
107
107
  version: '0'
108
108
  requirements: []
109
109
  rubygems_version: 3.4.19
110
- signing_key:
110
+ signing_key:
111
111
  specification_version: 4
112
112
  summary: ActiveRecord extension which adds typecasting to store accessors
113
113
  test_files: []