activerecord-import 2.3.0 → 3.0.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.
@@ -25,40 +25,53 @@ module ActiveRecord # :nodoc:
25
25
  def self.synchronize(instances, keys = [primary_key])
26
26
  return if instances.empty?
27
27
 
28
+ synchronize_instances(instances, keys) do |instance, fresh_attributes|
29
+ instance.instance_variable_set :@attributes, fresh_attributes
30
+ instance.clear_changes_information
31
+ end
32
+ instances
33
+ end
34
+
35
+ # Finds the records matching the passed in +instances+ on +keys+ and
36
+ # yields each instance that has one together with the attributes of
37
+ # its record, for the block to update the instance with. Returns the
38
+ # instances that had a matching record.
39
+ def self.synchronize_instances(instances, keys) # :nodoc:
40
+ return [] if instances.empty?
41
+
28
42
  conditions = {}
29
43
 
30
- key_values = keys.map { |key| instances.map(&key.to_sym) }
44
+ key_values = keys.map { |key| instances.map(&key.to_sym).uniq }
31
45
  keys.zip(key_values).each { |key, values| conditions[key] = values }
32
46
  order = keys.map { |key| "#{key} ASC" }.join(",")
33
47
 
34
48
  klass = instances.first.class
35
49
 
36
50
  fresh_instances = klass.unscoped.where(conditions).order(order)
37
- instances.each do |instance|
38
- matched_instance = fresh_instances.detect do |fresh_instance|
39
- keys.all? { |key| fresh_instance.send(key) == instance.send(key) }
40
- end
51
+ fresh_instances_by_key = fresh_instances.each_with_object({}) do |fresh_instance, records_by_key|
52
+ key = keys.map { |key_column| fresh_instance.send(key_column) }
53
+ records_by_key[key] ||= fresh_instance
54
+ end
55
+
56
+ instances.select do |instance|
57
+ key = keys.map { |key_column| instance.send(key_column) }
58
+ matched_instance = fresh_instances_by_key[key]
41
59
 
42
- next unless matched_instance
60
+ next false unless matched_instance
43
61
 
44
62
  instance.instance_variable_set :@association_cache, {}
63
+ # Aggregations is only included in models that call composed_of
45
64
  instance.send :clear_aggregation_cache if instance.respond_to?(:clear_aggregation_cache, true)
46
- instance.instance_variable_set :@attributes, matched_instance.instance_variable_get(:@attributes)
47
-
48
- if instance.respond_to?(:clear_changes_information)
49
- instance.clear_changes_information # Rails 4.2 and higher
50
- else
51
- instance.instance_variable_set :@attributes_cache, {} # Rails 4.0, 4.1
52
- instance.changed_attributes.clear # Rails 3.2
53
- instance.previous_changes.clear
54
- end
65
+ yield instance, matched_instance.instance_variable_get(:@attributes)
55
66
 
56
67
  # Since the instance now accurately reflects the record in
57
68
  # the database, ensure that instance.persisted? is true.
58
69
  instance.instance_variable_set '@new_record', false
59
70
  instance.instance_variable_set '@destroyed', false
71
+ true
60
72
  end
61
73
  end
74
+ private_class_method :synchronize_instances
62
75
 
63
76
  # See ActiveRecord::ConnectionAdapters::AbstractAdapter.synchronize
64
77
  def synchronize(instances, key = [ActiveRecord::Base.primary_key])
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ActiveRecord
4
4
  module Import
5
- VERSION = "2.3.0"
5
+ VERSION = "3.0.1"
6
6
  end
7
7
  end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Loads activerecord-import without aliasing +import+ and +import!+ to
4
+ # +bulk_import+ and +bulk_import!+, for compatibility with gems that define
5
+ # their own +import+ method on models, such as elasticsearch-model:
6
+ #
7
+ # gem "activerecord-import", require: "activerecord-import/without_aliases"
8
+ #
9
+ # This takes precedence over "activerecord-import" and "activerecord-import/base",
10
+ # regardless of the order they are required in.
11
+
12
+ require "active_support/lazy_load_hooks"
13
+
14
+ ActiveSupport.on_load(:active_record) do
15
+ require "activerecord-import/core"
16
+ ActiveRecord::Import.skip_aliases!
17
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-import
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.0
4
+ version: 3.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zach Dennis
@@ -15,14 +15,14 @@ dependencies:
15
15
  requirements:
16
16
  - - ">="
17
17
  - !ruby/object:Gem::Version
18
- version: '4.2'
18
+ version: '6.1'
19
19
  type: :runtime
20
20
  prerelease: false
21
21
  version_requirements: !ruby/object:Gem::Requirement
22
22
  requirements:
23
23
  - - ">="
24
24
  - !ruby/object:Gem::Version
25
- version: '4.2'
25
+ version: '6.1'
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: rake
28
28
  requirement: !ruby/object:Gem::Requirement
@@ -73,11 +73,14 @@ files:
73
73
  - lib/activerecord-import/adapters/sqlite3_proxy_adapter.rb
74
74
  - lib/activerecord-import/adapters/trilogy_adapter.rb
75
75
  - lib/activerecord-import/adapters/trilogy_proxy_adapter.rb
76
+ - lib/activerecord-import/aliases.rb
76
77
  - lib/activerecord-import/base.rb
78
+ - lib/activerecord-import/core.rb
77
79
  - lib/activerecord-import/import.rb
78
80
  - lib/activerecord-import/synchronize.rb
79
81
  - lib/activerecord-import/value_sets_parser.rb
80
82
  - lib/activerecord-import/version.rb
83
+ - lib/activerecord-import/without_aliases.rb
81
84
  homepage: https://github.com/zdennis/activerecord-import
82
85
  licenses:
83
86
  - MIT
@@ -90,14 +93,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
90
93
  requirements:
91
94
  - - ">="
92
95
  - !ruby/object:Gem::Version
93
- version: 2.4.0
96
+ version: 3.1.0
94
97
  required_rubygems_version: !ruby/object:Gem::Requirement
95
98
  requirements:
96
99
  - - ">="
97
100
  - !ruby/object:Gem::Version
98
101
  version: '0'
99
102
  requirements: []
100
- rubygems_version: 4.0.0
103
+ rubygems_version: 3.6.9
101
104
  specification_version: 4
102
105
  summary: Bulk insert extension for ActiveRecord
103
106
  test_files: []