active_record-nested_attributes-destroy_if 0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b3f9cedfeb35032c5d40a02c4b2c0733a3b9577ba4461038dccf551ac11cc350
4
+ data.tar.gz: d08dec119e05d52874ca47436a83dae3eb117642113ee227cca5b85d39da40fb
5
+ SHA512:
6
+ metadata.gz: bfb80b4fef7e1c456b582fb85a99aae4a5ca96e9440afed23b6be96fb2c838509ad96f1a69fc4117e3ab52dc690d3ce836b5218f09eec67f8c856eb3634a55f7
7
+ data.tar.gz: 4e96432270e348e0c1e2688814958eb99f0951e73e014864ab7bf38cc88f3ea2d491d41a7c5562d5cce749fb28c79f7a90a6ec76b805d82d3386885740226cb4
@@ -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,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.3
7
+ before_install: gem install bundler -v 2.0.1
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,59 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ active_record-nested_attributes-destroy_if (0.1.0)
5
+ activerecord (~> 5.2.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (5.2.4.1)
11
+ activesupport (= 5.2.4.1)
12
+ activerecord (5.2.4.1)
13
+ activemodel (= 5.2.4.1)
14
+ activesupport (= 5.2.4.1)
15
+ arel (>= 9.0)
16
+ activesupport (5.2.4.1)
17
+ concurrent-ruby (~> 1.0, >= 1.0.2)
18
+ i18n (>= 0.7, < 2)
19
+ minitest (~> 5.1)
20
+ tzinfo (~> 1.1)
21
+ arel (9.0.0)
22
+ byebug (11.1.0)
23
+ concurrent-ruby (1.1.5)
24
+ diff-lcs (1.3)
25
+ i18n (1.8.2)
26
+ concurrent-ruby (~> 1.0)
27
+ minitest (5.14.0)
28
+ rake (10.5.0)
29
+ rspec (3.9.0)
30
+ rspec-core (~> 3.9.0)
31
+ rspec-expectations (~> 3.9.0)
32
+ rspec-mocks (~> 3.9.0)
33
+ rspec-core (3.9.1)
34
+ rspec-support (~> 3.9.1)
35
+ rspec-expectations (3.9.0)
36
+ diff-lcs (>= 1.2.0, < 2.0)
37
+ rspec-support (~> 3.9.0)
38
+ rspec-mocks (3.9.1)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.9.0)
41
+ rspec-support (3.9.2)
42
+ sqlite3 (1.4.2)
43
+ thread_safe (0.3.6)
44
+ tzinfo (1.2.6)
45
+ thread_safe (~> 0.1)
46
+
47
+ PLATFORMS
48
+ ruby
49
+
50
+ DEPENDENCIES
51
+ active_record-nested_attributes-destroy_if!
52
+ bundler (~> 2.0)
53
+ byebug
54
+ rake (~> 10.0)
55
+ rspec (~> 3.0)
56
+ sqlite3
57
+
58
+ BUNDLED WITH
59
+ 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,67 @@
1
+ # ActiveRecord::NestedAttributesDestroyIf
2
+
3
+ Adds a `:destroy_if` option to `.accepts_nested_attributes_for`, which is basically a stronger version of `:reject_if` that also destroys existing records.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'active_record-nested_attributes-destroy_if'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ ## Usage
18
+
19
+ 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.
20
+
21
+ ```ruby
22
+ class Parent < ActiveRecord::Base
23
+ has_many :children
24
+ accepts_nested_attributes_for :children, destroy_if: proc { |attrs| attrs["name"].blank? }
25
+ end
26
+
27
+ class Child < ActiveRecord::Base
28
+ belongs_to :parent
29
+ end
30
+
31
+ tywin = Parent.create!(id: 1, name: "Tywin")
32
+ Child.create!(id: 1, parent_id: 1, name: "Jaime")
33
+ Child.create!(id: 2, parent_id: 1, name: "Tyrion")
34
+
35
+ tywin.children # => [<Child id: 1, parent_id: 1, name: "Jaime">, <Child id: 2, parent_id: 1, name: "Tyrion">]
36
+
37
+ tywin.update!({
38
+ children_attributes: {
39
+ "0" => {
40
+ id: 1,
41
+ name: "Ser Jaime",
42
+ },
43
+ "1" => {
44
+ id: 2,
45
+ name: "",
46
+ },
47
+ },
48
+ })
49
+
50
+ tywin.children # => [<Child id: 1, parent_id: 1, name: "Ser Jaime">]
51
+
52
+ Child.find(2) # => raises ActiveRecord::RecordNotFound! Tyrion was destroyed!
53
+ ```
54
+
55
+ ## Development
56
+
57
+ 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.
58
+
59
+ 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).
60
+
61
+ ## Contributing
62
+
63
+ Bug reports and pull requests are welcome on GitHub at https://github.com/botandrose/active_record-nested_attributes-destroy_if.
64
+
65
+ ## License
66
+
67
+ 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,33 @@
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.2.0"
27
+
28
+ spec.add_development_dependency "bundler", "~> 2.0"
29
+ spec.add_development_dependency "rake", "~> 10.0"
30
+ spec.add_development_dependency "rspec", "~> 3.0"
31
+ spec.add_development_dependency "sqlite3"
32
+ spec.add_development_dependency "byebug"
33
+ 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,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.1.0"
4
+ end
5
+ end
6
+
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_record-nested_attributes-destroy_if
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Micah Geisel
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-01-23 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
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sqlite3
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: byebug
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Adds :destroy_if option to accepts_nested_attributes_for, which is basically
98
+ a stronger version of :reject_if that destroys existing records, too.
99
+ email:
100
+ - micah@botandrose.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".rspec"
107
+ - ".ruby-gemset"
108
+ - ".ruby-version"
109
+ - ".travis.yml"
110
+ - Gemfile
111
+ - Gemfile.lock
112
+ - LICENSE.txt
113
+ - README.md
114
+ - Rakefile
115
+ - active_record-nested_attributes-destroy_if.gemspec
116
+ - bin/console
117
+ - bin/setup
118
+ - lib/active_record/nested_attributes/destroy_if.rb
119
+ - lib/active_record/nested_attributes/destroy_if/version.rb
120
+ homepage: https://github.com/botandrose/active_record-nested_attributes-destroy_if
121
+ licenses:
122
+ - MIT
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubygems_version: 3.0.3
140
+ signing_key:
141
+ specification_version: 4
142
+ summary: Adds :destroy_if option to accepts_nested_attributes_for
143
+ test_files: []