versioned_record 0.3.2 → 0.4.0

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,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 60698c47669db53300faa0ac76472e2f2c6bf165
4
- data.tar.gz: 2999d4a36edb3075cb52c7a87fc69362cbe8fbb6
3
+ metadata.gz: 562b1ce360d24ed679ebdfb9f20b2c3759882a27
4
+ data.tar.gz: f1f2c1ce89911fcc4563aa66cf0061485b0d2783
5
5
  SHA512:
6
- metadata.gz: 7d0636a118bfaef0caf890cbedadde46555b6e3da3fe1bc3c07f6cb2337db387c2ec46632e3cb904da4fd2bf7bb8c13283140eb8c9e8317f0f51e425c424b0bb
7
- data.tar.gz: 2875b2175b8419f973562920f2b85f06a425b1eb73f4731e3a9c7b113c9a6b8ec45b104387e92bdcaf362f3fa307abf46c46e5552e50f69490063f93297a7334
6
+ metadata.gz: 233b14b9f0ce7f0d7a0800f555d8b41e00cfdfec1a702f5e0480aab639a87a23248d06fbb45e98b2ce32d98e72788261a94f225792e2ade35ad96985f2295d5c
7
+ data.tar.gz: 8dd8dc76db0e376cfb2345811c2eb0345d384fe2878aeb3070d0268910831d7ce68a875bfc9315dac3a7db40f655fe7a3bf539fdf7ad8458b1c98b4b6113bd2d
data/README.md CHANGED
@@ -113,7 +113,6 @@ Right now, only PostgreSQL has been tested. MySQL may or may not work but if you
113
113
  ## Limitations
114
114
 
115
115
  * Does not currently work with ActiveRecord 4.1+
116
- * Polymorphic belongs to does not work on versioned records
117
116
  * HABTM where models on _both_ sides are versioned have not been tested
118
117
 
119
118
 
@@ -33,29 +33,43 @@ module VersionedRecord
33
33
  eq_predicates = [ association_fields[0].eq(fields[0]) ]
34
34
  case association.reflection.macro
35
35
  when :belongs_to
36
- # We don't handle Polymorphic associations at this stage
37
- if !association.options[:polymorphic]
38
- if association.reflection.klass.versioned?
39
- eq_predicates << association_table[:is_current_version].eq(true)
40
- end
41
- end
36
+ add_belongs_to_predicates!(eq_predicates, association_table)
42
37
  when :has_and_belongs_to_many
43
- if association.reflection.klass.versioned?
44
- if association.reflection.klass.table_name == association_table.name
45
- eq_predicates << association_table[:is_current_version].eq(true)
46
- end
47
- end
38
+ add_habtm_predicates!(eq_predicates, association, association_table)
48
39
  when :has_many, :has_one
49
- if association.reflection.klass.versioned?
50
- if association.reflection.klass.table_name == association_table.name
51
- eq_predicates << association_table[:is_current_version].eq(true)
52
- end
53
- end
40
+ add_has_x_predicates!(eq_predicates, association, association_table)
54
41
  end
55
42
  cpk_and_predicate(eq_predicates)
56
43
  else
57
44
  super
58
45
  end
59
46
  end
47
+
48
+ private
49
+ def add_belongs_to_predicates!(predicates, association_table)
50
+ if self.klass.versioned?
51
+ add_current_version_constraint!(predicates, association_table)
52
+ end
53
+ end
54
+
55
+ def add_habtm_predicates!(predicates, association, association_table)
56
+ if self.klass.versioned?
57
+ if association.reflection.klass.table_name == association_table.name
58
+ add_current_version_constraint!(predicates, association_table)
59
+ end
60
+ end
61
+ end
62
+
63
+ def add_has_x_predicates!(predicates, association, association_table)
64
+ if association.reflection.klass.versioned?
65
+ if association.reflection.klass.table_name == association_table.name
66
+ add_current_version_constraint!(predicates, association_table)
67
+ end
68
+ end
69
+ end
70
+
71
+ def add_current_version_constraint!(predicates, association_table)
72
+ predicates << association_table[:is_current_version].eq(true)
73
+ end
60
74
  end
61
75
  end
@@ -1,3 +1,3 @@
1
1
  module VersionedRecord
2
- VERSION = "0.3.2"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe User do
4
+ let(:company) { Company.create!(name: 'Google') }
5
+ let(:user) { User.create!(name: 'Lauren', entity: company) }
6
+
7
+ specify 'that the company is set as the entity' do
8
+ expect(user.entity).to eq(company)
9
+ end
10
+
11
+ describe 'a new version of the company is created' do
12
+ let!(:new_company) { company.create_version!(name: 'Google World Domination') }
13
+
14
+ specify 'that the company is still set as the entity' do
15
+ expect(user.reload.entity).to eq(new_company)
16
+ end
17
+ end
18
+ end
@@ -1,3 +1,4 @@
1
1
  class Company < ActiveRecord::Base
2
- has_many :users
2
+ include VersionedRecord
3
+ has_many :users, as: :entity
3
4
  end
@@ -4,7 +4,7 @@ ActiveRecord::Base.establish_connection(dbconfig[env]['postgresql'])
4
4
 
5
5
  ActiveRecord::Schema.define :version => 0 do
6
6
 
7
- create_table :companies, force: true do |t|
7
+ create_table :companies, force: true, versioned: true do |t|
8
8
  t.string :name
9
9
  end
10
10
 
@@ -62,4 +62,9 @@ ActiveRecord::Schema.define :version => 0 do
62
62
  create_table :catalogs, force: true, versioned: true do |t|
63
63
  t.string :name
64
64
  end
65
+
66
+ create_table :users, force: true do |t|
67
+ t.references :company, polymorphic: true
68
+ t.string :name
69
+ end
65
70
  end
@@ -0,0 +1,3 @@
1
+ class User < ActiveRecord::Base
2
+ belongs_to :entity, polymorphic: true
3
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: versioned_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Draper
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-05 00:00:00.000000000 Z
11
+ date: 2014-06-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -180,6 +180,7 @@ files:
180
180
  - spec/composite_onesided_habtm_spec.rb
181
181
  - spec/has_one_spec.rb
182
182
  - spec/has_one_through_spec.rb
183
+ - spec/simple_belongs_to_polymorphic_spec.rb
183
184
  - spec/simple_belongs_to_spec.rb
184
185
  - spec/simple_onesided_habtm_spec.rb
185
186
  - spec/spec_helper.rb
@@ -194,6 +195,7 @@ files:
194
195
  - spec/support/package.rb
195
196
  - spec/support/sale.rb
196
197
  - spec/support/tag.rb
198
+ - spec/support/user.rb
197
199
  - spec/support/versioned_product.rb
198
200
  - spec/version_class_methods_spec.rb
199
201
  - spec/versions_spec.rb
@@ -229,6 +231,7 @@ test_files:
229
231
  - spec/composite_onesided_habtm_spec.rb
230
232
  - spec/has_one_spec.rb
231
233
  - spec/has_one_through_spec.rb
234
+ - spec/simple_belongs_to_polymorphic_spec.rb
232
235
  - spec/simple_belongs_to_spec.rb
233
236
  - spec/simple_onesided_habtm_spec.rb
234
237
  - spec/spec_helper.rb
@@ -243,6 +246,7 @@ test_files:
243
246
  - spec/support/package.rb
244
247
  - spec/support/sale.rb
245
248
  - spec/support/tag.rb
249
+ - spec/support/user.rb
246
250
  - spec/support/versioned_product.rb
247
251
  - spec/version_class_methods_spec.rb
248
252
  - spec/versions_spec.rb