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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/activeupdate.rb +17 -25
  3. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 198d0eadfaae9ee593e8a53ed56a285357f1e8a0
4
- data.tar.gz: 8c72576fabd6abd5748b09b6b8ba0c20a7e187e1
3
+ metadata.gz: e2ab8ba417c9ad86b9cf1e83c6dcc1c998e95e30
4
+ data.tar.gz: da8e0c9e862a75cb7e73eb4b5b4d72560da9d1b2
5
5
  SHA512:
6
- metadata.gz: ea960d0015cd4045e6acda6cdb2804c21fdaa6ffc47686d250ada3c8ab2447041709383eeb4443e3a270056c5847b7c829c4d038bf50a01c6dad44cfe0cdf844
7
- data.tar.gz: 93bf67c5c3c77b551a4b2e346c989ec4f71c29cafe84bd83e7a127d43e3de76858db18f12557a2f93f7826086aa13468e3d5a4a9029db5cf70c3566c19bb7de9
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 resources_hash [Hash] a hash of resources, { '0' => { 'id' => 1, 'attribute' => 'test' }, '1' => { 'id' => 2, 'attribute' => 'cheese' } }
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!(resources_hash = nil)
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
- attribute_values = []
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
- resource_ids = []
16
+ attribute_hash = {}
22
17
 
23
- resource_ids << first_resources_id
24
18
  resources_id = resources[:id]
25
19
 
26
- first_resources_hash.each do |attribute, value|
27
- attribute_values << [resources[attribute.to_sym], Arel::Nodes::Case.new(resources_id).when(first_resources_id).then(value)]
28
- end
29
-
30
- resources_hash.each do |_, resource_attributes|
31
- resource_id = resource_attributes.delete('id').to_i
32
- resource_ids << resource_id
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
- attribute_values.map! do |attribute, values|
40
- [attribute, Arel::Nodes::SqlLiteral.new(values.to_sql)]
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(resource_ids))
44
- ActiveRecord::Base.connection.execute(update_manager.set(attribute_values).to_sql)
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.0.4
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-13 00:00:00.000000000 Z
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
- query
44
+ operation
45
45
  test_files: []