From 8dff4462cfcd43f44e10cc76d171349826715c0f Mon Sep 17 00:00:00 2001 From: nilsding Date: Wed, 26 Nov 2014 17:05:46 +0100 Subject: [PATCH] added (ajax) answer controller --- app/controllers/ajax/answer_controller.rb | 37 +++++++++++++++++++++ app/views/ajax/answer/destroy.json.jbuilder | 1 + config/routes.rb | 1 + 3 files changed, 39 insertions(+) create mode 100644 app/controllers/ajax/answer_controller.rb create mode 100644 app/views/ajax/answer/destroy.json.jbuilder diff --git a/app/controllers/ajax/answer_controller.rb b/app/controllers/ajax/answer_controller.rb new file mode 100644 index 00000000..245fa12f --- /dev/null +++ b/app/controllers/ajax/answer_controller.rb @@ -0,0 +1,37 @@ +class Ajax::AnswerController < ApplicationController + def destroy + params.require :answer + + unless current_user.nil? + current_user.increment! :asked_count unless params[:anonymousQuestion] == 'true' + end + + answer = Answer.find(params[:answer]) + + unless answer.user == current_user || privileged? + @status = :nopriv + @message = "check yuor privlegs" + @success = false + return + end + + answer.user.decrement! :answered_count + Inbox.create!(user: answer.user, question: answer.question, new: true) + answer.destroy + + @status = :okay + @message = "Successfully deleted answer." + @success = true + end + + private + + # TODO: + def privileged? + if current_user && current_user.admin? + true + else + false + end + end +end diff --git a/app/views/ajax/answer/destroy.json.jbuilder b/app/views/ajax/answer/destroy.json.jbuilder new file mode 100644 index 00000000..2c193af4 --- /dev/null +++ b/app/views/ajax/answer/destroy.json.jbuilder @@ -0,0 +1 @@ +json.partial! 'ajax/shared/status' \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 093fe72a..65a6b651 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 '/destroy_answer', to: 'answer#destroy', via: :post, as: :destroy_answer end match '/inbox', to: 'inbox#show', via: 'get'