moved answering code from inbox_controller.rb to answer_controller.rb

This commit is contained in:
nilsding 2015-01-03 18:40:39 +01:00
parent c4c6cb62d8
commit 7be7de11cd
5 changed files with 48 additions and 34 deletions

View file

@ -71,6 +71,7 @@ $(document).on "click", "button[name=ib-answer]", ->
id: iid
answer: $("textarea[name=ib-answer][data-id=#{iid}]").val()
share: JSON.stringify shareTo
inbox: true
success: (data, status, jqxhr) ->
if data.success
$("div.inbox-box[data-id=#{iid}]").slideUp()

View file

@ -1,4 +1,50 @@
class Ajax::AnswerController < ApplicationController
def create
params.require :id
params.require :answer
params.require :share
params.require :inbox
inbox = (params[:inbox] == 'true')
puts inbox
if inbox
inbox_entry = Inbox.find(params[:id])
unless current_user == inbox_entry.user
@status = :fail
@message = "question not in your inbox"
@success = false
return
end
else
question = Question.find(params[:id])
end
answer = nil
begin
answer = if inbox
inbox_entry.answer params[:answer], current_user
else
current_user.answer question, params[:answer]
end
rescue
@status = :err
@message = "An error occurred"
@success = false
return
end
services = JSON.parse params[:share]
ShareWorker.perform_async(current_user.id, answer.id, services)
@status = :okay
@message = "Successfully answered question."
@success = true
end
def destroy
params.require :answer

View file

@ -21,39 +21,6 @@ class Ajax::InboxController < ApplicationController
inbox.update(new: false)
end
def destroy
params.require :id
params.require :answer
params.require :share
inbox = Inbox.find(params[:id])
unless current_user == inbox.user
@status = :fail
@message = "question not in your inbox"
@success = false
return
end
answer = nil
begin
answer = inbox.answer params[:answer], current_user
rescue
@status = :err
@message = "An error occurred"
@success = false
return
end
services = JSON.parse params[:share]
ShareWorker.perform_async(current_user.id, answer.id, services)
@status = :okay
@message = "Successfully answered question."
@success = true
end
def remove
params.require :id

View file

@ -59,10 +59,10 @@ 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 '/generate_question', to: 'inbox#create', via: :post, as: :generate_question
match '/delete_inbox', to: 'inbox#remove', via: :post, as: :delete_inbox
match '/delete_all_inbox', to: 'inbox#remove_all', via: :post, as: :delete_all_inbox
match '/answer', to: 'answer#create', via: :post, as: :answer
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