Created
April 26, 2012 21:06
-
-
Save neotericdesign/2503251 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
def next | |
page = self.class.choose(self, 'rgt >', :first) | |
page ||= self.class.choose(self, 'lft <', :first) | |
end | |
def prev | |
page = self.class.choose(self, 'lft <', :last) | |
page ||= self.class.choose(self, 'rgt >', :last) | |
end | |
class << self | |
def choose(page, position_condition, pick) | |
position_method = position_condition.split.first | |
results = where("#{position_condition} ? AND depth = ? AND parent_id = ?", | |
page.send(position_method.to_sym), | |
page.depth, | |
page.parent_id) | |
.live | |
.in_menu | |
.order(position_method) # ASC is default | |
results.send(pick) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment