Class: ActiveRecord::Import::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/activerecord-import/import.rb

Instance Method Summary collapse

Constructor Details

#initialize(klass, options = {}) ⇒ Validator

Returns a new instance of Validator.



29
30
31
32
33
# File 'lib/activerecord-import/import.rb', line 29

def initialize(klass, options = {})
  @options = options
  @validator_class = klass
  init_validations(klass)
end

Instance Method Details

#init_validations(klass) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/activerecord-import/import.rb', line 35

def init_validations(klass)
  @validate_callbacks = klass._validate_callbacks.dup

  @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/ ||
                (!@options[:validate_uniqueness] &&
                 filter.is_a?(ActiveRecord::Validations::UniquenessValidator))

    callback = callback.dup
    filter = filter.dup
    attrs = filter.instance_variable_get(:@attributes).dup

    if filter.is_a?(ActiveRecord::Validations::UniquenessValidator)
      attrs = []
    else
      associations = klass.reflect_on_all_associations(:belongs_to)
      associations.each do |assoc|
        if (index = attrs.index(assoc.name))
          key = assoc.foreign_key.is_a?(Array) ? assoc.foreign_key.map(&:to_sym) : assoc.foreign_key.to_sym
          attrs[index] = key unless attrs.include?(key)
        end
      end
    end

    filter.instance_variable_set(:@attributes, attrs.flatten)

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

#valid_model?(model) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/activerecord-import/import.rb', line 76

def valid_model?(model)
  init_validations(model.class) unless model.instance_of?(@validator_class)

  validation_context = @options[:validate_with_context]
  validation_context ||= (model.new_record? ? :create : :update)
  current_context = model.send(:validation_context)

  begin
    model.send(:validation_context=, validation_context)
    model.errors.clear

    model.run_callbacks(:validation) do
      if defined?(ActiveSupport::Callbacks::Filters::Environment) # ActiveRecord >= 4.1
        runner = if @validate_callbacks.method(:compile).arity == 0
          @validate_callbacks.compile
        else # ActiveRecord >= 7.1
          @validate_callbacks.compile(nil)
        end
        env = ActiveSupport::Callbacks::Filters::Environment.new(model, false, nil)
        if runner.respond_to?(:call) # ActiveRecord < 5.1
          runner.call(env)
        else # ActiveRecord >= 5.1
          # Note that this is a gross simplification of ActiveSupport::Callbacks#run_callbacks.
          # It's technically possible for there to exist an "around" callback in the
          # :validate chain, but this would be an aberration, since Rails doesn't define
          # "around_validate". Still, rather than silently ignoring such callbacks, we
          # explicitly raise a RuntimeError, since activerecord-import was asked to perform
          # validations and it's unable to do so.
          #
          # The alternative here would be to copy-and-paste the bulk of the
          # ActiveSupport::Callbacks#run_callbacks method, which is undesirable if there's
          # no real-world use case for it.
          raise "The :validate callback chain contains an 'around' callback, which is unsupported" unless runner.final?
          runner.invoke_before(env)
          # Ensure a truthy value is returned. ActiveRecord < 7.2 always returned an array.
          runner.invoke_after(env) || []
        end
      elsif @validate_callbacks.method(:compile).arity == 0 # ActiveRecord = 4.0
        model.instance_eval @validate_callbacks.compile
      else # ActiveRecord 3.x
        model.instance_eval @validate_callbacks.compile(nil, model)
      end
    end

    model.errors.empty?
  ensure
    model.send(:validation_context=, current_context)
  end
end