Browse Source
* [CLDC-2202] Allow coordinators to set created_by * Scope user selection when data coordinator * Remove managing_for_other_user_enabled * Move sales created_by page and question out of common * Address comments - only select required users - remove not needed CYA checkspull/1601/head
Jack
2 years ago
committed by
GitHub
15 changed files with 256 additions and 247 deletions
@ -1,16 +0,0 @@
|
||||
class Form::Common::Pages::CreatedBy < ::Form::Page |
||||
def initialize(id, hsh, subsection) |
||||
super |
||||
@id = "created_by" |
||||
end |
||||
|
||||
def questions |
||||
@questions ||= [ |
||||
Form::Common::Questions::CreatedById.new(nil, nil, self), |
||||
] |
||||
end |
||||
|
||||
def routed_to?(_log, current_user) |
||||
!!current_user&.support? |
||||
end |
||||
end |
@ -1,46 +0,0 @@
|
||||
class Form::Common::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?" |
||||
@type = "select" |
||||
end |
||||
|
||||
def answer_options |
||||
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(log, _user = nil) |
||||
return answer_options unless log.owning_organisation |
||||
|
||||
user_ids = log.owning_organisation.users.pluck(:id) + [""] |
||||
answer_options.select { |k, _v| user_ids.include?(k) } |
||||
end |
||||
|
||||
def label_from_value(value, _log = nil, _user = nil) |
||||
return unless value |
||||
|
||||
answer_options[value] |
||||
end |
||||
|
||||
def hidden_in_check_answers?(_log, current_user) |
||||
!current_user.support? |
||||
end |
||||
|
||||
def derived? |
||||
true |
||||
end |
||||
|
||||
private |
||||
|
||||
def selected_answer_option_is_derived?(_log) |
||||
false |
||||
end |
||||
end |
@ -0,0 +1,19 @@
|
||||
class Form::Sales::Pages::CreatedBy < ::Form::Page |
||||
def initialize(id, hsh, subsection) |
||||
super |
||||
@id = "created_by" |
||||
end |
||||
|
||||
def questions |
||||
@questions ||= [ |
||||
Form::Sales::Questions::CreatedById.new(nil, nil, self), |
||||
] |
||||
end |
||||
|
||||
def routed_to?(_log, current_user) |
||||
return true if current_user&.support? |
||||
return true if current_user&.data_coordinator? |
||||
|
||||
false |
||||
end |
||||
end |
@ -0,0 +1,54 @@
|
||||
class Form::Sales::Questions::CreatedById < ::Form::Question |
||||
ANSWER_OPTS = { "" => "Select an option" }.freeze |
||||
|
||||
def initialize(id, hsh, page) |
||||
super |
||||
@id = "created_by_id" |
||||
@check_answer_label = "User" |
||||
@header = "Which user are you creating this log for?" |
||||
@type = "select" |
||||
end |
||||
|
||||
def answer_options |
||||
ANSWER_OPTS |
||||
end |
||||
|
||||
def displayed_answer_options(log, current_user = nil) |
||||
return ANSWER_OPTS unless log.owning_organisation |
||||
return ANSWER_OPTS unless current_user |
||||
|
||||
users = current_user.support? ? log.owning_organisation.users : current_user.organisation.users |
||||
|
||||
users.each_with_object(ANSWER_OPTS.dup) do |user, hsh| |
||||
hsh[user.id] = present_user(user) |
||||
hsh |
||||
end |
||||
end |
||||
|
||||
def label_from_value(value, _log = nil, _user = nil) |
||||
return unless value |
||||
|
||||
present_user(User.find(value)) |
||||
end |
||||
|
||||
def hidden_in_check_answers?(_log, current_user) |
||||
return false if current_user.support? |
||||
return false if current_user.data_coordinator? |
||||
|
||||
true |
||||
end |
||||
|
||||
def derived? |
||||
true |
||||
end |
||||
|
||||
private |
||||
|
||||
def present_user(user) |
||||
"#{user.name} (#{user.email})" |
||||
end |
||||
|
||||
def selected_answer_option_is_derived?(_log) |
||||
false |
||||
end |
||||
end |
@ -1,82 +0,0 @@
|
||||
require "rails_helper" |
||||
|
||||
RSpec.describe Form::Common::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 be_nil |
||||
end |
||||
|
||||
it "has the correct answer options" do |
||||
expect(question.answer_options).to eq(expected_answer_options) |
||||
end |
||||
|
||||
it "is marked as derived" do |
||||
expect(question.derived?).to be true |
||||
end |
||||
|
||||
context "when the current user is support" do |
||||
let(:support_user) { FactoryBot.build(:user, :support) } |
||||
|
||||
it "is shown in check answers" do |
||||
expect(question.hidden_in_check_answers?(nil, support_user)).to be false |
||||
end |
||||
end |
||||
|
||||
context "when the current user is not support" do |
||||
let(:user) { FactoryBot.build(:user) } |
||||
|
||||
it "is not shown in check answers" do |
||||
expect(question.hidden_in_check_answers?(nil, user)).to be true |
||||
end |
||||
end |
||||
|
||||
context "when the owning organisation is already set" do |
||||
let(:lettings_log) { FactoryBot.create(:lettings_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(lettings_log)).to eq(expected_answer_options) |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,88 @@
|
||||
require "rails_helper" |
||||
|
||||
RSpec.describe Form::Sales::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) } |
||||
|
||||
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 be_nil |
||||
end |
||||
|
||||
it "is marked as derived" do |
||||
expect(question.derived?).to be true |
||||
end |
||||
|
||||
context "when the current user is support" do |
||||
let(:support_user) { build(:user, :support) } |
||||
|
||||
it "is shown in check answers" do |
||||
expect(question.hidden_in_check_answers?(nil, support_user)).to be false |
||||
end |
||||
|
||||
describe "#displayed_answer_options" do |
||||
let(:owning_org_user) { create(:user) } |
||||
let(:sales_log) { create(:sales_log, owning_organisation: owning_org_user.organisation) } |
||||
let(:expected_answer_options) do |
||||
{ |
||||
"" => "Select an option", |
||||
owning_org_user.id => "#{owning_org_user.name} (#{owning_org_user.email})", |
||||
} |
||||
end |
||||
|
||||
it "only displays users that belong to the owning organisation" do |
||||
expect(question.displayed_answer_options(sales_log, support_user)).to eq(expected_answer_options) |
||||
end |
||||
end |
||||
end |
||||
|
||||
context "when the current user is data_coordinator" do |
||||
let(:data_coordinator) { create(:user, :data_coordinator) } |
||||
|
||||
it "is shown in check answers" do |
||||
expect(question.hidden_in_check_answers?(nil, data_coordinator)).to be false |
||||
end |
||||
|
||||
describe "#displayed_answer_options" do |
||||
let(:owning_org_user) { create(:user) } |
||||
let(:sales_log) { create(:sales_log, owning_organisation: owning_org_user.organisation) } |
||||
let!(:user_in_same_org) { create(:user, organisation: data_coordinator.organisation) } |
||||
|
||||
let(:expected_answer_options) do |
||||
{ |
||||
"" => "Select an option", |
||||
user_in_same_org.id => "#{user_in_same_org.name} (#{user_in_same_org.email})", |
||||
data_coordinator.id => "#{data_coordinator.name} (#{data_coordinator.email})", |
||||
} |
||||
end |
||||
|
||||
it "only displays users that belong user's org" do |
||||
expect(question.displayed_answer_options(sales_log, data_coordinator)).to eq(expected_answer_options) |
||||
end |
||||
end |
||||
end |
||||
end |
Loading…
Reference in new issue