Permalink
Browse files

ActiveRecord::RecordNotSaved fixes and tests

  • Loading branch information...
1 parent 5f43d64 commit 6b9e911eab1264ecf9c39649e40b4fb25142d260 Brian Jones committed Dec 9, 2011
Showing with 20 additions and 9 deletions.
  1. +9 −9 lib/resort.rb
  2. +11 −0 spec/resort_spec.rb
View
@@ -1,4 +1,5 @@
require 'generators/active_record/resort_generator' if defined?(Rails)
+require 'active_record' unless defined?(ActiveRecord)
# # Resort
#
@@ -155,10 +156,10 @@ def prepend
if _siblings.count > 0
delete_from_list
old_first = _siblings.first_in_order
- self.update_attribute(:next_id, old_first.id) || raise(RecordNotSaved)
- old_first.update_attribute(:first, false) || raise(RecordNotSaved)
+ raise(ActiveRecord::RecordNotSaved) unless self.update_attribute(:next_id, old_first.id)
+ raise(ActiveRecord::RecordNotSaved) unless old_first.update_attribute(:first, false)
end
- self.update_attribute(:first, true) || raise(RecordNotSaved)
+ raise(ActiveRecord::RecordNotSaved) unless self.update_attribute(:first, true)
end
end
@@ -178,10 +179,10 @@ def append_to(another)
another.lock!
delete_from_list
if self.next_id or (another && another.next_id)
- self.update_attribute(:next_id, another.next_id) || raise(RecordNotSaved)
+ raise(ActiveRecord::RecordNotSaved) unless self.update_attribute(:next_id, another.next_id)
end
if another
- another.update_attribute(:next_id, self.id) || raise(RecordNotSaved)
+ raise(ActiveRecord::RecordNotSaved) unless another.update_attribute(:next_id, self.id)
end
end
end
@@ -191,12 +192,12 @@ def append_to(another)
def delete_from_list
if first? && self.next
self.next.lock!
- self.next.update_attribute(:first, true) || raise(RecordNotSaved)
+ raise(ActiveRecord::RecordNotSaved) unless self.next.update_attribute(:first, true)
elsif self.previous
self.previous.lock!
p = self.previous
self.previous = nil unless frozen?
- p.update_attribute(:next_id, self.next_id) || raise(RecordNotSaved)
+ raise(ActiveRecord::RecordNotSaved) unless p.update_attribute(:next_id, self.next_id)
end
unless frozen?
self.first = false
@@ -212,7 +213,7 @@ def last?
def last!
self.class.transaction do
self.lock!
- _siblings.last_in_order.update_attribute(:next_id, self.id) || raise(RecordNotSaved)
+ raise(ActiveRecord::RecordNotSaved) unless _siblings.last_in_order.update_attribute(:next_id, self.id)
end
end
@@ -233,5 +234,4 @@ def resort!
end
end
-require 'active_record' unless defined?(ActiveRecord)
ActiveRecord::Base.extend Resort::ClassMethods
View
@@ -187,6 +187,7 @@ module Resort
first.next.name.should == 'Second'
second.next.should be_nil
end
+
end
after do
OrderedList.destroy_all
@@ -299,6 +300,11 @@ module Resort
article4.next.name.should == '1'
end
+ it 'will raise ActiveRecord::RecordNotSaved if update fails' do
+ @article2.should_receive(:update_attribute).and_return(false)
+ expect { @article2.prepend }.to raise_error(ActiveRecord::RecordNotSaved)
+ end
+
context 'when the article is already first' do
it 'does nothing' do
@article1.prepend
@@ -310,6 +316,11 @@ module Resort
end
describe "#append_to" do
+ it 'will raise ActiveRecord::RecordNotSaved if update fails' do
+ @article2.should_receive(:update_attribute).and_return(false)
+ expect { @article2.append_to(@article3) }.to raise_error(ActiveRecord::RecordNotSaved)
+ end
+
context 'appending 1 after 2' do
it "appends the element after another element" do
@article1.append_to(@article2)

0 comments on commit 6b9e911

Please sign in to comment.