activerecord-import 0.28.0 → 0.28.1

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
  SHA1:
3
- metadata.gz: 0b3c71766c00d489ea17001a179599e9bfe643cc
4
- data.tar.gz: cce704816a91718a1e81fee5c1e799447655bf3a
3
+ metadata.gz: dbd7badbb2e98c6725ddaf5edbb713c4f79bfc01
4
+ data.tar.gz: 0912df3959070820db94063d0172a43f4b027a9f
5
5
  SHA512:
6
- metadata.gz: 9880191ac0df7086dc8b5be319262655cd43d365ae9df6cde654bc8374d57d75a2aafe3018d5f63e1af1fbd64f9041bb15fb5a967d5ece0e0d5702dd739c0123
7
- data.tar.gz: 989fde16bae64ff5789e481053e6a596b6f172b0d9085846d7bb91c108fb1ebb01c8d8ea20c70794476b3b7ddbb4d15efe985f54780642fddcc98991061406cb
6
+ metadata.gz: 22fb70781c58df1795c41e5b8e954b8a89e6c85be0dbf33f2c2fb9265847778f99f414cbd7fe9ea945d6aa43f89d7557ea41541f938fa357a7bbcb2c0e6114df
7
+ data.tar.gz: b480446464f626518ac128b2e79d4e4dbb2b50e8e52d6781812a0ee1594e5328430214ad03be9590c09dcfd07661ae54130b53ccde1b9b7d2131d2f40c80a633
@@ -1,3 +1,10 @@
1
+ ## Changes in 0.28.1
2
+
3
+ ### Fixes
4
+
5
+ * Fix issue where ActiveRecord presence validations were being mutated.
6
+ Limited custom presence validation to bulk imports.
7
+
1
8
  ## Changes in 0.28.0
2
9
 
3
10
  ### New Features
@@ -24,42 +24,57 @@ module ActiveRecord::Import #:nodoc:
24
24
  end
25
25
 
26
26
  class Validator
27
- def initialize(options = {})
27
+ def initialize(klass, options = {})
28
28
  @options = options
29
+ init_validations(klass)
30
+ end
31
+
32
+ def init_validations(klass)
33
+ @validate_callbacks = klass._validate_callbacks.dup
34
+
35
+ klass._validate_callbacks.each_with_index do |callback, i|
36
+ filter = callback.raw_filter
37
+
38
+ if filter.class.name =~ /Validations::PresenceValidator/
39
+ callback = callback.dup
40
+ filter = filter.dup
41
+ associations = klass.reflect_on_all_associations(:belongs_to)
42
+ attrs = filter.instance_variable_get(:@attributes).dup
43
+ associations.each do |assoc|
44
+ if (index = attrs.index(assoc.name))
45
+ key = assoc.foreign_key.to_sym
46
+ attrs[index] = key unless attrs.include?(key)
47
+ end
48
+ end
49
+ filter.instance_variable_set(:@attributes, attrs)
50
+ if @validate_callbacks.respond_to?(:chain, true)
51
+ @validate_callbacks.send(:chain).tap do |chain|
52
+ callback.instance_variable_set(:@filter, filter)
53
+ chain[i] = callback
54
+ end
55
+ else
56
+ callback.raw_filter = filter
57
+ callback.filter = callback.send(:_compile_filter, filter)
58
+ @validate_callbacks[i] = callback
59
+ end
60
+ elsif !@options[:validate_uniqueness] && filter.is_a?(ActiveRecord::Validations::UniquenessValidator)
61
+ @validate_callbacks.delete(callback)
62
+ end
63
+ end
29
64
  end
30
65
 
31
66
  def valid_model?(model)
32
67
  validation_context = @options[:validate_with_context]
33
68
  validation_context ||= (model.new_record? ? :create : :update)
34
-
35
69
  current_context = model.send(:validation_context)
70
+
36
71
  begin
37
72
  model.send(:validation_context=, validation_context)
38
73
  model.errors.clear
39
74
 
40
- validate_callbacks = model._validate_callbacks.dup
41
-
42
- model._validate_callbacks.each do |callback|
43
- filter = callback.raw_filter
44
-
45
- if filter.class.name =~ /Validations::PresenceValidator/
46
- associations = model.class.reflect_on_all_associations(:belongs_to)
47
- attrs = filter.instance_variable_get(:@attributes)
48
- associations.each do |assoc|
49
- if (index = attrs.index(assoc.name))
50
- key = assoc.foreign_key.to_sym
51
- attrs[index] = key unless attrs.include?(key)
52
- end
53
- end
54
- filter.instance_variable_set(:@attributes, attrs)
55
- elsif !@options[:validate_uniqueness] && filter.is_a?(ActiveRecord::Validations::UniquenessValidator)
56
- validate_callbacks.delete(callback)
57
- end
58
- end
59
-
60
75
  model.run_callbacks(:validation) do
61
76
  if defined?(ActiveSupport::Callbacks::Filters::Environment) # ActiveRecord >= 4.1
62
- runner = validate_callbacks.compile
77
+ runner = @validate_callbacks.compile
63
78
  env = ActiveSupport::Callbacks::Filters::Environment.new(model, false, nil)
64
79
  if runner.respond_to?(:call) # ActiveRecord < 5.1
65
80
  runner.call(env)
@@ -78,10 +93,10 @@ module ActiveRecord::Import #:nodoc:
78
93
  runner.invoke_before(env)
79
94
  runner.invoke_after(env)
80
95
  end
81
- elsif validate_callbacks.method(:compile).arity == 0 # ActiveRecord = 4.0
82
- model.instance_eval validate_callbacks.compile
96
+ elsif @validate_callbacks.method(:compile).arity == 0 # ActiveRecord = 4.0
97
+ model.instance_eval @validate_callbacks.compile
83
98
  else # ActiveRecord 3.x
84
- model.instance_eval validate_callbacks.compile(nil, model)
99
+ model.instance_eval @validate_callbacks.compile(nil, model)
85
100
  end
86
101
  end
87
102
 
@@ -529,7 +544,7 @@ class ActiveRecord::Base
529
544
  options[:locking_column] = locking_column if attribute_names.include?(locking_column)
530
545
 
531
546
  is_validating = options[:validate_with_context].present? ? true : options[:validate]
532
- validator = ActiveRecord::Import::Validator.new(options)
547
+ validator = ActiveRecord::Import::Validator.new(self, options)
533
548
 
534
549
  # assume array of model objects
535
550
  if args.last.is_a?( Array ) && args.last.first.is_a?(ActiveRecord::Base)
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module Import
3
- VERSION = "0.28.0".freeze
3
+ VERSION = "0.28.1".freeze
4
4
  end
5
5
  end
@@ -316,6 +316,13 @@ describe "#import" do
316
316
  UserToken.import [:user_name, :token], [%w("Bob", "12345abcdef67890")]
317
317
  end
318
318
  end
319
+
320
+ it "should not mutate the defined validations" do
321
+ UserToken.import [:user_name, :token], [%w("Bob", "12345abcdef67890")]
322
+ ut = UserToken.new
323
+ ut.valid?
324
+ assert_includes ut.errors.messages, :user
325
+ end
319
326
  end
320
327
  end
321
328
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-import
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.28.0
4
+ version: 0.28.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zach Dennis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-12-28 00:00:00.000000000 Z
11
+ date: 2019-01-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord