Forked from robertjwhitney/activeadmin_sortable.js.coffee
Last active
August 29, 2015 14:05
-
-
Save raedatoui/14a1aad5f9a4a3feae42 to your computer and use it in GitHub Desktop.
Same concept, and make the collection_action return HTML markup to reflect the newly sorted items. Requires Nokogiri for parsing the html
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
# http://stackoverflow.com/a/8936202 | |
# | |
# ActiveAdmin already includes the necessary jquery in active_admin/base, | |
# so just add this to javascripts/active_admin.js after //= require active_admin/base | |
# | |
# | |
# Serialize and Sort | |
# | |
# model_name - you guessed it, the name of the model we are calling sort on. | |
# This is the actual variable name, no need to change it. | |
# | |
sendSortRequestOfModel = (model_name, tbody) -> | |
formData = $(tbody).sortable('serialize') | |
formData += '&' + $('meta[name=csrf-param]').attr('content') + | |
'=' + encodeURIComponent($('meta[name=csrf-token]').attr('content')) | |
content = $(tbody).html() | |
formData += '&markup=' + encodeURIComponent(content) | |
formData += '&utf8=✓' | |
$.ajaxSetup({ scriptCharset: "utf-8" ,contentType: "application/x-www-form-urlencoded; charset=UTF-8" }) | |
$.ajax | |
type: 'post' | |
data: formData | |
dataType: 'html' | |
contentType: 'application/x-www-form-urlencoded; charset=UTF-8' | |
url: '/admin/' + model_name + '/sort' | |
success: (data, textStatus, jqXHR) -> | |
$(tbody).html data | |
enableSort = (model_name, tbody) -> | |
$(tbody).disableSelection() | |
$(tbody).sortable | |
axis: 'y' | |
cursor: 'move' | |
update: (event, ui) -> | |
sendSortRequestOfModel(model_name, tbody) | |
jQuery ($) -> | |
if $('body.admin_profiles.index').length | |
enableSort('profiles', '#index_table_profiles tbody') |
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
ActiveAdmin.register Profile do | |
permit_params :name, :title, :image, :bio, :quote, :featured, :thumb, :second_col, :display_order | |
config.sort_order = "display_order_asc" | |
collection_action :sort, :method => :post do | |
params[:profile].each_with_index do |id, index| | |
Profile.update_all(['display_order=?', index+1], ['id=?', id]) | |
end | |
content = Nokogiri::HTML(params[:markup], nil, 'utf-8') | |
rows = content.css('tr') | |
counter = 1 | |
rows.each do |row| | |
display_order_col = row.at_css 'td.col-display_order' | |
if display_order_col | |
display_order_col.content = counter | |
end | |
row['class'] = counter % 2 == 0 ? 'even' : 'odd' | |
counter = counter+1 | |
end | |
render text: content.to_html | |
end | |
... | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment