2014-11-10 12:56:30 -08:00
|
|
|
class Ajax::QuestionController < ApplicationController
|
2015-04-18 15:42:58 -07:00
|
|
|
include MarkdownHelper
|
|
|
|
|
2015-05-08 16:31:31 -07:00
|
|
|
rescue_from(ActionController::ParameterMissing) do |param_miss_ex|
|
|
|
|
@status = :parameter_error
|
|
|
|
@message = "#{param_miss_ex.param.capitalize} is required"
|
|
|
|
@success = false
|
|
|
|
render partial: "ajax/shared/status"
|
|
|
|
end
|
|
|
|
|
2015-04-25 18:36:25 -07:00
|
|
|
def destroy
|
|
|
|
params.require :question
|
|
|
|
|
|
|
|
question = Question.find params[:question]
|
|
|
|
if question.nil?
|
|
|
|
@status = :not_found
|
|
|
|
@message = "Question does not exist"
|
|
|
|
@success = false
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2015-04-25 18:39:51 -07:00
|
|
|
if not (current_user.mod? or question.user == current_user)
|
|
|
|
@status = :not_authorized
|
|
|
|
@message = "You are not allowed to delete this question"
|
|
|
|
@success = false
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2015-04-25 18:36:25 -07:00
|
|
|
question.destroy!
|
|
|
|
|
|
|
|
@status = :okay
|
|
|
|
@message = "Successfully deleted question."
|
|
|
|
@success = true
|
|
|
|
end
|
|
|
|
|
2014-11-10 12:56:30 -08:00
|
|
|
def create
|
|
|
|
params.require :question
|
|
|
|
params.require :anonymousQuestion
|
|
|
|
params.require :rcpt
|
|
|
|
|
2014-12-08 06:34:37 -08:00
|
|
|
begin
|
|
|
|
question = Question.create!(content: params[:question],
|
|
|
|
author_is_anonymous: params[:anonymousQuestion],
|
|
|
|
user: current_user)
|
|
|
|
rescue ActiveRecord::RecordInvalid
|
|
|
|
@status = :rec_inv
|
|
|
|
@message = "Your question is too long."
|
|
|
|
@success = false
|
|
|
|
return
|
|
|
|
end
|
2014-11-10 12:56:30 -08:00
|
|
|
|
2014-11-10 22:18:34 -08:00
|
|
|
unless current_user.nil?
|
|
|
|
current_user.increment! :asked_count unless params[:anonymousQuestion] == 'true'
|
|
|
|
end
|
|
|
|
|
2014-12-07 11:13:45 -08:00
|
|
|
if params[:rcpt] == 'followers'
|
|
|
|
unless current_user.nil?
|
|
|
|
current_user.followers.each do |f|
|
|
|
|
Inbox.create!(user_id: f.id, question_id: question.id, new: true)
|
|
|
|
end
|
|
|
|
end
|
2015-01-13 06:45:32 -08:00
|
|
|
elsif params[:rcpt].start_with? 'grp:'
|
|
|
|
unless current_user.nil?
|
|
|
|
begin
|
|
|
|
current_user.groups.find_by_name!(params[:rcpt].sub 'grp:', '').members.each do |m|
|
|
|
|
Inbox.create!(user_id: m.user.id, question_id: question.id, new: true)
|
|
|
|
end
|
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
@status = :not_found
|
|
|
|
@message = "Group not found"
|
|
|
|
@success = false
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
2014-12-07 11:13:45 -08:00
|
|
|
else
|
|
|
|
Inbox.create!(user_id: params[:rcpt], question_id: question.id, new: true)
|
|
|
|
end
|
2014-11-10 12:56:30 -08:00
|
|
|
|
|
|
|
@status = :okay
|
|
|
|
@message = "Question asked successfully."
|
|
|
|
@success = true
|
|
|
|
end
|
2015-04-18 15:17:13 -07:00
|
|
|
|
|
|
|
def preview
|
|
|
|
params.require :md
|
|
|
|
|
|
|
|
@message = "Failed to render markdown."
|
|
|
|
begin
|
|
|
|
@markdown = markdown(params[:md], Time.new)
|
|
|
|
@message = "Successfully rendered markdown."
|
|
|
|
rescue
|
|
|
|
@status = :fail
|
|
|
|
@success = false
|
|
|
|
return
|
|
|
|
end
|
|
|
|
@status = :okay
|
|
|
|
@success = true
|
|
|
|
end
|
2014-11-10 12:56:30 -08:00
|
|
|
end
|