Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save raedatoui/14a1aad5f9a4a3feae42 to your computer and use it in GitHub Desktop.
Save raedatoui/14a1aad5f9a4a3feae42 to your computer and use it in GitHub Desktop.

Revisions

  1. raedatoui revised this gist Aug 16, 2014. No changes.
  2. raedatoui revised this gist Aug 16, 2014. No changes.
  3. raedatoui renamed this gist Aug 16, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. raedatoui revised this gist Aug 16, 2014. 2 changed files with 50 additions and 27 deletions.
    44 changes: 27 additions & 17 deletions activeadmin_sortable.js.coffee
    Original file line number Diff line number Diff line change
    @@ -2,30 +2,40 @@
    #
    # 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
    #
    #
    #
    # 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') +
    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: 'script'
    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)

    # 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')
    if $('body.admin_profiles.index').length
    enableSort('profiles', '#index_table_profiles tbody')
    33 changes: 23 additions & 10 deletions duck.rb
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,30 @@
    # admin/duck.rb
    # Duck is the Model this particular file refers to.
    # Ref. ActiveAdmin documentation for more.
    # http://activeadmin.info/
    ActiveAdmin.register Profile do

    ActiveAdmin.register Duck do
    config.sort_order = 'position_asc'
    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[:story].each_with_index do |id, index|
    Duck.update_all(['position=?', index+1], ['id=?', id])
    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 :nothing => true
    render text: content.to_html
    end

    ...

    end
  5. @robertjwhitney robertjwhitney revised this gist Jul 3, 2012. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions activeadmin_sortable.js.coffee
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # 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
    #
  6. @robertjwhitney robertjwhitney revised this gist Jul 3, 2012. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions activeadmin_sortable.js.coffee
    Original file line number Diff line number Diff line change
    @@ -9,8 +9,8 @@
    #
    sendSortRequestOfModel = (model_name) ->
    formData = $('#' + model_name + ' tbody').sortable('serialize')
    formData += "&" + $('meta[name=csrf-param]').attr("content") +
    "=" + encodeURIComponent($('meta[name=csrf-token]').attr("content"))
    formData += '&' + $('meta[name=csrf-param]').attr('content') +
    '=' + encodeURIComponent($('meta[name=csrf-token]').attr('content'))
    $.ajax
    type: 'post'
    data: formData
    @@ -21,9 +21,9 @@ sendSortRequestOfModel = (model_name) ->
    #
    jQuery ($) ->
    if $('body.admin_ducks.index').length
    $( "#ducks tbody" ).disableSelection()
    $( "#ducks tbody" ).sortable
    $( '#ducks tbody' ).disableSelection()
    $( '#ducks tbody' ).sortable
    axis: 'y'
    cursor: 'move'
    update: (event, ui) ->
    sendSortRequestOfModel("ducks")
    sendSortRequestOfModel('ducks')
  7. @robertjwhitney robertjwhitney revised this gist Jul 3, 2012. 2 changed files with 3 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion activeadmin_sortable.js.coffee
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    # ActiveAdmin already includes the necessary jquery in active_admin/base,
    # so just add this to javascripts/active_admin.js
    # so just add this to javascripts/active_admin.js after //= require active_admin/base
    #
    #
    # Serialize and Sort
    #
    1 change: 1 addition & 0 deletions adminduck.rb → duck.rb
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    # admin/duck.rb
    # Duck is the Model this particular file refers to.
    # Ref. ActiveAdmin documentation for more.
    # http://activeadmin.info/
  8. @robertjwhitney robertjwhitney created this gist Jul 3, 2012.
    28 changes: 28 additions & 0 deletions activeadmin_sortable.js.coffee
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    # ActiveAdmin already includes the necessary jquery in active_admin/base,
    # so just add this to javascripts/active_admin.js
    #
    # 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")
    16 changes: 16 additions & 0 deletions adminduck.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    # 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