activeupdate 0.0.4 → 0.1.4
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 +4 -4
- data/lib/activeupdate.rb +17 -25
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e2ab8ba417c9ad86b9cf1e83c6dcc1c998e95e30
|
4
|
+
data.tar.gz: da8e0c9e862a75cb7e73eb4b5b4d72560da9d1b2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7cb85304fa97be3baa494cbceb4977dd2052d17197181cc0518292c0070d4feb86350f3806b5988b6151a778d398f782c7db5d658adee3618a59298b2ba0c237
|
7
|
+
data.tar.gz: a3c353efafa7755997efbfbda96e2a64a27b93a3c5edfccf1d2706b7869508bce9234ecb945ae8a3cb05045b181938fbc69a64be2e651350b56400478f7eab23
|
data/lib/activeupdate.rb
CHANGED
@@ -5,43 +5,35 @@ module ActiveRecordExtension
|
|
5
5
|
|
6
6
|
# Updates an object (or multiple objects) and saves it to the database. The resulting object is returned whether the object was saved successfully to the database or not.
|
7
7
|
#
|
8
|
-
# @param
|
8
|
+
# @param ids [Array], an array of ids
|
9
|
+
# @param attributes [Array], an array of attribute hashes
|
9
10
|
# @return [Object] the object.
|
10
|
-
def update!(
|
11
|
-
return self unless resources_hash
|
12
|
-
|
11
|
+
def update!(ids, attributes)
|
13
12
|
update_manager = Arel::UpdateManager.new
|
14
|
-
resources = self.arel_table
|
15
13
|
|
16
|
-
|
17
|
-
|
18
|
-
first_resources_hash = resources_hash.delete('0')
|
19
|
-
first_resources_id = first_resources_hash.delete('id').to_i
|
14
|
+
resources = self.arel_table
|
20
15
|
|
21
|
-
|
16
|
+
attribute_hash = {}
|
22
17
|
|
23
|
-
resource_ids << first_resources_id
|
24
18
|
resources_id = resources[:id]
|
25
19
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
resource_attributes.each_with_index do |(_, value), index|
|
35
|
-
attribute_values[index][1].when(resource_id).then(value)
|
20
|
+
attributes.each_with_index do |attribute, index|
|
21
|
+
attribute.each do |key, value|
|
22
|
+
if attribute_hash[key]
|
23
|
+
attribute_hash[key].when(ids[index]).then(value)
|
24
|
+
else
|
25
|
+
attribute_hash[key] = Arel::Nodes::Case.new(resources_id).when(ids[index]).then(value)
|
26
|
+
end
|
36
27
|
end
|
37
28
|
end
|
38
29
|
|
39
|
-
|
40
|
-
[attribute
|
30
|
+
attribute_array = attribute_hash.map do |attribute, values|
|
31
|
+
attribute = resources[attribute.to_sym]
|
32
|
+
[attribute, Arel::Nodes::SqlLiteral.new(values.else(attribute).to_sql)]
|
41
33
|
end
|
42
34
|
|
43
|
-
update_manager.table(resources).where(resources_id.in(
|
44
|
-
ActiveRecord::Base.connection.execute(update_manager.set(
|
35
|
+
update_manager.table(resources).where(resources_id.in(ids))
|
36
|
+
ActiveRecord::Base.connection.execute(update_manager.set(attribute_array).to_sql)
|
45
37
|
|
46
38
|
self
|
47
39
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activeupdate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew H. Carpenter
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-04-
|
11
|
+
date: 2016-04-17 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email: drewwcarpenter@gmail.com
|
@@ -41,5 +41,5 @@ rubygems_version: 2.5.1
|
|
41
41
|
signing_key:
|
42
42
|
specification_version: 4
|
43
43
|
summary: activerecord extension to facilitate updating multiple records with single
|
44
|
-
|
44
|
+
operation
|
45
45
|
test_files: []
|