From ae4952db28e3cd3451e826c8eea506f10a806b28 Mon Sep 17 00:00:00 2001 From: nilsding Date: Sun, 30 Nov 2014 18:05:51 +0100 Subject: [PATCH] deleting questions from the inbox is now possible --- .../javascripts/application.js.erb.coffee | 21 ++++++++++ app/controllers/ajax/inbox_controller.rb | 41 +++++++++++++++---- app/models/inbox.rb | 17 ++++++++ app/views/ajax/inbox/remove.json.jbuilder | 1 + app/views/inbox/show.html.haml | 2 + config/routes.rb | 1 + 6 files changed, 75 insertions(+), 8 deletions(-) create mode 100644 app/views/ajax/inbox/remove.json.jbuilder diff --git a/app/assets/javascripts/application.js.erb.coffee b/app/assets/javascripts/application.js.erb.coffee index f2e4e380..123af69c 100644 --- a/app/assets/javascripts/application.js.erb.coffee +++ b/app/assets/javascripts/application.js.erb.coffee @@ -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" diff --git a/app/controllers/ajax/inbox_controller.rb b/app/controllers/ajax/inbox_controller.rb index 0f7dcc55..dfffa89e 100644 --- a/app/controllers/ajax/inbox_controller.rb +++ b/app/controllers/ajax/inbox_controller.rb @@ -11,19 +11,44 @@ class Ajax::InboxController < ApplicationController @success = false 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 diff --git a/app/models/inbox.rb b/app/models/inbox.rb index ee9fd450..6d0e835b 100644 --- a/app/models/inbox.rb +++ b/app/models/inbox.rb @@ -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 diff --git a/app/views/ajax/inbox/remove.json.jbuilder b/app/views/ajax/inbox/remove.json.jbuilder new file mode 100644 index 00000000..2c193af4 --- /dev/null +++ b/app/views/ajax/inbox/remove.json.jbuilder @@ -0,0 +1 @@ +json.partial! 'ajax/shared/status' \ No newline at end of file diff --git a/app/views/inbox/show.html.haml b/app/views/inbox/show.html.haml index c42b0813..816c2447 100644 --- a/app/views/inbox/show.html.haml +++ b/app/views/inbox/show.html.haml @@ -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? diff --git a/config/routes.rb b/config/routes.rb index d316dadc..49ed4547 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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