Skip to content

Instantly share code, notes, and snippets.

@gudata
Created July 18, 2012 14:41
Show Gist options
  • Save gudata/3136587 to your computer and use it in GitHub Desktop.
Save gudata/3136587 to your computer and use it in GitHub Desktop.
Easy implementation of nestedSortable with rails preserving the sort order http://mjsarfatti.com/sandbox/nestedSortable/
- class_for_root_only = ((root_ol rescue true) ? 'sortable' : @first_call = '')
ol class=class_for_root_only data-sort-url=sort_website_pages_path(@website)
li id="list_#{page.id}"
.handle
= link_to page.name, edit_website_page_path(@website, page)
'
= active_icon(page, :published)
.btn-group.pull-right
= link_to t('View'), website_page_path(@website, page), :class => 'btn btn-mini'
= link_to t('.edit', :default => t("helpers.links.edit")), edit_website_page_path(@website, page), :class => 'btn btn-mini'
= link_to t('.destroy', :default => t("helpers.links.destroy")), website_page_path(@website, page), :method => :delete, :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?'))}, :class => 'pull-right btn btn-mini btn-danger'
- if page.children.size > 0
= render :partial => 'page', :collection => page.children.order('position asc'), :locals => {:root_ol => false}
= render :partial => 'page', :collection => @pages
save_page_list=(event, ui)->
post_path = $(@).data('sort-url')
arraied = $(@).nestedSortable('serialize', {'key': 'page_ids'} )
$.post(post_path, arraied)
$('ol.sortable').nestedSortable({
disableNesting:'no-nest',
forcePlaceholderSize:true,
handle:'div.handle',
helper: 'clone',
items:'li',
maxLevels:5,
opacity:.6,
placeholder:'placeholder',
revert:250,
tabSize:25,
tolerance:'pointer',
toleranceElement:'>div',
update: save_page_list,
})
def sort
Page.transaction do
pages_order = params[:page_ids].keys
position = 10
pages_hash = Page.find(pages_order).inject({}){|hash, page| hash[page.id.to_s] = page; hash}
pages_order.each do |page_id|
page = pages_hash[page_id]
Rails.logger.debug("page #{page_id}")
Rails.logger.debug("..moving to: #{params[:page_ids][page_id]}")
if params[:page_ids][page_id] == 'null'
page.move_to_root
else
page.parent_id = params[:page_ids][page_id].to_i
end
position = page.position = position + 10
page.save
end
end
render :nothing => true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment