Skip to content

Instantly share code, notes, and snippets.

@nfriend21
Created June 8, 2015 14:13
Show Gist options
  • Save nfriend21/a377f0e6790e0c33b65f to your computer and use it in GitHub Desktop.
Save nfriend21/a377f0e6790e0c33b65f to your computer and use it in GitHub Desktop.
SiteManager::PagesController < ApplicationController
end
def sort_children(element,dbitem)
+ tries = 3
prevchild = nil
element['children'].each do |child|
- childitem = Page.find(child['id'])
- prevchild.nil? ? childitem.move_to_child_of(dbitem) : childitem.move_to_right_of(prevchild)
- sort_children(child, childitem) unless child['children'].nil?
- prevchild = childitem
- childitem.touch
+ begin
+ self.transaction do
+ childitem = Page.find(child['id'])
+ prevchild.nil? ? childitem.move_to_child_of(dbitem) : childitem.move_to_right_of(prevchild)
+ sort_children(child, childitem) unless child['children'].nil?
+ prevchild = childitem
+ childitem.touch
+ end
+ rescue ActiveRecord::StatementInvalid => e
+ tries -= 1
+ if retries > 0
+ logger.error "Error sorting Pages in SiteManager. Retry #{3-tries}"
+ sleep 0.2
+ retry
+ else
+ raise e
+ end
+ end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment