Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix version expanding on version destroy and bump to 0.4.8

  • Loading branch information...
commit f6d7e884217e638d5c2ec2ecd30b27c20343fa5a 1 parent f6962c0
@JosephHalter JosephHalter authored
View
32 lib/sequel/plugins/bitemporal.rb
@@ -77,8 +77,11 @@ def current?
valid_from.to_time<=n &&
valid_to.to_time>n
end
- def destroy
- master.destroy_version self
+ def destroy(opts={})
+ expand_previous_version = opts.fetch(:expand_previous_version){
+ valid_from.to_time>::Sequel::Plugins::Bitemporal.now
+ }
+ master.destroy_version self, expand_previous_version
end
end
unless opts[:delegate]==false
@@ -173,20 +176,27 @@ def destroy
versions_dataset.where(expired_at: nil).where("valid_to>valid_from").update expired_at: Time.now
end
- def destroy_version(version)
+ def destroy_version(version, expand_previous_version)
point_in_time = Time.now
return false if version.valid_to.to_time<=point_in_time
model.db.transaction do
success = true
- previous = versions_dataset.where({
- expired_at: nil,
- valid_to: version.valid_from,
- }).where("valid_to>valid_from").first
- if previous
- success &&= save_fossil previous, created_at: point_in_time, valid_to: version.valid_to
- success &&= previous.update expired_at: point_in_time
+ version_was_valid = point_in_time>=version.valid_from.to_time
+ if expand_previous_version
+ previous = versions_dataset.where({
+ expired_at: nil,
+ valid_to: version.valid_from,
+ }).where("valid_to>valid_from").first
+ if previous
+ if version_was_valid
+ success &&= save_fossil previous, created_at: point_in_time, valid_from: point_in_time, valid_to: version.valid_to
+ else
+ success &&= save_fossil previous, created_at: point_in_time, valid_to: version.valid_to
+ success &&= previous.update expired_at: point_in_time
+ end
+ end
end
- success &&= save_fossil version, created_at: point_in_time, valid_to: point_in_time if point_in_time>=version.valid_from.to_time
+ success &&= save_fossil version, created_at: point_in_time, valid_to: point_in_time if version_was_valid
success &&= version.update expired_at: point_in_time
raise Sequel::Rollback unless success
success
View
2  sequel_bitemporal.gemspec
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
Gem::Specification.new do |s|
s.name = "sequel_bitemporal"
- s.version = "0.4.7"
+ s.version = "0.4.8"
s.authors = ["Joseph HALTER", "Jonathan TRON"]
s.email = ["joseph.halter@thetalentbox.com", "jonathan.tron@thetalentbox.com"]
s.homepage = "https://github.com/TalentBox/sequel_bitemporal"
View
32 spec/bitemporal_date_spec.rb
@@ -197,18 +197,37 @@
end
it "allows deleting current version" do
master = @master_class.new
+ master.update_attributes name: "Single Standard", price: 92, valid_from: Date.today-2, valid_to: Date.today
master.update_attributes name: "Single Standard", price: 98
master.update_attributes name: "Single Standard", price: 94, valid_from: Date.today+2
Timecop.freeze Date.today+1
master.current_version.destroy.should be_true
master.should have_versions %Q{
| name | price | created_at | expired_at | valid_from | valid_to | current |
+ | Single Standard | 92 | 2009-11-28 | | 2009-11-26 | 2009-11-28 | |
| Single Standard | 98 | 2009-11-28 | 2009-11-28 | 2009-11-28 | MAX DATE | |
| Single Standard | 98 | 2009-11-28 | 2009-11-29 | 2009-11-28 | 2009-11-30 | |
| Single Standard | 94 | 2009-11-28 | | 2009-11-30 | MAX DATE | |
| Single Standard | 98 | 2009-11-29 | | 2009-11-28 | 2009-11-29 | |
}
end
+ it "allows deleting current version to restore the previous one" do
+ master = @master_class.new
+ master.update_attributes name: "Single Standard", price: 92, valid_from: Date.today-2, valid_to: Date.today
+ master.update_attributes name: "Single Standard", price: 98
+ master.update_attributes name: "Single Standard", price: 94, valid_from: Date.today+2
+ Timecop.freeze Date.today+1
+ master.current_version.destroy(expand_previous_version: true).should be_true
+ master.should have_versions %Q{
+ | name | price | created_at | expired_at | valid_from | valid_to | current |
+ | Single Standard | 92 | 2009-11-28 | | 2009-11-26 | 2009-11-28 | |
+ | Single Standard | 98 | 2009-11-28 | 2009-11-28 | 2009-11-28 | MAX DATE | |
+ | Single Standard | 98 | 2009-11-28 | 2009-11-29 | 2009-11-28 | 2009-11-30 | |
+ | Single Standard | 94 | 2009-11-28 | | 2009-11-30 | MAX DATE | |
+ | Single Standard | 92 | 2009-11-29 | | 2009-11-29 | 2009-11-30 | true |
+ | Single Standard | 98 | 2009-11-29 | | 2009-11-28 | 2009-11-29 | |
+ }
+ end
it "allows deleting a future version" do
master = @master_class.new
master.update_attributes name: "Single Standard", price: 98
@@ -223,6 +242,19 @@
| Single Standard | 98 | 2009-11-29 | | 2009-11-28 | MAX DATE | true |
}
end
+ it "allows deleting a future version without expanding the current one" do
+ master = @master_class.new
+ master.update_attributes name: "Single Standard", price: 98
+ master.update_attributes name: "Single Standard", price: 94, valid_from: Date.today+2
+ Timecop.freeze Date.today+1
+ master.versions.last.destroy(expand_previous_version: false).should be_true
+ master.should have_versions %Q{
+ | name | price | created_at | expired_at | valid_from | valid_to | current |
+ | Single Standard | 98 | 2009-11-28 | 2009-11-28 | 2009-11-28 | MAX DATE | |
+ | Single Standard | 98 | 2009-11-28 | | 2009-11-28 | 2009-11-30 | true |
+ | Single Standard | 94 | 2009-11-28 | 2009-11-29 | 2009-11-30 | MAX DATE | |
+ }
+ end
it "allows deleting all versions" do
master = @master_class.new
master.update_attributes name: "Single Standard", price: 98
Please sign in to comment.
Something went wrong with that request. Please try again.