Browse Source
* Add an organisation question for the support user (WIP) Co-authored-by: baarkerlounger <baarkerlounger@users.noreply.github.com> * All the things are green * Add user question * Restrict shown answer options based on already answered questions * Derive managing org * Guard against missing DB connection * Remove empty file * Only select the fields we actually need * All is redundant * Fix spec description * Fix comments Co-authored-by: Kat <katrina@madetech.com> Co-authored-by: baarkerlounger <baarkerlounger@users.noreply.github.com>pull/681/head
baarkerlounger
3 years ago
committed by
GitHub
22 changed files with 502 additions and 13 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,24 @@
|
||||
class Form::Setup::Pages::Organisation < ::Form::Page |
||||
def initialize(id, hsh, subsection) |
||||
super |
||||
@id = "organisation" |
||||
@header = "" |
||||
@description = "" |
||||
@questions = questions |
||||
@subsection = subsection |
||||
end |
||||
|
||||
def questions |
||||
[ |
||||
Form::Setup::Questions::OwningOrganisationId.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,39 @@
|
||||
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" } |
||||
return answer_opts unless ActiveRecord::Base.connected? |
||||
|
||||
User.select(:id, :name).each_with_object(answer_opts) do |user, hsh| |
||||
hsh[user.id] = user.name |
||||
hsh |
||||
end |
||||
end |
||||
|
||||
def displayed_answer_options(case_log) |
||||
return answer_options unless case_log.owning_organisation |
||||
|
||||
user_ids = case_log.owning_organisation.users.pluck(:id) + [""] |
||||
answer_options.select { |k, _v| user_ids.include?(k) } |
||||
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,39 @@
|
||||
class Form::Setup::Questions::OwningOrganisationId < ::Form::Question |
||||
def initialize(id, hsh, page) |
||||
super |
||||
@id = "owning_organisation_id" |
||||
@check_answer_label = "Owning organisation" |
||||
@header = "Which organisation is the owning organisation for this log?" |
||||
@hint_text = "" |
||||
@type = "select" |
||||
@page = page |
||||
@answer_options = answer_options_values |
||||
end |
||||
|
||||
def answer_options_values |
||||
answer_opts = { "" => "Select an option" } |
||||
return answer_opts unless ActiveRecord::Base.connected? |
||||
|
||||
Organisation.select(:id, :name).each_with_object(answer_opts) do |organisation, hsh| |
||||
hsh[organisation.id] = organisation.name |
||||
hsh |
||||
end |
||||
end |
||||
|
||||
def displayed_answer_options(case_log) |
||||
return answer_options unless case_log.created_by |
||||
|
||||
ids = ["", case_log.created_by.organisation.id] |
||||
answer_options.select { |k, _v| ids.include?(k) } |
||||
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,65 @@
|
||||
require "rails_helper" |
||||
|
||||
RSpec.describe Form::Setup::Pages::Organisation, 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[owning_organisation_id]) |
||||
end |
||||
|
||||
it "has the correct id" do |
||||
expect(page.id).to eq("organisation") |
||||
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,90 @@
|
||||
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 |
||||
|
||||
context "when the owning organisation is already set" do |
||||
let(:case_log) { FactoryBot.create(:case_log, owning_organisation: user_2.organisation) } |
||||
let(:expected_answer_options) do |
||||
{ |
||||
"" => "Select an option", |
||||
user_2.id => user_2.name, |
||||
} |
||||
end |
||||
|
||||
it "only displays users that belong to that organisation" do |
||||
expect(question.displayed_answer_options(case_log)).to eq(expected_answer_options) |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,91 @@
|
||||
require "rails_helper" |
||||
|
||||
RSpec.describe Form::Setup::Questions::OwningOrganisationId, 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!(:organisation_1) { FactoryBot.create(:organisation, name: "first test org") } |
||||
let!(:organisation_2) { FactoryBot.create(:organisation, name: "second test org") } |
||||
let(:expected_answer_options) do |
||||
{ |
||||
"" => "Select an option", |
||||
organisation_1.id => organisation_1.name, |
||||
organisation_2.id => organisation_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("owning_organisation_id") |
||||
end |
||||
|
||||
it "has the correct header" do |
||||
expect(question.header).to eq("Which organisation is the owning organisation for this log?") |
||||
end |
||||
|
||||
it "has the correct check_answer_label" do |
||||
expect(question.check_answer_label).to eq("Owning organisation") |
||||
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 |
||||
|
||||
context "when the user is already set" do |
||||
let(:user) { FactoryBot.create(:user, organisation: organisation_2) } |
||||
let(:case_log) { FactoryBot.create(:case_log, created_by: user) } |
||||
let(:expected_answer_options) do |
||||
{ |
||||
"" => "Select an option", |
||||
organisation_2.id => organisation_2.name, |
||||
} |
||||
end |
||||
|
||||
it "only displays users that belong to that organisation" do |
||||
expect(question.displayed_answer_options(case_log)).to eq(expected_answer_options) |
||||
end |
||||
end |
||||
end |
Loading…
Reference in new issue