Skip to content

Instantly share code, notes, and snippets.

@JeanMertz
Created April 27, 2011 19:47
Show Gist options
  • Save JeanMertz/945027 to your computer and use it in GitHub Desktop.
Save JeanMertz/945027 to your computer and use it in GitHub Desktop.
update_positions.rb
#PagesController
def update
@page = current_account.pages.find(params[:id], :readonly => false)
@page.update_attributes(params[:page])
end
#Page
before_update :change_positions, :if => :position_changed?
def change_positions
# Update position of all other pages
if position_was < self.position
a = position_was
b = self.position
else
a = self.position
b = position_was
end
pages = account.pages.where(:id.not_eq => id_was,
:parent_id => parent_id_was,
:position => a..b)#.order(:position)
pages.each do |page|
page.update_attribute(:position, page.position + 1) if a == self.position
page.update_attribute(:position, page.position - 1) if a == position_was
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment