9 changed files with 209 additions and 6 deletions
@ -0,0 +1,24 @@ |
|||||||
|
class Form::Setup::Pages::CreatedBy < ::Form::Page |
||||||
|
def initialize(id, hsh, subsection) |
||||||
|
super |
||||||
|
@id = "created_by" |
||||||
|
@header = "" |
||||||
|
@description = "" |
||||||
|
@questions = questions |
||||||
|
@subsection = subsection |
||||||
|
end |
||||||
|
|
||||||
|
def questions |
||||||
|
[ |
||||||
|
Form::Setup::Questions::CreatedById.new(nil, nil, self), |
||||||
|
] |
||||||
|
end |
||||||
|
|
||||||
|
def routed_to?(_case_log) |
||||||
|
!!form.current_user&.support? |
||||||
|
end |
||||||
|
|
||||||
|
def invalidated?(_case_log) |
||||||
|
false |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,30 @@ |
|||||||
|
class Form::Setup::Questions::CreatedById < ::Form::Question |
||||||
|
def initialize(id, hsh, page) |
||||||
|
super |
||||||
|
@id = "created_by_id" |
||||||
|
@check_answer_label = "User" |
||||||
|
@header = "Which user are you creating this log for?" |
||||||
|
@hint_text = "" |
||||||
|
@type = "select" |
||||||
|
@page = page |
||||||
|
@answer_options = answer_options_values |
||||||
|
end |
||||||
|
|
||||||
|
def answer_options_values |
||||||
|
answer_opts = { "" => "Select an option" } |
||||||
|
User.all.each_with_object(answer_opts) do |user, hsh| |
||||||
|
hsh[user.id] = user.name |
||||||
|
hsh |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
def label_from_value(value) |
||||||
|
return unless value |
||||||
|
|
||||||
|
answer_options[value] |
||||||
|
end |
||||||
|
|
||||||
|
def hidden_in_check_answers |
||||||
|
!form.current_user.support? |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,65 @@ |
|||||||
|
require "rails_helper" |
||||||
|
|
||||||
|
RSpec.describe Form::Setup::Pages::CreatedBy, type: :model do |
||||||
|
subject(:page) { described_class.new(page_id, page_definition, subsection) } |
||||||
|
|
||||||
|
let(:page_id) { nil } |
||||||
|
let(:page_definition) { nil } |
||||||
|
let(:subsection) { instance_double(Form::Subsection) } |
||||||
|
let(:form) { instance_double(Form) } |
||||||
|
let(:case_log) { instance_double(CaseLog) } |
||||||
|
|
||||||
|
it "has correct subsection" do |
||||||
|
expect(page.subsection).to eq(subsection) |
||||||
|
end |
||||||
|
|
||||||
|
it "has correct questions" do |
||||||
|
expect(page.questions.map(&:id)).to eq(%w[created_by_id]) |
||||||
|
end |
||||||
|
|
||||||
|
it "has the correct id" do |
||||||
|
expect(page.id).to eq("created_by") |
||||||
|
end |
||||||
|
|
||||||
|
it "has the correct header" do |
||||||
|
expect(page.header).to eq("") |
||||||
|
end |
||||||
|
|
||||||
|
it "has the correct description" do |
||||||
|
expect(page.description).to eq("") |
||||||
|
end |
||||||
|
|
||||||
|
it "has the correct depends_on" do |
||||||
|
expect(page.depends_on).to be nil |
||||||
|
end |
||||||
|
|
||||||
|
it "has the correct derived" do |
||||||
|
expect(page.derived).to be nil |
||||||
|
end |
||||||
|
|
||||||
|
context "when the current user is a support user" do |
||||||
|
let(:support_user) { FactoryBot.build(:user, :support) } |
||||||
|
|
||||||
|
before do |
||||||
|
allow(subsection).to receive(:form).and_return(form) |
||||||
|
allow(form).to receive(:current_user).and_return(support_user) |
||||||
|
end |
||||||
|
|
||||||
|
it "is shown" do |
||||||
|
expect(page.routed_to?(case_log)).to be true |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
context "when the current user is not a support user" do |
||||||
|
let(:user) { FactoryBot.build(:user) } |
||||||
|
|
||||||
|
before do |
||||||
|
allow(subsection).to receive(:form).and_return(form) |
||||||
|
allow(form).to receive(:current_user).and_return(user) |
||||||
|
end |
||||||
|
|
||||||
|
it "is not shown" do |
||||||
|
expect(page.routed_to?(case_log)).to be false |
||||||
|
end |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,76 @@ |
|||||||
|
require "rails_helper" |
||||||
|
|
||||||
|
RSpec.describe Form::Setup::Questions::CreatedById, type: :model do |
||||||
|
subject(:question) { described_class.new(question_id, question_definition, page) } |
||||||
|
|
||||||
|
let(:question_id) { nil } |
||||||
|
let(:question_definition) { nil } |
||||||
|
let(:page) { instance_double(Form::Page) } |
||||||
|
let(:subsection) { instance_double(Form::Subsection) } |
||||||
|
let(:form) { instance_double(Form) } |
||||||
|
let!(:user_1) { FactoryBot.create(:user, name: "first user") } |
||||||
|
let!(:user_2) { FactoryBot.create(:user, name: "second user") } |
||||||
|
let(:expected_answer_options) do |
||||||
|
{ |
||||||
|
"" => "Select an option", |
||||||
|
user_1.id => user_1.name, |
||||||
|
user_2.id => user_2.name, |
||||||
|
} |
||||||
|
end |
||||||
|
|
||||||
|
it "has correct page" do |
||||||
|
expect(question.page).to eq(page) |
||||||
|
end |
||||||
|
|
||||||
|
it "has the correct id" do |
||||||
|
expect(question.id).to eq("created_by_id") |
||||||
|
end |
||||||
|
|
||||||
|
it "has the correct header" do |
||||||
|
expect(question.header).to eq("Which user are you creating this log for?") |
||||||
|
end |
||||||
|
|
||||||
|
it "has the correct check_answer_label" do |
||||||
|
expect(question.check_answer_label).to eq("User") |
||||||
|
end |
||||||
|
|
||||||
|
it "has the correct type" do |
||||||
|
expect(question.type).to eq("select") |
||||||
|
end |
||||||
|
|
||||||
|
it "has the correct hint_text" do |
||||||
|
expect(question.hint_text).to eq("") |
||||||
|
end |
||||||
|
|
||||||
|
it "has the correct answer options" do |
||||||
|
expect(question.answer_options).to eq(expected_answer_options) |
||||||
|
end |
||||||
|
|
||||||
|
context "when the current user is support" do |
||||||
|
let(:support_user) { FactoryBot.build(:user, :support) } |
||||||
|
|
||||||
|
before do |
||||||
|
allow(page).to receive(:subsection).and_return(subsection) |
||||||
|
allow(subsection).to receive(:form).and_return(form) |
||||||
|
allow(form).to receive(:current_user).and_return(support_user) |
||||||
|
end |
||||||
|
|
||||||
|
it "is shown in check answers" do |
||||||
|
expect(question.hidden_in_check_answers).to be false |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
context "when the current user is not support" do |
||||||
|
let(:user) { FactoryBot.build(:user) } |
||||||
|
|
||||||
|
before do |
||||||
|
allow(page).to receive(:subsection).and_return(subsection) |
||||||
|
allow(subsection).to receive(:form).and_return(form) |
||||||
|
allow(form).to receive(:current_user).and_return(user) |
||||||
|
end |
||||||
|
|
||||||
|
it "is not shown in check answers" do |
||||||
|
expect(question.hidden_in_check_answers).to be true |
||||||
|
end |
||||||
|
end |
||||||
|
end |
Loading…
Reference in new issue