Created
July 3, 2012 03:37
-
-
Save robertjwhitney/3037442 to your computer and use it in GitHub Desktop.
Simple drag/drop reordering of records in an ActiveAdmin view table
This file contains 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) -> | |
formData = $('#' + model_name + ' tbody').sortable('serialize') | |
formData += '&' + $('meta[name=csrf-param]').attr('content') + | |
'=' + encodeURIComponent($('meta[name=csrf-token]').attr('content')) | |
$.ajax | |
type: 'post' | |
data: formData | |
dataType: 'script' | |
url: '/admin/' + model_name + '/sort' | |
# Don't forget we are sorting Duck, so ducks refers specifically to that. | |
# | |
jQuery ($) -> | |
if $('body.admin_ducks.index').length | |
$( '#ducks tbody' ).disableSelection() | |
$( '#ducks tbody' ).sortable | |
axis: 'y' | |
cursor: 'move' | |
update: (event, ui) -> | |
sendSortRequestOfModel('ducks') |
This file contains 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
# admin/duck.rb | |
# Duck is the Model this particular file refers to. | |
# Ref. ActiveAdmin documentation for more. | |
# http://activeadmin.info/ | |
ActiveAdmin.register Duck do | |
config.sort_order = 'position_asc' | |
... | |
collection_action :sort, :method => :post do | |
params[:story].each_with_index do |id, index| | |
Duck.update_all(['position=?', index+1], ['id=?', id]) | |
end | |
render :nothing => true | |
end | |
end |
You are the man. Great solution.
In case someone else encounters this and considers implementing this for has_many
- this is already done:
activeadmin/activeadmin#2656 (comment)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
works like a charm - thnx!