dependent-auto-rails 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b0596b3547afda10b9f5dbde19305f2f82513c08f0e0f2cbba8ea61d6c7d3e36
4
- data.tar.gz: 34d8c36bcec58674d9b1d98f130335e102896493ab7a86aba4ee7ff1aada0b28
3
+ metadata.gz: a862d80808c8cc6d619f6c5d2b2b2eb064a54f1382ee27a806f81608e5d83165
4
+ data.tar.gz: e6c078c89f423af15585de3a697688fb314aa24ffb3995005f479cb2c6ac4650
5
5
  SHA512:
6
- metadata.gz: fb17812a18ac89155498d56a7a11fbdc36076c9f0859765e18dd6abbaf33d26d7495582cf294b9f464fe49fc316eb23ed529c52dc94e91d7a0098782119a9bbb
7
- data.tar.gz: e242a528349742be08232e9fae587d630de0d78db8962151ff64af97b414d668dc3f62db70b82cac136692a7bd9d345a8c99420384408f0a1430237191303469
6
+ metadata.gz: bee37a9ce116600e8458bd1af1580011e0fd91ea9f2cf70eb91b5e62ce9f3ab353cce542e0d6f8ce3fd973aaea70149df915a4411f5d47841a728cd6ac4de988
7
+ data.tar.gz: 6aceba442aae030f9eeebb8735623a89823b018ddf531ffcde1c9d179fffbe1b4b892bd8c2b046eccc20a819018c4fddcf3904089cf0de30512959958d239354
data/README.md CHANGED
@@ -1,24 +1,44 @@
1
- # [WIP] Dependent::Auto::Rails
1
+ # dependent-auto-rails
2
2
 
3
- TODO: Delete this and the text below, and describe your gem
3
+ This gem provides a new `dependent` option for ActiveRecord associations, `:auto`. Using this option will automatically select between `:destroy` and `:delete` / `:delete_all` during runtime based on whether or not the associated model has any destroy callbacks defined. This is useful since `dependent: :destroy` always initialises the associated records in order to execute their destroy callbacks regardless of whether or not there are any defined. This can be expensive if there are many records to destroy.
4
4
 
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/dependent/auto/rails`. To experiment with that code, run `bin/console` for an interactive prompt.
5
+ It is also useful since a model's associations are rarely updated, but it's business logic can change frequently. This means that if destroy callbacks are added or removed on the associated model, the `dependent` option on the parent model's association may need to be updated to reflect this. Using `dependent: :auto` will automatically select the appropriate `dependent` option based on the current state of the model.
6
6
 
7
- ## Installation
7
+ **NOTE**: The `:auto` option **ONLY** decides between `:destroy` and `:delete` / `:delete_all`. It does not support any other `dependent` options such as:
8
+ - `:nullify`
9
+ - `:destroy_async`
10
+ - `:restrict_with_error`
11
+ - `:restrict_with_exception`
12
+
13
+ **NOTE**: If for some reason the `:auto` option is unable to decide between `:destroy` and `:delete` / `:delete_all`, it will default to `:destroy`.
8
14
 
9
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
15
+ ## Installation
10
16
 
11
- Install the gem and add to the application's Gemfile by executing:
17
+ Install the gem and add it to the application's Gemfile by executing:
12
18
 
13
- $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
19
+ $ bundle add dependent-auto-rails
14
20
 
15
21
  If bundler is not being used to manage dependencies, install the gem by executing:
16
22
 
17
- $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
23
+ $ gem installdependent-auto-rails
18
24
 
19
25
  ## Usage
20
26
 
21
- TODO: Write usage instructions here
27
+ To use this gem, all you need to do is add `dependent: :auto` to your model associations where you would usually use one of:
28
+ - `dependent: :destroy` (`belongs_to`, `has_one`, `has_many`)
29
+ - `dependent: :delete` (`belongs_to`, `has_one`)
30
+ - `dependent: :delete_all` (`has_many`).
31
+
32
+ ```ruby
33
+ class Post < ApplicationRecord
34
+ belongs_to :author, dependent: :auto
35
+
36
+ has_one :pinned_comment, dependent: :auto
37
+
38
+ has_many :comments, dependent: :auto
39
+ end
40
+
41
+ ```
22
42
 
23
43
  ## Development
24
44
 
@@ -28,7 +48,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
28
48
 
29
49
  ## Contributing
30
50
 
31
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/dependent-auto-rails. 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]/dependent-auto-rails/blob/main/CODE_OF_CONDUCT.md).
51
+ Bug reports and pull requests are welcome on GitHub at https://github.com/joshuay03/dependent-auto-rails. 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]/dependent-auto-rails/blob/main/CODE_OF_CONDUCT.md).
32
52
 
33
53
  ## License
34
54
 
@@ -8,56 +8,3 @@ module ActiveRecord::Associations::Builder
8
8
  private_class_method :macro, :valid_options, :valid_dependent_options
9
9
  end
10
10
  end
11
-
12
- module ActiveRecord
13
- module Associations
14
- class HasManyAssociation < CollectionAssociation
15
- def handle_dependency
16
- case options[:dependent]
17
- when :restrict_with_exception
18
- raise ActiveRecord::DeleteRestrictionError.new(reflection.name) unless empty?
19
-
20
- when :restrict_with_error
21
- unless empty?
22
- record = owner.class.human_attribute_name(reflection.name).downcase
23
- owner.errors.add(:base, :"restrict_dependent_destroy.has_many", record: record)
24
- throw(:abort)
25
- end
26
-
27
- when :destroy
28
- # No point in executing the counter update since we're going to destroy the parent anyway
29
- load_target.each { |t| t.destroyed_by_association = reflection }
30
- destroy_all
31
- when :destroy_async
32
- load_target.each do |t|
33
- t.destroyed_by_association = reflection
34
- end
35
-
36
- unless target.empty?
37
- association_class = target.first.class
38
- if association_class.query_constraints_list
39
- primary_key_column = association_class.query_constraints_list.map(&:to_sym)
40
- ids = target.collect { |assoc| primary_key_column.map { |col| assoc.public_send(col) } }
41
- else
42
- primary_key_column = association_class.primary_key.to_sym
43
- ids = target.collect { |assoc| assoc.public_send(primary_key_column) }
44
- end
45
-
46
- ids.each_slice(owner.class.destroy_association_async_batch_size || ids.size) do |ids_batch|
47
- enqueue_destroy_association(
48
- owner_model_name: owner.class.to_s,
49
- owner_id: owner.id,
50
- association_class: reflection.klass.to_s,
51
- association_ids: ids_batch,
52
- association_primary_key_column: primary_key_column,
53
- ensuring_owner_was_method: options.fetch(:ensuring_owner_was, nil)
54
- )
55
- end
56
- end
57
- else
58
- delete_all
59
- end
60
- end
61
- end
62
- end
63
- end
@@ -20,8 +20,9 @@ module ActiveRecord
20
20
  return super unless key == :dependent && super(:dependent) == :auto
21
21
  return fallback_method if defining_dependent_callbacks?
22
22
 
23
+ # TODO: This path can be memoized
23
24
  model = super(:association_model_name).constantize
24
- return :destroy unless valid_destroy_callbacks(model).empty?
25
+ return :destroy unless model._destroy_callbacks.empty?
25
26
 
26
27
  case super(:association_type)
27
28
  when :singular then :delete
@@ -39,13 +40,6 @@ module ActiveRecord
39
40
  def defining_dependent_callbacks?
40
41
  caller.any? { |line| line.include?("active_record/associations/builder/association.rb") }
41
42
  end
42
-
43
- def valid_destroy_callbacks(model)
44
- model._destroy_callbacks.reject do |callback|
45
- # ignore #handle_dependency callback
46
- callback.filter.to_s.include?("active_record/associations/builder/association.rb")
47
- end
48
- end
49
43
  end
50
44
  end
51
45
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DependentAutoRails
4
- VERSION = "0.1.6"
4
+ VERSION = "0.1.7"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dependent-auto-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua Young
@@ -80,6 +80,6 @@ requirements: []
80
80
  rubygems_version: 3.5.3
81
81
  signing_key:
82
82
  specification_version: 4
83
- summary: Automatically and safely decides between :destroy and :delete / :delete_all
84
- for your Rails associations.
83
+ summary: Automatically decides between :destroy and :delete / :delete_all for your
84
+ Rails associations.
85
85
  test_files: []