Permalink
Browse files

Complete support for `#reset!`

  • Loading branch information...
1 parent 1d770b9 commit a0cabe1c5afa6b08a47b9f4acff50c1dee4e593d @jlogsdon jlogsdon committed Mar 15, 2012
@@ -0,0 +1,13 @@
+Feature: Reset Approvals
+ In order to allow reseting record creation approval for future-approval.
+
+ Background:
+ Given a record created with create approval
+ And the record is stale
+
+ Scenario: a stale record is encountered
+ Then it should be stale
+
+ Scenario: a stale record is reset
+ When I reset the record
+ Then it should not be stale
@@ -10,7 +10,17 @@ def support_file_path(file)
end
end
-When /^I (approve|reject) the (record|changes?)$/ do |state, type|
+Given /^the record is (stale)$/ do |state|
+ case state
+ when 'stale'
+ @record.title = 'changed'
+ sleep(1) # Save will put updated_at in a stale place
+ end
+
+ @record.save_without_approval!
+end
+
+When /^I (approve|reject|reset) the (record|changes?)$/ do |state, type|
begin
method = "#{state}!".to_sym
@@ -44,9 +54,17 @@ def support_file_path(file)
@update = table.rows_hash
end
-Then /^it should be (pending|approved|rejected)$/ do |state|
+Then /^it should (not )?be (pending|approved|rejected|stale)$/ do |invert, state|
method = "#{state}?".to_sym
- @record.send(method).should be_true
+ record = state == 'stale' ? @record.approval : @record
+
+ unless invert == 'not '
+ record.send(method).should be_true
+ else
+ record.send(method).should be_false
+ end
+
+ @record.reload
end
Then /^it should have (no )?pending changes$/ do |empty|
@@ -180,6 +180,8 @@ def reset!
item.set_approval_state('pending')
item.save_without_approval!
+
+ state_will_change! # Force an update to the record
update_attributes!(:state => 'rejected')
end
@@ -69,6 +69,11 @@ def reject!
approval.reject!
end
+ def reset!
+ return unless approvable_on?(:create) && approval.present?
+ approval.reset!
+ end
+
private
def approvable_create?
approvals_enabled? and approvable_on?(:create)
@@ -201,13 +201,19 @@
it 'should not raise an InvalidTransition error' do
expect { subject.reset! }.not_to raise_error(ActsAsApprovable::Error::InvalidTransition)
end
+
+ it 'should save even if no values change' do
+ subject.stub(:item => DefaultApprovable.new)
+ subject.should_receive(:save!).and_return(true)
+ subject.reset!
+ end
end
end
context 'when the approval is unlocked' do
before(:each) do
@item = DefaultApprovable.without_approval { |m| m.create }
- subject.stub(:locked? => false, :created_at => Time.now, :item => @item)
+ subject.stub(:locked? => false, :updated_at => Time.now, :item => @item)
@item.stub(:updated_at => Time.now)
end
@@ -252,7 +258,7 @@
context 'when the approval is newer than the last update' do
before(:each) do
- subject.stub(:created_at => @item.updated_at + 60)
+ subject.stub(:updated_at => @item.updated_at + 60)
end
it { should_not be_stale }
@@ -261,7 +267,7 @@
context 'when the approval is older than the last update' do
before(:each) do
- subject.stub(:created_at => @item.updated_at - 60)
+ subject.stub(:updated_at => @item.updated_at - 60)
end
it { should be_stale }
@@ -145,5 +145,25 @@
subject.reject!
end
end
+
+ describe '#reset!' do
+ it 'proxies to the approval record for approval' do
+ subject.should_receive(:approval)
+ subject.reset!
+ end
+
+ context 'when the approval is stale' do
+ before(:each) do
+ subject.approval.class.record_timestamps = false
+ subject.approval.update_attribute(:updated_at, subject.approval.updated_at - 1)
+ subject.approval.class.record_timestamps = true
+ end
+
+ it 'puts the approval back to fresh' do
+ subject.reset!
+ subject.approval.should be_fresh
+ end
+ end
+ end
end
end

0 comments on commit a0cabe1

Please sign in to comment.