Refactor Ajax::*Controllers

Also removed the unused `Ajax::QuestionController#preview` method and
route
This commit is contained in:
Georg Gadinger 2020-04-28 20:27:59 +02:00
parent acc394a110
commit e07d069c73
41 changed files with 264 additions and 388 deletions

View File

@ -1,11 +1,4 @@
class Ajax::AnswerController < ApplicationController
rescue_from(ActionController::ParameterMissing) do |titanic_param|
@status = :parameter_error
@message = I18n.t('messages.parameter_error', parameter: titanic_param.param.capitalize)
@success = false
render partial: "ajax/shared/status"
end
class Ajax::AnswerController < AjaxController
def create
params.require :id
params.require :answer
@ -18,27 +11,24 @@ class Ajax::AnswerController < ApplicationController
inbox_entry = Inbox.find(params[:id])
unless current_user == inbox_entry.user
@status = :fail
@message = I18n.t('messages.answer.create.fail')
@success = false
@response[:status] = :fail
@response[:message] = I18n.t('messages.answer.create.fail')
return
end
else
question = Question.find(params[:id])
unless question.user.privacy_allow_stranger_answers
@status = :privacy_stronk
@message = I18n.t('messages.answer.create.privacy_stronk')
@success = false
@response[:status] = :privacy_stronk
@response[:message] = I18n.t('messages.answer.create.privacy_stronk')
return
end
end
# this should never trigger because empty params throw ParameterMissing
unless params[:answer].length > 0
@status = :peter_dinklage
@message = I18n.t('messages.answer.create.peter_dinklage')
@success = false
@response[:status] = :peter_dinklage
@response[:message] = I18n.t('messages.answer.create.peter_dinklage')
return
end
@ -51,9 +41,8 @@ class Ajax::AnswerController < ApplicationController
current_user.answer question, params[:answer]
end
rescue
@status = :err
@message = I18n.t('messages.error')
@success = false
@response[:status] = :err
@response[:message] = I18n.t('messages.error')
return
end
@ -61,12 +50,13 @@ class Ajax::AnswerController < ApplicationController
ShareWorker.perform_async(current_user.id, answer.id, services)
@status = :okay
@message = I18n.t('messages.answer.create.okay')
@success = true
@response[:status] = :okay
@response[:message] = I18n.t('messages.answer.create.okay')
@response[:success] = true
unless inbox
# this assign is needed because shared/_answerbox relies on it, I think
@question = 1
@render = render_to_string(partial: 'shared/answerbox', locals: { a: answer, show_question: false })
@response[:render] = render_to_string(partial: 'shared/answerbox', locals: { a: answer, show_question: false })
end
end
@ -76,9 +66,8 @@ class Ajax::AnswerController < ApplicationController
answer = Answer.find(params[:answer])
unless (current_user == answer.user) or (privileged? answer.user)
@status = :nopriv
@message = I18n.t('messages.answer.destroy.nopriv')
@success = false
@response[:status] = :nopriv
@response[:message] = I18n.t('messages.answer.destroy.nopriv')
return
end
@ -87,8 +76,8 @@ class Ajax::AnswerController < ApplicationController
end # TODO: decide what happens with the question
answer.destroy
@status = :okay
@message = I18n.t('messages.answer.destroy.okay')
@success = true
@response[:status] = :okay
@response[:message] = I18n.t('messages.answer.destroy.okay')
@response[:success] = true
end
end

View File

@ -1,11 +1,4 @@
class Ajax::CommentController < ApplicationController
rescue_from(ActionController::ParameterMissing) do |param_miss_ex|
@status = :parameter_error
@message = I18n.t('messages.parameter_error', parameter: param_miss_ex.param.capitalize)
@success = false
render partial: "ajax/shared/status"
end
class Ajax::CommentController < AjaxController
def create
params.require :answer
params.require :comment
@ -15,38 +8,35 @@ class Ajax::CommentController < ApplicationController
begin
current_user.comment(answer, params[:comment])
rescue ActiveRecord::RecordInvalid
@status = :rec_inv
@message = I18n.t('messages.comment.create.rec_inv')
@success = false
@response[:status] = :rec_inv
@response[:message] = I18n.t('messages.comment.create.rec_inv')
return
end
@status = :okay
@message = I18n.t('messages.comment.create.okay')
@success = true
@render = render_to_string(partial: 'shared/comments', locals: { a: answer })
@count = answer.comment_count
@response[:status] = :okay
@response[:message] = I18n.t('messages.comment.create.okay')
@response[:success] = true
@response[:render] = render_to_string(partial: 'shared/comments', locals: { a: answer })
@response[:count] = answer.comment_count
end
def destroy
params.require :comment
@status = :err
@success = false
@response[:status] = :err
comment = Comment.find(params[:comment])
unless (current_user == comment.user) or (current_user == comment.answer.user) or (privileged? comment.user)
@status = :nopriv
@message = I18n.t('messages.comment.destroy.nopriv')
@success = false
@response[:status] = :nopriv
@response[:message] = I18n.t('messages.comment.destroy.nopriv')
return
end
@count = comment.answer.comment_count - 1
@response[:count] = comment.answer.comment_count - 1
comment.destroy
@status = :okay
@message = I18n.t('messages.comment.destroy.okay')
@success = true
@response[:status] = :okay
@response[:message] = I18n.t('messages.comment.destroy.okay')
@response[:success] = true
end
end

View File

@ -1,11 +1,4 @@
class Ajax::FriendController < ApplicationController
rescue_from(ActionController::ParameterMissing) do |param_miss_ex|
@status = :parameter_error
@message = I18n.t('messages.parameter_error', parameter: param_miss_ex.param.capitalize)
@success = false
render partial: "ajax/shared/status"
end
class Ajax::FriendController < AjaxController
def create
params.require :screen_name
@ -14,15 +7,14 @@ class Ajax::FriendController < ApplicationController
begin
current_user.follow target_user
rescue
@status = :fail
@message = I18n.t('messages.friend.create.fail')
@success = false
@response[:status] = :fail
@response[:message] = I18n.t('messages.friend.create.fail')
return
end
@status = :okay
@message = I18n.t('messages.friend.create.okay')
@success = true
@response[:status] = :okay
@response[:message] = I18n.t('messages.friend.create.okay')
@response[:success] = true
end
def destroy
@ -33,14 +25,13 @@ class Ajax::FriendController < ApplicationController
begin
current_user.unfollow target_user
rescue
@status = :fail
@message = I18n.t('messages.friend.destroy.fail')
@success = false
@response[:status] = :fail
@response[:message] = I18n.t('messages.friend.destroy.fail')
return
end
@status = :okay
@message = I18n.t('messages.friend.destroy.okay')
@success = true
@response[:status] = :okay
@response[:message] = I18n.t('messages.friend.destroy.okay')
@response[:success] = true
end
end

View File

@ -1,26 +1,18 @@
class Ajax::GroupController < ApplicationController
rescue_from(ActionController::ParameterMissing) do |param_miss_ex|
@status = :parameter_error
@message = I18n.t('messages.parameter_error', parameter: param_miss_ex.param.capitalize)
@success = false
render partial: "ajax/shared/status"
end
class Ajax::GroupController < AjaxController
def create
@status = :err
@success = false
@response[:status] = :err
unless user_signed_in?
@status = :noauth
@message = I18n.t('messages.noauth')
@response[:status] = :noauth
@response[:message] = I18n.t('messages.noauth')
return
end
begin
params.require :name
rescue ActionController::ParameterMissing
@status = :toolong
@message = I18n.t('messages.group.create.noname')
@response[:status] = :toolong
@response[:message] = I18n.t('messages.group.create.noname')
return
end
params.require :user
@ -29,32 +21,31 @@ class Ajax::GroupController < ApplicationController
target_user = User.find_by_screen_name(params[:user])
group = Group.create! user: current_user, display_name: params[:name]
rescue ActiveRecord::RecordInvalid
@status = :toolong
@message = I18n.t('messages.group.create.toolong')
@response[:status] = :toolong
@response[:message] = I18n.t('messages.group.create.toolong')
return
rescue ActiveRecord::RecordNotFound
@status = :notfound
@message = I18n.t('messages.group.create.notfound')
@response[:status] = :notfound
@response[:message] = I18n.t('messages.group.create.notfound')
return
rescue ActiveRecord::RecordNotUnique
@status = :exists
@message = I18n.t('messages.group.create.exists')
@response[:status] = :exists
@response[:message] = I18n.t('messages.group.create.exists')
return
end
@status = :okay
@success = true
@message = I18n.t('messages.group.create.okay')
@render = render_to_string(partial: 'user/modal_group_item', locals: { group: group, user: target_user })
@response[:status] = :okay
@response[:success] = true
@response[:message] = I18n.t('messages.group.create.okay')
@response[:render] = render_to_string(partial: 'user/modal_group_item', locals: { group: group, user: target_user })
end
def destroy
@status = :err
@success = false
@response[:status] = :err
unless user_signed_in?
@status = :noauth
@message = I18n.t('messages.noauth')
@response[:status] = :noauth
@response[:message] = I18n.t('messages.noauth')
return
end
@ -63,23 +54,22 @@ class Ajax::GroupController < ApplicationController
begin
Group.where(user: current_user, name: params[:group]).first.destroy!
rescue ActiveRecord::RecordNotFound
@status = :notfound
@message = I18n.t('messages.group.destroy.notfound')
@response[:status] = :notfound
@response[:message] = I18n.t('messages.group.destroy.notfound')
return
end
@status = :okay
@success = true
@message = I18n.t('messages.group.destroy.okay')
@response[:status] = :okay
@response[:success] = true
@response[:message] = I18n.t('messages.group.destroy.okay')
end
def membership
@status = :err
@success = false
@response[:status] = :err
unless user_signed_in?
@status = :noauth
@message = I18n.t('messages.noauth')
@response[:status] = :noauth
@response[:message] = I18n.t('messages.noauth')
return
end
@ -92,8 +82,8 @@ class Ajax::GroupController < ApplicationController
begin
group = current_user.groups.find_by_name(params[:group])
rescue ActiveRecord::RecordNotFound
@status = :notfound
@message = I18n.t('messages.group.membership.notfound')
@response[:status] = :notfound
@response[:message] = I18n.t('messages.group.membership.notfound')
return
end
@ -101,15 +91,15 @@ class Ajax::GroupController < ApplicationController
if add
group.add_member target_user if group.members.find_by_user_id(target_user.id).nil?
@checked = true
@message = I18n.t('messages.group.membership.add')
@response[:checked] = true
@response[:message] = I18n.t('messages.group.membership.add')
else
group.remove_member target_user unless group.members.find_by_user_id(target_user.id).nil?
@checked = false
@message = I18n.t('messages.group.membership.remove')
@response[:checked] = false
@response[:message] = I18n.t('messages.group.membership.remove')
end
@status = :okay
@success = true
@response[:status] = :okay
@response[:success] = true
end
end

View File

@ -1,16 +1,8 @@
class Ajax::InboxController < ApplicationController
rescue_from(ActionController::ParameterMissing) do |param_miss_ex|
@status = :parameter_error
@message = I18n.t('messages.parameter_error', parameter: param_miss_ex.param.capitalize)
@success = false
render partial: "ajax/shared/status"
end
class Ajax::InboxController < AjaxController
def create
unless user_signed_in?
@status = :noauth
@message = I18n.t('messages.noauth')
@success = false
@response[:status] = :noauth
@response[:message] = I18n.t('messages.noauth')
return
end
@ -21,10 +13,10 @@ class Ajax::InboxController < ApplicationController
inbox = Inbox.create!(user: current_user, question_id: question.id, new: true)
@status = :okay
@message = I18n.t('messages.inbox.create.okay')
@success = true
@render = render_to_string(partial: 'inbox/entry', locals: { i: inbox })
@response[:status] = :okay
@response[:message] = I18n.t('messages.inbox.create.okay')
@response[:success] = true
@response[:render] = render_to_string(partial: 'inbox/entry', locals: { i: inbox })
inbox.update(new: false)
end
@ -34,40 +26,36 @@ class Ajax::InboxController < ApplicationController
inbox = Inbox.find(params[:id])
unless current_user == inbox.user
@status = :fail
@message = I18n.t('messages.inbox.remove.fail')
@success = false
@response[:status] = :fail
@response[:message] = I18n.t('messages.inbox.remove.fail')
return
end
begin
inbox.remove
rescue
@status = :err
@message = I18n.t('messages.error')
@success = false
@response[:status] = :err
@response[:message] = I18n.t('messages.error')
return
end
@status = :okay
@message = I18n.t('messages.inbox.remove.okay')
@success = true
@response[:status] = :okay
@response[:message] = I18n.t('messages.inbox.remove.okay')
@response[:success] = true
end
def remove_all
begin
Inbox.where(user: current_user).each { |i| i.remove }
rescue
@status = :err
@message = I18n.t('messages.error')
@success = false
@response[:status] = :err
@response[:message] = I18n.t('messages.error')
return
end
@status = :okay
@message = I18n.t('messages.inbox.remove_all.okay')
@success = true
render 'ajax/inbox/remove'
@response[:status] = :okay
@response[:message] = I18n.t('messages.inbox.remove_all.okay')
@response[:success] = true
end
def remove_all_author
@ -77,15 +65,13 @@ class Ajax::InboxController < ApplicationController
.where(questions: { user_id: @target_user.id, author_is_anonymous: false })
@inbox.each { |i| i.remove }
rescue
@status = :err
@message = I18n.t('messages.error')
@success = false
@response[:status] = :err
@response[:message] = I18n.t('messages.error')
return
end
@status = :okay
@message = I18n.t('messages.inbox.remove_all.okay')
@success = true
render 'ajax/inbox/remove'
@response[:status] = :okay
@response[:message] = I18n.t('messages.inbox.remove_all.okay')
@response[:success] = true
end
end

View File

@ -1,11 +1,4 @@
class Ajax::ModerationController < ApplicationController
rescue_from(ActionController::ParameterMissing) do |param_miss_ex|
@status = :parameter_error
@message = I18n.t('messages.parameter_error', parameter: param_miss_ex.param.capitalize)
@success = false
render partial: "ajax/shared/status"
end
class Ajax::ModerationController < AjaxController
def vote
params.require :id
params.require :upvote
@ -15,16 +8,15 @@ class Ajax::ModerationController < ApplicationController
begin
current_user.report_vote(report, params[:upvote])
rescue
@status = :fail
@message = I18n.t('messages.moderation.vote.fail')
@success = false
@response[:status] = :fail
@response[:message] = I18n.t('messages.moderation.vote.fail')
return
end
@count = report.votes
@status = :okay
@message = I18n.t('messages.moderation.vote.okay')
@success = true
@response[:count] = report.votes
@response[:status] = :okay
@response[:message] = I18n.t('messages.moderation.vote.okay')
@response[:success] = true
end
def destroy_vote
@ -35,16 +27,15 @@ class Ajax::ModerationController < ApplicationController
begin
current_user.report_unvote report
rescue
@status = :fail
@message = I18n.t('messages.moderation.destroy_vote.fail')
@success = false
@response[:status] = :fail
@response[:message] = I18n.t('messages.moderation.destroy_vote.fail')
return
end
@count = report.votes
@status = :okay
@message = I18n.t('messages.moderation.destroy_vote.okay')
@success = true
@response[:count] = report.votes
@response[:status] = :okay
@response[:message] = I18n.t('messages.moderation.destroy_vote.okay')
@response[:success] = true
end
def destroy_report
@ -56,15 +47,14 @@ class Ajax::ModerationController < ApplicationController
report.deleted = true
report.save
rescue
@status = :fail
@message = I18n.t('messages.moderation.destroy_report.fail')
@success = false
@response[:status] = :fail
@response[:message] = I18n.t('messages.moderation.destroy_report.fail')
return
end
@status = :okay
@message = I18n.t('messages.moderation.destroy_report.okay')
@success = true
@response[:status] = :okay
@response[:message] = I18n.t('messages.moderation.destroy_report.okay')
@response[:success] = true
end
def create_comment
@ -73,48 +63,44 @@ class Ajax::ModerationController < ApplicationController
report = Report.find(params[:id])
@success = false
begin
current_user.report_comment(report, params[:comment])
rescue ActiveRecord::RecordInvalid
@status = :rec_inv
@message = I18n.t('messages.moderation.create_comment.rec_inv')
@response[:status] = :rec_inv
@response[:message] = I18n.t('messages.moderation.create_comment.rec_inv')
return
end
@status = :okay
@message = I18n.t('messages.moderation.create_comment.okay')
@success = true
@render = render_to_string(partial: 'moderation/discussion', locals: { report: report })
@count = report.moderation_comments.all.count
@response[:status] = :okay
@response[:message] = I18n.t('messages.moderation.create_comment.okay')
@response[:success] = true
@response[:render] = render_to_string(partial: 'moderation/discussion', locals: { report: report })
@response[:count] = report.moderation_comments.all.count
end
def destroy_comment
params.require :comment
@status = :err
@success = false
@response[:status] = :err
comment = ModerationComment.find(params[:comment])
unless current_user == comment.user
@status = :nopriv
@message = I18n.t('messages.moderation.destroy_comment.nopriv')
@success = false
@response[:status] = :nopriv
@response[:message] = I18n.t('messages.moderation.destroy_comment.nopriv')
return
end
comment.destroy
@status = :okay
@message = I18n.t('messages.moderation.destroy_comment.okay')
@success = true
@response[:status] = :okay
@response[:message] = I18n.t('messages.moderation.destroy_comment.okay')
@response[:success] = true
end
def ban
@status = :err
@message = I18n.t('messages.moderation.ban.error')
@success = false
@response[:status] = :err
@response[:message] = I18n.t('messages.moderation.ban.error')
params.require :user
params.require :ban
@ -128,32 +114,30 @@ class Ajax::ModerationController < ApplicationController
buntil = DateTime.strptime params[:until], "%m/%d/%Y %I:%M %p" unless unban || perma
if !unban && target.has_role?(:administrator)
@status = :nopriv
@message = I18n.t('messages.moderation.ban.nopriv')
@success = false
@response[:status] = :nopriv
@response[:message] = I18n.t('messages.moderation.ban.nopriv')
return
end
if unban
target.unban
@message = I18n.t('messages.moderation.ban.unban')
@success = true
@response[:message] = I18n.t('messages.moderation.ban.unban')
@response[:success] = true
elsif perma
target.ban nil, reason
@message = I18n.t('messages.moderation.ban.perma')
@response[:message] = I18n.t('messages.moderation.ban.perma')
else
target.ban buntil, reason
@message = I18n.t('messages.moderation.ban.temp', date: buntil.to_s)
@response[:message] = I18n.t('messages.moderation.ban.temp', date: buntil.to_s)
end
target.save!
@status = :okay
@success = target.banned? == !unban
@response[:status] = :okay
@response[:success] = target.banned? == !unban
end
def privilege
@status = :err
@success = false
@response[:status] = :err
params.require :user
params.require :type
@ -163,17 +147,16 @@ class Ajax::ModerationController < ApplicationController
target_user = User.find_by_screen_name(params[:user])
@message = I18n.t('messages.moderation.privilege.nope')
@response[:message] = I18n.t('messages.moderation.privilege.nope')
return unless %w(moderator admin).include? params[:type].downcase
unless current_user.has_role?(:administrator)
@status = :nopriv
@message = I18n.t('messages.moderation.privilege.nopriv')
@success = false
@response[:status] = :nopriv
@response[:message] = I18n.t('messages.moderation.privilege.nopriv')
return
end
@checked = status
@response[:checked] = status
type = params[:type].downcase
target_role = {"admin" => "administrator"}.fetch(type, type).to_sym
@ -184,9 +167,9 @@ class Ajax::ModerationController < ApplicationController
end
target_user.save!
@message = I18n.t('messages.moderation.privilege.checked', privilege: params[:type])
@response[:message] = I18n.t('messages.moderation.privilege.checked', privilege: params[:type])
@status = :okay
@success = true
@response[:status] = :okay
@response[:success] = true
end
end

View File

@ -1,36 +1,25 @@
class Ajax::QuestionController < ApplicationController
include MarkdownHelper
rescue_from(ActionController::ParameterMissing) do |param_miss_ex|
@status = :parameter_error
@message = I18n.t('messages.parameter_error', parameter: param_miss_ex.param.capitalize)
@success = false
render partial: "ajax/shared/status"
end
class Ajax::QuestionController < AjaxController
def destroy
params.require :question
question = Question.find params[:question]
if question.nil?
@status = :not_found
@message = I18n.t('messages.question.destroy.not_found')
@success = false
@response[:status] = :not_found
@response[:message] = I18n.t('messages.question.destroy.not_found')
return
end
if not (current_user.mod? or question.user == current_user)
@status = :not_authorized
@message = I18n.t('messages.question.destroy.not_authorized')
@success = false
@response[:status] = :not_authorized
@response[:message] = I18n.t('messages.question.destroy.not_authorized')
return
end
question.destroy!
@status = :okay
@message = I18n.t('messages.question.destroy.okay')
@success = true
@response[:status] = :okay
@response[:message] = I18n.t('messages.question.destroy.okay')
@response[:success] = true
end
def create
@ -43,9 +32,8 @@ class Ajax::QuestionController < ApplicationController
author_is_anonymous: params[:anonymousQuestion],
user: current_user)
rescue ActiveRecord::RecordInvalid
@status = :rec_inv
@message = I18n.t('messages.question.create.rec_inv')
@success = false
@response[:status] = :rec_inv
@response[:message] = I18n.t('messages.question.create.rec_inv')
return
end
@ -63,41 +51,23 @@ class Ajax::QuestionController < ApplicationController
current_user.groups.find_by_name!(params[:rcpt].sub 'grp:', '')
QuestionWorker.perform_async params[:rcpt], current_user.id, question.id
rescue ActiveRecord::RecordNotFound
@status = :not_found
@message = I18n.t('messages.question.create.not_found')
@success = false
@response[:status] = :not_found
@response[:message] = I18n.t('messages.question.create.not_found')
return
end
end
else
if User.find(params[:rcpt]).nil?
@status = :not_found
@message = I18n.t('messages.question.create.not_found')
@success = false
@response[:status] = :not_found
@response[:message] = I18n.t('messages.question.create.not_found')
return
end
Inbox.create!(user_id: params[:rcpt], question_id: question.id, new: true)
end
@status = :okay
@message = I18n.t('messages.question.create.okay')
@success = true
end
def preview
params.require :md
@message = I18n.t('messages.question.preview.fail')
begin
@markdown = markdown params[:md]
@message = I18n.t('messages.question.preview.okay')
rescue
@status = :fail
@success = false
return
end
@status = :okay
@success = true
@response[:status] = :okay
@response[:message] = I18n.t('messages.question.create.okay')
@response[:success] = true
end
end

View File

@ -1,25 +1,17 @@
class Ajax::ReportController < ApplicationController
rescue_from(ActionController::ParameterMissing) do |param_miss_ex|
@status = :parameter_error
@message = I18n.t('messages.parameter_error', parameter: param_miss_ex.param.capitalize)
@success = false
render partial: "ajax/shared/status"
end
class Ajax::ReportController < AjaxController
def create
params.require :id
params.require :type
@status = :err
@success = false
@response[:status] = :err
if current_user.nil?
@message = I18n.t('messages.report.create.login')
@response[:message] = I18n.t('messages.report.create.login')
return
end
unless %w(answer comment question user).include? params[:type]
@message = I18n.t('messages.report.create.unknown')
@response[:message] = I18n.t('messages.report.create.unknown')
return
end
@ -39,14 +31,14 @@ class Ajax::ReportController < ApplicationController
end
if object.nil?
@message = I18n.t('messages.report.create.not_found', parameter: params[:type])
@response[:message] = I18n.t('messages.report.create.not_found', parameter: params[:type])
return
end
current_user.report object, params[:reason]
@status = :okay
@message = I18n.t('messages.report.create.okay', parameter: params[:type])
@success = true
@response[:status] = :okay
@response[:message] = I18n.t('messages.report.create.okay', parameter: params[:type])
@response[:success] = true
end
end

View File

@ -1,11 +1,4 @@
class Ajax::SmileController < ApplicationController
rescue_from(ActionController::ParameterMissing) do |param_miss_ex|
@status = :parameter_error
@message = I18n.t('messages.parameter_error', parameter: param_miss_ex.param.capitalize)
@success = false
render partial: "ajax/shared/status"
end
class Ajax::SmileController < AjaxController
def create
params.require :id
@ -14,15 +7,14 @@ class Ajax::SmileController < ApplicationController
begin
current_user.smile answer
rescue
@status = :fail
@message = I18n.t('messages.smile.create.fail')
@success = false
@response[:status] = :fail
@response[:message] = I18n.t('messages.smile.create.fail')
return
end
@status = :okay
@message = I18n.t('messages.smile.create.okay')
@success = true
@response[:status] = :okay
@response[:message] = I18n.t('messages.smile.create.okay')
@response[:success] = true
end
def destroy
@ -33,15 +25,14 @@ class Ajax::SmileController < ApplicationController
begin
current_user.unsmile answer
rescue
@status = :fail
@message = I18n.t('messages.smile.destroy.fail')
@success = false
@response[:status] = :fail
@response[:message] = I18n.t('messages.smile.destroy.fail')
return
end
@status = :okay
@message = I18n.t('messages.smile.destroy.okay')
@success = true
@response[:status] = :okay
@response[:message] = I18n.t('messages.smile.destroy.okay')
@response[:success] = true
end
def create_comment
@ -52,15 +43,14 @@ class Ajax::SmileController < ApplicationController
begin
current_user.smile_comment comment
rescue
@status = :fail
@message = I18n.t('messages.smile.create_comment.fail')
@success = false
@response[:status] = :fail
@response[:message] = I18n.t('messages.smile.create_comment.fail')
return
end
@status = :okay
@message = I18n.t('messages.smile.create_comment.okay')
@success = true
@response[:status] = :okay
@response[:message] = I18n.t('messages.smile.create_comment.okay')
@response[:success] = true
end
def destroy_comment
@ -71,14 +61,13 @@ class Ajax::SmileController < ApplicationController
begin
current_user.unsmile_comment comment
rescue
@status = :fail
@message = I18n.t('messages.smile.destroy_comment.fail')
@success = false
@response[:status] = :fail
@response[:message] = I18n.t('messages.smile.destroy_comment.fail')
return
end
@status = :okay
@message = I18n.t('messages.smile.destroy_comment.okay')
@success = true
@response[:status] = :okay
@response[:message] = I18n.t('messages.smile.destroy_comment.okay')
@response[:success] = true
end
end

View File

@ -1,25 +1,19 @@
class Ajax::SubscriptionController < ApplicationController
class Ajax::SubscriptionController < AjaxController
before_action :authenticate_user!
rescue_from(ActionController::ParameterMissing) do |param_miss_ex|
@status = :parameter_error
@message = I18n.t('messages.parameter_error', parameter: param_miss_ex.param.capitalize)
@success = false
render partial: "ajax/shared/status"
end
def subscribe
params.require :answer
@status = 418
@message = I18n.t('messages.subscription.torpedo')
@response[:status] = 418
@response[:message] = I18n.t('messages.subscription.torpedo')
state = Subscription.subscribe(current_user, Answer.find(params[:answer])).nil?
@success = state == false
@response[:success] = state == false
end
def unsubscribe
params.require :answer
@status = 418
@message = I18n.t('messages.subscription.torpedo')
@response[:status] = 418
@response[:message] = I18n.t('messages.subscription.torpedo')
state = Subscription.unsubscribe(current_user, Answer.find(params[:answer])).nil?
@success = state == false
@response[:success] = state == false
end
end

View File

@ -0,0 +1,57 @@
# frozen_string_literal: true
class AjaxController < ApplicationController
before_action :build_response
after_action :return_response
respond_to :json
rescue_from(ActiveRecord::RecordNotFound) do |e|
NewRelic::Agent.notice_error(e)
@response = {
success: false,
message: "Record not found",
status: :not_found
}
return_response
end
rescue_from(ActionController::ParameterMissing) do |e|
NewRelic::Agent.notice_error(e)
@response = {
success: false,
message: I18n.t('messages.parameter_error', parameter: param_miss_ex.param.capitalize),
status: :parameter_error
}
return_response
end
def find_active_announcements
# We do not need announcements here
end
private
def build_response
@response = {
success: false,
message: '',
status: 'unknown'
}
end
def return_response
# Q: Why don't we just use render(json:) here?
# A: Because otherwise Rails wants us to use views, which do not make much sense here.
#
# Q: Why do we always return 200?
# A: Because JQuery might not do things we want it to if we don't.
response.status = 200
response.headers["Content-Type"] = "application/json"
response.body = @response.to_json
end
end

View File

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

View File

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

View File

@ -1,3 +0,0 @@
json.partial! 'ajax/shared/status'
json.render @render if @render
json.count @count if @count

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -152,9 +152,6 @@ cs:
rec_inv: "Vaše otázka je příliš dlhá."
not_found: "Skupina nebyla nalezena"
okay: "Otázka úspěšně zeptána."
preview:
fail: "Renderování kódu selhalo."
okay: "Úspěšně renderováno kód."
report:
create:
login: "přihlášení povinné"

View File

@ -154,9 +154,6 @@ de:
rec_inv: "Deine Frage ist zu lang."
not_found: "Gruppe nicht gefunden"
okay: "Frage erfolgreich gestellt."
preview:
fail: "Das Markdownrendern ist fehlgeschlagen."
okay: "Markdown erfolgreich gerendert."
report:
create:
login: "Login benötigt"
@ -455,4 +452,4 @@ de:
contributor: Contributor
blogger: Blogger
banned: Gebannt
translator: Übersetzer
translator: Übersetzer

View File

@ -151,9 +151,6 @@ en:
rec_inv: "Your question is too long."
not_found: "Group not found"
okay: "Question asked successfully."
preview:
fail: "Failed to render markdown."
okay: "Successfully rendered markdown."
report:
create:
login: "login required"

View File

@ -151,9 +151,6 @@ en_dizzle:
rec_inv: "Yo crazy-ass question is too long."
not_found: "Group not found"
okay: "Question asked successfully."
preview:
fail: "Failed ta render markdown."
okay: "Successfully rendered markdown."
report:
create:
login: "login required"

View File

@ -151,9 +151,6 @@ en_pirate:
rec_inv: "Your inquiry is too long."
not_found: "Crew not found"
okay: "Inquiry asked successfully."
preview:
fail: "Failed to render markdown."
okay: "Successfully rendered markdown."
report:
create:
login: "login required"

View File

@ -154,9 +154,6 @@ fr:
rec_inv: "Votre question est trop longue."
not_found: "Le groupe est introuvable."
okay: "La question a été posée avec succès."
preview:
fail: "Échec de rendu du markdown."
okay: "Markdown rendu avec succès."
report:
create:
login: "connexion requise"

View File

@ -151,9 +151,6 @@ it:
rec_inv: "La tua domanda è troppo lunga."
not_found: "Gruppo non trovato"
okay: "Domanda chiesta."
preview:
fail: "Impossibile renderizzare markdown."
okay: "Markdown renderizzato."
report:
create:
login: "devi prima fare il login"

View File

@ -153,9 +153,6 @@ ja:
rec_inv: 質問が長過ぎます。
not_found: グループが見つかりません
okay: 質問の投稿に成功しました。
preview:
fail: 書式処理に失敗しました。
okay: 書式処理に成功しました。
report:
create:
login: ログインが必要です

View File

@ -106,7 +106,6 @@ Rails.application.routes.draw do
match '/create_group', to: 'group#create', via: :post, as: :create_group
match '/destroy_group', to: 'group#destroy', via: :post, as: :destroy_group
match '/group_membership', to: 'group#membership', via: :post, as: :group_membership
match '/preview', to: "question#preview", via: :post, as: :preview
match '/subscribe', to: 'subscription#subscribe', via: :post, as: :subscribe_answer
match '/unsubscribe', to: 'subscription#unsubscribe', via: :post, as: :unsubscribe_answer
end