Skip to content

Commit

Permalink
Disable validations for associations
Browse files Browse the repository at this point in the history
  • Loading branch information
jkowens committed Aug 29, 2024
1 parent efb99e5 commit 3efa9a3
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions lib/activerecord-import/import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,18 @@ def init_validations(klass)
@validate_callbacks.each_with_index do |callback, i|
filter = callback.respond_to?(:raw_filter) ? callback.raw_filter : callback.filter
next unless filter.class.name =~ /Validations::PresenceValidator/ ||
(filter.is_a?(Symbol) && filter.to_s =~ /validate_associated_records_for/) ||
(!@options[:validate_uniqueness] &&
filter.is_a?(ActiveRecord::Validations::UniquenessValidator))

callback = callback.dup
filter = filter.dup
attrs = filter.instance_variable_get(:@attributes).dup
attrs = filter.instance_variable_get(:@attributes).dup || []
if_condition = callback.instance_variable_get(:@if).dup
skip_proc = -> { false }

if filter.is_a?(ActiveRecord::Validations::UniquenessValidator)
attrs = []
if filter.is_a?(Symbol) || filter.is_a?(ActiveRecord::Validations::UniquenessValidator)
if_condition = [skip_proc] unless filter.is_a?(Symbol) && options[:all_or_none]
else
associations = klass.reflect_on_all_associations(:belongs_to)
associations.each do |assoc|
Expand All @@ -57,16 +60,18 @@ def init_validations(klass)
end
end

filter.instance_variable_set(:@attributes, attrs.flatten)
filter.instance_variable_set(:@attributes, attrs.flatten) unless filter.is_a?(Symbol)

if @validate_callbacks.respond_to?(:chain, true)
@validate_callbacks.send(:chain).tap do |chain|
callback.instance_variable_set(:@filter, filter)
callback.instance_variable_set(:@if, if_condition)
callback.instance_variable_set(:@compiled, nil)
chain[i] = callback
end
else
callback.raw_filter = filter
callback.options = callback.options.merge(if: if_condition)
callback.filter = callback.send(:_compile_filter, filter)
@validate_callbacks[i] = callback
end
Expand Down

0 comments on commit 3efa9a3

Please sign in to comment.