Class: ActiveRecordBetterDependentErrorMessages::DestroyValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record_better_dependent_error_messages/destroy_validator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_model:, model:, trace:, args:) ⇒ DestroyValidator

Returns a new instance of DestroyValidator.



4
5
6
7
8
9
# File 'lib/active_record_better_dependent_error_messages/destroy_validator.rb', line 4

def initialize(root_model:, model:, trace:, args:)
  @args = args
  @root_model = root_model
  @model = model
  @trace = trace
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



2
3
4
# File 'lib/active_record_better_dependent_error_messages/destroy_validator.rb', line 2

def args
  @args
end

#modelObject (readonly)

Returns the value of attribute model.



2
3
4
# File 'lib/active_record_better_dependent_error_messages/destroy_validator.rb', line 2

def model
  @model
end

#root_modelObject (readonly)

Returns the value of attribute root_model.



2
3
4
# File 'lib/active_record_better_dependent_error_messages/destroy_validator.rb', line 2

def root_model
  @root_model
end

#traceObject (readonly)

Returns the value of attribute trace.



2
3
4
# File 'lib/active_record_better_dependent_error_messages/destroy_validator.rb', line 2

def trace
  @trace
end

Class Method Details

.call(root_model: nil, model:, trace: nil, **args) ⇒ Object



11
12
13
14
15
16
# File 'lib/active_record_better_dependent_error_messages/destroy_validator.rb', line 11

def self.call(root_model: nil, model:, trace: nil, **args)
  root_model ||= model
  trace ||= [model]

  ActiveRecordBetterDependentErrorMessages::DestroyValidator.new(root_model: root_model, model: model, trace: trace, args: args).()
end

Instance Method Details

#callObject



18
19
20
21
22
23
24
25
26
# File 'lib/active_record_better_dependent_error_messages/destroy_validator.rb', line 18

def call
  model.class.reflect_on_all_associations.each do |association|
    if association.options[:dependent] == :restrict_with_error || association.options[:dependent] == :restrict_with_exception
      check_restrict_association(association)
    elsif association.options[:dependent] == :destroy
      check_sub_destroy_association(association)
    end
  end
end