Remove AJAX question generation code
This commit is contained in:
parent
d3eae22f13
commit
44c0136c9e
|
@ -1,25 +1,4 @@
|
||||||
class Ajax::InboxController < AjaxController
|
class Ajax::InboxController < AjaxController
|
||||||
def create
|
|
||||||
unless user_signed_in?
|
|
||||||
@response[:status] = :noauth
|
|
||||||
@response[:message] = t(".noauth")
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
question = Question.create!(content: QuestionGenerator.generate,
|
|
||||||
author_is_anonymous: true,
|
|
||||||
author_identifier: "justask",
|
|
||||||
user: current_user)
|
|
||||||
|
|
||||||
inbox = Inbox.create!(user: current_user, question_id: question.id, new: true)
|
|
||||||
|
|
||||||
@response[:status] = :okay
|
|
||||||
@response[:message] = t(".success")
|
|
||||||
@response[:success] = true
|
|
||||||
@response[:render] = render_to_string(partial: 'inbox/entry', locals: { i: inbox })
|
|
||||||
inbox.update(new: false)
|
|
||||||
end
|
|
||||||
|
|
||||||
def remove
|
def remove
|
||||||
params.require :id
|
params.require :id
|
||||||
|
|
||||||
|
|
|
@ -1,21 +0,0 @@
|
||||||
import { post } from '@rails/request.js';
|
|
||||||
|
|
||||||
import I18n from 'retrospring/i18n';
|
|
||||||
import { updateDeleteButton } from './delete';
|
|
||||||
import { showErrorNotification } from 'utilities/notifications';
|
|
||||||
|
|
||||||
export function generateQuestionHandler(): void {
|
|
||||||
post('/ajax/generate_question')
|
|
||||||
.then(async response => {
|
|
||||||
const data = await response.json;
|
|
||||||
|
|
||||||
if (!data.success) return false;
|
|
||||||
|
|
||||||
document.querySelector('#entries').insertAdjacentHTML('afterbegin', data.render);
|
|
||||||
updateDeleteButton();
|
|
||||||
})
|
|
||||||
.catch(err => {
|
|
||||||
console.log(err);
|
|
||||||
showErrorNotification(I18n.translate('frontend.error.message'));
|
|
||||||
});
|
|
||||||
}
|
|
|
@ -2,11 +2,9 @@ import registerEvents from 'utilities/registerEvents';
|
||||||
import registerInboxEntryEvents from './entry';
|
import registerInboxEntryEvents from './entry';
|
||||||
import { authorSearchHandler } from './author';
|
import { authorSearchHandler } from './author';
|
||||||
import { deleteAllAuthorQuestionsHandler, deleteAllQuestionsHandler } from './delete';
|
import { deleteAllAuthorQuestionsHandler, deleteAllQuestionsHandler } from './delete';
|
||||||
import { generateQuestionHandler } from './generate';
|
|
||||||
|
|
||||||
export default (): void => {
|
export default (): void => {
|
||||||
registerEvents([
|
registerEvents([
|
||||||
{ type: 'click', target: '#ib-generate-question', handler: generateQuestionHandler, global: true },
|
|
||||||
{ type: 'click', target: '#ib-delete-all', handler: deleteAllQuestionsHandler, global: true },
|
{ type: 'click', target: '#ib-delete-all', handler: deleteAllQuestionsHandler, global: true },
|
||||||
{ type: 'click', target: '#ib-delete-all-author', handler: deleteAllAuthorQuestionsHandler, global: true },
|
{ type: 'click', target: '#ib-delete-all-author', handler: deleteAllAuthorQuestionsHandler, global: true },
|
||||||
{ type: 'submit', target: '#author-form', handler: authorSearchHandler, global: true }
|
{ type: 'submit', target: '#author-form', handler: authorSearchHandler, global: true }
|
||||||
|
|
|
@ -110,7 +110,6 @@ Rails.application.routes.draw do
|
||||||
namespace :ajax do
|
namespace :ajax do
|
||||||
post "/ask", to: "question#create", as: :ask
|
post "/ask", to: "question#create", as: :ask
|
||||||
post "/destroy_question", to: "question#destroy", as: :destroy_question
|
post "/destroy_question", to: "question#destroy", as: :destroy_question
|
||||||
post "/generate_question", to: "inbox#create", as: :generate_question
|
|
||||||
post "/delete_inbox", to: "inbox#remove", as: :delete_inbox
|
post "/delete_inbox", to: "inbox#remove", as: :delete_inbox
|
||||||
post "/delete_all_inbox", to: "inbox#remove_all", as: :delete_all_inbox
|
post "/delete_all_inbox", to: "inbox#remove_all", as: :delete_all_inbox
|
||||||
post "/delete_all_inbox/:author", to: "inbox#remove_all_author", as: :delete_all_author
|
post "/delete_all_inbox/:author", to: "inbox#remove_all_author", as: :delete_all_author
|
||||||
|
|
|
@ -4,46 +4,6 @@
|
||||||
require "rails_helper"
|
require "rails_helper"
|
||||||
|
|
||||||
describe Ajax::InboxController, :ajax_controller, type: :controller do
|
describe Ajax::InboxController, :ajax_controller, type: :controller do
|
||||||
describe "#create" do
|
|
||||||
subject { post(:create) }
|
|
||||||
|
|
||||||
context "when user is signed in" do
|
|
||||||
before(:each) { sign_in(user) }
|
|
||||||
|
|
||||||
let(:expected_response) do
|
|
||||||
{
|
|
||||||
"success" => true,
|
|
||||||
"status" => "okay",
|
|
||||||
"message" => anything,
|
|
||||||
"render" => anything
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
it "creates a generated question to the user's inbox" do
|
|
||||||
allow(QuestionGenerator).to receive(:generate).and_return("Is Mayonnaise an instrument?")
|
|
||||||
expect { subject }.to(change { user.inboxes.count }.by(1))
|
|
||||||
expect(user.inboxes.last.question.author_is_anonymous).to eq(true)
|
|
||||||
expect(user.inboxes.last.question.author_identifier).to eq("justask")
|
|
||||||
expect(user.inboxes.last.question.user).to eq(user)
|
|
||||||
expect(user.inboxes.last.question.content).to eq("Is Mayonnaise an instrument?")
|
|
||||||
end
|
|
||||||
|
|
||||||
include_examples "returns the expected response"
|
|
||||||
end
|
|
||||||
|
|
||||||
context "when user is not signed in" do
|
|
||||||
let(:expected_response) do
|
|
||||||
{
|
|
||||||
"success" => false,
|
|
||||||
"status" => "noauth",
|
|
||||||
"message" => anything
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
include_examples "returns the expected response"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "#remove" do
|
describe "#remove" do
|
||||||
let(:params) do
|
let(:params) do
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue