Browse Source

Add user question

pull/674/head
baarkerlounger 3 years ago
parent
commit
c6d06c2363
  1. 4
      app/controllers/case_logs_controller.rb
  2. 2
      app/models/case_log.rb
  3. 24
      app/models/form/setup/pages/created_by.rb
  4. 30
      app/models/form/setup/questions/created_by_id.rb
  5. 1
      app/models/form/setup/subsections/setup.rb
  6. 65
      spec/models/form/setup/pages/created_by_spec.rb
  7. 76
      spec/models/form/setup/questions/created_by_id_spec.rb
  8. 11
      spec/models/form/setup/subsections/setup_spec.rb
  9. 2
      spec/models/form_handler_spec.rb

4
app/controllers/case_logs_controller.rb

@ -109,9 +109,7 @@ private
end
def case_log_params
if current_user && current_user.support?
{ "created_by_id": current_user.id }.merge(api_case_log_params)
elsif current_user
if current_user && !current_user.support?
org_params.merge(api_case_log_params)
else
api_case_log_params

2
app/models/case_log.rb

@ -34,7 +34,7 @@ class CaseLog < ApplicationRecord
belongs_to :owning_organisation, class_name: "Organisation", optional: true
belongs_to :managing_organisation, class_name: "Organisation", optional: true
belongs_to :created_by, class_name: "User"
belongs_to :created_by, class_name: "User", optional: true
scope :filter_by_organisation, ->(org, _user = nil) { where(owning_organisation: org).or(where(managing_organisation: org)) }
scope :filter_by_status, ->(status, _user = nil) { where status: }

24
app/models/form/setup/pages/created_by.rb

@ -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

30
app/models/form/setup/questions/created_by_id.rb

@ -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

1
app/models/form/setup/subsections/setup.rb

@ -10,6 +10,7 @@ class Form::Subsections::Setup < ::Form::Subsection
def pages
[
Form::Setup::Pages::Organisation.new(nil, nil, self),
Form::Setup::Pages::CreatedBy.new(nil, nil, self),
Form::Setup::Pages::NeedsType.new(nil, nil, self),
Form::Setup::Pages::Renewal.new(nil, nil, self),
Form::Setup::Pages::TenancyStartDate.new(nil, nil, self),

65
spec/models/form/setup/pages/created_by_spec.rb

@ -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

76
spec/models/form/setup/questions/created_by_id_spec.rb

@ -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

11
spec/models/form/setup/subsections/setup_spec.rb

@ -12,7 +12,16 @@ RSpec.describe Form::Setup::Subsections::Setup, type: :model do
end
it "has correct pages" do
expect(setup.pages.map(&:id)).to eq(%w[organisation needs_type renewal tenancy_start_date rent_type tenant_code property_reference])
expect(setup.pages.map(&:id)).to eq(
%w[organisation
created_by
needs_type
renewal
tenancy_start_date
rent_type
tenant_code
property_reference],
)
end
it "has the correct id" do

2
spec/models/form_handler_spec.rb

@ -17,7 +17,7 @@ RSpec.describe FormHandler do
form_handler = described_class.instance
form = form_handler.get_form(test_form_name)
expect(form).to be_a(Form)
expect(form.pages.count).to eq(42)
expect(form.pages.count).to eq(43)
end
end

Loading…
Cancel
Save