Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #8 from bjones/AR_3.1

Support activerecord 3.1
  • Loading branch information...
commit 5adf0235f51483e5375fdd97886fd29dea352632 2 parents 303bad0 + 6b9e911
@txus txus authored
View
2  .rvmrc
@@ -1 +1 @@
-rvm --create use ruby-1.9.2@resort
+rvm --create use ruby-1.9.3-p0@resort
View
9 Gemfile
@@ -1,3 +1,12 @@
source "http://rubygems.org"
gemspec
+
+#group :development, :test do
+# # For debugging under ruby 1.9 special gems are needed
+# gem 'ruby-debug19', :platform => :mri
+# # See http://blog.wyeworks.com/2011/11/1/ruby-1-9-3-and-ruby-debug
+# gem 'ruby-debug-base19', '>=0.11.26'
+# gem 'linecache19', '>=0.5.13'
+#end
+
View
26 lib/resort.rb
@@ -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)
- old_first.update_attribute(:first, false)
+ 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(ActiveRecord::RecordNotSaved) unless self.update_attribute(:first, true)
end
end
@@ -177,8 +178,12 @@ def append_to(another)
return if another.next_id == id
another.lock!
delete_from_list
- self.update_attribute(:next_id, another.next_id) if self.next_id or (another && another.next_id)
- another.update_attribute(:next_id, self.id) if another
+ if self.next_id or (another && another.next_id)
+ raise(ActiveRecord::RecordNotSaved) unless self.update_attribute(:next_id, another.next_id)
+ end
+ if another
+ raise(ActiveRecord::RecordNotSaved) unless another.update_attribute(:next_id, self.id)
+ end
end
end
@@ -187,16 +192,16 @@ def append_to(another)
def delete_from_list
if first? && self.next
self.next.lock!
- self.next.update_attribute(:first, true)
+ raise(ActiveRecord::RecordNotSaved) unless self.next.update_attribute(:first, true)
elsif self.previous
self.previous.lock!
- self.previous.update_attribute(:next_id, self.next_id)
+ p = self.previous
+ self.previous = nil unless frozen?
+ raise(ActiveRecord::RecordNotSaved) unless p.update_attribute(:next_id, self.next_id)
end
-
unless frozen?
self.first = false
self.next = nil
- self.previous = nil
save!
end
end
@@ -208,7 +213,7 @@ def last?
def last!
self.class.transaction do
self.lock!
- _siblings.last_in_order.update_attribute(:next_id, self.id)
+ raise(ActiveRecord::RecordNotSaved) unless _siblings.last_in_order.update_attribute(:next_id, self.id)
end
end
@@ -229,5 +234,4 @@ def resort!
end
end
-require 'active_record' unless defined?(ActiveRecord)
ActiveRecord::Base.extend Resort::ClassMethods
View
3  resort.gemspec
@@ -14,7 +14,8 @@ Gem::Specification.new do |s|
s.rubyforge_project = "resort"
- s.add_runtime_dependency 'activerecord', '~> 3.0.5'
+ s.add_runtime_dependency 'activerecord', '>= 3.0.5', '< 3.2'
+# s.add_runtime_dependency 'activerecord', '~> 3.0.5'
s.add_development_dependency 'sqlite3'
s.add_development_dependency 'rspec', '~> 2.5.0'
s.add_development_dependency 'yard'
View
11 spec/resort_spec.rb
@@ -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)
View
6 spec/spec_helper.rb
@@ -1,6 +1,12 @@
require 'rspec'
module Rails
+ class << self
+ # 3.0 defaults this, 3.1 does not
+ def application
+ "application"
+ end
+ end
end
require 'resort'
Please sign in to comment.
Something went wrong with that request. Please try again.