Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raedatoui/14a1aad5f9a4a3feae42 to your computer and use it in GitHub Desktop.
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
# 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')
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