Created
January 12, 2012 17:25
-
-
Save sekrett/1601872 to your computer and use it in GitHub Desktop.
Positioned module
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
# Example: | |
# | |
# class Page < ActiveRecord::Base | |
# include Positioned | |
# end | |
module Positioned | |
def self.included model_class | |
model_class.class_eval do | |
before_save :set_position | |
end | |
end | |
protected | |
def set_position | |
self.position ||= 1 + eval("#{self.class.to_s}.maximum(:position)").to_i | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Now available as a gem: https://github.com/sekrett/simple_position