dependent-auto-rails 0.1.2 → 0.1.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c1982e7a765a8ef3c1f24c32b132d561b64995e02c8ee593d6e932ff10d4105b
4
- data.tar.gz: c57c2666e92829fc28847f483d6cdc117a14063d6db999c4f767aa282a394670
3
+ metadata.gz: d8a5de40e738a480461d249edc98b76df49778d58e77fee8a3a54f90ffa017ad
4
+ data.tar.gz: 8bbb14703a6e1a20da97cde693f05000f77202eae0e05ecbfe37d0fd7ce8d749
5
5
  SHA512:
6
- metadata.gz: 010013ca9d2b1c30d36cebce8d79a60e2c97eff4757df3180774dce02cb3a4f7aa32e45480d4995ae0a8028b06f2b50f0adae15208105a068434351cafa6d149
7
- data.tar.gz: ca790ffffa4bbad4e3b9adf2d9a32832f346f3460d7b8807e0fb7a989780ed5e042c2bb86e242c1036c43df63c73af4ef77be327197f0745a67fdcd190beb10c
6
+ metadata.gz: 828ba4145c234101a34c753c6d247739f4ee97734a8ab1a5c065d443e5b60cdcb97713c7180d0fc317ba536760827363ec14cb8676e4b59bd628b0f38e00f4ad
7
+ data.tar.gz: 1efacf7ea4308f2afe9e7d998ed0fc8fa9e8e23bf06bd0d8ee484c10a7e4bc5ea3cc50a61588577ab8163854b123ae884204255362287e93b832f3396ce1d806
@@ -2,27 +2,67 @@
2
2
 
3
3
  module ActiveRecord::Associations::Builder
4
4
  class Association
5
- def self.build(model, name, scope, options, &block)
6
- if model.dangerous_attribute_method?(name)
7
- raise ArgumentError, "You tried to define an association named #{name} on the model #{model.name}, but " \
8
- "this will conflict with a method #{name} already defined by Active Record. " \
9
- "Please choose a different association name."
10
- end
5
+ def self.create_reflection(model, name, scope, options, &block)
6
+ raise ArgumentError, "association names must be a Symbol" unless name.is_a?(Symbol)
7
+
8
+ validate_options(options)
9
+
10
+ extension = define_extensions(model, name, &block)
11
+ options[:extend] = [*options[:extend], extension] if extension
12
+
13
+ scope = build_scope(scope)
14
+
15
+ ActiveRecord::Reflection.create(macro, name, scope, dynamic_reflection_options(model, options), model)
16
+ end
17
+
18
+ def self.dynamic_reflection_options(model, options)
19
+ DynamicReflectionOptionsHash.new.merge!(
20
+ options,
21
+ {
22
+ model_name: model.name,
23
+ association_type: if self < ActiveRecord::Associations::Builder::SingularAssociation
24
+ :singular
25
+ elsif self < ActiveRecord::Associations::Builder::CollectionAssociation
26
+ :collection
27
+ else
28
+ raise DependentAutoRails::Error, "Unsupported association type"
29
+ end
30
+ }
31
+ )
32
+ end
33
+ private_class_method :dynamic_reflection_options
34
+
35
+ class DynamicReflectionOptionsHash < Hash
36
+ def [](key)
37
+ return super unless key == :dependent && super(:dependent) == :auto
38
+ return fallback_method if defining_dependent_callbacks?
11
39
 
12
- if options[:dependent] == :auto
13
- options[:dependent] = if model._destroy_callbacks.empty?
14
- :destroy
15
- else
16
- :delete
40
+ model = super(:model_name).constantize
41
+ return :destroy unless valid_destroy_callbacks(model).empty?
42
+
43
+ case super(:association_type)
44
+ when :singular then :delete
45
+ when :collection then :delete_all
46
+ else fallback_method
17
47
  end
18
48
  end
19
49
 
20
- reflection = create_reflection(model, name, scope, options, &block)
21
- define_accessors model, reflection
22
- define_callbacks model, reflection
23
- define_validations model, reflection
24
- define_change_tracking_methods model, reflection
25
- reflection
50
+ private
51
+
52
+ def fallback_method
53
+ :destroy
54
+ end
55
+
56
+ def defining_dependent_callbacks?
57
+ caller.any? { |line| line.include?("active_record/associations/builder/association.rb") }
58
+ end
59
+
60
+ def valid_destroy_callbacks(model)
61
+ model._destroy_callbacks.reject do |callback|
62
+ # ignore #handle_dependency callback
63
+ callback.filter.to_s.include?("active_record/associations/builder/association.rb")
64
+ end
65
+ end
26
66
  end
27
67
  end
28
68
  end
@@ -5,3 +5,7 @@ require "active_record"
5
5
  ActiveSupport.on_load(:active_record) do
6
6
  require "dependent-auto-rails/activerecord/associations/builder/association"
7
7
  end
8
+
9
+ module DependentAutoRails
10
+ class Error < StandardError; end
11
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DependentAutoRails
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.4"
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.2
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua Young
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  description:
28
42
  email:
29
43
  - djry1999@gmail.com
@@ -64,6 +78,6 @@ requirements: []
64
78
  rubygems_version: 3.5.3
65
79
  signing_key:
66
80
  specification_version: 4
67
- summary: Automatically and safely decides between :destroy and :delete for your Rails
68
- associations.
81
+ summary: Automatically and safely decides between :destroy and :delete / :delete_all
82
+ for your Rails associations.
69
83
  test_files: []