active_record_patch_first_or_create 0.0.3 → 0.0.4
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
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c4c3527b478c90a4703461e25825ec2c791915ee
|
4
|
+
data.tar.gz: 18c3d931f8d3488462230f4b362e20dcfda45f06
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cd7c194effb1086fae26b403052a3aa68687204cd7307c82b5b908fda6ecd945ff9159e6b4a8d7185feb7d78b98ca14f038d41d8481b18f2e0f055d585c8a08f
|
7
|
+
data.tar.gz: d2082aa45475d5b30a535dac09aae845b6a9c65f6696cbe369be4de0e57d7fea8a3f1e4338ac1e1c9ecb81efce03902f86cf0bc8aa59a31aa61c3e909e747daa
|
@@ -4,6 +4,7 @@ module ActiveRecordPatchFirstOrCreate
|
|
4
4
|
def self.first_or_create relation, attributes
|
5
5
|
attributes = attributes.merge(relation.where_values_hash)
|
6
6
|
attributes = attributes.merge(rails_timestamps(relation))
|
7
|
+
attributes = serialize_columns(relation, attributes)
|
7
8
|
|
8
9
|
sql = "INSERT INTO #{relation.table_name} (#{attributes.keys.join(",")})
|
9
10
|
SELECT #{(['?'] * attributes.values.count).join(', ')}
|
@@ -18,6 +19,14 @@ module ActiveRecordPatchFirstOrCreate
|
|
18
19
|
time = Time.now
|
19
20
|
{ created_at: time, updated_at: time }.keep_if { |key| relation.klass.column_names.include?(key.to_s) }
|
20
21
|
end
|
22
|
+
|
23
|
+
def self.serialize_columns relation, attributes
|
24
|
+
Hash[attributes.map do |key, value|
|
25
|
+
serializer = relation.klass.serialized_attributes[key.to_s]
|
26
|
+
value = serializer.dump(value) unless serializer.nil?
|
27
|
+
[key, value]
|
28
|
+
end]
|
29
|
+
end
|
21
30
|
end
|
22
31
|
|
23
32
|
module ActiveRecord
|