active_record_upsert 0.9.1 → 0.9.2
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 98aadc2aa0c0144f2e76c0ccfffa7102a93e3ef1ea1b073df4bc89f1f162f9cb
|
4
|
+
data.tar.gz: 9066f7f3cc3b0eedc43778de03ab2d9599d645992f4466f8aa987dfff3eaeb06
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eec007a7ae73f4ccb52fda438a9c73182b2f87f8154a5bdfebd1ba49f7a4ea67289e224eafa9bfa1d571a028f37ddc12d8b795c1ef75772a8bde2c70369e11b7
|
7
|
+
data.tar.gz: e6d6f827e67c2c77c22eba302a9be7d3bf4fc435320e0725aa501c618f5ead4d592640f958bf316a866861549adc2bd293daeee816e448d954cc8a66c28fb9f6
|
@@ -5,7 +5,7 @@ module ActiveRecordUpsert
|
|
5
5
|
raise ::ActiveRecord::ReadOnlyRecord, "#{self.class} is marked as readonly" if readonly?
|
6
6
|
raise ::ActiveRecord::RecordSavedError, "Can't upsert a record that has already been saved" if persisted?
|
7
7
|
validate == false || perform_validations || raise_validation_error
|
8
|
-
|
8
|
+
run_callbacks(:save) {
|
9
9
|
run_callbacks(:create) {
|
10
10
|
attributes ||= changed
|
11
11
|
attributes = attributes +
|
@@ -15,12 +15,6 @@ module ActiveRecordUpsert
|
|
15
15
|
}
|
16
16
|
}
|
17
17
|
|
18
|
-
# When a migration adds a column to a table, the upsert will start
|
19
|
-
# returning the new attribute, and assign_attributes will fail,
|
20
|
-
# because Rails doesn't know about it yet (until the app is restarted).
|
21
|
-
#
|
22
|
-
# This checks that only known attributes are being assigned.
|
23
|
-
assign_attributes(values.first.to_h.slice(*self.attributes.keys))
|
24
18
|
self
|
25
19
|
end
|
26
20
|
|
@@ -33,10 +27,17 @@ module ActiveRecordUpsert
|
|
33
27
|
def _upsert_record(upsert_attribute_names = changed, arel_condition = nil)
|
34
28
|
existing_attributes = attributes_with_values_for_create(self.attributes.keys)
|
35
29
|
values = self.class._upsert_record(existing_attributes, upsert_attribute_names, [arel_condition].compact)
|
30
|
+
@attributes = self.class.attributes_builder.build_from_database(values.first.to_h)
|
36
31
|
@new_record = false
|
37
32
|
values
|
38
33
|
end
|
39
34
|
|
35
|
+
def upsert_operation
|
36
|
+
created_record = self['_upsert_created_record']
|
37
|
+
return if created_record.nil?
|
38
|
+
created_record ? :create : :update
|
39
|
+
end
|
40
|
+
|
40
41
|
module ClassMethods
|
41
42
|
def upsert!(attributes, arel_condition: nil, validate: true, &block)
|
42
43
|
if attributes.is_a?(Array)
|
@@ -57,6 +58,15 @@ module ActiveRecordUpsert
|
|
57
58
|
def _upsert_record(existing_attributes, upsert_attributes_names, wheres) # :nodoc:
|
58
59
|
upsert_keys = self.upsert_keys || [primary_key]
|
59
60
|
upsert_attributes_names = upsert_attributes_names - [*upsert_keys, 'created_at']
|
61
|
+
|
62
|
+
existing_attributes = existing_attributes
|
63
|
+
.transform_keys { |name| _prepare_column(name) }
|
64
|
+
.reject { |key, _| key.nil? }
|
65
|
+
|
66
|
+
upsert_attributes_names = upsert_attributes_names
|
67
|
+
.map { |name| _prepare_column(name) }
|
68
|
+
.compact
|
69
|
+
|
60
70
|
values_for_upsert = existing_attributes.select { |(name, _value)| upsert_attributes_names.include?(name) }
|
61
71
|
|
62
72
|
insert_manager = arel_table.compile_upsert(
|
@@ -70,6 +80,16 @@ module ActiveRecordUpsert
|
|
70
80
|
connection.upsert(insert_manager, "#{self} Upsert")
|
71
81
|
end
|
72
82
|
|
83
|
+
def _prepare_column(column)
|
84
|
+
column = attribute_alias(column) if attribute_alias?(column)
|
85
|
+
|
86
|
+
if columns_hash.key?(column)
|
87
|
+
column
|
88
|
+
elsif reflections.key?(column)
|
89
|
+
reflections[column].foreign_key
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
73
93
|
def upsert_keys(*keys)
|
74
94
|
return @_upsert_keys if keys.empty?
|
75
95
|
options = keys.extract_options!
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_record_upsert
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jesper Josefsson
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2018-
|
12
|
+
date: 2018-08-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|