Skip to content

Commit

Permalink
Update for Rails 4.2
Browse files Browse the repository at this point in the history
  • Loading branch information
oriolgual committed Apr 12, 2015
1 parent 70cab10 commit d91db8a
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 15 deletions.
1 change: 1 addition & 0 deletions .ruby-version
@@ -0,0 +1 @@
2.2.1
1 change: 0 additions & 1 deletion .rvmrc

This file was deleted.

8 changes: 5 additions & 3 deletions Readme.md
@@ -1,6 +1,6 @@
#resort [![Build Status](https://secure.travis-ci.org/codegram/resort.png)](http://travis-ci.org/codegram/resort)

Resort provides sorting capabilities to your Rails 3 models.
Resort provides sorting capabilities to your Rails models.

##Install

Expand Down Expand Up @@ -72,8 +72,10 @@ Moreover, now a `product` responds to the following methods:

* `first?` — Returns true if the element is the first of the tree.
* `append_to(other_element)` — Appends the element _after_ another element.
* `next` — Returns the next element in the list
* `previous` — Returns the previous element in the list
* `prepend` — Moves the elemnt to the beggining of the list (sets it as
first).
* `next` — Returns the next element in the list.
* `previous` — Returns the previous element in the list.

And the class Product has a new scope named `ordered` that returns the
products in order.
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/active_record/resort_generator.rb
Expand Up @@ -17,7 +17,7 @@ def self.next_migration_number(dirname)
"%.3d" % (current_migration_number(dirname) + 1)
end
end

desc "Creates a Resort migration."
source_root File.expand_path("../templates", __FILE__)

Expand Down
18 changes: 9 additions & 9 deletions lib/resort.rb
Expand Up @@ -84,7 +84,7 @@ def last_in_order
all.where(:next_id => nil).first
end


# Returns eager-loaded Components in order.
#
# OPTIMIZE: Use IdentityMap when available
Expand All @@ -102,7 +102,7 @@ def ordered
end

raise "Multiple or no first items in the list where found. Consider defining a siblings method" if ordered_elements.length != 1 && elements.length > 0

elements.length.times do
ordered_elements << elements[ordered_elements.last.next_id]
end
Expand Down Expand Up @@ -144,7 +144,7 @@ def include_in_list!
self.class.transaction do
self.lock!
_siblings.count > 0 ? last!\
: prepend
: prepend
end
end

Expand All @@ -156,8 +156,8 @@ def prepend
if _siblings.count > 0
delete_from_list
old_first = _siblings.first_in_order
raise(ActiveRecord::RecordNotSaved) unless self.update_attribute(:next_id, old_first.id)
raise(ActiveRecord::RecordNotSaved) unless old_first.update_attribute(:first, false)
raise(ActiveRecord::RecordNotSaved.new("[Resort] - Couldn't set next_id from previous first element.")) unless self.update_attribute(:next_id, old_first.id)
raise(ActiveRecord::RecordNotSaved.new("[Resort] - Couldn't reset previous firt element")) unless old_first.update_attribute(:first, false)
end
raise(ActiveRecord::RecordNotSaved) unless self.update_attribute(:first, true)
end
Expand All @@ -179,10 +179,10 @@ def append_to(another)
another.lock!
delete_from_list
if self.next_id or (another && another.next_id)
raise(ActiveRecord::RecordNotSaved) unless self.update_attribute(:next_id, another.next_id)
raise(ActiveRecord::RecordNotSaved.new("[Resort] - Couldn't append element")) unless self.update_attribute(:next_id, another.next_id)
end
if another
raise(ActiveRecord::RecordNotSaved) unless another.update_attribute(:next_id, self.id)
raise(ActiveRecord::RecordNotSaved.new("[Resort] - Couldn't set this element to another's next")) unless another.update_attribute(:next_id, self.id)
end
end
end
Expand All @@ -200,8 +200,8 @@ def delete_from_list
raise(ActiveRecord::RecordNotSaved) unless p.update_attribute(:next_id, self.next_id)
end
unless frozen?
self.first = false
self.next = nil
self.first = false
self.next = nil
save!
end
end
Expand Down
2 changes: 1 addition & 1 deletion resort.gemspec
Expand Up @@ -14,7 +14,7 @@ Gem::Specification.new do |s|

s.rubyforge_project = "resort"

s.add_runtime_dependency 'activerecord', '>= 4.0.4'
s.add_runtime_dependency 'activerecord', '~> 4'
s.add_development_dependency 'sqlite3'
s.add_development_dependency 'rspec', '~> 2.14.1'
s.add_development_dependency 'yard'
Expand Down

0 comments on commit d91db8a

Please sign in to comment.