-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Notify all admins whenever an error is logged * Add custom styling to error notifications * Fix missing tag close * User custom formatter isntead of logger Fixes issue that caused logs not going to STDOUT due to some weird Meta-Programming-issues * Add explaining comment to conditional inheritance * Migrate tests to HartFormatter * Refactor tests * Use centralized config for formatter * Use icon to indicate errors instead of red text * Avoid duplicate notifications Add a lot of tests * Only remove duplicate notifications that happened during the same minute * refactorings * Try to fix tests on travis * Apply suggested changes * Fix(Hart Formatter): Make it really clear why different loggers are used * Fix(Notification): Don't overwrite created_at, set during new * Comment(User): Ordering of notifications * Comment(User): no way to log a failing save for notifications * Comment(application.rb): Describe what the custom logger does * Fix(Hart Formatter Spec, User Spec): 'be ==' -> 'eq' * Fix(Notification Views Spec): assert_select -> expect * Fix(Notifications Views Spec): Use string interpolation * Fix(Notification List Spec): Ensure matches in notifications
- Loading branch information
1 parent
6b7ff09
commit cf3ca9e
Showing
13 changed files
with
244 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
# When running in production mode use the default `Formatter`, which outputs all information | ||
# When running outside of production, e.g. in development, use `SimpleFormatter`, | ||
# which only displays the message, suppressing PID and timestamp | ||
# https://www.rubydoc.info/github/rails/rails/ActiveSupport/Logger/SimpleFormatter | ||
base_logger = Rails.env == 'production' ? ActiveSupport::Logger::Formatter : ActiveSupport::Logger::SimpleFormatter | ||
|
||
class HartFormatter < base_logger | ||
def call(severity, timestamp, progname, msg) | ||
if severity == 'ERROR' | ||
User.admin.each do |admin| | ||
admin.notify 'An Error occured!', msg.to_s, type: :error | ||
end | ||
end | ||
|
||
super(severity, timestamp, progname, msg) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
class AddTypeToNotifications < ActiveRecord::Migration[5.2] | ||
def change | ||
add_column :notifications, :notification_type, :integer, default: 0 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddCountToNotifications < ActiveRecord::Migration[5.2] | ||
def change | ||
add_column :notifications, :count, :integer, default: 1 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
describe HartFormatter do | ||
|
||
before do | ||
# ensure we have at least one admin | ||
FactoryBot.create :admin | ||
end | ||
|
||
it 'notifies admins when an error is logged' do | ||
expect do | ||
Rails.logger.error('My Error') | ||
end.to change(Notification, :count).by(User.admin.size) | ||
end | ||
|
||
it 'does not notify admins when the log is not an error' do | ||
expect do | ||
Rails.logger.info('My Info') | ||
end.not_to change(Notification, :count) | ||
end | ||
|
||
it 'does not notify admins multiple times when the same error occurs' do | ||
expect do | ||
3.times { Rails.logger.error('My Error') } | ||
end.to change(Notification, :count).by(User.admin.size) | ||
end | ||
|
||
it 'sets the notification count correctly' do | ||
3.times { Rails.logger.error('My Error') } | ||
expect(Notification.all.first.count).to eq(3) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe 'notifications/_list.html.erb', type: :view do | ||
|
||
before do | ||
assign :notifications, notifications | ||
render | ||
end | ||
|
||
describe 'list of notifications' do | ||
|
||
let(:read_notification) { FactoryBot.create :read_notification } | ||
|
||
let(:unread_notification) { FactoryBot.create :notification } | ||
|
||
let(:notifications) do | ||
[FactoryBot.create(:error_notification), read_notification, unread_notification] | ||
end | ||
|
||
it 'renders the title of each notification' do | ||
notifications.each do |notification| | ||
expect(rendered).to have_text(notification.title) | ||
end | ||
end | ||
|
||
it 'renders the message of each notification' do | ||
notifications.each do |notification| | ||
expect(rendered).to have_text(notification.message) | ||
end | ||
end | ||
end | ||
|
||
context 'when the notification is an error' do | ||
let(:notifications) do | ||
[FactoryBot.create(:error_notification)] | ||
end | ||
|
||
it 'renders an error icon' do | ||
expect(rendered).to have_css('.notification') do | ||
have_css('i', class: 'fa fa-exclamation-triangle') | ||
end | ||
end | ||
end | ||
|
||
context 'when the nofification is normal' do | ||
let(:notifications) do | ||
[FactoryBot.create(:notification)] | ||
end | ||
|
||
it 'does not render an error icon' do | ||
expect(rendered).to have_css('.notification') do | ||
not_have_css('i', class: 'fa fa-exclamation-triangle') | ||
end | ||
end | ||
end | ||
|
||
describe 'notification count' do | ||
# we can declare :count later because let works using lazy initialization | ||
# pro tip: if you want to use a non-lazy let, you can use let! | ||
let(:notifications) { [FactoryBot.create(:notification, count: count)] } | ||
|
||
context 'when the notification occurred multiple times' do | ||
let(:count) { 3 } | ||
|
||
it 'displays the count' do | ||
expect(rendered).to have_css('.notification') do | ||
have_text("#{count} times") | ||
end | ||
end | ||
end | ||
|
||
context 'when the notification occured once' do | ||
let(:count) { 1 } | ||
|
||
it 'does not display the count' do | ||
expect(rendered).to have_css('.notification') do | ||
not_have_text("#{count} times") | ||
end | ||
end | ||
end | ||
end | ||
end |