composite_primary_keys 6.0.1 → 6.0.3

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 CHANGED
@@ -1,7 +1,15 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: c978ed5109a507f202bab7ad54362c0f543d7ed6
4
- data.tar.gz: 65dac9d9389d1cbf8727ec25a5cc1a93a706d91f
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NWNiZWUyNjQ5MTQ5Y2UzYTI4Nzg1NjcyOGMxOTBhYzJmZTAzMmZmNQ==
5
+ data.tar.gz: !binary |-
6
+ OWM0NTg5MmYyMzI5OTViODJmYTJlYmZhOGEyOWNjMjY2MWQzMzNjYQ==
5
7
  SHA512:
6
- metadata.gz: 340806c9233d93bb1d18e4203080650600de58400231302bcf50b9f019f056d99d0c9883dc534d8483d081249b968bcf205f29e35fdb852565cc795ac9f4da5c
7
- data.tar.gz: 10e4247371f8e979b96074ebe2e55ee79da37cc6a37ea03be8f21e48610180d122625f6690cd70eabf6fec65d4c017061cc52042ea81376b5bec0b72a94f3833
8
+ metadata.gz: !binary |-
9
+ MzE2Y2ViOTJiMDVkZGU4ZTQwMTJjODBjZjViN2ZmOGViMGI5MDJmOGQ1YTMw
10
+ OGMxYzg1YjhjNDVkMTYzZjIyNzU3Njg5YWI5YzY4NjY5NTJmYWY0NDllZGI1
11
+ ZmIxZmY0MjMxYzkyN2IzNmU0ZTgyZjk3MGUyYWZkMzkyNmMwMjU=
12
+ data.tar.gz: !binary |-
13
+ NTBlNGNhNjBiMTEwMWM5YjcyZmY5OGNjMmQzNTQ4YjhkN2IxZWQ5OGE0MjYw
14
+ ZGNhOTE5NzY2M2MxZWNjMmNkYjU3NWIyYTQ0OWYyYzYwYzM1YWJjYTkyOTI0
15
+ MDExYTkyYzIwNTgzNzI2OTY4ZTBmOGIwYmQ5Njc1Mjc5ZmI5ODg=
@@ -1,3 +1,12 @@
1
+ == 6.0.3 (2014-04-28)
2
+
3
+ * Fixes setting of primary key when CPK is not used for a given model (see #191)
4
+
5
+ == 6.0.2 (2014-04-28)
6
+
7
+ * Fixes relating to ActiveRecord 4.0.x compatibility (Dan Draper)
8
+ * Ensure that primary key is set on instance when creating new records (Dan Draper)
9
+
1
10
  == 6.0.1 (2013-11-29)
2
11
 
3
12
  * Handle records == :all in HasManyAssociation#delete_records. Without this calling
@@ -12,7 +12,7 @@ This RubyGem extends the activerecord gem to provide CPK support.
12
12
  == Usage
13
13
 
14
14
  require 'composite_primary_keys'
15
- class ProductVariation
15
+ class ProductVariation < ActiveRecord::Base
16
16
  self.primary_keys = :product_id, :variation_seq
17
17
  end
18
18
 
@@ -48,6 +48,7 @@ require 'active_record/associations/preloader/association'
48
48
  require 'active_record/associations/preloader/belongs_to'
49
49
  require 'active_record/associations/preloader/has_and_belongs_to_many'
50
50
 
51
+
51
52
  require 'active_model/dirty'
52
53
 
53
54
  require 'active_record/attribute_methods/dirty'
@@ -67,6 +68,7 @@ require 'active_record/validations/uniqueness'
67
68
 
68
69
  # CPK files
69
70
  require 'composite_primary_keys/persistence'
71
+ require 'composite_primary_keys/active_record_overides'
70
72
  require 'composite_primary_keys/base'
71
73
  require 'composite_primary_keys/core'
72
74
  require 'composite_primary_keys/composite_arrays'
@@ -102,4 +104,4 @@ require 'composite_primary_keys/relation/calculations'
102
104
  require 'composite_primary_keys/relation/finder_methods'
103
105
  require 'composite_primary_keys/relation/query_methods'
104
106
 
105
- require 'composite_primary_keys/validations/uniqueness'
107
+ require 'composite_primary_keys/validations/uniqueness'
@@ -0,0 +1,37 @@
1
+ module CompositePrimaryKeys
2
+ module ActiveRecord
3
+ module Overides
4
+
5
+ # This override ensures that pkeys are set on the instance when records are created.
6
+ # However, while ActiveRecord::Persistence defines a create_record method
7
+ # the call in create_or_update is actually calling the method create_record in the Dirty concern
8
+ # which removes the pkey attrs and also sets updated/created at timestamps
9
+ # For some reason when we overide here we lose dirty!
10
+ # So, for now, timestamps are recorded explicitly
11
+ def create_record(attribute_names = nil)
12
+ record_timestamps!
13
+ attribute_names ||= keys_for_partial_write
14
+ attributes_values = arel_attributes_with_values_for_create(attribute_names)
15
+
16
+ new_id = self.class.unscoped.insert attributes_values
17
+ self.id = new_id if self.class.primary_key
18
+
19
+ @new_record = false
20
+ id
21
+ end
22
+
23
+ def record_timestamps!
24
+ if self.record_timestamps
25
+ current_time = current_time_from_proper_timezone
26
+
27
+ all_timestamp_attributes.each do |column|
28
+ if respond_to?(column) && respond_to?("#{column}=") && self.send(column).nil?
29
+ write_attribute(column.to_s, current_time)
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+
@@ -23,9 +23,9 @@ module ActiveRecord
23
23
  end
24
24
  end
25
25
 
26
- def primary_keys=(keys)
26
+ def primary_key_with_composite_key_support=(keys)
27
27
  unless keys.kind_of?(Array)
28
- self.primary_key = keys
28
+ self.primary_key_without_composite_key_support = keys
29
29
  return
30
30
  end
31
31
 
@@ -34,8 +34,11 @@ module ActiveRecord
34
34
  class_eval <<-EOV
35
35
  extend CompositeClassMethods
36
36
  include CompositeInstanceMethods
37
+ include CompositePrimaryKeys::ActiveRecord::Overides
37
38
  EOV
38
39
  end
40
+ alias_method_chain :primary_key=, :composite_key_support
41
+ alias_method :primary_keys=, :primary_key=
39
42
 
40
43
  def set_primary_keys(*keys)
41
44
  ActiveSupport::Deprecation.warn(
@@ -113,7 +116,7 @@ module ActiveRecord
113
116
 
114
117
  def ==(comparison_object)
115
118
  return true if equal? comparison_object
116
- ids.is_a?(Array) ? super(comparison_object) && ids.all? {|id| id.present?} : super(comparison_object)
119
+ ids.is_a?(Array) ? super(comparison_object) && ids.all? {|id| !id.nil?} : super(comparison_object)
117
120
  end
118
121
 
119
122
  def can_change_primary_key_values?
@@ -31,6 +31,16 @@ module ActiveRecord
31
31
 
32
32
  [sql, binds]
33
33
  end
34
+
35
+ # Returns a single value if query returns a single element
36
+ # otherwise returns an array coresponding to the composite keys
37
+ #
38
+ def last_inserted_id(result)
39
+ row = result.rows.first
40
+ if Array === row
41
+ row.size == 1 ? row[0] : row
42
+ end
43
+ end
34
44
  end
35
45
  end
36
- end
46
+ end
@@ -24,7 +24,7 @@ module CompositePrimaryKeys
24
24
  # records = relation.where(table[primary_key].gteq(start)).all
25
25
  records = self.primary_key.reduce(relation) do |rel, key|
26
26
  rel.where(table[key].gteq(start))
27
- end.all
27
+ end
28
28
 
29
29
  while records.any?
30
30
  records_size = records.size
@@ -2,7 +2,7 @@ module CompositePrimaryKeys
2
2
  module VERSION
3
3
  MAJOR = 6
4
4
  MINOR = 0
5
- TINY = 1
5
+ TINY = 3
6
6
  STRING = [MAJOR, MINOR, TINY].join('.')
7
7
  end
8
8
  end
File without changes
@@ -25,6 +25,8 @@ ActiveRecord::Base.configurations[:test] = spec
25
25
  # Tell ActiveRecord where to find models
26
26
  ActiveSupport::Dependencies.autoload_paths << File.join(PROJECT_ROOT, 'test', 'fixtures')
27
27
 
28
+ I18n.config.enforce_available_locales = true
29
+
28
30
  class ActiveSupport::TestCase
29
31
  include ActiveRecord::TestFixtures
30
32
 
@@ -71,7 +73,7 @@ class ActiveSupport::TestCase
71
73
  @klass_info = classes[@key_test]
72
74
  @klass, @primary_keys = @klass_info[:class], @klass_info[:primary_keys]
73
75
  order = @klass.primary_key.is_a?(String) ? @klass.primary_key : @klass.primary_key.join(',')
74
- @first = @klass.find(:first, :order => order)
76
+ @first = @klass.order(order).first
75
77
  yield
76
78
  end
77
79
  end
@@ -0,0 +1,8 @@
1
+ class ReferenceCodeUsingCompositeKeyAlias < ActiveRecord::Base
2
+ self.table_name = 'reference_codes'
3
+ self.primary_key = [:reference_type_id, :reference_code]
4
+
5
+ belongs_to :reference_type, :foreign_key => "reference_type_id"
6
+
7
+ validates_presence_of :reference_code, :code_label, :abbreviation
8
+ end
@@ -0,0 +1,8 @@
1
+ class ReferenceCodeUsingSimpleKeyAlias < ActiveRecord::Base
2
+ self.table_name = 'reference_codes'
3
+ self.primary_key = :code_label
4
+
5
+ belongs_to :reference_type, :foreign_key => "reference_type_id"
6
+
7
+ validates_presence_of :reference_code, :code_label, :abbreviation
8
+ end
@@ -6,6 +6,6 @@ class Room < ActiveRecord::Base
6
6
  has_many :room_attributes, :through => :room_attribute_assignments
7
7
 
8
8
  def find_custom_room_attributes
9
- room_attributes.find(:all, :conditions => ["room_attributes.name != ?", "type"])
9
+ room_attributes.where("room_attributes.name != ?", "type")
10
10
  end
11
11
  end
@@ -5,6 +5,6 @@ class User < ActiveRecord::Base
5
5
  has_many :hacks, :through => :comments, :source => :hack
6
6
 
7
7
  def find_custom_articles
8
- articles.find(:all, :conditions => ["name = ?", "Article One"])
8
+ articles.where("name = ?", "Article One")
9
9
  end
10
10
  end
@@ -0,0 +1,18 @@
1
+ require File.expand_path('../abstract_unit', __FILE__)
2
+
3
+ class TestAliases < ActiveSupport::TestCase
4
+ fixtures :reference_codes
5
+
6
+ def test_primary_key_setter_alias_composite_key
7
+ reference_code = ReferenceCodeUsingCompositeKeyAlias.find([1, 2])
8
+ assert_equal 'MRS', reference_code.code_label
9
+ assert_equal 'Mrs', reference_code.abbreviation
10
+ end
11
+
12
+ def test_primary_key_setter_alias_simple_key
13
+ reference_code = ReferenceCodeUsingSimpleKeyAlias.find('MRS')
14
+ assert_equal 1, reference_code.reference_type_id
15
+ assert_equal 2, reference_code.reference_code
16
+ assert_equal 'Mrs', reference_code.abbreviation
17
+ end
18
+ end
@@ -29,7 +29,7 @@ class TestAssociations < ActiveSupport::TestCase
29
29
  # Its not generating the instances of associated classes from the rows
30
30
  def test_find_includes
31
31
  # Old style
32
- products = Product.find(:all, :include => :product_tariffs)
32
+ products = Product.includes(:product_tariffs).all
33
33
  assert_equal(3, products.length)
34
34
  assert_equal(3, products.inject(0) {|sum, product| sum + product.product_tariffs.length})
35
35
 
@@ -44,8 +44,9 @@ class TestAssociations < ActiveSupport::TestCase
44
44
  product_tarrif = product_tariffs(:second_free)
45
45
 
46
46
  # Old style, include a where clause to force eager loading
47
- products = Product.find(:all, :include => :product_tariffs,
48
- :conditions => ["product_tariffs.product_id = ?", product.id])
47
+ #products = Product.find(:all, :include => :product_tariffs,
48
+ # :conditions => ["product_tariffs.product_id = ?", product.id])
49
+ products = Product.includes(:product_tariffs).where("product_tariffs.product_id = ?", product.id).references(:product_tariffs)
49
50
 
50
51
  assert_equal(1, products.length)
51
52
  assert_equal(product, products.first)
@@ -60,7 +61,7 @@ class TestAssociations < ActiveSupport::TestCase
60
61
 
61
62
  def test_find_includes_tariffs
62
63
  # Old style
63
- tariffs = Tariff.find(:all, :include => :product_tariffs)
64
+ tariffs = Tariff.includes(:product_tariffs)
64
65
  assert_equal(3, tariffs.length)
65
66
  assert_equal(3, tariffs.inject(0) {|sum, tariff| sum + tariff.product_tariffs.length})
66
67
 
@@ -72,7 +73,7 @@ class TestAssociations < ActiveSupport::TestCase
72
73
 
73
74
  def test_find_includes_product_tariffs_product
74
75
  # Old style
75
- product_tariffs = ProductTariff.find(:all, :include => :product)
76
+ product_tariffs = ProductTariff.includes(:product)
76
77
  assert_not_nil(product_tariffs)
77
78
  assert_equal(3, product_tariffs.length)
78
79
 
@@ -84,7 +85,7 @@ class TestAssociations < ActiveSupport::TestCase
84
85
 
85
86
  def test_find_includes_product_tariffs_tariff
86
87
  # Old style
87
- product_tariffs = ProductTariff.find(:all, :include => :tariff)
88
+ product_tariffs = ProductTariff.includes(:tariff)
88
89
  assert_equal(3, product_tariffs.length)
89
90
 
90
91
  # New style
@@ -93,7 +94,7 @@ class TestAssociations < ActiveSupport::TestCase
93
94
  end
94
95
 
95
96
  def test_has_many_through
96
- products = Product.find(:all, :include => :tariffs)
97
+ products = Product.includes(:tariffs)
97
98
  assert_equal(3, products.length)
98
99
 
99
100
  tarrifs_length = products.inject(0) {|sum, product| sum + product.tariffs.length}
@@ -101,12 +102,12 @@ class TestAssociations < ActiveSupport::TestCase
101
102
  end
102
103
 
103
104
  def test_new_style_includes_with_conditions
104
- product_tariff = ProductTariff.includes(:tariff).where('tariffs.amount < 5').first
105
+ product_tariff = ProductTariff.includes(:tariff).where('tariffs.amount < 5').references(:tariffs).first
105
106
  assert_equal(0, product_tariff.tariff.amount)
106
107
  end
107
108
 
108
109
  def test_find_product_includes
109
- products = Product.find(:all, :include => {:product_tariffs => :tariff})
110
+ products = Product.includes(:product_tariffs => :tariff)
110
111
  assert_equal(3, products.length)
111
112
 
112
113
  product_tariffs_length = products.inject(0) {|sum, product| sum + product.product_tariffs.length}
@@ -114,7 +115,7 @@ class TestAssociations < ActiveSupport::TestCase
114
115
  end
115
116
 
116
117
  def test_find_tariffs_includes
117
- tariffs = Tariff.find(:all, :include => {:product_tariffs => :product})
118
+ tariffs = Tariff.includes(:product_tariffs => :product)
118
119
  assert_equal(3, tariffs.length)
119
120
 
120
121
  product_tariffs_length = tariffs.inject(0) {|sum, tariff| sum + tariff.product_tariffs.length}
@@ -122,7 +123,7 @@ class TestAssociations < ActiveSupport::TestCase
122
123
  end
123
124
 
124
125
  def test_has_many_through_when_not_pre_loaded
125
- student = Student.find(:first)
126
+ student = Student.first
126
127
  rooms = student.rooms
127
128
  assert_equal(1, rooms.size)
128
129
  assert_equal(1, rooms.first.dorm_id)
@@ -130,7 +131,7 @@ class TestAssociations < ActiveSupport::TestCase
130
131
  end
131
132
 
132
133
  def test_has_many_through_when_through_association_is_composite
133
- dorm = Dorm.find(:first)
134
+ dorm = Dorm.first
134
135
  assert_equal(3, dorm.rooms.length)
135
136
  assert_equal(1, dorm.rooms.first.room_attributes.length)
136
137
  assert_equal('type', dorm.rooms.first.room_attributes.first.name)
@@ -143,10 +144,10 @@ class TestAssociations < ActiveSupport::TestCase
143
144
  suburb = Suburb.find([2, 1])
144
145
  assert_equal 1, suburb.first_streets.size
145
146
 
146
- suburb = Suburb.find([2, 1], :include => :streets)
147
+ suburb = Suburb.includes(:streets).find([2, 1])
147
148
  assert_equal 2, suburb.streets.size
148
149
 
149
- suburb = Suburb.find([2, 1], :include => :first_streets)
150
+ suburb = Suburb.includes(:first_streets).find([2, 1])
150
151
  assert_equal 1, suburb.first_streets.size
151
152
  end
152
153
 
@@ -220,53 +221,46 @@ class TestAssociations < ActiveSupport::TestCase
220
221
  end
221
222
 
222
223
  def test_joins_has_many_with_primary_key
223
- @membership = Membership.find(:first, :joins => :readings, :conditions => { :readings => { :id => 1 } })
224
+ #@membership = Membership.find(:first, :joins => :readings, :conditions => { :readings => { :id => 1 } })
225
+ @membership = Membership.joins(:readings).where(readings: { id: 1 }).first
224
226
 
225
227
  assert_equal [1, 1], @membership.id
226
228
  end
227
229
 
228
230
  def test_joins_has_one_with_primary_key
229
- @membership = Membership.find(:first, :joins => :readings,
230
- :conditions => { :readings => { :id => 2 } })
231
+ @membership = Membership.joins(:readings).where(readings: { id: 2 }).first
231
232
 
232
233
  assert_equal [1, 1], @membership.id
233
234
  end
234
235
 
235
236
  def test_has_many_through_with_conditions_when_through_association_is_not_composite
236
- user = User.find(:first)
237
- assert_equal 1, user.articles.find(:all, :conditions => ["articles.name = ?", "Article One"]).size
237
+ user = User.first
238
+ assert_equal 1, user.articles.where("articles.name = ?", "Article One").size
238
239
  end
239
240
 
240
241
  def test_has_many_through_with_conditions_when_through_association_is_composite
241
- room = Room.find(:first)
242
- assert_equal 0, room.room_attributes.find(:all, :conditions => ["room_attributes.name != ?", "type"]).size
242
+ room = Room.first
243
+ assert_equal 0, room.room_attributes.where("room_attributes.name != ?", "type").size
243
244
  end
244
245
 
245
246
  def test_has_many_through_on_custom_finder_when_through_association_is_composite_finder_when_through_association_is_not_composite
246
- user = User.find(:first)
247
+ user = User.first
247
248
  assert_equal(1, user.find_custom_articles.size)
248
249
  end
249
250
 
250
251
  def test_has_many_through_on_custom_finder_when_through_association_is_composite
251
- room = Room.find(:first)
252
+ room = Room.first
252
253
  assert_equal(0, room.find_custom_room_attributes.size)
253
254
  end
254
255
 
255
256
  def test_has_many_with_primary_key_with_associations
256
- # Trigger Active Records find_with_associations method
257
- memberships = Membership.find(:all, :include => :statuses,
258
- :conditions => ["membership_statuses.status = ?",
259
- 'Active'])
260
-
257
+ memberships = Membership.includes(:statuses).where("membership_statuses.status = ?", 'Active').references(:membership_statuses)
261
258
  assert_equal(1, memberships.length)
262
259
  assert_equal([1,1], memberships[0].id)
263
260
  end
264
261
 
265
262
  def test_limitable_reflections
266
- memberships = Membership.find(:all, :include => :statuses,
267
- :conditions => ["membership_statuses.status = ?",
268
- 'Active'],
269
- :limit => 1)
263
+ memberships = Membership.includes(:statuses).where("membership_statuses.status = ?", 'Active').limit(1)
270
264
  assert_equal(1, memberships.length)
271
265
  assert_equal([1,1], memberships[0].id)
272
266
  end
@@ -6,28 +6,27 @@ class TestCalculations < ActiveSupport::TestCase
6
6
  :departments, :employees, :memberships, :membership_statuses
7
7
 
8
8
  def test_count
9
- assert_equal(3, Product.count(:include => :product_tariffs))
10
- assert_equal(3, Tariff.count(:include => :product_tariffs))
9
+ assert_equal(3, Product.includes(:product_tariffs).count)
10
+ assert_equal(3, Tariff.includes(:product_tariffs).count)
11
11
 
12
12
  expected = {Date.today => 2,
13
13
  Date.today.next => 1}
14
14
 
15
- assert_equal(expected, Tariff.count(:group => :start_date))
15
+ assert_equal(expected, Tariff.group(:start_date).count)
16
16
  end
17
17
 
18
18
  def test_count_distinct
19
19
  product = products(:first_product)
20
- assert_equal(1, product.product_tariffs.select('tariff_start_date').count(:distinct => true))
20
+ assert_equal(1, product.product_tariffs.select('tariff_start_date').distinct.count)
21
21
  end
22
22
 
23
23
  def test_count_not_distinct
24
24
  product = products(:first_product)
25
- assert_equal(2, product.product_tariffs.select('tariff_start_date').count(:distinct => false))
25
+ assert_equal(2, product.product_tariffs.select('tariff_start_date').count)
26
26
  end
27
27
 
28
28
  def test_count_includes
29
- count = Dorm.count(:include => :rooms,
30
- :conditions => ["rooms.room_id = ?", 2])
29
+ count = Dorm.where("rooms.room_id = ?", 2).includes(:rooms).references(:rooms).count
31
30
  assert_equal(1, count)
32
31
  end
33
32
 
@@ -35,4 +34,4 @@ class TestCalculations < ActiveSupport::TestCase
35
34
  count = Tariff.includes(:product_tariffs).references(:product_tariffs).where("product_tariffs.tariff_id = ?", 2).count
36
35
  assert_equal(1, count)
37
36
  end
38
- end
37
+ end