-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6a8bd53
commit b7fa872
Showing
3 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# frozen_string_literal: true | ||
|
||
class FeedbacksController < ApplicationController | ||
skip_before_action :authenticate_user!, only: [:create] | ||
before_action :load_form, only: [:create] | ||
|
||
def create | ||
# Accessible by users who are not logged in | ||
skip_authorization | ||
raise Pundit::NotAuthorizedError if @form.secret != params[:secret] | ||
|
||
if create_feedback | ||
redirect_to thank_you_path | ||
else | ||
flash[:alert] = 'No such form exists.' | ||
redirect_to root_path | ||
end | ||
end | ||
|
||
private | ||
|
||
def feedback_params | ||
params.fetch(:feedback, {}).permit(:form_id) | ||
end | ||
|
||
def load_form | ||
@form = Form.find_by(id: feedback_params[:form_id]) | ||
end | ||
|
||
def create_feedback | ||
ApplicationRecord.transaction do | ||
feedback = Feedback.create | ||
params.fetch(:string_answers, []).each do |string_answer_params| | ||
string_answer_params | ||
.permit(:answer, :string_question_id) | ||
.merge(feedback: feedback) | ||
.then { StringAnswer.create(_1) } | ||
end | ||
params.fetch(:text_answers, []).each do |text_answer_params| | ||
text_answer_params | ||
.permit(:answer, :text_question_id) | ||
.merge(feedback: feedback) | ||
.then { TextAnswer.create(_1) } | ||
end | ||
params.fetch(:select_answers, []).each do |select_answer_params| | ||
select_answer_params | ||
.permit(:answer, :select_question_id) | ||
.merge(feedback: feedback) | ||
.then { SelectAnswer.create(_1) } | ||
end | ||
params.fetch(:scale_answers, []).each do |scale_answer_params| | ||
scale_answer_params | ||
.permit(:answer, :scale_question_id) | ||
.merge(feedback: feedback) | ||
.then { ScaleAnswer.create(_1) } | ||
end | ||
true | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe 'Feedbacks' do | ||
describe 'Create' do | ||
let(:form) { create(:form) } | ||
let(:feedback_params) { { feedback: { form_id: form.id } } } | ||
let(:secret_params) { { secret: form.secret } } | ||
|
||
context 'for mismatching secret' do | ||
let(:params) { feedback_params } | ||
|
||
it 'redirects not authorized' do | ||
post feedbacks_path, params: params | ||
|
||
expect(response).to redirect_to root_path | ||
end | ||
end | ||
|
||
context 'simple submit' do | ||
let(:params) { feedback_params.merge(secret_params) } | ||
|
||
it 'creates feedback' do | ||
expect { post feedbacks_path, params: params } | ||
.to change { Feedback.count }.by(1) | ||
end | ||
|
||
it 'redirects correclty' do | ||
post feedbacks_path, params: params | ||
|
||
expect(response).to redirect_to thank_you_path | ||
end | ||
end | ||
|
||
context 'nested attributes' do | ||
let(:string_question) { create(:string_question, form: form) } | ||
let(:string_answer) do | ||
build(:string_answer, string_question: string_question).attributes.slice('answer', 'string_question_id') | ||
end | ||
let(:text_question) { create(:text_question, form: form) } | ||
let(:text_answer) do | ||
build(:text_answer, text_question: text_question).attributes.slice('answer', 'text_question_id') | ||
end | ||
let(:select_question) { create(:select_question, form: form) } | ||
let(:select_answer) do | ||
build(:select_answer, select_question: select_question).attributes.slice('answer', 'select_question_id') | ||
end | ||
let(:scale_question) { create(:scale_question, form: form) } | ||
let(:scale_answer) do | ||
build(:scale_answer, scale_question: scale_question).attributes.slice('answer', 'scale_question_id') | ||
end | ||
let(:params) do | ||
feedback_params | ||
.merge(secret_params) | ||
.merge(string_answers: [string_answer]) | ||
.merge(text_answers: [text_answer]) | ||
.merge(select_answers: [select_answer]) | ||
.merge(scale_answers: [scale_answer]) | ||
end | ||
|
||
it 'creates associated record' do | ||
expect { post feedbacks_path, params: params } | ||
.to change { StringAnswer.count }.by(1) | ||
end | ||
end | ||
end | ||
end |