active_denormalize 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +109 -0
- data/Rakefile +5 -0
- data/lib/active_denormalize/associations/extension.rb +117 -0
- data/lib/active_denormalize/version.rb +5 -0
- data/lib/active_denormalize.rb +6 -0
- metadata +66 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 148b91bf184914f2ed121d72c24e47e6128d9de79bf79b0d6e89aee87b846071
|
4
|
+
data.tar.gz: 5156b8a2066c0767fb2ae3f75f1b86f17435e7247d16683c81750c1a72613b14
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 860141aa31a24db2981ada37c98f6ac806e9ee4ad22c71f4f4ed59c82f7b02c24efa69d33661f7e16544738d8071348f918101fe5fa032f10d6acff4ca005ee6
|
7
|
+
data.tar.gz: 61f6c010ced24e1e17783f34bfa11d713f2207a3b16ff35769b35500c3e802768b65185293612e5f7dc048e195a089f52d858b90c106ae318195a185aa1e07c0
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2023 Garrett Bjerkhoel
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
# ActiveDenormalize
|
2
|
+
|
3
|
+
ActiveDenormalize is a gem that allows you to denormalize data across your ActiveRecord models. It currently supports `belongs_to` relationships.
|
4
|
+
|
5
|
+
Below is a simple example which you can use to get started (and mostly until further documentation is added in this README).
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
class Product < ApplicationRecord
|
9
|
+
has_many :inventory_checks
|
10
|
+
end
|
11
|
+
|
12
|
+
class InventoryCheck < ApplicationRecord
|
13
|
+
belongs_to :product, denormalize: true
|
14
|
+
end
|
15
|
+
```
|
16
|
+
|
17
|
+
ActiveDenormalize works by adding create, update, and destroy `after_*_commit` callbacks to the host model, in this case InventoryCheck, which will update the denormalized attributes on the Product model.
|
18
|
+
|
19
|
+
To know which attributes to denormalize on to the Product model, ActiveDenormalize will look for `inventory_check` prefixed columns on the Product model. For example, if you have a `inventory_check_id` column on the Product model, ActiveDenormalize will update that column with the ID of most recent InventoryCheck record that belong to the Product.
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
# No InventoryCheck has been created yet
|
23
|
+
> product = Product.create(name: "Product 1")
|
24
|
+
> product.inventory_check_id
|
25
|
+
=> nil
|
26
|
+
> product.inventory_check_created_at
|
27
|
+
=> nil
|
28
|
+
> product.inventory_check_status
|
29
|
+
=> nil
|
30
|
+
> product.inventory_check_denormalized_at
|
31
|
+
=> nil
|
32
|
+
|
33
|
+
# Updates the Product record to know about the newest InventoryCheck
|
34
|
+
> first_inventory_check = InventoryCheck.create(product: product, status: "backordered")
|
35
|
+
> first_inventory_check.id
|
36
|
+
=> 1
|
37
|
+
> product.reload.inventory_check_id
|
38
|
+
=> 1
|
39
|
+
> product.inventory_check_created_at
|
40
|
+
=> Sun, 17 Sep 2023 17:28:13.674818427 UTC +00:00
|
41
|
+
> product.inventory_check_status
|
42
|
+
=> "backordered"
|
43
|
+
> product.inventory_check_denormalized_at
|
44
|
+
=> Sun, 17 Sep 2023 17:28:13.674818427 UTC +00:00
|
45
|
+
|
46
|
+
# Updates the Product record to know about the newest InventoryCheck
|
47
|
+
> second_inventory_check = InventoryCheck.create(product: product, status: "available")
|
48
|
+
> second_inventory_check.id
|
49
|
+
=> 2
|
50
|
+
> product.reload.inventory_check_id
|
51
|
+
=> 2
|
52
|
+
> product.inventory_check_created_at
|
53
|
+
=> Sun, 17 Sep 2023 17:28:53.903842001 UTC +00:00
|
54
|
+
> product.inventory_check_status
|
55
|
+
=> "available"
|
56
|
+
> product.inventory_check_denormalized_at
|
57
|
+
=> Sun, 17 Sep 2023 17:28:53.903842001 UTC +00:00
|
58
|
+
> second_inventory_check.destroy
|
59
|
+
|
60
|
+
# Reverts to the previous InventoryCheck record
|
61
|
+
> product.reload.inventory_check_id
|
62
|
+
=> 1
|
63
|
+
> product.inventory_check_created_at
|
64
|
+
=> Sun, 17 Sep 2023 17:28:13.674818427 UTC +00:00
|
65
|
+
> product.inventory_check_status
|
66
|
+
=> "backordered"
|
67
|
+
> product.inventory_check_denormalized_at
|
68
|
+
=> Sun, 17 Sep 2023 17:28:13.674818427 UTC +00:00
|
69
|
+
|
70
|
+
# Updates the denormalized representation of the InventoryCheck on Product
|
71
|
+
> first_inventory_check.update!(status: "available")
|
72
|
+
> product.reload.inventory_check_id
|
73
|
+
=> 1
|
74
|
+
> product.inventory_check_created_at
|
75
|
+
=> Sun, 17 Sep 2023 17:28:13.674818427 UTC +00:00
|
76
|
+
> product.inventory_check_status
|
77
|
+
=> "available"
|
78
|
+
> product.inventory_check_denormalized_at # This timestamp will be updated
|
79
|
+
=> Sun, 17 Sep 2023 17:31:16.388224345 UTC +00:00
|
80
|
+
```
|
81
|
+
|
82
|
+
|
83
|
+
## Installation
|
84
|
+
|
85
|
+
Add this line to your application's Gemfile:
|
86
|
+
|
87
|
+
```ruby
|
88
|
+
gem "active_denormalize"
|
89
|
+
```
|
90
|
+
|
91
|
+
And then execute:
|
92
|
+
|
93
|
+
```bash
|
94
|
+
$ bundle
|
95
|
+
```
|
96
|
+
|
97
|
+
Or install it yourself as:
|
98
|
+
|
99
|
+
```bash
|
100
|
+
$ gem install active_denormalize
|
101
|
+
```
|
102
|
+
|
103
|
+
## Contributing
|
104
|
+
|
105
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/active_denormalize. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/active_denormalize/blob/main/CODE_OF_CONDUCT.md).
|
106
|
+
|
107
|
+
## License
|
108
|
+
|
109
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveDenormalize
|
4
|
+
module Associations
|
5
|
+
module Extension
|
6
|
+
OPTIONS = [:denormalize].freeze
|
7
|
+
|
8
|
+
def self.build(model, reflection)
|
9
|
+
return unless reflection.options[:denormalize]
|
10
|
+
|
11
|
+
define_accessors(model, reflection)
|
12
|
+
add_after_commit_callback(model, reflection)
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.valid_options
|
16
|
+
OPTIONS
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.define_accessors(model, reflection)
|
20
|
+
denormalized_column_prefix = model.model_name.singular
|
21
|
+
target_mixin = reflection.klass.generated_association_methods
|
22
|
+
|
23
|
+
target_mixin.class_eval do
|
24
|
+
define_method("clear_denormalized_#{denormalized_column_prefix}") do
|
25
|
+
denormalized_columns = model.denormalized_column_mapping.keys & self.class.column_names
|
26
|
+
cleared_denormalized_attributes = denormalized_columns.index_with { |_column| nil }
|
27
|
+
update!(cleared_denormalized_attributes)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
model.class_eval do
|
32
|
+
# Needed to lookup the next valid record to denormalize when destroying the latest record.
|
33
|
+
scope :denormalize, -> {}
|
34
|
+
|
35
|
+
def denormalize?
|
36
|
+
true
|
37
|
+
end
|
38
|
+
|
39
|
+
def denormalize(target)
|
40
|
+
denormalized_columns = self.class.denormalized_column_mapping.keys & target.class.column_names
|
41
|
+
attributes = denormalized_columns.to_h do |denormalized_column|
|
42
|
+
column = self.class.denormalized_column_mapping[denormalized_column]
|
43
|
+
|
44
|
+
if column == "denormalized_at"
|
45
|
+
[denormalized_column, Time.zone.now]
|
46
|
+
else
|
47
|
+
value = send(column)
|
48
|
+
if (enum_mapping = defined_enums[column])
|
49
|
+
value = enum_mapping.fetch(value)
|
50
|
+
end
|
51
|
+
|
52
|
+
[denormalized_column, value]
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
if attributes.empty?
|
57
|
+
Rails.logger.debug { "No denormalized columns found for #{target.class}" }
|
58
|
+
else
|
59
|
+
Rails.logger.debug { "Denormalizing #{target.class} #{target.id} with #{attributes}" }
|
60
|
+
target.update!(attributes)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.denormalized_column_mapping
|
65
|
+
@denormalized_column_mapping ||= begin
|
66
|
+
prefix = model_name.singular
|
67
|
+
|
68
|
+
mapping = column_names.index_by do |column_name|
|
69
|
+
"#{prefix}_#{column_name}"
|
70
|
+
end
|
71
|
+
mapping["#{prefix}_denormalized_at"] = "denormalized_at"
|
72
|
+
mapping
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def self.add_after_commit_callback(model, reflection)
|
79
|
+
denormalized_column_prefix = model.model_name.singular
|
80
|
+
denormalized_primary_key = "#{denormalized_column_prefix}_#{model.primary_key}"
|
81
|
+
|
82
|
+
model.before_destroy do
|
83
|
+
if (target = public_send(reflection.name))
|
84
|
+
primary_key = public_send(self.class.primary_key)
|
85
|
+
denormalized_primary_key_id = target.public_send(denormalized_primary_key)
|
86
|
+
|
87
|
+
# The record being destroyed is currently denormalized on its association and needs to be replaced
|
88
|
+
if denormalized_primary_key_id == primary_key
|
89
|
+
replacement = target.association(model.model_name.collection).scope.denormalize.where.not(id: primary_key).order(created_at: :desc).first
|
90
|
+
if replacement.present?
|
91
|
+
replacement.denormalize(target)
|
92
|
+
else
|
93
|
+
target.public_send("clear_denormalized_#{denormalized_column_prefix}")
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
model.after_create_commit do
|
100
|
+
if denormalize? && (target = public_send(reflection.name))
|
101
|
+
denormalize(target)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
model.after_update_commit do
|
106
|
+
if (target = public_send(reflection.name))
|
107
|
+
primary_key = public_send(self.class.primary_key)
|
108
|
+
denormalized_primary_key_id = target.public_send(denormalized_primary_key)
|
109
|
+
|
110
|
+
# After an update of a denormalized record, check the association to see if it is still the current denormalized record or if it has changed.
|
111
|
+
denormalize(target) if denormalized_primary_key_id == primary_key
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_denormalize
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Garrett Bjerkhoel
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-09-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 7.0.6
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 7.0.6
|
27
|
+
description: Description of ActiveDenormalize.
|
28
|
+
email:
|
29
|
+
- me@garrettbjerkhoel.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- MIT-LICENSE
|
35
|
+
- README.md
|
36
|
+
- Rakefile
|
37
|
+
- lib/active_denormalize.rb
|
38
|
+
- lib/active_denormalize/associations/extension.rb
|
39
|
+
- lib/active_denormalize/version.rb
|
40
|
+
homepage: https://github.com/dewski/active_denormalize
|
41
|
+
licenses:
|
42
|
+
- MIT
|
43
|
+
metadata:
|
44
|
+
homepage_uri: https://github.com/dewski/active_denormalize
|
45
|
+
source_code_uri: https://github.com/dewski/active_denormalize
|
46
|
+
changelog_uri: https://github.com/dewski/active_denormalize/blob/main/CODE_OF_CONDUCT.md
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
requirements: []
|
62
|
+
rubygems_version: 3.3.7
|
63
|
+
signing_key:
|
64
|
+
specification_version: 4
|
65
|
+
summary: Summary of ActiveDenormalize.
|
66
|
+
test_files: []
|