-
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.
- Loading branch information
Showing
13 changed files
with
236 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,75 @@ | ||
|
||
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 | ||
assert_select 'i[class="fa fa-exclamation-triangle"]' | ||
end | ||
end | ||
|
||
context 'when the nofification is normal' do | ||
let(:notifications) do | ||
[FactoryBot.create(:notification)] | ||
end | ||
|
||
it 'does not render an error icon' do | ||
assert_select 'i[class="fa fa-exclamation-triangle"]', false | ||
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_text(count.to_s + ' times') | ||
end | ||
end | ||
|
||
context 'when the notification occured once' do | ||
let(:count) { 1 } | ||
|
||
it 'does not display the count' do | ||
expect(rendered).not_to have_text(count.to_s + ' times') | ||
end | ||
end | ||
end | ||
end |