composite_primary_keys 9.0.0 → 9.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
  SHA1:
3
- metadata.gz: 407e2b2f6781c2f4dbc7e34e576942f3833dbb98
4
- data.tar.gz: 189eb2e8ac24f2124288a15189fbab23d119bba5
3
+ metadata.gz: f4248480dc399e7b5855cdcb24c52aa0d316cb84
4
+ data.tar.gz: 8cf2391613840634669fbfac2d0df07fcc4b89d0
5
5
  SHA512:
6
- metadata.gz: 024d036a68642775e0763f05962303e5f4b4ccb5b60942820daa9e46fb82f16eb6f7bcd0bfe61bdcb383ebcfac2e3d74805c2e75184fdc488e8a20cef8cadab1
7
- data.tar.gz: 5ce0de500d3c796b374716dcbe4a356930a3537490f6e6e26eecd59bce02bf0626abfc1e19c6b536736d06d689121881ed08cb47aa0a5e8eaf802f8d6ab18c26
6
+ metadata.gz: 55a92a161646aedc85be40fc0832651050962e5c53e941f3d97f0a89ace2fbc9363b1bec30f7115dca9d91409b95389fd1dcabe29cbfbd2768113c60e116d0c4
7
+ data.tar.gz: cd1df5d88752ae0ea029e4b24239cc1038f8a012951c5859a5c3a23dd8183648b6880cebffcd148424a7789e53509af39ecb9e760b3eaa959f127dd35748392f
@@ -1,9 +1,17 @@
1
+ == 9.0.1 (2016-07-27)
2
+ * Create OR predicates in a nicely balanced tree fixing #320 (Nathan Samson)
3
+
1
4
  == 9.0.0 (2016-07-06)
2
5
  * Support connection_specification_name in rails5.0.0.rc1 (pocari)
3
6
 
4
7
  == 9.0.0.beta1 (2016-04-16)
5
8
  * Rails 5 beta support (Sammy Larbi)
6
9
 
10
+ == 8.1.4 (2016-07-27)
11
+
12
+ * Create OR predicates in a nicely balanced tree fixing #320 (Nathan Samson)
13
+ * Fix find in batches (apurvis)
14
+
7
15
  == 8.1.3 (2016-04-16)
8
16
 
9
17
  * Make CPK work for Arel::Attributes::Attribute (Rick Xing)
@@ -1,7 +1,23 @@
1
1
  module ActiveRecord
2
2
  module Associations
3
3
  class HasManyAssociation
4
- silence_warnings do
4
+ include CompositePrimaryKeys::Predicates
5
+
6
+ def delete_records(records, method)
7
+ if method == :destroy
8
+ records.each(&:destroy!)
9
+ update_counter(-records.length) unless reflection.inverse_updates_counter_cache?
10
+ return
11
+ # Zerista
12
+ elsif self.reflection.klass.composite?
13
+ predicate = cpk_in_predicate(self.scope.table, self.reflection.klass.primary_keys, records.map(&:id))
14
+ scope = self.scope.where(predicate)
15
+ else
16
+ scope = self.scope.where(reflection.klass.primary_key => records)
17
+ end
18
+ update_counter(-delete_count(method, scope))
19
+ end
20
+
5
21
  def delete_count(method, scope)
6
22
  if method == :delete_all
7
23
  scope.delete_all
@@ -27,5 +43,4 @@ module ActiveRecord
27
43
  end
28
44
  end
29
45
  end
30
- end
31
46
  end
@@ -8,13 +8,23 @@ module CompositePrimaryKeys
8
8
  end
9
9
  end
10
10
 
11
- def cpk_or_predicate(predicates)
12
- or_predicate = predicates.map do |predicate|
13
- ::Arel::Nodes::Grouping.new(predicate)
14
- end.inject do |memo, node|
15
- ::Arel::Nodes::Or.new(memo, node)
11
+ def cpk_or_predicate(predicates, group = true)
12
+ if predicates.length <= 1
13
+ predicates.first
14
+ else
15
+ split_point = predicates.length / 2
16
+ predicates_first_half = predicates[0...split_point]
17
+ predicates_second_half = predicates[split_point..-1]
18
+
19
+ or_predicate = ::Arel::Nodes::Or.new(cpk_or_predicate(predicates_first_half, false),
20
+ cpk_or_predicate(predicates_second_half, false))
21
+
22
+ if group
23
+ ::Arel::Nodes::Grouping.new(or_predicate)
24
+ else
25
+ or_predicate
26
+ end
16
27
  end
17
- ::Arel::Nodes::Grouping.new(or_predicate)
18
28
  end
19
29
 
20
30
  def cpk_id_predicate(table, keys, values)
@@ -2,7 +2,7 @@ module CompositePrimaryKeys
2
2
  module VERSION
3
3
  MAJOR = 9
4
4
  MINOR = 0
5
- TINY = 0
5
+ TINY = 1
6
6
  STRING = [MAJOR, MINOR, TINY].join('.')
7
7
  end
8
8
  end
@@ -2,10 +2,9 @@ class Employee < ActiveRecord::Base
2
2
  belongs_to :department, :foreign_key => [:department_id, :location_id]
3
3
  has_many :comments, :as => :person
4
4
  has_and_belongs_to_many :groups
5
- has_many :salaries,
6
- :primary_key => [:id, :location_id],
7
- :foreign_key => [:employee_id, :location_id]
8
- has_one :one_salary, :class_name => "Salary",
9
- :primary_key => [:id, :location_id],
10
- :foreign_key => [:employee_id, :location_id]
5
+ has_many :salaries, :primary_key => [:id, :location_id],
6
+ :foreign_key => [:employee_id, :location_id]
7
+ has_one :one_salary, :class_name => "Salary",
8
+ :primary_key => [:id, :location_id],
9
+ :foreign_key => [:employee_id, :location_id]
11
10
  end
@@ -1,5 +1,5 @@
1
- class Tariff < ActiveRecord::Base
2
- self.primary_keys = [:tariff_id, :start_date]
3
- has_many :product_tariffs, :foreign_key => [:tariff_id, :tariff_start_date]
4
- has_many :products, :through => :product_tariffs, :foreign_key => [:tariff_id, :tariff_start_date]
5
- end
1
+ class Tariff < ActiveRecord::Base
2
+ self.primary_keys = [:tariff_id, :start_date]
3
+ has_many :product_tariffs, :foreign_key => [:tariff_id, :tariff_start_date], :dependent => :delete_all
4
+ has_many :products, :through => :product_tariffs, :foreign_key => [:tariff_id, :tariff_start_date]
5
+ end
@@ -257,7 +257,7 @@ class TestAssociations < ActiveSupport::TestCase
257
257
  assert_not_nil(department.head)
258
258
  end
259
259
 
260
- def test_has_many_build__simple_key
260
+ def test_has_many_build_simple_key
261
261
  user = users(:santiago)
262
262
  reading = user.readings.build
263
263
  assert_equal user.id, reading.user_id
@@ -19,36 +19,36 @@ 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
52
 
53
53
  def test_clear_association
54
54
  department = Department.find([1,1])
@@ -71,6 +71,17 @@ class TestDelete < ActiveSupport::TestCase
71
71
  assert_equal 1, department.employees.size, "After delete and a reload from DB employee count should be 1."
72
72
  end
73
73
 
74
+ def test_has_many_replace
75
+ tariff = tariffs(:flat)
76
+ assert_equal(1, tariff.product_tariffs.length)
77
+
78
+ tariff.product_tariffs = [product_tariffs(:first_free), product_tariffs(:second_free)]
79
+ tariff.reload
80
+
81
+ assert_equal(2, tariff.product_tariffs.length)
82
+ refute(tariff.product_tariffs.include?(tariff))
83
+ end
84
+
74
85
  def test_destroy_has_one
75
86
  # In this case the association is a has_one with
76
87
  # dependent set to :destroy
@@ -16,7 +16,26 @@ class TestPredicates < ActiveSupport::TestCase
16
16
 
17
17
  connection = ActiveRecord::Base.connection
18
18
  quoted = "#{connection.quote_table_name('departments')}.#{connection.quote_column_name('id')}"
19
- expected = "((#{quoted} = 0) OR (#{quoted} = 1) OR (#{quoted} = 2))"
19
+ expected = "(#{quoted} = 0 OR #{quoted} = 1 OR #{quoted} = 2)"
20
+
21
+ pred = cpk_or_predicate(predicates)
22
+ assert_equal(with_quoted_identifiers(expected), pred.to_sql)
23
+ end
24
+
25
+ def test_or_with_many_values
26
+ dep = Arel::Table.new(:departments)
27
+
28
+ predicates = Array.new
29
+
30
+ number_of_predicates = 3000 # This should really be big
31
+ number_of_predicates.times do |i|
32
+ predicates << dep[:id].eq(i)
33
+ end
34
+
35
+ connection = ActiveRecord::Base.connection
36
+ quoted = "#{connection.quote_table_name('departments')}.#{connection.quote_column_name('id')}"
37
+ expected_ungrouped = ((0...number_of_predicates).map { |i| "#{quoted} = #{i}" }).join(' OR ')
38
+ expected = "(#{expected_ungrouped})"
20
39
 
21
40
  pred = cpk_or_predicate(predicates)
22
41
  assert_equal(with_quoted_identifiers(expected), pred.to_sql)
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.0
4
+ version: 9.0.1
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-06 00:00:00.000000000 Z
11
+ date: 2016-07-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -279,7 +279,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
279
279
  version: '0'
280
280
  requirements: []
281
281
  rubyforge_project:
282
- rubygems_version: 2.5.1
282
+ rubygems_version: 2.6.6
283
283
  signing_key:
284
284
  specification_version: 4
285
285
  summary: Composite key support for ActiveRecord