composite_primary_keys 9.0.1 → 9.0.2

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: f4248480dc399e7b5855cdcb24c52aa0d316cb84
4
- data.tar.gz: 8cf2391613840634669fbfac2d0df07fcc4b89d0
3
+ metadata.gz: f7253caf1292f03bdcac265e1bdad518994aa19a
4
+ data.tar.gz: 16b2e5c1fd9f528c04b69f91e5fb613ab22ce9aa
5
5
  SHA512:
6
- metadata.gz: 55a92a161646aedc85be40fc0832651050962e5c53e941f3d97f0a89ace2fbc9363b1bec30f7115dca9d91409b95389fd1dcabe29cbfbd2768113c60e116d0c4
7
- data.tar.gz: cd1df5d88752ae0ea029e4b24239cc1038f8a012951c5859a5c3a23dd8183648b6880cebffcd148424a7789e53509af39ecb9e760b3eaa959f127dd35748392f
6
+ metadata.gz: fcc63c86117a04c279a555237ab7dc1753f78debb5ef6ad26ce797d6f90d100a6283073e1c7204ab9d1c657275dd40482003644573979d9913397f95d9b803a4
7
+ data.tar.gz: 4df6e19d983774d48afd8ba36d9b6774929569e9a34f0c91dbca9ffa5d0013fbbbfaf0a392e12d362d9b473605a3797f589c39fb46f6e0d61f1d0463837b3768
data/History.rdoc CHANGED
@@ -1,3 +1,6 @@
1
+ == 9.0.2 (2016-08-10)
2
+ * Fix delete_all when used with a join (Charlie Savage)
3
+
1
4
  == 9.0.1 (2016-07-27)
2
5
  * Create OR predicates in a nicely balanced tree fixing #320 (Nathan Samson)
3
6
 
@@ -113,3 +113,6 @@ require 'composite_primary_keys/relation/query_methods'
113
113
  require 'composite_primary_keys/validations/uniqueness'
114
114
 
115
115
  require 'composite_primary_keys/composite_relation'
116
+
117
+ require 'composite_primary_keys/arel/in'
118
+ require 'composite_primary_keys/arel/to_sql'
@@ -0,0 +1,6 @@
1
+ module CompositePrimaryKeys
2
+ module Nodes
3
+ class In < ::Arel::Nodes::Equality
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,26 @@
1
+ module Arel
2
+ module Visitors
3
+ class ToSql
4
+ def visit_CompositePrimaryKeys_Nodes_In o, collector
5
+ collector << "("
6
+ visit(o.left, collector)
7
+ collector << ")"
8
+ collector << " IN ("
9
+ visit(o.right, collector) << ")"
10
+ end
11
+
12
+ def visit_CompositePrimaryKeys_CompositeKeys o, collector
13
+ values = o.map do |key|
14
+ case key
15
+ when Arel::Attributes::Attribute
16
+ "#{key.relation.name}.#{key.name}"
17
+ else
18
+ key
19
+ end
20
+ end
21
+ collector << "#{values.join(', ')}"
22
+ collector
23
+ end
24
+ end
25
+ end
26
+ end
@@ -33,6 +33,14 @@ module CompositePrimaryKeys
33
33
  end
34
34
  end
35
35
 
36
+ def in(other)
37
+ case other
38
+ when Arel::SelectManager
39
+ CompositePrimaryKeys::Nodes::In.new(self, other.ast)
40
+ end
41
+ end
42
+
43
+
36
44
  def to_s
37
45
  # Doing this makes it easier to parse Base#[](attr_name)
38
46
  join(ID_SEP)
@@ -46,5 +46,48 @@ module ActiveRecord
46
46
  )
47
47
  end
48
48
  end
49
+
50
+ def delete_all(conditions = nil)
51
+ invalid_methods = INVALID_METHODS_FOR_DELETE_ALL.select { |method|
52
+ if MULTI_VALUE_METHODS.include?(method)
53
+ send("#{method}_values").any?
54
+ elsif SINGLE_VALUE_METHODS.include?(method)
55
+ send("#{method}_value")
56
+ elsif CLAUSE_METHODS.include?(method)
57
+ send("#{method}_clause").any?
58
+ end
59
+ }
60
+ if invalid_methods.any?
61
+ raise ActiveRecordError.new("delete_all doesn't support #{invalid_methods.join(', ')}")
62
+ end
63
+
64
+ if conditions
65
+ ActiveSupport::Deprecation.warn(<<-MESSAGE.squish)
66
+ Passing conditions to delete_all is deprecated and will be removed in Rails 5.1.
67
+ To achieve the same use where(conditions).delete_all.
68
+ MESSAGE
69
+ where(conditions).delete_all
70
+ else
71
+ stmt = Arel::DeleteManager.new
72
+ stmt.from(table)
73
+
74
+ # CPK
75
+ if joins_values.any? && @klass.composite?
76
+ arel_attributes = Array(primary_key).map do |key|
77
+ arel_attribute(key)
78
+ end.to_composite_keys
79
+ @klass.connection.join_to_delete(stmt, arel, arel_attributes)
80
+ elsif joins_values.any?
81
+ @klass.connection.join_to_delete(stmt, arel, arel_attribute(primary_key))
82
+ else
83
+ stmt.wheres = arel.constraints
84
+ end
85
+
86
+ affected = @klass.connection.delete(stmt, 'SQL', bound_attributes)
87
+
88
+ reset
89
+ affected
90
+ end
91
+ end
49
92
  end
50
93
  end
@@ -2,7 +2,7 @@ module CompositePrimaryKeys
2
2
  module VERSION
3
3
  MAJOR = 9
4
4
  MINOR = 0
5
- TINY = 1
5
+ TINY = 2
6
6
  STRING = [MAJOR, MINOR, TINY].join('.')
7
7
  end
8
8
  end
data/test/test_delete.rb CHANGED
@@ -19,36 +19,49 @@ class TestDelete < ActiveSupport::TestCase
19
19
  self.class.classes = CLASSES
20
20
  end
21
21
 
22
- # def test_destroy_one
23
- # testing_with do
24
- # assert @first.destroy
25
- # end
26
- # end
27
- #
28
- # def test_destroy_one_alone_via_class
29
- # testing_with do
30
- # assert @klass.destroy(@first.id)
31
- # end
32
- # end
33
- #
34
- # def test_delete_one_alone
35
- # testing_with do
36
- # assert @klass.delete(@first.id)
37
- # end
38
- # end
39
- #
40
- # def test_delete_many
41
- # testing_with do
42
- # to_delete = @klass.limit(2)
43
- # assert_equal 2, to_delete.length
44
- # end
45
- # end
46
- #
47
- # def test_delete_all
48
- # testing_with do
49
- # @klass.delete_all
50
- # end
51
- # end
22
+ def test_destroy_one
23
+ testing_with do
24
+ assert @first.destroy
25
+ end
26
+ end
27
+
28
+ def test_destroy_one_alone_via_class
29
+ testing_with do
30
+ assert @klass.destroy(@first.id)
31
+ end
32
+ end
33
+
34
+ def test_delete_one_alone
35
+ testing_with do
36
+ assert @klass.delete(@first.id)
37
+ end
38
+ end
39
+
40
+ def test_delete_many
41
+ testing_with do
42
+ to_delete = @klass.limit(2)
43
+ assert_equal 2, to_delete.length
44
+ end
45
+ end
46
+
47
+ def test_delete_all
48
+ testing_with do
49
+ @klass.delete_all
50
+ end
51
+ end
52
+
53
+ def test_delete_all_with_join
54
+ department = departments(:accounting)
55
+
56
+ assert_equal(4, Department.count)
57
+
58
+ Department.joins(:employees).
59
+ where('departments.department_id = ?', department.department_id).
60
+ where('departments.location_id = ?', department.location_id).
61
+ delete_all
62
+
63
+ assert_equal(3, Department.count)
64
+ end
52
65
 
53
66
  def test_clear_association
54
67
  department = Department.find([1,1])
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: composite_primary_keys
3
3
  version: !ruby/object:Gem::Version
4
- version: 9.0.1
4
+ version: 9.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Charlie Savage
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-28 00:00:00.000000000 Z
11
+ date: 2016-08-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -91,6 +91,8 @@ files:
91
91
  - README_DB2.rdoc
92
92
  - Rakefile
93
93
  - lib/composite_primary_keys.rb
94
+ - lib/composite_primary_keys/arel/in.rb
95
+ - lib/composite_primary_keys/arel/to_sql.rb
94
96
  - lib/composite_primary_keys/associations/association.rb
95
97
  - lib/composite_primary_keys/associations/association_scope.rb
96
98
  - lib/composite_primary_keys/associations/collection_association.rb