active_record-nested_attributes-destroy_if 0.2.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d56e8c00af23e990dcd632555134bf0e2e30dea27b9ec3d5c36206ec1e0d3569
4
+ data.tar.gz: 6f9848d9b2da9e36048f825e10483100d079018b2a468c140c1b4d1a77e12171
5
+ SHA512:
6
+ metadata.gz: 0ce6638255bfd2d3f12e4bf2a563d9d09980d737ba90e7cf6670a86b37f9bf3d5dadecb35117b46e746e754518fb24950a0923bd921e40e35d501f7542e1c0bb
7
+ data.tar.gz: 96d9b7fa7c3f4b3b1e787f89e841167fc0429fd1e8cb4eafeb6140ff7a68b0f820acf00b09b49efc72ccaf504d1531e70271419c9ee7196c368d9e8c742b0a34
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1 @@
1
+ active_record-nested_attributes-destroy_if
@@ -0,0 +1 @@
1
+ ruby-2.6.3
@@ -0,0 +1,13 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5
7
+ - 2.6
8
+ - 2.7
9
+ gemfile:
10
+ - gemfiles/rails_5.1.gemfile
11
+ - gemfiles/rails_5.2.gemfile
12
+ - gemfiles/rails_6.0.gemfile
13
+ before_install: gem install bundler -v 2.0.1
@@ -0,0 +1,12 @@
1
+ appraise "rails-5.1" do
2
+ gem "rails", "~>5.1.0"
3
+ end
4
+
5
+ appraise "rails-5.2" do
6
+ gem "rails", "~>5.2.0"
7
+ end
8
+
9
+ appraise "rails-6.0" do
10
+ gem "rails", "~>6.0.0"
11
+ end
12
+
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in active_record-nested_attributes-destroy_if.gemspec
4
+ gemspec
5
+
@@ -0,0 +1,65 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ active_record-nested_attributes-destroy_if (0.2.0)
5
+ activerecord (>= 5.1, < 6.1)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (6.0.2.1)
11
+ activesupport (= 6.0.2.1)
12
+ activerecord (6.0.2.1)
13
+ activemodel (= 6.0.2.1)
14
+ activesupport (= 6.0.2.1)
15
+ activesupport (6.0.2.1)
16
+ concurrent-ruby (~> 1.0, >= 1.0.2)
17
+ i18n (>= 0.7, < 2)
18
+ minitest (~> 5.1)
19
+ tzinfo (~> 1.1)
20
+ zeitwerk (~> 2.2)
21
+ appraisal (2.2.0)
22
+ bundler
23
+ rake
24
+ thor (>= 0.14.0)
25
+ byebug (11.1.0)
26
+ concurrent-ruby (1.1.5)
27
+ diff-lcs (1.3)
28
+ i18n (1.8.2)
29
+ concurrent-ruby (~> 1.0)
30
+ minitest (5.14.0)
31
+ rake (10.5.0)
32
+ rspec (3.9.0)
33
+ rspec-core (~> 3.9.0)
34
+ rspec-expectations (~> 3.9.0)
35
+ rspec-mocks (~> 3.9.0)
36
+ rspec-core (3.9.1)
37
+ rspec-support (~> 3.9.1)
38
+ rspec-expectations (3.9.0)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.9.0)
41
+ rspec-mocks (3.9.1)
42
+ diff-lcs (>= 1.2.0, < 2.0)
43
+ rspec-support (~> 3.9.0)
44
+ rspec-support (3.9.2)
45
+ sqlite3 (1.4.2)
46
+ thor (1.0.1)
47
+ thread_safe (0.3.6)
48
+ tzinfo (1.2.6)
49
+ thread_safe (~> 0.1)
50
+ zeitwerk (2.2.2)
51
+
52
+ PLATFORMS
53
+ ruby
54
+
55
+ DEPENDENCIES
56
+ active_record-nested_attributes-destroy_if!
57
+ appraisal
58
+ bundler (~> 2.0)
59
+ byebug
60
+ rake (~> 10.0)
61
+ rspec (~> 3.0)
62
+ sqlite3
63
+
64
+ BUNDLED WITH
65
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Micah Geisel
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.
@@ -0,0 +1,57 @@
1
+ # ActiveRecord::NestedAttributes::DestroyIf
2
+
3
+ [![Build Status](https://travis-ci.org/botandrose/active_record-nested_attributes-destroy_if.svg?branch=master)](https://travis-ci.org/botandrose/active_record-nested_attributes-destroy_if)
4
+
5
+ Adds a `:destroy_if` option to `.accepts_nested_attributes_for`, which is basically a stronger version of `:reject_if` that also destroys existing records.
6
+
7
+ ## Usage
8
+
9
+ Use the `destroy_if` option when `reject_if` isn't strong enough, i.e. you want to also destroy existing records that pass the test, not just reject new records.
10
+
11
+ ```ruby
12
+ class Parent < ActiveRecord::Base
13
+ has_many :children
14
+ accepts_nested_attributes_for :children, destroy_if: proc { |attrs| attrs["name"].blank? }
15
+ end
16
+
17
+ class Child < ActiveRecord::Base
18
+ belongs_to :parent
19
+ end
20
+
21
+ tywin = Parent.create!(id: 1, name: "Tywin")
22
+ Child.create!(id: 1, parent_id: 1, name: "Jaime")
23
+ Child.create!(id: 2, parent_id: 1, name: "Tyrion")
24
+
25
+ tywin.children # => [<Child id: 1, parent_id: 1, name: "Jaime">, <Child id: 2, parent_id: 1, name: "Tyrion">]
26
+
27
+ tywin.update!({
28
+ children_attributes: {
29
+ "0" => {
30
+ id: 1,
31
+ name: "Ser Jaime",
32
+ },
33
+ "1" => {
34
+ id: 2,
35
+ name: "",
36
+ },
37
+ },
38
+ })
39
+
40
+ tywin.children # => [<Child id: 1, parent_id: 1, name: "Ser Jaime">]
41
+
42
+ Child.find(2) # => raises ActiveRecord::RecordNotFound! Tyrion was destroyed!
43
+ ```
44
+
45
+ ## Development
46
+
47
+ 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.
48
+
49
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
50
+
51
+ ## Contributing
52
+
53
+ Bug reports and pull requests are welcome on GitHub at https://github.com/botandrose/active_record-nested_attributes-destroy_if.
54
+
55
+ ## License
56
+
57
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,34 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "active_record/nested_attributes/destroy_if/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "active_record-nested_attributes-destroy_if"
8
+ spec.version = ActiveRecord::NestedAttributesDestroyIf::VERSION
9
+ spec.authors = ["Micah Geisel"]
10
+ spec.email = ["micah@botandrose.com"]
11
+
12
+ spec.summary = "Adds :destroy_if option to accepts_nested_attributes_for"
13
+ spec.description = "Adds :destroy_if option to accepts_nested_attributes_for, which is basically a stronger version of :reject_if that destroys existing records, too."
14
+ spec.homepage = "https://github.com/botandrose/active_record-nested_attributes-destroy_if"
15
+ spec.license = "MIT"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_dependency "activerecord", ">=5.1", "<6.1"
27
+
28
+ spec.add_development_dependency "bundler", "~> 2.0"
29
+ spec.add_development_dependency "appraisal"
30
+ spec.add_development_dependency "rake", "~> 10.0"
31
+ spec.add_development_dependency "rspec", "~> 3.0"
32
+ spec.add_development_dependency "sqlite3"
33
+ spec.add_development_dependency "byebug"
34
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "active_record/nested_attributes/destroy_if"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -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,2 @@
1
+ ---
2
+ BUNDLE_RETRY: "1"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "~>5.0.0"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "~>5.1.0"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,144 @@
1
+ PATH
2
+ remote: ..
3
+ specs:
4
+ active_record-nested_attributes-destroy_if (0.2.0)
5
+ activerecord (>= 5.1, < 6.1)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actioncable (5.1.7)
11
+ actionpack (= 5.1.7)
12
+ nio4r (~> 2.0)
13
+ websocket-driver (~> 0.6.1)
14
+ actionmailer (5.1.7)
15
+ actionpack (= 5.1.7)
16
+ actionview (= 5.1.7)
17
+ activejob (= 5.1.7)
18
+ mail (~> 2.5, >= 2.5.4)
19
+ rails-dom-testing (~> 2.0)
20
+ actionpack (5.1.7)
21
+ actionview (= 5.1.7)
22
+ activesupport (= 5.1.7)
23
+ rack (~> 2.0)
24
+ rack-test (>= 0.6.3)
25
+ rails-dom-testing (~> 2.0)
26
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
27
+ actionview (5.1.7)
28
+ activesupport (= 5.1.7)
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.1.7)
34
+ activesupport (= 5.1.7)
35
+ globalid (>= 0.3.6)
36
+ activemodel (5.1.7)
37
+ activesupport (= 5.1.7)
38
+ activerecord (5.1.7)
39
+ activemodel (= 5.1.7)
40
+ activesupport (= 5.1.7)
41
+ arel (~> 8.0)
42
+ activesupport (5.1.7)
43
+ concurrent-ruby (~> 1.0, >= 1.0.2)
44
+ i18n (>= 0.7, < 2)
45
+ minitest (~> 5.1)
46
+ tzinfo (~> 1.1)
47
+ appraisal (2.2.0)
48
+ bundler
49
+ rake
50
+ thor (>= 0.14.0)
51
+ arel (8.0.0)
52
+ builder (3.2.4)
53
+ byebug (11.1.1)
54
+ concurrent-ruby (1.1.5)
55
+ crass (1.0.6)
56
+ diff-lcs (1.3)
57
+ erubi (1.9.0)
58
+ globalid (0.4.2)
59
+ activesupport (>= 4.2.0)
60
+ i18n (1.8.2)
61
+ concurrent-ruby (~> 1.0)
62
+ loofah (2.4.0)
63
+ crass (~> 1.0.2)
64
+ nokogiri (>= 1.5.9)
65
+ mail (2.7.1)
66
+ mini_mime (>= 0.1.1)
67
+ method_source (0.9.2)
68
+ mini_mime (1.0.2)
69
+ mini_portile2 (2.4.0)
70
+ minitest (5.14.0)
71
+ nio4r (2.5.2)
72
+ nokogiri (1.10.7)
73
+ mini_portile2 (~> 2.4.0)
74
+ rack (2.1.1)
75
+ rack-test (1.1.0)
76
+ rack (>= 1.0, < 3)
77
+ rails (5.1.7)
78
+ actioncable (= 5.1.7)
79
+ actionmailer (= 5.1.7)
80
+ actionpack (= 5.1.7)
81
+ actionview (= 5.1.7)
82
+ activejob (= 5.1.7)
83
+ activemodel (= 5.1.7)
84
+ activerecord (= 5.1.7)
85
+ activesupport (= 5.1.7)
86
+ bundler (>= 1.3.0)
87
+ railties (= 5.1.7)
88
+ sprockets-rails (>= 2.0.0)
89
+ rails-dom-testing (2.0.3)
90
+ activesupport (>= 4.2.0)
91
+ nokogiri (>= 1.6)
92
+ rails-html-sanitizer (1.3.0)
93
+ loofah (~> 2.3)
94
+ railties (5.1.7)
95
+ actionpack (= 5.1.7)
96
+ activesupport (= 5.1.7)
97
+ method_source
98
+ rake (>= 0.8.7)
99
+ thor (>= 0.18.1, < 2.0)
100
+ rake (10.5.0)
101
+ rspec (3.9.0)
102
+ rspec-core (~> 3.9.0)
103
+ rspec-expectations (~> 3.9.0)
104
+ rspec-mocks (~> 3.9.0)
105
+ rspec-core (3.9.1)
106
+ rspec-support (~> 3.9.1)
107
+ rspec-expectations (3.9.0)
108
+ diff-lcs (>= 1.2.0, < 2.0)
109
+ rspec-support (~> 3.9.0)
110
+ rspec-mocks (3.9.1)
111
+ diff-lcs (>= 1.2.0, < 2.0)
112
+ rspec-support (~> 3.9.0)
113
+ rspec-support (3.9.2)
114
+ sprockets (4.0.0)
115
+ concurrent-ruby (~> 1.0)
116
+ rack (> 1, < 3)
117
+ sprockets-rails (3.2.1)
118
+ actionpack (>= 4.0)
119
+ activesupport (>= 4.0)
120
+ sprockets (>= 3.0.0)
121
+ sqlite3 (1.4.2)
122
+ thor (1.0.1)
123
+ thread_safe (0.3.6)
124
+ tzinfo (1.2.6)
125
+ thread_safe (~> 0.1)
126
+ websocket-driver (0.6.5)
127
+ websocket-extensions (>= 0.1.0)
128
+ websocket-extensions (0.1.4)
129
+
130
+ PLATFORMS
131
+ ruby
132
+
133
+ DEPENDENCIES
134
+ active_record-nested_attributes-destroy_if!
135
+ appraisal
136
+ bundler (~> 2.0)
137
+ byebug
138
+ rails (~> 5.1.0)
139
+ rake (~> 10.0)
140
+ rspec (~> 3.0)
141
+ sqlite3
142
+
143
+ BUNDLED WITH
144
+ 2.1.4
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "~>5.2.0"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,152 @@
1
+ PATH
2
+ remote: ..
3
+ specs:
4
+ active_record-nested_attributes-destroy_if (0.2.0)
5
+ activerecord (>= 5.1, < 6.1)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actioncable (5.2.4.1)
11
+ actionpack (= 5.2.4.1)
12
+ nio4r (~> 2.0)
13
+ websocket-driver (>= 0.6.1)
14
+ actionmailer (5.2.4.1)
15
+ actionpack (= 5.2.4.1)
16
+ actionview (= 5.2.4.1)
17
+ activejob (= 5.2.4.1)
18
+ mail (~> 2.5, >= 2.5.4)
19
+ rails-dom-testing (~> 2.0)
20
+ actionpack (5.2.4.1)
21
+ actionview (= 5.2.4.1)
22
+ activesupport (= 5.2.4.1)
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.4.1)
28
+ activesupport (= 5.2.4.1)
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.4.1)
34
+ activesupport (= 5.2.4.1)
35
+ globalid (>= 0.3.6)
36
+ activemodel (5.2.4.1)
37
+ activesupport (= 5.2.4.1)
38
+ activerecord (5.2.4.1)
39
+ activemodel (= 5.2.4.1)
40
+ activesupport (= 5.2.4.1)
41
+ arel (>= 9.0)
42
+ activestorage (5.2.4.1)
43
+ actionpack (= 5.2.4.1)
44
+ activerecord (= 5.2.4.1)
45
+ marcel (~> 0.3.1)
46
+ activesupport (5.2.4.1)
47
+ concurrent-ruby (~> 1.0, >= 1.0.2)
48
+ i18n (>= 0.7, < 2)
49
+ minitest (~> 5.1)
50
+ tzinfo (~> 1.1)
51
+ appraisal (2.2.0)
52
+ bundler
53
+ rake
54
+ thor (>= 0.14.0)
55
+ arel (9.0.0)
56
+ builder (3.2.4)
57
+ byebug (11.1.1)
58
+ concurrent-ruby (1.1.5)
59
+ crass (1.0.6)
60
+ diff-lcs (1.3)
61
+ erubi (1.9.0)
62
+ globalid (0.4.2)
63
+ activesupport (>= 4.2.0)
64
+ i18n (1.8.2)
65
+ concurrent-ruby (~> 1.0)
66
+ loofah (2.4.0)
67
+ crass (~> 1.0.2)
68
+ nokogiri (>= 1.5.9)
69
+ mail (2.7.1)
70
+ mini_mime (>= 0.1.1)
71
+ marcel (0.3.3)
72
+ mimemagic (~> 0.3.2)
73
+ method_source (0.9.2)
74
+ mimemagic (0.3.3)
75
+ mini_mime (1.0.2)
76
+ mini_portile2 (2.4.0)
77
+ minitest (5.14.0)
78
+ nio4r (2.5.2)
79
+ nokogiri (1.10.7)
80
+ mini_portile2 (~> 2.4.0)
81
+ rack (2.1.1)
82
+ rack-test (1.1.0)
83
+ rack (>= 1.0, < 3)
84
+ rails (5.2.4.1)
85
+ actioncable (= 5.2.4.1)
86
+ actionmailer (= 5.2.4.1)
87
+ actionpack (= 5.2.4.1)
88
+ actionview (= 5.2.4.1)
89
+ activejob (= 5.2.4.1)
90
+ activemodel (= 5.2.4.1)
91
+ activerecord (= 5.2.4.1)
92
+ activestorage (= 5.2.4.1)
93
+ activesupport (= 5.2.4.1)
94
+ bundler (>= 1.3.0)
95
+ railties (= 5.2.4.1)
96
+ sprockets-rails (>= 2.0.0)
97
+ rails-dom-testing (2.0.3)
98
+ activesupport (>= 4.2.0)
99
+ nokogiri (>= 1.6)
100
+ rails-html-sanitizer (1.3.0)
101
+ loofah (~> 2.3)
102
+ railties (5.2.4.1)
103
+ actionpack (= 5.2.4.1)
104
+ activesupport (= 5.2.4.1)
105
+ method_source
106
+ rake (>= 0.8.7)
107
+ thor (>= 0.19.0, < 2.0)
108
+ rake (10.5.0)
109
+ rspec (3.9.0)
110
+ rspec-core (~> 3.9.0)
111
+ rspec-expectations (~> 3.9.0)
112
+ rspec-mocks (~> 3.9.0)
113
+ rspec-core (3.9.1)
114
+ rspec-support (~> 3.9.1)
115
+ rspec-expectations (3.9.0)
116
+ diff-lcs (>= 1.2.0, < 2.0)
117
+ rspec-support (~> 3.9.0)
118
+ rspec-mocks (3.9.1)
119
+ diff-lcs (>= 1.2.0, < 2.0)
120
+ rspec-support (~> 3.9.0)
121
+ rspec-support (3.9.2)
122
+ sprockets (4.0.0)
123
+ concurrent-ruby (~> 1.0)
124
+ rack (> 1, < 3)
125
+ sprockets-rails (3.2.1)
126
+ actionpack (>= 4.0)
127
+ activesupport (>= 4.0)
128
+ sprockets (>= 3.0.0)
129
+ sqlite3 (1.4.2)
130
+ thor (1.0.1)
131
+ thread_safe (0.3.6)
132
+ tzinfo (1.2.6)
133
+ thread_safe (~> 0.1)
134
+ websocket-driver (0.7.1)
135
+ websocket-extensions (>= 0.1.0)
136
+ websocket-extensions (0.1.4)
137
+
138
+ PLATFORMS
139
+ ruby
140
+
141
+ DEPENDENCIES
142
+ active_record-nested_attributes-destroy_if!
143
+ appraisal
144
+ bundler (~> 2.0)
145
+ byebug
146
+ rails (~> 5.2.0)
147
+ rake (~> 10.0)
148
+ rspec (~> 3.0)
149
+ sqlite3
150
+
151
+ BUNDLED WITH
152
+ 2.1.4
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "~>6.0.0"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,168 @@
1
+ PATH
2
+ remote: ..
3
+ specs:
4
+ active_record-nested_attributes-destroy_if (0.2.0)
5
+ activerecord (>= 5.1, < 6.1)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actioncable (6.0.2.1)
11
+ actionpack (= 6.0.2.1)
12
+ nio4r (~> 2.0)
13
+ websocket-driver (>= 0.6.1)
14
+ actionmailbox (6.0.2.1)
15
+ actionpack (= 6.0.2.1)
16
+ activejob (= 6.0.2.1)
17
+ activerecord (= 6.0.2.1)
18
+ activestorage (= 6.0.2.1)
19
+ activesupport (= 6.0.2.1)
20
+ mail (>= 2.7.1)
21
+ actionmailer (6.0.2.1)
22
+ actionpack (= 6.0.2.1)
23
+ actionview (= 6.0.2.1)
24
+ activejob (= 6.0.2.1)
25
+ mail (~> 2.5, >= 2.5.4)
26
+ rails-dom-testing (~> 2.0)
27
+ actionpack (6.0.2.1)
28
+ actionview (= 6.0.2.1)
29
+ activesupport (= 6.0.2.1)
30
+ rack (~> 2.0, >= 2.0.8)
31
+ rack-test (>= 0.6.3)
32
+ rails-dom-testing (~> 2.0)
33
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
34
+ actiontext (6.0.2.1)
35
+ actionpack (= 6.0.2.1)
36
+ activerecord (= 6.0.2.1)
37
+ activestorage (= 6.0.2.1)
38
+ activesupport (= 6.0.2.1)
39
+ nokogiri (>= 1.8.5)
40
+ actionview (6.0.2.1)
41
+ activesupport (= 6.0.2.1)
42
+ builder (~> 3.1)
43
+ erubi (~> 1.4)
44
+ rails-dom-testing (~> 2.0)
45
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
46
+ activejob (6.0.2.1)
47
+ activesupport (= 6.0.2.1)
48
+ globalid (>= 0.3.6)
49
+ activemodel (6.0.2.1)
50
+ activesupport (= 6.0.2.1)
51
+ activerecord (6.0.2.1)
52
+ activemodel (= 6.0.2.1)
53
+ activesupport (= 6.0.2.1)
54
+ activestorage (6.0.2.1)
55
+ actionpack (= 6.0.2.1)
56
+ activejob (= 6.0.2.1)
57
+ activerecord (= 6.0.2.1)
58
+ marcel (~> 0.3.1)
59
+ activesupport (6.0.2.1)
60
+ concurrent-ruby (~> 1.0, >= 1.0.2)
61
+ i18n (>= 0.7, < 2)
62
+ minitest (~> 5.1)
63
+ tzinfo (~> 1.1)
64
+ zeitwerk (~> 2.2)
65
+ appraisal (2.2.0)
66
+ bundler
67
+ rake
68
+ thor (>= 0.14.0)
69
+ builder (3.2.4)
70
+ byebug (11.1.1)
71
+ concurrent-ruby (1.1.5)
72
+ crass (1.0.6)
73
+ diff-lcs (1.3)
74
+ erubi (1.9.0)
75
+ globalid (0.4.2)
76
+ activesupport (>= 4.2.0)
77
+ i18n (1.8.2)
78
+ concurrent-ruby (~> 1.0)
79
+ loofah (2.4.0)
80
+ crass (~> 1.0.2)
81
+ nokogiri (>= 1.5.9)
82
+ mail (2.7.1)
83
+ mini_mime (>= 0.1.1)
84
+ marcel (0.3.3)
85
+ mimemagic (~> 0.3.2)
86
+ method_source (0.9.2)
87
+ mimemagic (0.3.3)
88
+ mini_mime (1.0.2)
89
+ mini_portile2 (2.4.0)
90
+ minitest (5.14.0)
91
+ nio4r (2.5.2)
92
+ nokogiri (1.10.7)
93
+ mini_portile2 (~> 2.4.0)
94
+ rack (2.1.1)
95
+ rack-test (1.1.0)
96
+ rack (>= 1.0, < 3)
97
+ rails (6.0.2.1)
98
+ actioncable (= 6.0.2.1)
99
+ actionmailbox (= 6.0.2.1)
100
+ actionmailer (= 6.0.2.1)
101
+ actionpack (= 6.0.2.1)
102
+ actiontext (= 6.0.2.1)
103
+ actionview (= 6.0.2.1)
104
+ activejob (= 6.0.2.1)
105
+ activemodel (= 6.0.2.1)
106
+ activerecord (= 6.0.2.1)
107
+ activestorage (= 6.0.2.1)
108
+ activesupport (= 6.0.2.1)
109
+ bundler (>= 1.3.0)
110
+ railties (= 6.0.2.1)
111
+ sprockets-rails (>= 2.0.0)
112
+ rails-dom-testing (2.0.3)
113
+ activesupport (>= 4.2.0)
114
+ nokogiri (>= 1.6)
115
+ rails-html-sanitizer (1.3.0)
116
+ loofah (~> 2.3)
117
+ railties (6.0.2.1)
118
+ actionpack (= 6.0.2.1)
119
+ activesupport (= 6.0.2.1)
120
+ method_source
121
+ rake (>= 0.8.7)
122
+ thor (>= 0.20.3, < 2.0)
123
+ rake (10.5.0)
124
+ rspec (3.9.0)
125
+ rspec-core (~> 3.9.0)
126
+ rspec-expectations (~> 3.9.0)
127
+ rspec-mocks (~> 3.9.0)
128
+ rspec-core (3.9.1)
129
+ rspec-support (~> 3.9.1)
130
+ rspec-expectations (3.9.0)
131
+ diff-lcs (>= 1.2.0, < 2.0)
132
+ rspec-support (~> 3.9.0)
133
+ rspec-mocks (3.9.1)
134
+ diff-lcs (>= 1.2.0, < 2.0)
135
+ rspec-support (~> 3.9.0)
136
+ rspec-support (3.9.2)
137
+ sprockets (4.0.0)
138
+ concurrent-ruby (~> 1.0)
139
+ rack (> 1, < 3)
140
+ sprockets-rails (3.2.1)
141
+ actionpack (>= 4.0)
142
+ activesupport (>= 4.0)
143
+ sprockets (>= 3.0.0)
144
+ sqlite3 (1.4.2)
145
+ thor (1.0.1)
146
+ thread_safe (0.3.6)
147
+ tzinfo (1.2.6)
148
+ thread_safe (~> 0.1)
149
+ websocket-driver (0.7.1)
150
+ websocket-extensions (>= 0.1.0)
151
+ websocket-extensions (0.1.4)
152
+ zeitwerk (2.2.2)
153
+
154
+ PLATFORMS
155
+ ruby
156
+
157
+ DEPENDENCIES
158
+ active_record-nested_attributes-destroy_if!
159
+ appraisal
160
+ bundler (~> 2.0)
161
+ byebug
162
+ rails (~> 6.0.0)
163
+ rake (~> 10.0)
164
+ rspec (~> 3.0)
165
+ sqlite3
166
+
167
+ BUNDLED WITH
168
+ 2.1.4
@@ -0,0 +1,161 @@
1
+ require "active_record/nested_attributes/destroy_if/version"
2
+ require "active_support/concern"
3
+ require "active_record"
4
+
5
+ module ActiveRecord
6
+ module NestedAttributesDestroyIf
7
+ extend ActiveSupport::Concern
8
+
9
+ class_methods do
10
+ def accepts_nested_attributes_for(*attr_names)
11
+ options = { allow_destroy: false, update_only: false }
12
+ options.update(attr_names.extract_options!)
13
+ options.assert_valid_keys(:allow_destroy, :reject_if, :destroy_if, :limit, :update_only)
14
+ options[:reject_if] = REJECT_ALL_BLANK_PROC if options[:reject_if] == :all_blank
15
+
16
+ attr_names.each do |association_name|
17
+ if reflection = _reflect_on_association(association_name)
18
+ reflection.autosave = true
19
+ define_autosave_validation_callbacks(reflection)
20
+
21
+ nested_attributes_options = self.nested_attributes_options.dup
22
+ nested_attributes_options[association_name.to_sym] = options
23
+ self.nested_attributes_options = nested_attributes_options
24
+
25
+ type = (reflection.collection? ? :collection : :one_to_one)
26
+ generate_association_writer(association_name, type)
27
+ else
28
+ raise ArgumentError, "No association found for name `#{association_name}'. Has it been defined yet?"
29
+ end
30
+ end
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ def assign_nested_attributes_for_one_to_one_association(association_name, attributes)
37
+ options = nested_attributes_options[association_name]
38
+ if attributes.respond_to?(:permitted?)
39
+ attributes = attributes.to_h
40
+ end
41
+ attributes = attributes.with_indifferent_access
42
+ existing_record = send(association_name)
43
+
44
+ if (options[:update_only] || !attributes["id"].blank?) && existing_record &&
45
+ (options[:update_only] || existing_record.id.to_s == attributes["id"].to_s)
46
+ assign_to_or_mark_for_destruction(existing_record, attributes, options[:allow_destroy], options[:destroy_if]) unless call_reject_if(association_name, attributes)
47
+
48
+ elsif attributes["id"].present?
49
+ raise_nested_attributes_record_not_found!(association_name, attributes["id"])
50
+
51
+ elsif !reject_new_record?(association_name, attributes)
52
+ assignable_attributes = attributes.except(*ActiveRecord::NestedAttributes::UNASSIGNABLE_KEYS)
53
+
54
+ if existing_record && existing_record.new_record?
55
+ existing_record.assign_attributes(assignable_attributes)
56
+ association(association_name).initialize_attributes(existing_record)
57
+ else
58
+ method = "build_#{association_name}"
59
+ if respond_to?(method)
60
+ send(method, assignable_attributes)
61
+ else
62
+ raise ArgumentError, "Cannot build association `#{association_name}'. Are you trying to build a polymorphic one-to-one association?"
63
+ end
64
+ end
65
+ end
66
+ end
67
+
68
+ def assign_nested_attributes_for_collection_association(association_name, attributes_collection)
69
+ options = nested_attributes_options[association_name]
70
+ if attributes_collection.respond_to?(:permitted?)
71
+ attributes_collection = attributes_collection.to_h
72
+ end
73
+
74
+ unless attributes_collection.is_a?(Hash) || attributes_collection.is_a?(Array)
75
+ raise ArgumentError, "Hash or Array expected for attribute `#{association_name}`, got #{attributes_collection.class.name} (#{attributes_collection.inspect})"
76
+ end
77
+
78
+ check_record_limit!(options[:limit], attributes_collection)
79
+
80
+ if attributes_collection.is_a? Hash
81
+ keys = attributes_collection.keys
82
+ attributes_collection = if keys.include?("id") || keys.include?(:id)
83
+ [attributes_collection]
84
+ else
85
+ attributes_collection.values
86
+ end
87
+ end
88
+
89
+ association = association(association_name)
90
+
91
+ existing_records = if association.loaded?
92
+ association.target
93
+ else
94
+ attribute_ids = attributes_collection.map { |a| a["id"] || a[:id] }.compact
95
+ attribute_ids.empty? ? [] : association.scope.where(association.klass.primary_key => attribute_ids)
96
+ end
97
+
98
+ attributes_collection.each do |attributes|
99
+ if attributes.respond_to?(:permitted?)
100
+ attributes = attributes.to_h
101
+ end
102
+ attributes = attributes.with_indifferent_access
103
+
104
+ if attributes["id"].blank?
105
+ unless reject_new_record?(association_name, attributes)
106
+ association.build(attributes.except(*ActiveRecord::NestedAttributes::UNASSIGNABLE_KEYS))
107
+ end
108
+ elsif existing_record = existing_records.detect { |record| record.id.to_s == attributes["id"].to_s }
109
+ unless call_reject_if(association_name, attributes)
110
+ # Make sure we are operating on the actual object which is in the association's
111
+ # proxy_target array (either by finding it, or adding it if not found)
112
+ # Take into account that the proxy_target may have changed due to callbacks
113
+ target_record = association.target.detect { |record| record.id.to_s == attributes["id"].to_s }
114
+ if target_record
115
+ existing_record = target_record
116
+ else
117
+ association.add_to_target(existing_record, :skip_callbacks)
118
+ end
119
+
120
+ assign_to_or_mark_for_destruction(existing_record, attributes, options[:allow_destroy], options[:destroy_if])
121
+ end
122
+ else
123
+ raise_nested_attributes_record_not_found!(association_name, attributes["id"])
124
+ end
125
+ end
126
+ end
127
+
128
+ def reject_new_record?(association_name, attributes)
129
+ will_be_destroyed?(association_name, attributes) ||
130
+ call_reject_if(association_name, attributes) ||
131
+ call_destroy_if(association_name, attributes)
132
+ end
133
+
134
+ def assign_to_or_mark_for_destruction record, attributes, allow_destroy, destroy_if
135
+ record.assign_attributes attributes.except(*ActiveRecord::NestedAttributes::UNASSIGNABLE_KEYS)
136
+ should_destroy =
137
+ has_destroy_flag?(attributes) && allow_destroy ||
138
+ case destroy_if
139
+ when Symbol
140
+ method(destroy_if).arity == 0 ? send(destroy_if) : send(destroy_if, attributes)
141
+ when Proc
142
+ destroy_if.call(attributes)
143
+ end
144
+ record.mark_for_destruction if should_destroy
145
+ end
146
+
147
+ def call_destroy_if(association_name, attributes)
148
+ return false if will_be_destroyed?(association_name, attributes)
149
+
150
+ case callback = nested_attributes_options[association_name][:destroy_if]
151
+ when Symbol
152
+ method(callback).arity == 0 ? send(callback) : send(callback, attributes)
153
+ when Proc
154
+ callback.call(attributes)
155
+ end
156
+ end
157
+ end
158
+ end
159
+
160
+ ActiveRecord::Base.include ActiveRecord::NestedAttributesDestroyIf
161
+
@@ -0,0 +1,6 @@
1
+ module ActiveRecord
2
+ module NestedAttributesDestroyIf
3
+ VERSION = "0.2.0"
4
+ end
5
+ end
6
+
metadata ADDED
@@ -0,0 +1,172 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_record-nested_attributes-destroy_if
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Micah Geisel
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-01-27 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.1'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '6.1'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '5.1'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '6.1'
33
+ - !ruby/object:Gem::Dependency
34
+ name: bundler
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '2.0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '2.0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: appraisal
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rake
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '10.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '10.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rspec
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '3.0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '3.0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: sqlite3
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: byebug
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ description: Adds :destroy_if option to accepts_nested_attributes_for, which is basically
118
+ a stronger version of :reject_if that destroys existing records, too.
119
+ email:
120
+ - micah@botandrose.com
121
+ executables: []
122
+ extensions: []
123
+ extra_rdoc_files: []
124
+ files:
125
+ - ".gitignore"
126
+ - ".rspec"
127
+ - ".ruby-gemset"
128
+ - ".ruby-version"
129
+ - ".travis.yml"
130
+ - Appraisals
131
+ - Gemfile
132
+ - Gemfile.lock
133
+ - LICENSE.txt
134
+ - README.md
135
+ - Rakefile
136
+ - active_record-nested_attributes-destroy_if.gemspec
137
+ - bin/console
138
+ - bin/setup
139
+ - gemfiles/.bundle/config
140
+ - gemfiles/rails_5.0.gemfile
141
+ - gemfiles/rails_5.1.gemfile
142
+ - gemfiles/rails_5.1.gemfile.lock
143
+ - gemfiles/rails_5.2.gemfile
144
+ - gemfiles/rails_5.2.gemfile.lock
145
+ - gemfiles/rails_6.0.gemfile
146
+ - gemfiles/rails_6.0.gemfile.lock
147
+ - lib/active_record/nested_attributes/destroy_if.rb
148
+ - lib/active_record/nested_attributes/destroy_if/version.rb
149
+ homepage: https://github.com/botandrose/active_record-nested_attributes-destroy_if
150
+ licenses:
151
+ - MIT
152
+ metadata: {}
153
+ post_install_message:
154
+ rdoc_options: []
155
+ require_paths:
156
+ - lib
157
+ required_ruby_version: !ruby/object:Gem::Requirement
158
+ requirements:
159
+ - - ">="
160
+ - !ruby/object:Gem::Version
161
+ version: '0'
162
+ required_rubygems_version: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ requirements: []
168
+ rubygems_version: 3.0.3
169
+ signing_key:
170
+ specification_version: 4
171
+ summary: Adds :destroy_if option to accepts_nested_attributes_for
172
+ test_files: []