Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Complete support for `#reset!`

  • Loading branch information...
commit a0cabe1c5afa6b08a47b9f4acff50c1dee4e593d 1 parent 1d770b9
James Logsdon jlogsdon authored
13 features/reset_approval.feature
... ... @@ -0,0 +1,13 @@
  1 +Feature: Reset Approvals
  2 + In order to allow reseting record creation approval for future-approval.
  3 +
  4 + Background:
  5 + Given a record created with create approval
  6 + And the record is stale
  7 +
  8 + Scenario: a stale record is encountered
  9 + Then it should be stale
  10 +
  11 + Scenario: a stale record is reset
  12 + When I reset the record
  13 + Then it should not be stale
24 features/step_definitions/cucumber_steps.rb
@@ -10,7 +10,17 @@ def support_file_path(file)
10 10 end
11 11 end
12 12
13   -When /^I (approve|reject) the (record|changes?)$/ do |state, type|
  13 +Given /^the record is (stale)$/ do |state|
  14 + case state
  15 + when 'stale'
  16 + @record.title = 'changed'
  17 + sleep(1) # Save will put updated_at in a stale place
  18 + end
  19 +
  20 + @record.save_without_approval!
  21 +end
  22 +
  23 +When /^I (approve|reject|reset) the (record|changes?)$/ do |state, type|
14 24 begin
15 25 method = "#{state}!".to_sym
16 26
@@ -44,9 +54,17 @@ def support_file_path(file)
44 54 @update = table.rows_hash
45 55 end
46 56
47   -Then /^it should be (pending|approved|rejected)$/ do |state|
  57 +Then /^it should (not )?be (pending|approved|rejected|stale)$/ do |invert, state|
48 58 method = "#{state}?".to_sym
49   - @record.send(method).should be_true
  59 + record = state == 'stale' ? @record.approval : @record
  60 +
  61 + unless invert == 'not '
  62 + record.send(method).should be_true
  63 + else
  64 + record.send(method).should be_false
  65 + end
  66 +
  67 + @record.reload
50 68 end
51 69
52 70 Then /^it should have (no )?pending changes$/ do |empty|
2  lib/acts_as_approvable/approval.rb
@@ -180,6 +180,8 @@ def reset!
180 180
181 181 item.set_approval_state('pending')
182 182 item.save_without_approval!
  183 +
  184 + state_will_change! # Force an update to the record
183 185 update_attributes!(:state => 'rejected')
184 186 end
185 187
5 lib/acts_as_approvable/model/create_instance_methods.rb
@@ -69,6 +69,11 @@ def reject!
69 69 approval.reject!
70 70 end
71 71
  72 + def reset!
  73 + return unless approvable_on?(:create) && approval.present?
  74 + approval.reset!
  75 + end
  76 +
72 77 private
73 78 def approvable_create?
74 79 approvals_enabled? and approvable_on?(:create)
12 spec/acts_as_approvable/approval_spec.rb
@@ -201,13 +201,19 @@
201 201 it 'should not raise an InvalidTransition error' do
202 202 expect { subject.reset! }.not_to raise_error(ActsAsApprovable::Error::InvalidTransition)
203 203 end
  204 +
  205 + it 'should save even if no values change' do
  206 + subject.stub(:item => DefaultApprovable.new)
  207 + subject.should_receive(:save!).and_return(true)
  208 + subject.reset!
  209 + end
204 210 end
205 211 end
206 212
207 213 context 'when the approval is unlocked' do
208 214 before(:each) do
209 215 @item = DefaultApprovable.without_approval { |m| m.create }
210   - subject.stub(:locked? => false, :created_at => Time.now, :item => @item)
  216 + subject.stub(:locked? => false, :updated_at => Time.now, :item => @item)
211 217 @item.stub(:updated_at => Time.now)
212 218 end
213 219
@@ -252,7 +258,7 @@
252 258
253 259 context 'when the approval is newer than the last update' do
254 260 before(:each) do
255   - subject.stub(:created_at => @item.updated_at + 60)
  261 + subject.stub(:updated_at => @item.updated_at + 60)
256 262 end
257 263
258 264 it { should_not be_stale }
@@ -261,7 +267,7 @@
261 267
262 268 context 'when the approval is older than the last update' do
263 269 before(:each) do
264   - subject.stub(:created_at => @item.updated_at - 60)
  270 + subject.stub(:updated_at => @item.updated_at - 60)
265 271 end
266 272
267 273 it { should be_stale }
20 spec/acts_as_approvable/model/create_instance_methods_spec.rb
@@ -145,5 +145,25 @@
145 145 subject.reject!
146 146 end
147 147 end
  148 +
  149 + describe '#reset!' do
  150 + it 'proxies to the approval record for approval' do
  151 + subject.should_receive(:approval)
  152 + subject.reset!
  153 + end
  154 +
  155 + context 'when the approval is stale' do
  156 + before(:each) do
  157 + subject.approval.class.record_timestamps = false
  158 + subject.approval.update_attribute(:updated_at, subject.approval.updated_at - 1)
  159 + subject.approval.class.record_timestamps = true
  160 + end
  161 +
  162 + it 'puts the approval back to fresh' do
  163 + subject.reset!
  164 + subject.approval.should be_fresh
  165 + end
  166 + end
  167 + end
148 168 end
149 169 end

0 comments on commit a0cabe1

Please sign in to comment.
Something went wrong with that request. Please try again.