diff --git a/app/models/user.rb b/app/models/user.rb index 02b9af1..ed06635 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -60,6 +60,14 @@ def init self.username ||= email.split('@', 2)[0] end + def has_contact_request? + contact_requests.any? + end + + def notification_count + contact_requests.length + end + def sent_contact_request?(user) (user.contact_requests.include? self) or (contacts.include? user) end diff --git a/app/views/layouts/_navbar.html.erb b/app/views/layouts/_navbar.html.erb index e445b9f..8bad671 100644 --- a/app/views/layouts/_navbar.html.erb +++ b/app/views/layouts/_navbar.html.erb @@ -1,9 +1,16 @@ <%# https://getbootstrap.com/docs/4.5/components/navbar/ %> \ No newline at end of file + diff --git a/spec/features/header_and_footer_spec.rb b/spec/features/header_and_footer_spec.rb index 9d4d236..2b297e6 100644 --- a/spec/features/header_and_footer_spec.rb +++ b/spec/features/header_and_footer_spec.rb @@ -18,4 +18,20 @@ expect(page).not_to have_link('My contacts') end end + + describe 'notification' do + let(:alice) { FactoryBot.create :user } + let(:bob) { FactoryBot.create :user } + + before do + alice.contact_requests << bob # bob added a contact request for alice + sign_in alice + visit notes_path + end + + it 'shows the number of notifications in the navbar' do + element = page.find('nav.navbar') + expect(element).to have_text '1' + end + end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index d68be20..af0f36a 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -106,6 +106,18 @@ user.contacts << contact expect(user.sent_contact_request?(contact)).to be true end + + it 'determines if there are any contact requests' do + expect(user.has_contact_request?).to be false + user.contact_requests << contact + expect(user.has_contact_request?).to be true + end + + it 'counts my contact requests' do + expect(user.count_contact_requests).to be 0 + user.contact_requests << contact + expect(user.count_contact_requests).to be 1 + end end describe 'status scope' do