Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Support for status messages + cancel

Use $.ui.sortable#cancel to move the item back to
its original position on a failed response from
the server.

Additionally, provide a status message with any
error(s) so the user has some feedback.
  • Loading branch information...
commit 77108acd6ace2b047427349f209f904dcc664731 1 parent 05296ab
@scryptmouse authored
View
29 app/assets/javascripts/active_admin/sortable.js.coffee
@@ -20,6 +20,7 @@ window.ActiveAdminSortableEvent = do ->
}
$ ->
+
$('.disclose').bind 'click', (event) ->
$(this).closest('li').toggleClass('mjs-nestedSortable-collapsed').toggleClass('mjs-nestedSortable-expanded')
@@ -43,6 +44,9 @@ $ ->
.disableSelection()
+ status_tag = (message, status_class = 'error') ->
+ "<span class='status_tag #{status_class}'>#{message}</span>"
+
$(".index_as_sortable [data-sortable-type]").each ->
$this = $(@)
if $this.data('sortable-type') == "tree"
@@ -71,21 +75,42 @@ $ ->
toleranceElement: '> div'
isTree: true
startCollapsed: $this.data("start-collapsed")
- update: ->
+ update: (event, ui) ->
$this.nestedSortable("disable")
+
+ $this.find('.status_message').empty()
+
$.ajax
url: $this.data("sortable-url")
type: "post"
data: $this.nestedSortable("serialize")
+ dataType: 'json'
+ beforeSend: (xhr) ->
+ # Avoid 'no element found' error on HTTP 204 in Firefox.
+ xhr.overrideMimeType 'text/plain; charset=x-user-defined'
.always ->
$this.find('.item').each (index) ->
+
if index % 2
$(this).removeClass('odd').addClass('even')
else
$(this).removeClass('even').addClass('odd')
+
$this.nestedSortable("enable")
+
ActiveAdminSortableEvent.trigger('ajaxAlways')
.done ->
ActiveAdminSortableEvent.trigger('ajaxDone')
- .fail ->
+ .fail (xhr) ->
+ response_text = xhr.responseText
+
+ status_message_content = if response_text[0] == '['
+ $.map($.parseJSON(response_text), status_tag).join("")
+ else
+ status_tag response_text
+
+ $(ui.item).find('.status_message').html status_message_content
+
+ $this.sortable('cancel')
+
ActiveAdminSortableEvent.trigger('ajaxFail')
View
18 lib/active_admin/sortable/controller_actions.rb
@@ -31,7 +31,7 @@ def sortable(options = {})
res
end
- errors = []
+ errors = {}
ActiveRecord::Base.transaction do
records.each_with_index do |(record, parent_record), position|
@@ -41,13 +41,25 @@ def sortable(options = {})
record.send "#{options[:parent_method]}=", parent_record
end
- errors << {record.id => record.errors} if !record.save
+ unless record.save
+ record_name = view_context.display_name(record) || record.id
+
+ errors[record_name] = record.errors.full_messages.to_sentence
+ end
end
end
if errors.empty?
- head 204
+ 204, content_type: 'text/plain'
else
+ if errors.length == 1
+ errors = errors.values.first
+ else
+ errors = errors.map do |record_name, message|
+ "#{record_name}: #{message}"
+ end
+ end
+
render json: errors, status: 422
end
end
View
4 lib/active_admin/views/index_as_sortable.rb
@@ -116,9 +116,13 @@ def build_nested_item(item)
h3 :class => "cell left" do
call_method_or_proc_on(item, @label)
end
+
div :class => "cell right" do
build_actions(item)
end
+
+ div :class => "cell right status_message" do
+ end
end
ol do
Please sign in to comment.
Something went wrong with that request. Please try again.