deleting questions from the inbox is now possible

This commit is contained in:
nilsding 2014-11-30 18:05:51 +01:00
parent 05591f51ef
commit ae4952db28
6 changed files with 75 additions and 8 deletions

View File

@ -72,6 +72,27 @@ $(document).on "click", "button[name=ib-answer]", ->
btn.button "reset"
$("textarea[name=ib-answer][data-id=#{iid}]").removeAttr "readonly"
$(document).on "click", "button[name=ib-destroy]", ->
btn = $(this)
btn.button "loading"
iid = btn[0].dataset.ibId
$("textarea[name=ib-answer][data-id=#{iid}]").attr "readonly", "readonly"
$.ajax
url: '/ajax/delete_inbox'
type: 'POST'
data:
id: iid
success: (data, status, jqxhr) ->
if data.success
$("div.inbox-box[data-id=#{iid}]").slideUp()
showNotification data.message, data.success
error: (jqxhr, status, error) ->
console.log jqxhr, status, error
showNotification "An error occurred, a developer should check the console for details", false
complete: (jqxhr, status) ->
btn.button "reset"
$("textarea[name=ib-answer][data-id=#{iid}]").removeAttr "readonly"
$(document).on "click", "button[name=ab-destroy]", ->
btn = $(this)
btn.button "loading"

View File

@ -12,18 +12,43 @@ class Ajax::InboxController < ApplicationController
return
end
answer = Answer.create(content: params[:answer],
user: current_user,
question: inbox.question)
unless current_user.nil?
current_user.increment! :answered_count
begin
inbox.answer params[:answer], current_user
rescue
@status = :err
@message = "An error occurred"
@success = false
return
end
Inbox.destroy inbox.id
@status = :okay
@message = "Successfully answered question."
@success = true
end
def remove
params.require :id
inbox = Inbox.find(params[:id])
unless current_user == inbox.user
@status = :fail
@message = "question not in your inbox"
@success = false
return
end
begin
inbox.remove
rescue
@status = :err
@message = "An error occurred"
@success = false
return
end
@status = :okay
@message = "Successfully deleted question."
@success = true
end
end

View File

@ -1,4 +1,21 @@
class Inbox < ActiveRecord::Base
belongs_to :user
belongs_to :question
def answer(answer, user)
Answer.create(content: answer,
user: user,
question: self.question)
user.increment! :answered_count
self.destroy
end
def remove
unless self.question.user.nil?
self.question.user.decrement! :asked_count
end
self.question.destroy
self.destroy
end
end

View File

@ -0,0 +1 @@
json.partial! 'ajax/shared/status'

View File

@ -14,6 +14,8 @@
%br/
%button.btn.btn-success{name: 'ib-answer', 'data-ib-id' => i.id}
Answer
%button.btn.btn-danger{name: 'ib-destroy', 'data-ib-id' => i.id}
Delete
- if @inbox.empty?

View File

@ -26,6 +26,7 @@ Rails.application.routes.draw do
namespace :ajax do
match '/ask', to: 'question#create', via: :post, as: :ask
match '/answer', to: 'inbox#destroy', via: :post, as: :answer
match '/delete_inbox', to: 'inbox#remove', via: :post, as: :delete_inbox
match '/destroy_answer', to: 'answer#destroy', via: :post, as: :destroy_answer
match '/create_friend', to: 'friend#create', via: :post, as: :create_friend
match '/destroy_friend', to: 'friend#destroy', via: :post, as: :destroy_friend