activerecord-nested_attribute_destruction 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: af843c559bcd2739db207cfb8f4ce7976d5fccb8b003af5693f1f875c2926cfd
4
+ data.tar.gz: bf2f1ca17b5b9af2114c360fbad1311f976a064a6d4521894ed8666f8a03a456
5
+ SHA512:
6
+ metadata.gz: a5197efe66c61cf0c9d1cfbe4bbc31c63d7250e16bf3849bfc884825739bc9623b1558aba9465ad7575b96aa5333a1abcfad56939982d408503b3c39c8fd6d76
7
+ data.tar.gz: 599de273a9aefa457d6370696f8f8a24539d109f84642787b8f9abd688b02585ebfceffda744def5baa0cf1efa25c126cb336901f23a9ec27abf6311e767605a
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,29 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
14
+
15
+ Metrics/BlockLength:
16
+ Enabled: true
17
+ Exclude:
18
+ - 'spec/**/*_spec.rb'
19
+
20
+ Style/Documentation:
21
+ Enabled: false
22
+
23
+ Style/FrozenStringLiteralComment:
24
+ Enabled: false
25
+
26
+ Naming/FileName:
27
+ Enabled: true
28
+ Exclude:
29
+ - 'lib/activerecord-nested_attribute_destruction.rb'
data/.tool-versions ADDED
@@ -0,0 +1 @@
1
+ ruby 2.6.9
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2021-12-22
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at brandoncc@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in nested_attribute_destruction.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rails", "~> 5.2"
13
+
14
+ gem "sqlite3", "~> 1.4.2"
15
+
16
+ gem "pry"
17
+
18
+ gem "rubocop"
19
+ gem "rubocop-rails"
20
+ gem "rubocop-rake"
21
+ gem "rubocop-rspec"
data/Gemfile.lock ADDED
@@ -0,0 +1,184 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ activerecord-nested_attribute_destruction (0.1.0)
5
+ activerecord (>= 5.2.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actioncable (5.2.6)
11
+ actionpack (= 5.2.6)
12
+ nio4r (~> 2.0)
13
+ websocket-driver (>= 0.6.1)
14
+ actionmailer (5.2.6)
15
+ actionpack (= 5.2.6)
16
+ actionview (= 5.2.6)
17
+ activejob (= 5.2.6)
18
+ mail (~> 2.5, >= 2.5.4)
19
+ rails-dom-testing (~> 2.0)
20
+ actionpack (5.2.6)
21
+ actionview (= 5.2.6)
22
+ activesupport (= 5.2.6)
23
+ rack (~> 2.0, >= 2.0.8)
24
+ rack-test (>= 0.6.3)
25
+ rails-dom-testing (~> 2.0)
26
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
27
+ actionview (5.2.6)
28
+ activesupport (= 5.2.6)
29
+ builder (~> 3.1)
30
+ erubi (~> 1.4)
31
+ rails-dom-testing (~> 2.0)
32
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
33
+ activejob (5.2.6)
34
+ activesupport (= 5.2.6)
35
+ globalid (>= 0.3.6)
36
+ activemodel (5.2.6)
37
+ activesupport (= 5.2.6)
38
+ activerecord (5.2.6)
39
+ activemodel (= 5.2.6)
40
+ activesupport (= 5.2.6)
41
+ arel (>= 9.0)
42
+ activestorage (5.2.6)
43
+ actionpack (= 5.2.6)
44
+ activerecord (= 5.2.6)
45
+ marcel (~> 1.0.0)
46
+ activesupport (5.2.6)
47
+ concurrent-ruby (~> 1.0, >= 1.0.2)
48
+ i18n (>= 0.7, < 2)
49
+ minitest (~> 5.1)
50
+ tzinfo (~> 1.1)
51
+ arel (9.0.0)
52
+ ast (2.4.2)
53
+ builder (3.2.4)
54
+ coderay (1.1.3)
55
+ concurrent-ruby (1.1.9)
56
+ crass (1.0.6)
57
+ diff-lcs (1.5.0)
58
+ erubi (1.10.0)
59
+ globalid (1.0.0)
60
+ activesupport (>= 5.0)
61
+ i18n (1.8.11)
62
+ concurrent-ruby (~> 1.0)
63
+ loofah (2.13.0)
64
+ crass (~> 1.0.2)
65
+ nokogiri (>= 1.5.9)
66
+ mail (2.7.1)
67
+ mini_mime (>= 0.1.1)
68
+ marcel (1.0.2)
69
+ method_source (1.0.0)
70
+ mini_mime (1.1.2)
71
+ mini_portile2 (2.6.1)
72
+ minitest (5.15.0)
73
+ nio4r (2.5.8)
74
+ nokogiri (1.12.5)
75
+ mini_portile2 (~> 2.6.1)
76
+ racc (~> 1.4)
77
+ nokogiri (1.12.5-arm64-darwin)
78
+ racc (~> 1.4)
79
+ parallel (1.21.0)
80
+ parser (3.0.3.2)
81
+ ast (~> 2.4.1)
82
+ pry (0.14.1)
83
+ coderay (~> 1.1)
84
+ method_source (~> 1.0)
85
+ racc (1.6.0)
86
+ rack (2.2.3)
87
+ rack-test (1.1.0)
88
+ rack (>= 1.0, < 3)
89
+ rails (5.2.6)
90
+ actioncable (= 5.2.6)
91
+ actionmailer (= 5.2.6)
92
+ actionpack (= 5.2.6)
93
+ actionview (= 5.2.6)
94
+ activejob (= 5.2.6)
95
+ activemodel (= 5.2.6)
96
+ activerecord (= 5.2.6)
97
+ activestorage (= 5.2.6)
98
+ activesupport (= 5.2.6)
99
+ bundler (>= 1.3.0)
100
+ railties (= 5.2.6)
101
+ sprockets-rails (>= 2.0.0)
102
+ rails-dom-testing (2.0.3)
103
+ activesupport (>= 4.2.0)
104
+ nokogiri (>= 1.6)
105
+ rails-html-sanitizer (1.4.2)
106
+ loofah (~> 2.3)
107
+ railties (5.2.6)
108
+ actionpack (= 5.2.6)
109
+ activesupport (= 5.2.6)
110
+ method_source
111
+ rake (>= 0.8.7)
112
+ thor (>= 0.19.0, < 2.0)
113
+ rainbow (3.0.0)
114
+ rake (13.0.6)
115
+ regexp_parser (2.2.0)
116
+ rexml (3.2.5)
117
+ rspec (3.10.0)
118
+ rspec-core (~> 3.10.0)
119
+ rspec-expectations (~> 3.10.0)
120
+ rspec-mocks (~> 3.10.0)
121
+ rspec-core (3.10.1)
122
+ rspec-support (~> 3.10.0)
123
+ rspec-expectations (3.10.1)
124
+ diff-lcs (>= 1.2.0, < 2.0)
125
+ rspec-support (~> 3.10.0)
126
+ rspec-mocks (3.10.2)
127
+ diff-lcs (>= 1.2.0, < 2.0)
128
+ rspec-support (~> 3.10.0)
129
+ rspec-support (3.10.3)
130
+ rubocop (1.24.0)
131
+ parallel (~> 1.10)
132
+ parser (>= 3.0.0.0)
133
+ rainbow (>= 2.2.2, < 4.0)
134
+ regexp_parser (>= 1.8, < 3.0)
135
+ rexml
136
+ rubocop-ast (>= 1.15.0, < 2.0)
137
+ ruby-progressbar (~> 1.7)
138
+ unicode-display_width (>= 1.4.0, < 3.0)
139
+ rubocop-ast (1.15.0)
140
+ parser (>= 3.0.1.1)
141
+ rubocop-rails (2.12.4)
142
+ activesupport (>= 4.2.0)
143
+ rack (>= 1.1)
144
+ rubocop (>= 1.7.0, < 2.0)
145
+ rubocop-rake (0.6.0)
146
+ rubocop (~> 1.0)
147
+ rubocop-rspec (2.6.0)
148
+ rubocop (~> 1.19)
149
+ ruby-progressbar (1.11.0)
150
+ sprockets (4.0.2)
151
+ concurrent-ruby (~> 1.0)
152
+ rack (> 1, < 3)
153
+ sprockets-rails (3.4.2)
154
+ actionpack (>= 5.2)
155
+ activesupport (>= 5.2)
156
+ sprockets (>= 3.0.0)
157
+ sqlite3 (1.4.2)
158
+ thor (1.1.0)
159
+ thread_safe (0.3.6)
160
+ tzinfo (1.2.9)
161
+ thread_safe (~> 0.1)
162
+ unicode-display_width (2.1.0)
163
+ websocket-driver (0.7.5)
164
+ websocket-extensions (>= 0.1.0)
165
+ websocket-extensions (0.1.5)
166
+
167
+ PLATFORMS
168
+ arm64-darwin-21
169
+ ruby
170
+
171
+ DEPENDENCIES
172
+ activerecord-nested_attribute_destruction!
173
+ pry
174
+ rails (~> 5.2)
175
+ rake (~> 13.0)
176
+ rspec (~> 3.0)
177
+ rubocop
178
+ rubocop-rails
179
+ rubocop-rake
180
+ rubocop-rspec
181
+ sqlite3 (~> 1.4.2)
182
+
183
+ BUNDLED WITH
184
+ 2.3.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Brandon Conway
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,145 @@
1
+ # activerecord-nested\_attribute\_destruction
2
+
3
+ Active Record offers introspection of saved changes for almost everything you
4
+ could want. The one thing I have repeatedly found myself wishing for was a way
5
+ to know if a nested attribute was destroyed during save via
6
+ accepts\_nested\_attributes.
7
+
8
+ The only way I know to find that information is to set a flag in a callback
9
+ before saving, then use that flag for state comparison after the save completes.
10
+
11
+ This gem handles setting the flags before saving and adds introspection into
12
+ the state after the save.
13
+
14
+ ## Installation
15
+
16
+ Add this line to your application's Gemfile:
17
+
18
+ ```ruby
19
+ gem 'activerecord-nested_attribute_destruction'
20
+ ```
21
+
22
+ And then execute:
23
+
24
+ $ bundle install
25
+
26
+ Or install it yourself as:
27
+
28
+ $ gem install activerecord-nested_attribute_destruction
29
+
30
+ ## Usage
31
+
32
+ ### API
33
+
34
+ When you call `#accepts_nested_attributes_for`, this gem will automatically
35
+ install its hooks on the class. One method is also added to the class instance
36
+ API: `<association_name>_destroyed_during_save?`.
37
+
38
+ For example:
39
+
40
+ ```ruby
41
+ has_many :ships
42
+ has_one :accountant
43
+
44
+ accepts_nested_attributes_for :ships, :accountant, allow_destroy: true
45
+ ```
46
+
47
+ You can call `ships_destroyed_during_save?` and
48
+ `accountant_destroyed_during_save?` after a save to check if associated ships or
49
+ the accountant were destroyed through nested attribute assignment during the
50
+ save.
51
+
52
+ ### Example
53
+
54
+ Here is an example of how it works, adapted from the test suite.
55
+
56
+ ```ruby
57
+ def print_result(h)
58
+ if h.ships_destroyed_during_save?
59
+ puts "At least one ship was destroyed during the last save operation"
60
+ else
61
+ puts "No ships were destroyed during the last save operation"
62
+ end
63
+ end
64
+
65
+ harbor = DangerousHarbor.create(name: 'Blue Lagoon')
66
+ ship1 = (harbor.ships << Ship.create(harbor: harbor)).first
67
+ ship2 = (harbor.ships << Ship.create(harbor: harbor)).first
68
+
69
+ harbor.ships_attributes = [
70
+ ship1.attributes.merge('_destroy': true),
71
+ ship2.attributes.merge(fuel_remaining: 10)
72
+ ]
73
+
74
+ harbor.save!
75
+ print_result(harbor)
76
+
77
+ harbor.save!
78
+ print_result(harbor)
79
+ ```
80
+
81
+ and the output when the code above is run:
82
+
83
+ ```
84
+ At least one ship was destroyed during the last save operation
85
+ No ships were destroyed during the last save operation
86
+ ```
87
+
88
+ To see more examples, take a look at [the tests](https://github.com/brandoncc/activerecord-nested_attribute_destruction/blob/main/spec/functionality_spec.rb).
89
+
90
+ ## Compatibility
91
+
92
+ This gem is built and its tests are run for the following ruby/active record
93
+ combinations:
94
+
95
+ ### Sorted by Active Record version
96
+
97
+ | Active Record version | Ruby Version |
98
+ |-----------------------|--------------|
99
+ | 5.2 | 2.5 |
100
+ | 5.2 | 2.6 |
101
+ | 5.2 | 2.7 |
102
+ | 6.0 | 2.5 |
103
+ | 6.0 | 2.6 |
104
+ | 6.0 | 2.7 |
105
+ | 6.1 | 2.5 |
106
+ | 6.1 | 2.6 |
107
+ | 6.1 | 2.7 |
108
+ | 6.1 | 3.0 |
109
+ | 7.0 | 2.7 |
110
+ | 7.0 | 3.0 |
111
+
112
+ ### Sorted by Ruby version
113
+
114
+ | Ruby Version | Active Record version |
115
+ |--------------|-----------------------|
116
+ | 2.5 | 5.2 |
117
+ | 2.5 | 6.0 |
118
+ | 2.5 | 6.1 |
119
+ | 2.6 | 5.2 |
120
+ | 2.6 | 6.0 |
121
+ | 2.6 | 6.1 |
122
+ | 2.7 | 5.2 |
123
+ | 2.7 | 6.0 |
124
+ | 2.7 | 6.1 |
125
+ | 2.7 | 7.0 |
126
+ | 3.0 | 6.1 |
127
+ | 3.0 | 7.0 |
128
+
129
+ ## Development
130
+
131
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
132
+
133
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
134
+
135
+ ## Contributing
136
+
137
+ Bug reports and pull requests are welcome on GitHub at https://github.com/brandoncc/activerecord-nested\_attribute\_destruction. 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/brandoncc/activerecord-nested_attribute_destruction/blob/main/CODE_OF_CONDUCT.md).
138
+
139
+ ## License
140
+
141
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
142
+
143
+ ## Code of Conduct
144
+
145
+ Everyone interacting in the project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/brandoncc/activerecord-nested_attribute_destruction/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "rubygems"
5
+ require "active_record/railtie"
6
+ require "bundler/setup"
7
+ require "nested_attribute_destruction"
8
+
9
+ ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
10
+
11
+ require_relative "../spec/support/models/harbor"
12
+ require_relative "../spec/support/models/lighthouse"
13
+ require_relative "../spec/support/models/ship"
14
+
15
+ load "spec/support/schema.rb"
16
+
17
+ require "pry"
18
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,49 @@
1
+ # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
2
+
3
+ unless defined?(ActiveRecord::NestedAttributes)
4
+ raise "activerecord-nested_attribute_destruction gem must be loaded after active_record"
5
+ end
6
+
7
+ module ActiveRecord
8
+ module NestedAttributes
9
+ module ClassMethods
10
+ alias _rails_accepts_nested_attributes_for accepts_nested_attributes_for
11
+
12
+ # rubocop:disable Metrics/AbcSize
13
+ def accepts_nested_attributes_for(*attr_names)
14
+ _rails_accepts_nested_attributes_for(*attr_names)
15
+
16
+ # remove any options from the end of the array
17
+ attr_names.extract_options!
18
+
19
+ unless respond_to?(:nested_attribute_destruction_watch_associations)
20
+ self.class.define_method(:nested_attribute_destruction_watch_associations) do
21
+ @nested_attribute_destruction_watch_associations || {}
22
+ end
23
+ end
24
+
25
+ unless respond_to?(:nested_attribute_destruction_watch_association)
26
+ self.class.define_method(:nested_attribute_destruction_watch_association) do |assoc, type|
27
+ @nested_attribute_destruction_watch_associations ||= {}
28
+ @nested_attribute_destruction_watch_associations[assoc.to_sym] = type
29
+ end
30
+ end
31
+
32
+ NestedAttributeDestruction::Monitor.define_hooks(self) if attr_names.any?
33
+
34
+ attr_names.each do |association_name|
35
+ reflection = _reflect_on_association(association_name)
36
+ next unless reflection
37
+
38
+ type = (reflection.collection? ? :many : :one)
39
+
40
+ nested_attribute_destruction_watch_association(association_name, type)
41
+ NestedAttributeDestruction::Monitor.define_predicate(self, association_name)
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ # rubocop:enable Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
49
+ # rubocop:enable Metrics/AbcSize
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "nested_attribute_destruction"
@@ -0,0 +1,124 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NestedAttributeDestruction
4
+ class Monitor
5
+ def initialize
6
+ @attributes_marked_for_destruction = Set.new
7
+ @attributes_destroyed_during_last_save = Set.new
8
+ end
9
+
10
+ class << self
11
+ def define_hooks(klass)
12
+ return if hooks_defined?(klass)
13
+
14
+ define_callbacks(klass)
15
+ redefine_reload(klass)
16
+
17
+ hooks_defined!(klass)
18
+ end
19
+
20
+ def define_predicate(klass, assoc_name)
21
+ klass.define_method("#{assoc_name}_destroyed_during_save?") do
22
+ @nested_attribute_destruction_monitor ||= NestedAttributeDestruction::Monitor.new
23
+ @nested_attribute_destruction_monitor.send(:destroyed_during_save?, assoc_name.to_sym)
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def define_callbacks(klass)
30
+ klass.before_update do |obj|
31
+ @nested_attribute_destruction_monitor ||= NestedAttributeDestruction::Monitor.new
32
+ @nested_attribute_destruction_monitor.send(:store_attributes_marked_for_destruction, obj)
33
+ end
34
+
35
+ klass.after_commit(on: :update) do
36
+ @nested_attribute_destruction_monitor.send(:save_succeeded)
37
+ end
38
+ end
39
+
40
+ def hooks_defined?(klass)
41
+ klass.instance_variable_get(:@nested_attribute_destruction_monitor_hooks_defined)
42
+ end
43
+
44
+ def hooks_defined!(klass)
45
+ klass.instance_variable_set(:@nested_attribute_destruction_monitor_hooks_defined, true)
46
+ end
47
+
48
+ def redefine_reload(klass)
49
+ klass.alias_method :_rails_reload, :reload
50
+
51
+ klass.class_eval do
52
+ def reload(*args)
53
+ ret_val = _rails_reload(*args)
54
+
55
+ # run our code after rails' code, in case the rails code blows up
56
+ @nested_attribute_destruction_monitor&.send(:reset)
57
+
58
+ # make sure we return the value that rails intended
59
+ ret_val
60
+ end
61
+ end
62
+ end
63
+ end
64
+
65
+ private
66
+
67
+ def clear_attributes_marked_for_destruction
68
+ @attributes_marked_for_destruction.clear
69
+ end
70
+
71
+ def clear_attributes_stored_during_last_save
72
+ @attributes_destroyed_during_last_save.clear
73
+ end
74
+
75
+ def destroyed_during_save?(assoc_name)
76
+ @attributes_destroyed_during_last_save.include?(assoc_name.to_sym)
77
+ end
78
+
79
+ def reset
80
+ @attributes_marked_for_destruction.clear
81
+ @attributes_destroyed_during_last_save.clear
82
+ end
83
+
84
+ def save_succeeded
85
+ clear_attributes_stored_during_last_save
86
+ stored_attributes_were_destroyed
87
+ clear_attributes_marked_for_destruction
88
+ end
89
+
90
+ def store_attributes_marked_for_destruction(obj)
91
+ watched_associations(obj).each_pair do |assoc, type|
92
+ if type == :many
93
+ store_many_association(obj, assoc)
94
+ else
95
+ store_one_association(obj, assoc)
96
+ end
97
+ end
98
+ end
99
+
100
+ def store_many_association(obj, assoc_name)
101
+ return unless obj.send(assoc_name).load_target.any? do |el|
102
+ el.marked_for_destruction? && !el.destroyed?
103
+ end
104
+
105
+ @attributes_marked_for_destruction.add(assoc_name)
106
+ end
107
+
108
+ def store_one_association(obj, assoc_name)
109
+ return unless obj.send(assoc_name)&.marked_for_destruction? &&
110
+ !obj.send(assoc_name)&.destroyed?
111
+
112
+ @attributes_marked_for_destruction.add(assoc_name)
113
+ end
114
+
115
+ def stored_attributes_were_destroyed
116
+ @attributes_destroyed_during_last_save +=
117
+ @attributes_marked_for_destruction
118
+ end
119
+
120
+ def watched_associations(obj)
121
+ obj.class.nested_attribute_destruction_watch_associations
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NestedAttributeDestruction
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "nested_attribute_destruction/version"
4
+ require_relative "nested_attribute_destruction/monitor"
5
+
6
+ require_relative "active_record/nested_attributes/class_methods"
7
+
8
+ module NestedAttributeDestruction
9
+ class Error < StandardError; end
10
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-nested_attribute_destruction
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Brandon Conway
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-12-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 5.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 5.2.0
27
+ description: Check if nested attributes were destroyed during the last save
28
+ email:
29
+ - brandoncc@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".rspec"
35
+ - ".rubocop.yml"
36
+ - ".tool-versions"
37
+ - CHANGELOG.md
38
+ - CODE_OF_CONDUCT.md
39
+ - Gemfile
40
+ - Gemfile.lock
41
+ - LICENSE.txt
42
+ - README.md
43
+ - Rakefile
44
+ - bin/console
45
+ - bin/setup
46
+ - lib/active_record/nested_attributes/class_methods.rb
47
+ - lib/activerecord-nested_attribute_destruction.rb
48
+ - lib/nested_attribute_destruction.rb
49
+ - lib/nested_attribute_destruction/monitor.rb
50
+ - lib/nested_attribute_destruction/version.rb
51
+ homepage: https://github.com/brandoncc/nested_attribute_destruction
52
+ licenses:
53
+ - MIT
54
+ metadata:
55
+ homepage_uri: https://github.com/brandoncc/nested_attribute_destruction
56
+ source_code_uri: https://github.com/brandoncc/nested_attribute_destruction
57
+ changelog_uri: https://github.com/brandoncc/nested_attribute_destruction/blob/main/CHANGELOG.md
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '2.5'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubygems_version: 3.0.3.1
74
+ signing_key:
75
+ specification_version: 4
76
+ summary: Nested attribute destruction monitoring
77
+ test_files: []