sequel_bitemporal 0.6.7 → 0.6.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -132,11 +132,11 @@ module Sequel
132
132
  super
133
133
  pending_version.errors.each do |key, key_errors|
134
134
  key_errors.each{|error| errors.add key, error}
135
- end if pending_version && !pending_version.valid?
135
+ end if pending_version_holds_changes? && !pending_version.valid?
136
136
  end
137
137
 
138
138
  def pending_or_current_version
139
- pending_version || current_version
139
+ pending_version || current_version || initial_version
140
140
  end
141
141
 
142
142
  def attributes
@@ -145,22 +145,20 @@ module Sequel
145
145
  elsif current_version
146
146
  current_version.values
147
147
  else
148
- {}
148
+ initial_version.values
149
149
  end
150
150
  end
151
151
 
152
152
  def attributes=(attributes)
153
- if attributes_hold_changes? attributes
154
- @pending_version ||= begin
155
- current_attributes = {master_id: id}
156
- current_version.keys.each do |key|
157
- next if excluded_columns.include? key
158
- current_attributes[key] = current_version.send key
159
- end if current_version?
160
- model.version_class.new current_attributes
161
- end
162
- pending_version.set attributes
153
+ @pending_version ||= begin
154
+ current_attributes = {master_id: id}
155
+ current_version.keys.each do |key|
156
+ next if excluded_columns.include? key
157
+ current_attributes[key] = current_version.send key
158
+ end if current_version?
159
+ model.version_class.new current_attributes
163
160
  end
161
+ pending_version.set attributes
164
162
  end
165
163
 
166
164
  def update_attributes(attributes={})
@@ -170,13 +168,13 @@ module Sequel
170
168
 
171
169
  def after_create
172
170
  super
173
- if pending_version
171
+ if pending_version_holds_changes?
174
172
  return false unless save_pending_version
175
173
  end
176
174
  end
177
175
 
178
176
  def before_update
179
- if pending_version
177
+ if pending_version_holds_changes?
180
178
  expire_previous_versions
181
179
  return false unless save_pending_version
182
180
  end
@@ -249,13 +247,14 @@ module Sequel
249
247
  @last_version = nil
250
248
  @current_version_values = nil
251
249
  @pending_version = nil
250
+ @initial_version = nil
252
251
  super
253
252
  end
254
253
 
255
254
  private
256
255
 
257
256
  def prepare_pending_version
258
- return unless pending_version
257
+ return unless pending_version_holds_changes?
259
258
  now = ::Sequel::Plugins::Bitemporal.now
260
259
  point_in_time = ::Sequel::Plugins::Bitemporal.point_in_time
261
260
  pending_version.created_at = point_in_time
@@ -369,10 +368,11 @@ module Sequel
369
368
  !new? && current_version
370
369
  end
371
370
 
372
- def attributes_hold_changes?(attributes)
371
+ def pending_version_holds_changes?
372
+ return false unless pending_version
373
373
  return true unless current_version?
374
374
  @current_version_values = current_version.values
375
- attributes.detect do |key, new_value|
375
+ pending_version.values.detect do |key, new_value|
376
376
  case key
377
377
  when :id, :master_id, :created_at, :expired_at
378
378
  false
@@ -399,6 +399,10 @@ module Sequel
399
399
  Sequel::Plugins::Bitemporal.bitemporal_excluded_columns
400
400
  end
401
401
 
402
+ def initial_version
403
+ @initial_version ||= model.version_class.new
404
+ end
405
+
402
406
  end
403
407
  end
404
408
  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.7"
6
+ s.version = "0.6.8"
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"
@@ -390,15 +390,21 @@ describe "Sequel::Plugins::Bitemporal" do
390
390
  master = @master_class.new
391
391
  master.attributes.should == {}
392
392
  master.pending_version.should be_nil
393
- master.pending_or_current_version.should be_nil
393
+ master.current_version.should be_nil
394
+ master.name.should be_nil
395
+
396
+ master.pending_or_current_version.name.should be_nil
394
397
  master.update_attributes name: "Single Standard", price: 98
395
398
  master.attributes[:name].should == "Single Standard"
396
399
  master.pending_version.should be_nil
397
400
  master.pending_or_current_version.name.should == "Single Standard"
401
+ master.name.should == "Single Standard"
402
+
398
403
  master.attributes = {name: "King Size"}
399
404
  master.attributes[:name].should == "King Size"
400
405
  master.pending_version.should be
401
406
  master.pending_or_current_version.name.should == "King Size"
407
+ master.name.should == "King Size"
402
408
  end
403
409
  it "allows to go back in time" do
404
410
  master = @master_class.new
@@ -313,15 +313,21 @@ describe "Sequel::Plugins::Bitemporal" do
313
313
  master = @master_class.new
314
314
  master.attributes.should == {}
315
315
  master.pending_version.should be_nil
316
- master.pending_or_current_version.should be_nil
316
+ master.current_version.should be_nil
317
+ master.pending_or_current_version.name.should be_nil
318
+ master.name.should be_nil
319
+
317
320
  master.update_attributes name: "Single Standard", price: 98
318
321
  master.attributes[:name].should == "Single Standard"
319
322
  master.pending_version.should be_nil
320
323
  master.pending_or_current_version.name.should == "Single Standard"
324
+ master.name.should == "Single Standard"
325
+
321
326
  master.attributes = {name: "King Size"}
322
327
  master.attributes[:name].should == "King Size"
323
328
  master.pending_version.should be
324
329
  master.pending_or_current_version.name.should == "King Size"
330
+ master.name.should == "King Size"
325
331
  end
326
332
  it "allows to go back in time" do
327
333
  master = @master_class.new
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: sequel_bitemporal
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.6.7
5
+ version: 0.6.8
6
6
  platform: ruby
7
7
  authors:
8
8
  - Joseph HALTER
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-03-07 00:00:00.000000000 Z
13
+ date: 2013-03-11 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: sequel
@@ -125,7 +125,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
125
125
  - !ruby/object:Gem::Version
126
126
  segments:
127
127
  - 0
128
- hash: 186878115490887272
128
+ hash: 4539624897102282475
129
129
  version: '0'
130
130
  none: false
131
131
  required_rubygems_version: !ruby/object:Gem::Requirement
@@ -134,7 +134,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
134
134
  - !ruby/object:Gem::Version
135
135
  segments:
136
136
  - 0
137
- hash: 186878115490887272
137
+ hash: 4539624897102282475
138
138
  version: '0'
139
139
  none: false
140
140
  requirements: []