'
extractAndInsertForm formPath, fieldset
init_polymorphic_sortable = ->
elems = $('.polymorphic_has_many_container[data-sortable]:not(.ui-sortable)')
elems.sortable
axis: 'y'
items: '> fieldset',
handle: '> ol > .handle',
stop: recompute_positions
elems.each recompute_positions
# Removes relations if id or type is not specified
# For example when user clicked add relation button, but didn't selected type
stripEmptyRelations = ->
$('.polymorphic_has_many_fields input:hidden').each ->
if $(@).val() == ""
$(@).parents('.polymorphic_has_many_fields').remove()
recompute_positions = (parent)->
parent = if parent instanceof jQuery then parent else $(@)
input_name = parent.data 'sortable'
position = parseInt(parent.data('sortable-start') || 0, 10)
parent.children('fieldset').each ->
# We ignore nested inputs, so when defining your has_many, be sure to keep
# your sortable input at the root of the has_many block.
destroy_input = $(@).find "> ol > .input > :input[name$='[_destroy]']"
sortable_input = $(@).find "> ol > .input > :input[name$='[#{input_name}]']"
if sortable_input.length
sortable_input.val if destroy_input.is ':checked' then '' else position++
window.extractAndInsertForm = (url, target)->
target = $ target
$.get url, (data)->
elements = $(data)
form = $('#main_content form', elements).first()
$(form).find('.actions').remove()
$(form).on 'submit', -> return false
target.prepend form
container = $(target).closest '.polymorphic_has_many_container'
container.trigger "polymorphic_has_many_form:inserted", [form]
window.loadErrors = (target) ->
$(target).off('ajax:success') # unbind successfull action for json form
$(target).trigger('submit.rails').on 'ajax:success', (event, data, result) ->
# duplicates method above. refactor using callbacks
elements = $(data)
form = $('#main_content form', elements).first()
$(form).find('.actions').remove()
$(form).on 'submit', -> return false
$(target).replaceWith(form)
window.remoteSubmit = (target, callback)->
$(target).data('remote', true)
$(target).removeAttr('novalidate')
action = $(target).attr('action')
$(target).find("input[type=file]").remove()
# we gonna burn in hell for that
# perhaps we can use ajax:before callback
# to set type json
action_with_json = action + '.json'
$(target).attr('action', action_with_json)
# unbind callbacks action for form if it was submitted before
$(target).off('ajax:success').off('ajax:aborted:file').off('ajax:error')
$(target).trigger('submit.rails')
.on 'ajax:aborted:file', (inputs) ->
false
.on 'ajax:error', (event, response, status)->
$(target).attr('action', action)
if response.status == 422
loadErrors(target)
.on 'ajax:success', (event, object, status, response) ->
$(target).attr('action', action)
unless $(target).next().find('input:first').val() # create
$(target).next().find('input:first').val(object.id)
# replace new form with edit form
# to update form method to PATCH and form action
url = "#{action}/#{object.id}/edit"
extractAndInsertForm(url, $(target).parent('fieldset'))
$(target).remove()
callback()