Browse Source

make email appear in created by if name is blank (#773)

pull/776/head
Dushan 2 years ago committed by GitHub
parent
commit
2a6774d904
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      app/models/form/setup/questions/created_by_id.rb
  2. 2
      spec/models/form/setup/questions/created_by_id_spec.rb

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

@ -13,8 +13,9 @@ class Form::Setup::Questions::CreatedById < ::Form::Question
answer_opts = { "" => "Select an option" } answer_opts = { "" => "Select an option" }
return answer_opts unless ActiveRecord::Base.connected? return answer_opts unless ActiveRecord::Base.connected?
User.select(:id, :name).each_with_object(answer_opts) do |user, hsh| User.select(:id, :name, :email).each_with_object(answer_opts) do |user, hsh|
hsh[user.id] = user.name hsh[user.id] = user.name if user.name.present?
hsh[user.id] = user.email if user.name.blank?
hsh hsh
end end
end end

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

@ -10,11 +10,13 @@ RSpec.describe Form::Setup::Questions::CreatedById, type: :model do
let(:form) { instance_double(Form) } let(:form) { instance_double(Form) }
let!(:user_1) { FactoryBot.create(:user, name: "first user") } let!(:user_1) { FactoryBot.create(:user, name: "first user") }
let!(:user_2) { FactoryBot.create(:user, name: "second user") } let!(:user_2) { FactoryBot.create(:user, name: "second user") }
let!(:user_3) { FactoryBot.create(:user, name: nil, email: "madeupmail@example.com") }
let(:expected_answer_options) do let(:expected_answer_options) do
{ {
"" => "Select an option", "" => "Select an option",
user_1.id => user_1.name, user_1.id => user_1.name,
user_2.id => user_2.name, user_2.id => user_2.name,
user_3.id => user_3.email,
} }
end end

Loading…
Cancel
Save