Created
July 13, 2011 22:37
-
-
Save mleszcz/1081491 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module OrderedModel | |
def up | |
self.class.update_all 'ord = ord + 1', "ord = #{self.ord-1}" | |
update_attribute :ord, self.ord-1 | |
end | |
def down | |
self.class.update_all 'ord = ord - 1', "ord = #{self.ord+1}" | |
update_attribute :ord, self.ord+1 | |
end | |
def set_ord | |
self.class.update_all 'ord = ord - 1', "ord > #{self.ord}" | |
end | |
end | |
class Gallery < ActiveRecord::Base | |
include OrderedModel | |
has_many :photos | |
before_destroy :set_ord | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment