activeupdate 0.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.
- checksums.yaml +7 -0
- data/lib/activeupdate.rb +50 -0
- metadata +45 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 63e7daca1ce3622d873870bb74cafd763f958707
|
4
|
+
data.tar.gz: fa3614f33e14b23d8ac6c5dd3a54432569dc92d7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8acb001f7d7d07d94ad38d6ced4ddb8b7726633fc04be4a12164f697e78e231d9b5fe2b92f456a2e7ec8efc46b5bf1501f5f8dce4ac78601ca7b2f7c3dd78449
|
7
|
+
data.tar.gz: 239f0e5e45c65c891caba80d7e8ec195d08cfc19933851d80fe5fafb045be4de0c961f588fd0eaedae6a165268b61dbefc6f9bf9ff3c020e5b9a2f26e7f01479
|
data/lib/activeupdate.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
module ActiveRecordExtension
|
2
|
+
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
# add your static(class) methods here
|
6
|
+
module ClassMethods
|
7
|
+
|
8
|
+
def update!(resources_hash = nil)
|
9
|
+
return self unless resources_hash
|
10
|
+
|
11
|
+
update_manager = Arel::UpdateManager.new
|
12
|
+
resources = self.arel_table
|
13
|
+
|
14
|
+
attribute_values = []
|
15
|
+
|
16
|
+
first_resources_hash = resources_hash.delete('0')
|
17
|
+
first_resources_id = first_resources_hash.delete('id').to_i
|
18
|
+
|
19
|
+
resource_ids = []
|
20
|
+
|
21
|
+
resource_ids << first_resources_id
|
22
|
+
resources_id = resources[:id]
|
23
|
+
|
24
|
+
first_resources_hash.each do |attribute, value|
|
25
|
+
attribute_values << [resources[attribute.to_sym], Arel::Nodes::Case.new(resources_id).when(first_resources_id).then(value)]
|
26
|
+
end
|
27
|
+
|
28
|
+
resources_hash.each do |_, resource_attributes|
|
29
|
+
resource_id = resource_attributes.delete('id').to_i
|
30
|
+
resource_ids << resource_id
|
31
|
+
|
32
|
+
resource_attributes.each_with_index do |(_, value), index|
|
33
|
+
attribute_values[index][1].when(resource_id).then(value)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
attribute_values.map! do |attribute, values|
|
38
|
+
[attribute, Arel::Nodes::SqlLiteral.new(values.to_sql)]
|
39
|
+
end
|
40
|
+
|
41
|
+
update_manager.table(resources).where(resources_id.in(resource_ids))
|
42
|
+
ActiveRecord::Base.connection.execute(update_manager.set(attribute_values).to_sql)
|
43
|
+
|
44
|
+
self
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# include the extension
|
50
|
+
ActiveRecord::Base.send(:include, ActiveRecordExtension)
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activeupdate
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrew H. Carpenter
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-04-12 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email: drewwcarpenter@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/activeupdate.rb
|
20
|
+
homepage:
|
21
|
+
licenses:
|
22
|
+
- MIT
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubyforge_project:
|
40
|
+
rubygems_version: 2.5.1
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: activerecord extension to facilitate updating multiple records with single
|
44
|
+
query
|
45
|
+
test_files: []
|