sequel_bitemporal 0.6.2 → 0.6.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -35,6 +35,10 @@ module Sequel
35
35
  @bitemporal_version_columns ||= [:master_id, :valid_from, :valid_to, :created_at, :expired_at]
36
36
  end
37
37
 
38
+ def self.bitemporal_excluded_columns
39
+ @bitemporal_excluded_columns ||= [:id, *bitemporal_version_columns]
40
+ end
41
+
38
42
  def self.configure(master, opts = {})
39
43
  version = opts[:version_class]
40
44
  raise Error, "please specify version class to use for bitemporal plugin" unless version
@@ -149,11 +153,8 @@ module Sequel
149
153
  @pending_version ||= begin
150
154
  current_attributes = {}
151
155
  current_version.keys.each do |key|
152
- case key
153
- when :id, :valid_from, :valid_to, :created_at, :expired_at
154
- else
155
- current_attributes[key] = current_version.send key
156
- end
156
+ next if excluded_columns.include? key
157
+ current_attributes[key] = current_version.send key
157
158
  end if current_version?
158
159
  model.version_class.new current_attributes
159
160
  end
@@ -214,6 +215,42 @@ module Sequel
214
215
  end
215
216
  end
216
217
 
218
+ def deleted?
219
+ !new? && !current_version
220
+ end
221
+
222
+ def last_version
223
+ @last_version ||= begin
224
+ return if new?
225
+ t = ::Sequel::Plugins::Bitemporal.point_in_time
226
+ n = ::Sequel::Plugins::Bitemporal.now
227
+ versions_dataset.where do
228
+ (created_at <= t) & ({expired_at=>nil} | (expired_at > t)) &
229
+ (valid_from <= n)
230
+ end.order(:valid_to.desc).first
231
+ end
232
+ end
233
+
234
+ def restore(attrs={})
235
+ return false unless deleted?
236
+ last_version_attributes = if last_version
237
+ last_version.values.reject do |column, _|
238
+ excluded_columns.include? column
239
+ end
240
+ else
241
+ {}
242
+ end
243
+ update_attributes last_version_attributes.merge attrs
244
+ @last_version = nil
245
+ end
246
+
247
+ def reload
248
+ @last_version = nil
249
+ @current_version_values = nil
250
+ @pending_version = nil
251
+ super
252
+ end
253
+
217
254
  private
218
255
 
219
256
  def prepare_pending_version
@@ -254,7 +291,6 @@ module Sequel
254
291
  futures = futures.where "valid_from>?", pending_version.valid_from
255
292
  futures = futures.order(:valid_from).all
256
293
 
257
- excluded_columns = Sequel::Plugins::Bitemporal.bitemporal_version_columns + [:id]
258
294
  to_check_columns = self.class.version_class.columns - excluded_columns
259
295
  updated_by = (send(self.class.audit_updated_by_method) if audited?)
260
296
  previous_values = @current_version_values
@@ -340,7 +376,13 @@ module Sequel
340
376
  when :id, :master_id, :created_at, :expired_at
341
377
  false
342
378
  when :valid_from
343
- new_value && new_value<current_version.valid_from
379
+ new_value && (
380
+ new_value<current_version.valid_from ||
381
+ (
382
+ current_version.valid_to &&
383
+ new_value>current_version.valid_to
384
+ )
385
+ )
344
386
  when :valid_to
345
387
  new_value || new_value!=current_version.valid_to
346
388
  else
@@ -349,6 +391,10 @@ module Sequel
349
391
  end
350
392
  end
351
393
 
394
+ def excluded_columns
395
+ Sequel::Plugins::Bitemporal.bitemporal_excluded_columns
396
+ end
397
+
352
398
  end
353
399
  end
354
400
  end
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "sequel_bitemporal"
6
- s.version = "0.6.2"
6
+ s.version = "0.6.3"
7
7
  s.authors = ["Joseph HALTER", "Jonathan TRON"]
8
8
  s.email = ["joseph.halter@thetalentbox.com", "jonathan.tron@thetalentbox.com"]
9
9
  s.homepage = "https://github.com/TalentBox/sequel_bitemporal"
@@ -582,6 +582,51 @@ describe "Sequel::Plugins::Bitemporal" do
582
582
  | King Size | 98 | 2009-11-29 | | 2009-11-27 | 2009-11-28 | |
583
583
  }
584
584
  end
585
+ context "#deleted?" do
586
+ subject{ @master_class.new }
587
+ it "is false unless persisted" do
588
+ subject.should_not be_deleted
589
+ end
590
+ it "is false when persisted with a current version" do
591
+ subject.update_attributes(name: "Single Standard", price: 94).should_not be_deleted
592
+ end
593
+ it "is true when persisted without a current version" do
594
+ subject.update_attributes(name: "Single Standard", price: 94, valid_from: Date.today+1).should be_deleted
595
+ end
596
+ end
597
+ context "#last_version" do
598
+ subject{ @master_class.new }
599
+ it "is nil unless persisted" do
600
+ subject.last_version.should be_nil
601
+ end
602
+ it "is current version when persisted with a current version" do
603
+ subject.update_attributes name: "Single Standard", price: 94
604
+ subject.last_version.should == subject.current_version
605
+ end
606
+ it "is nil with future version but no current version" do
607
+ subject.update_attributes name: "Single Standard", price: 94, valid_from: Date.today+1
608
+ subject.last_version.should be_nil
609
+ end
610
+ it "is last version with previous version but no current version" do
611
+ subject.update_attributes name: "Single Standard", price: 94, valid_from: Date.today-2, valid_to: Date.today-1
612
+ subject.current_version.should be_nil
613
+ subject.last_version.should == subject.versions.last
614
+ end
615
+ end
616
+ context "#restore" do
617
+ subject{ @master_class.new }
618
+ it "make last version current" do
619
+ subject.update_attributes name: "Single Standard", price: 94, valid_from: Date.today-2, valid_to: Date.today-1
620
+ subject.restore
621
+ subject.current_version.should == subject.last_version
622
+ end
623
+ it "can add additional attributes to apply" do
624
+ subject.update_attributes name: "Single Standard", price: 94, valid_from: Date.today-2, valid_to: Date.today-1
625
+ subject.restore name: "New Standard"
626
+ subject.current_version.name.should == "New Standard"
627
+ subject.current_version.price.should == 94
628
+ end
629
+ end
585
630
  end
586
631
 
587
632
  describe "Sequel::Plugins::Bitemporal", "with audit" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequel_bitemporal
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.6.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-01-24 00:00:00.000000000 Z
13
+ date: 2013-01-28 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: sequel
@@ -127,7 +127,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
127
127
  version: '0'
128
128
  segments:
129
129
  - 0
130
- hash: 3142511012562917394
130
+ hash: 2205031577835886321
131
131
  required_rubygems_version: !ruby/object:Gem::Requirement
132
132
  none: false
133
133
  requirements:
@@ -136,7 +136,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
136
136
  version: '0'
137
137
  segments:
138
138
  - 0
139
- hash: 3142511012562917394
139
+ hash: 2205031577835886321
140
140
  requirements: []
141
141
  rubyforge_project:
142
142
  rubygems_version: 1.8.23