Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added notifications #161

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ def init
self.username ||= email.split('@', 2)[0]
end

def has_contact_request?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def has_contact_request?
def notifications?

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
Expand Down
17 changes: 12 additions & 5 deletions app/views/layouts/_navbar.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
<%# https://getbootstrap.com/docs/4.5/components/navbar/ %>
<nav class="navbar navbar-expand-md navbar-dark bg-dark">
<div class="navbar-brand"><%= link_to I18n.t('index.portal_name'), root_path, class: 'portal-link' %></div>
<form class="form-inline">
<i class="fa fa-bell" style="color: white; padding: 10px"></i>
<a href="<%= search_users_path %>"><i class="fa fa-search" style="color: white; padding: 10px"></i> </a>
</form>
<% if user_signed_in? %>
<form class="form-inline">
<% if current_user.has_contact_request? %>
<a href="<%= user_contact_requests_path(current_user) %>"> <i class="fa fa-bell" style="color: red; padding: 3px"></i> </a>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would extract all styles into an appropriate css file

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't like to do that, because if someone else uses the bell icon, they don't necessarily want my styling...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes but you could use a class like notification-bell or something like that. I just don't like to have HTML, CSS and application logic all in one file 😅 But if you prefer the inline styling that's fine for me because it's not such a big issue 👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @felixauringer. If someone doesn't like the styling, the class can be changed even easier

<span class="badge badge-pill badge-danger"><%= current_user.count_contact_requests%></span>
<% else %>
<i class="fa fa-bell" style="color: white; padding: 10px"></i>
<% end %>
<a href="<%= search_users_path %>"><i class="fa fa-search" style="color: white; padding: 10px"></i> </a>
</form>
<% end %>
<%= render 'layouts/navbar_desktop_part' %>
</nav>
</nav>
16 changes: 16 additions & 0 deletions spec/features/header_and_footer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 12 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down