composite_primary_keys 13.0.0 → 13.0.1

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
  SHA256:
3
- metadata.gz: fa8acb9d77d6a2de529283cbaa0cbea67daec08813942c8bfa83f39b0bdf299f
4
- data.tar.gz: deb46f06053d8a07b19903a46b3d30e3ae82699d265f339c5463e810900416b6
3
+ metadata.gz: d9e6df76c33e6eae43b2be918772faba1bfaceaeb2849deedce615f94d6c84bd
4
+ data.tar.gz: cd28e3d91fea6190f64fe43a52eaea23fdd25ff601cfdf9fd4268cb361d98e1c
5
5
  SHA512:
6
- metadata.gz: d31a495089eafc1cced865e0cd55e6e7a09698b124f30c7d18a754aa396c611f85abcfcf32af55e5c23534432d0f74c7eee5f1d674574c0a0b0c937d61fb16fd
7
- data.tar.gz: ab7776729d53de9d12c08bb0035c256ef1d4cc00dab2b7be029a4c610c6cd36b6e1397160ebde56d7acaecc29817b1456e83b482a6a7f33c0a5ac15c6ba715e2
6
+ metadata.gz: '093c59b0bdbd0281194cd28bc70101b26c3402bb13ef7fa3fe1e60f865f2423dbe90f39a669a833173a19cc129c5457476d5233f6cdaf0a7a1859cb1d3b7e36c'
7
+ data.tar.gz: 9e08a5afafe648410b7a2eecf3190959300a27baf57e93da0988853862a851ef28e5a655032f3b1236b55d39c435f3b71721abad0740cdf136bdd0d7c66439b1
data/History.rdoc CHANGED
@@ -1,3 +1,8 @@
1
+ == 13.0.1
2
+ * Fix invalid sql generation for some cases of scoped associations (Ryan Mulligan)
3
+ * Fix unintentional connection to database (Kazuhiro Masuda)
4
+ * Zip values then keys - fixes #548 (Charlie Savage)
5
+
1
6
  == 13.0.0
2
7
  * Update to ActiveRecord 6.1 (Javier Julio, Charlie Savage, Sammy Larbi)
3
8
 
@@ -1,6 +1,25 @@
1
1
  module ActiveRecord
2
2
  module Associations
3
3
  class JoinDependency
4
+
5
+ class JoinAssociation < JoinPart # :nodoc:
6
+ private
7
+ def append_constraints(join, constraints)
8
+ if join.is_a?(Arel::Nodes::StringJoin)
9
+ join_string = Arel::Nodes::And.new(constraints.unshift join.left)
10
+ join.left = Arel.sql(base_klass.connection.visitor.compile(join_string))
11
+ else
12
+ right = join.right
13
+ # CPK
14
+ if right.expr.children.empty?
15
+ right.expr = Arel::Nodes::And.new(constraints)
16
+ else
17
+ right.expr = Arel::Nodes::And.new(constraints.unshift right.expr)
18
+ end
19
+ end
20
+ end
21
+ end
22
+
4
23
  class Aliases # :nodoc:
5
24
  def column_alias(node, column)
6
25
  # CPK
@@ -32,7 +32,8 @@ module CompositePrimaryKeys
32
32
  end
33
33
 
34
34
  def cpk_id_predicate(table, keys, values)
35
- eq_predicates = keys.zip(values).map do |key, value|
35
+ # We zip on values then keys in case values are not provided for each key field
36
+ eq_predicates = values.zip(keys).map do |value, key|
36
37
  table[key].eq(value)
37
38
  end
38
39
  cpk_and_predicate(eq_predicates)
@@ -29,7 +29,7 @@ module ActiveRecord
29
29
  stmt.key = table[primary_key]
30
30
 
31
31
  # CPK
32
- if @klass.composite? && stmt.to_sql =~ /['"]#{primary_key.to_s}['"]/
32
+ if @klass.composite? && @klass.connection.visitor.compile(stmt.ast) =~ /['"]#{primary_key.to_s}['"]/
33
33
  stmt = Arel::UpdateManager.new
34
34
  stmt.table(arel_table)
35
35
  cpk_subquery(stmt)
@@ -74,7 +74,7 @@ module ActiveRecord
74
74
  stmt.key = table[primary_key]
75
75
 
76
76
  # CPK
77
- if @klass.composite? && stmt.to_sql =~ /['"]#{primary_key.to_s}['"]/
77
+ if @klass.composite? && @klass.connection.visitor.compile(stmt.ast) =~ /['"]#{primary_key.to_s}['"]/
78
78
  stmt = Arel::DeleteManager.new
79
79
  stmt.from(arel_table)
80
80
  cpk_subquery(stmt)
@@ -2,7 +2,7 @@ module CompositePrimaryKeys
2
2
  module VERSION
3
3
  MAJOR = 13
4
4
  MINOR = 0
5
- TINY = 0
5
+ TINY = 1
6
6
  STRING = [MAJOR, MINOR, TINY].join('.')
7
7
  end
8
8
  end
@@ -1,7 +1,9 @@
1
- class Membership < ActiveRecord::Base
2
- self.primary_keys = :user_id, :group_id
3
- belongs_to :user
4
- belongs_to :group
5
- has_many :statuses, :class_name => 'MembershipStatus', :foreign_key => [:user_id, :group_id]
6
- has_many :readings, :primary_key => :user_id, :foreign_key => :user_id
1
+ class Membership < ActiveRecord::Base
2
+ self.primary_keys = :user_id, :group_id
3
+ belongs_to :user
4
+ belongs_to :group
5
+ has_many :statuses, :class_name => 'MembershipStatus', :foreign_key => [:user_id, :group_id]
6
+ has_many :active_statuses, -> { where('membership_statuses.status = ?', 'Active') },
7
+ :class_name => 'MembershipStatus', :foreign_key => [:user_id, :group_id]
8
+ has_many :readings, :primary_key => :user_id, :foreign_key => :user_id
7
9
  end
@@ -343,6 +343,14 @@ class TestAssociations < ActiveSupport::TestCase
343
343
  assert_equal([3,2], memberships[1].id)
344
344
  end
345
345
 
346
+ def test_scoped_has_many_with_primary_key_with_associations
347
+ memberships = Membership.joins(:active_statuses)
348
+ assert_equal(2, memberships.length)
349
+
350
+ assert_equal([1,1], memberships[0].id)
351
+ assert_equal([3,2], memberships[1].id)
352
+ end
353
+
346
354
  def test_foreign_key_present_with_null_association_ids
347
355
  group = Group.new
348
356
  group.memberships.build
data/test/test_delete.rb CHANGED
@@ -31,6 +31,8 @@ class TestDelete < ActiveSupport::TestCase
31
31
  end
32
32
 
33
33
  def test_delete_all_with_join
34
+ tested_delete_all = false
35
+ Arel::Table.engine = nil # should not rely on the global Arel::Table.engine
34
36
  employee = employees(:mindy)
35
37
 
36
38
  assert_equal(5, Department.count)
@@ -41,6 +43,10 @@ class TestDelete < ActiveSupport::TestCase
41
43
 
42
44
  assert_equal(4, Department.count)
43
45
  assert_equal(1, deleted)
46
+ tested_delete_all = true
47
+ ensure
48
+ Arel::Table.engine = ActiveRecord::Base
49
+ assert tested_delete_all
44
50
  end
45
51
 
46
52
  def test_clear_association
data/test/test_update.rb CHANGED
@@ -74,6 +74,8 @@ class TestUpdate < ActiveSupport::TestCase
74
74
  end
75
75
 
76
76
  def test_update_all_join
77
+ tested_update_all = false
78
+ Arel::Table.engine = nil # should not rely on the global Arel::Table.engine
77
79
  ReferenceCode.joins(:reference_type).
78
80
  where('reference_types.reference_type_id = ?', 2).
79
81
  update_all(:description => 'random value')
@@ -82,6 +84,10 @@ class TestUpdate < ActiveSupport::TestCase
82
84
  where(:description => 'random value')
83
85
 
84
86
  assert_equal(2, query.count)
87
+ tested_update_all = true
88
+ ensure
89
+ Arel::Table.engine = ActiveRecord::Base
90
+ assert tested_update_all
85
91
  end
86
92
 
87
93
  def test_update_with_uniqueness
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: 13.0.0
4
+ version: 13.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Charlie Savage
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-05-09 00:00:00.000000000 Z
11
+ date: 2021-11-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -39,7 +39,7 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  description: Composite key support for ActiveRecord
42
- email:
42
+ email:
43
43
  executables: []
44
44
  extensions: []
45
45
  extra_rdoc_files: []
@@ -201,7 +201,7 @@ homepage: https://github.com/composite-primary-keys/composite_primary_keys
201
201
  licenses:
202
202
  - MIT
203
203
  metadata: {}
204
- post_install_message:
204
+ post_install_message:
205
205
  rdoc_options: []
206
206
  require_paths:
207
207
  - lib
@@ -216,16 +216,16 @@ required_rubygems_version: !ruby/object:Gem::Requirement
216
216
  - !ruby/object:Gem::Version
217
217
  version: '0'
218
218
  requirements: []
219
- rubygems_version: 3.1.4
220
- signing_key:
219
+ rubygems_version: 3.2.31
220
+ signing_key:
221
221
  specification_version: 4
222
222
  summary: Composite key support for ActiveRecord
223
223
  test_files:
224
- - test/abstract_unit.rb
225
224
  - test/README_tests.rdoc
225
+ - test/abstract_unit.rb
226
226
  - test/test_associations.rb
227
- - test/test_attributes.rb
228
227
  - test/test_attribute_methods.rb
228
+ - test/test_attributes.rb
229
229
  - test/test_calculations.rb
230
230
  - test/test_callbacks.rb
231
231
  - test/test_composite_arrays.rb