delta_attributes 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/README.md +16 -4
- data/delta_attributes.gemspec +5 -3
- data/lib/delta_attributes/mysql.rb +1 -1
- data/lib/delta_attributes/to_sql.rb +24 -2
- data/lib/delta_attributes/version.rb +1 -1
- metadata +55 -39
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
This gem makes updating specified number fields by ActiveRecord in unusual way.
|
4
4
|
|
5
|
-
Instead
|
5
|
+
Instead of generating sql script to update value in usual way like this:
|
6
6
|
|
7
7
|
UPDATE users
|
8
8
|
SET money = 10
|
@@ -38,11 +38,23 @@ Or install it yourself as:
|
|
38
38
|
To mark numeric field to be updated by "field = field + d" way you just need to add field
|
39
39
|
name to delta_attributes like this:
|
40
40
|
|
41
|
-
|
41
|
+
class User < ActiveRecord::Base
|
42
42
|
|
43
|
-
|
43
|
+
delta_attributes :money
|
44
44
|
|
45
|
-
|
45
|
+
end
|
46
|
+
|
47
|
+
Now you can:
|
48
|
+
|
49
|
+
u = User.first # money = 3, id = 1
|
50
|
+
u.money = 5
|
51
|
+
u.save
|
52
|
+
|
53
|
+
will generate
|
54
|
+
|
55
|
+
UPDATE users SET money = money + 2 where id = 1
|
56
|
+
|
57
|
+
Tested with rails 3.2.8.
|
46
58
|
|
47
59
|
## Contributing
|
48
60
|
|
data/delta_attributes.gemspec
CHANGED
@@ -6,12 +6,12 @@ require 'delta_attributes/version'
|
|
6
6
|
Gem::Specification.new do |gem|
|
7
7
|
gem.name = "delta_attributes"
|
8
8
|
gem.version = DeltaAttributes::VERSION
|
9
|
-
gem.authors = ["
|
9
|
+
gem.authors = ["Oleh Novosad, Yuriy Lavryk, Arya"]
|
10
10
|
gem.email = ["oleh.novosad@gmail.com"]
|
11
11
|
gem.description = %q{
|
12
12
|
This gem makes updating specified number fields by ActiveRecord in unusual way.
|
13
13
|
|
14
|
-
Instead
|
14
|
+
Instead of generating sql script to update value in usual way like this:
|
15
15
|
|
16
16
|
UPDATE users
|
17
17
|
SET money = 10
|
@@ -25,7 +25,9 @@ Gem::Specification.new do |gem|
|
|
25
25
|
|
26
26
|
where d is difference between old value and new value of that field.
|
27
27
|
|
28
|
-
This solves problem with simultaneous updating of same field.
|
28
|
+
This solves problem with simultaneous updating of the same field by different threads.
|
29
|
+
|
30
|
+
Source code: https://github.com/izbor/delta_attributes
|
29
31
|
}
|
30
32
|
gem.summary = %q{ delta attributes }
|
31
33
|
gem.homepage = ""
|
@@ -7,7 +7,7 @@ module Arel
|
|
7
7
|
def visit_Arel_Nodes_UpdateStatement o
|
8
8
|
[
|
9
9
|
"UPDATE #{visit o.relation}",
|
10
|
-
("SET #{o.values_changed.map { |value| visit value }.join ', '}" unless o.
|
10
|
+
("SET #{o.values_changed.map { |value| visit value }.join ', '}" unless o.values_changed.empty?),
|
11
11
|
("WHERE #{o.wheres.map { |x| visit x }.join ' AND '}" unless o.wheres.empty?),
|
12
12
|
("ORDER BY #{o.orders.map { |x| visit x }.join(', ')}" unless o.orders.empty?),
|
13
13
|
(visit(o.limit) if o.limit),
|
@@ -16,10 +16,32 @@ module Arel
|
|
16
16
|
"#{visit o.arel_node.left} = #{delta_string}"
|
17
17
|
end
|
18
18
|
|
19
|
-
#alias_method_chain :visit_Arel_Nodes_Assignment, :deltas
|
20
|
-
|
21
19
|
alias_method :visit_Arel_Nodes_Assignment_without_deltas, :visit_Arel_Nodes_Assignment
|
22
20
|
alias_method :visit_Arel_Nodes_Assignment, :visit_Arel_Nodes_Assignment_with_deltas
|
21
|
+
|
22
|
+
def visit_Arel_Nodes_UpdateStatement o
|
23
|
+
if o.orders.empty? && o.limit.nil?
|
24
|
+
wheres = o.wheres
|
25
|
+
else
|
26
|
+
key = o.key
|
27
|
+
unless key
|
28
|
+
warn(<<-eowarn) if $VERBOSE
|
29
|
+
(#{caller.first}) Using UpdateManager without setting UpdateManager#key is
|
30
|
+
deprecated and support will be removed in ARel 4.0.0. Please set the primary
|
31
|
+
key on UpdateManager using UpdateManager#key=
|
32
|
+
eowarn
|
33
|
+
key = o.relation.primary_key
|
34
|
+
end
|
35
|
+
|
36
|
+
wheres = [Nodes::In.new(key, [build_subselect(key, o)])]
|
37
|
+
end
|
38
|
+
|
39
|
+
[
|
40
|
+
"UPDATE #{visit o.relation}",
|
41
|
+
("SET #{o.values_changed.map { |value| visit value }.join ', '}" unless o.values_changed.empty?),
|
42
|
+
("WHERE #{wheres.map { |x| visit x }.join ' AND '}" unless wheres.empty?),
|
43
|
+
].compact.join ' '
|
44
|
+
end
|
23
45
|
end
|
24
46
|
end
|
25
47
|
end
|
metadata
CHANGED
@@ -1,44 +1,49 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: delta_attributes
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
6
11
|
platform: ruby
|
7
|
-
authors:
|
8
|
-
-
|
12
|
+
authors:
|
13
|
+
- Oleh Novosad, Yuriy Lavryk, Arya
|
9
14
|
autorequire:
|
10
15
|
bindir: bin
|
11
16
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
17
|
+
|
18
|
+
date: 2012-11-20 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
15
22
|
name: rails
|
16
|
-
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
|
-
requirements:
|
19
|
-
- - ~>
|
20
|
-
- !ruby/object:Gem::Version
|
21
|
-
version: 3.2.8
|
22
|
-
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
25
|
none: false
|
26
|
-
requirements:
|
26
|
+
requirements:
|
27
27
|
- - ~>
|
28
|
-
- !ruby/object:Gem::Version
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 31
|
30
|
+
segments:
|
31
|
+
- 3
|
32
|
+
- 2
|
33
|
+
- 8
|
29
34
|
version: 3.2.8
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
id = 1;\n\n where d is difference between old value and new value of that field.\n\n
|
35
|
-
\ This solves problem with simultaneous updating of same field.\n "
|
36
|
-
email:
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
37
|
+
description: "\n This gem makes updating specified number fields by ActiveRecord in unusual way.\n\n Instead of generating sql script to update value in usual way like this:\n\n UPDATE users\n SET money = 10\n WHERE id = 1;\n\n It replaces it with\n\n UPDATE users\n SET money = money + d\n WHERE id = 1;\n\n where d is difference between old value and new value of that field.\n\n This solves problem with simultaneous updating of the same field by different threads.\n\n Source code: https://github.com/izbor/delta_attributes\n "
|
38
|
+
email:
|
37
39
|
- oleh.novosad@gmail.com
|
38
40
|
executables: []
|
41
|
+
|
39
42
|
extensions: []
|
43
|
+
|
40
44
|
extra_rdoc_files: []
|
41
|
-
|
45
|
+
|
46
|
+
files:
|
42
47
|
- .gitignore
|
43
48
|
- Gemfile
|
44
49
|
- LICENSE.txt
|
@@ -55,28 +60,39 @@ files:
|
|
55
60
|
- lib/delta_attributes/update_statement.rb
|
56
61
|
- lib/delta_attributes/version.rb
|
57
62
|
- lib/delta_attributes/visitor.rb
|
58
|
-
|
63
|
+
has_rdoc: true
|
64
|
+
homepage: ""
|
59
65
|
licenses: []
|
66
|
+
|
60
67
|
post_install_message:
|
61
68
|
rdoc_options: []
|
62
|
-
|
69
|
+
|
70
|
+
require_paths:
|
63
71
|
- lib
|
64
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
73
|
none: false
|
66
|
-
requirements:
|
67
|
-
- -
|
68
|
-
- !ruby/object:Gem::Version
|
69
|
-
|
70
|
-
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
hash: 3
|
78
|
+
segments:
|
79
|
+
- 0
|
80
|
+
version: "0"
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
82
|
none: false
|
72
|
-
requirements:
|
73
|
-
- -
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
hash: 3
|
87
|
+
segments:
|
88
|
+
- 0
|
89
|
+
version: "0"
|
76
90
|
requirements: []
|
91
|
+
|
77
92
|
rubyforge_project:
|
78
|
-
rubygems_version: 1.
|
93
|
+
rubygems_version: 1.5.3
|
79
94
|
signing_key:
|
80
95
|
specification_version: 3
|
81
96
|
summary: delta attributes
|
82
97
|
test_files: []
|
98
|
+
|