Skip to content

Commit 2c3a7c6

Browse files
authored
Resolves #5153: Filter by user should show email if no name (#5156)
* feat: add method preferred_name * feat: add predferred_name to view * feat: add tests for preferred_name method in user model * fix: correct indentation for let statements in user_spec * Add user filter dropdown tests for preferred names in adjustments index * Remove redundant test for current user's preferred name in user filter dropdown
1 parent 794ef20 commit 2c3a7c6

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

app/models/user.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ def display_name
9595
name.presence || "Name Not Provided"
9696
end
9797

98+
def preferred_name
99+
name.presence || email
100+
end
101+
98102
def formatted_email
99103
email.present? ? "#{name} <#{email}>" : ""
100104
end

app/views/adjustments/index.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
</div>
3939
<div class="form-group col-lg-4 col-md-4 col-sm-6 col-xs-12">
40-
<%= filter_select(scope: :by_user, collection: @users, key: :id, value: :name, selected: @selected_user) %>
40+
<%= filter_select(scope: :by_user, collection: @users, key: :id, value: :preferred_name, selected: @selected_user) %>
4141
</div>
4242
<div class="form-group col-lg-4 col-md-4 col-sm-6 col-xs-12">
4343
<%= label_tag "Date Range" %>

spec/models/user_spec.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,35 @@
6464
end
6565
end
6666

67+
describe '#preferred_name' do
68+
let(:user) { create(:user, name: name, email: email) }
69+
let(:email) { '[email protected]' }
70+
71+
context 'when name is present' do
72+
let(:name) { 'John Smith' }
73+
74+
it 'returns the name' do
75+
expect(user.preferred_name).to eq('John Smith')
76+
end
77+
end
78+
79+
context 'when name is nil' do
80+
let(:name) { nil }
81+
82+
it 'returns the email' do
83+
expect(user.preferred_name).to eq('[email protected]')
84+
end
85+
end
86+
87+
context 'when name is an empty string' do
88+
let(:name) { '' }
89+
90+
it 'returns the email' do
91+
expect(user.preferred_name).to eq('[email protected]')
92+
end
93+
end
94+
end
95+
6796
describe "Scopes >" do
6897
describe "->alphabetized" do
6998
let!(:z_name_user) { create(:user, name: 'Zachary') }

spec/requests/adjustments_requests_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,19 @@
6868
end
6969
end
7070
end
71+
72+
context 'dropdown for user filter' do
73+
let!(:another_user) { create(:user, organization: organization, name: "Alice Smith", email: "[email protected]") }
74+
let!(:user_without_name) { create(:user, organization: organization, name: nil, email: "[email protected]") }
75+
76+
before { get adjustments_path } # Make the request once for this context
77+
78+
it "displays the preferred name of users in the dropdown" do
79+
expect(response.body).to include("<option value=\"#{another_user.id}\">#{another_user.preferred_name}</option>")
80+
expect(response.body).to include("<option value=\"#{user_without_name.id}\">#{user_without_name.preferred_name}</option>")
81+
expect(response.body).to include("<option value=\"#{user.id}\">#{user.preferred_name}</option>")
82+
end
83+
end
7184
end
7285

7386
context "csv" do

0 commit comments

Comments
 (0)