     @hamBtn.addClass('disabled').removeClass('btn-warning')
     @deleteBtn.addClass('disabled').removeClass('btn-danger')
 
-  stopEvent: (event) ->
-    event.stopPropagation()
-    event.preventDefault()
-
-  getChecked: () => @element.find("input:checkbox:checked[data-checkbox='child']")
-  getId:      (c) => $(c).data('id')
-  getIds:     () => $.map(@getChecked(), (c) => @getId(c))
-
 $.fn.actsAsMessagesTable = () ->
   @each ->
-    new MessagesTable($(this))
+    checkboxable = new Checkboxable($(this))
+    new MessagesTable($(this), checkboxable)
 
 jQuery ->
   $("table.messages.js-checkboxable").each ->

class @MessagesTable

  constructor: (element) ->
    @element          = $(element)
    @checkboxes       = @element.find("input:checkbox")
    @deleteBtn        = @element.find('.js-delete')
    @spamBtn          = @element.find('.js-spam')
    @hamBtn           = @element.find('.js-ham')
    @setBindings()

  setBindings: () =>
    @checkboxes.on('change', @update)
    @deleteBtn.on('click', @delete)
    @spamBtn.on('click', @spam)
    @hamBtn.on('click', @ham)

  update: () =>
    if @getChecked().length then @enableActions() else @disableActions()

  delete: (event) =>
    @stopEvent(event)
    $.ajax(
      url: "/admin/messages/#{@getIds()}"
      type: 'DELETE'
    )

  spam: (event) =>
    @stopEvent(event)

      type: "POST"
      dataType: "JSON"
      url: "/admin/messages/#{@getIds()}/ham"
      success: @removeMessages
    ).always(@notify)

  removeMessages: () =>
    $.each @getIds(), (i, message_id) => @removeMessage(message_id)

  removeMessage: (message_id) ->
    $("tr#message_#{message_id}").remove();
