Skip to content

Commit

Permalink
refactor notification helper
Browse files Browse the repository at this point in the history
  • Loading branch information
fosterfarrell9 committed Jan 5, 2019
1 parent 1ff7d0a commit 2f5eac9
Show file tree
Hide file tree
Showing 12 changed files with 252 additions and 178 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2017 fosterfarrell9
Copyright (c) 2017 Denis Vogel

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
7 changes: 6 additions & 1 deletion app/assets/javascripts/notifications.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/

# adjust generic counters and dropdown menu after notification ist destroyed
adjustNotificationCounter = (notificationId) ->
# remove dropdown item
$('[data-itemNotification="'+notificationId+'"]').remove()
Expand All @@ -18,11 +19,13 @@ adjustNotificationCounter = (notificationId) ->
document.title = 'MaMpf'
# this is only relevant for index page
$('#notificationCardRow')
.append('<div class="col-12">Es liegen keine neuen Benachrichtigungen für Dich vor.</div>')
.append('<div class="col-12">Es liegen keine neuen Benachrichtigungen
für Dich vor.</div>')
return

$(document).on 'turbolinks:load', ->

# clean up after lecture notification is clicked away
$('.removeLectureNotification').on 'click', ->
notificationId = $(this).data('id')
lectureId = $(this).data('lecture')
Expand All @@ -39,6 +42,7 @@ $(document).on 'turbolinks:load', ->
adjustNotificationCounter(notificationId)
return

# clean up after notification is clicked away in user's notification overview
$('.removeNotification').on 'click', ->
notificationId = $(this).data('id')
# fade out notification card in notification index
Expand All @@ -48,6 +52,7 @@ $(document).on 'turbolinks:load', ->
adjustNotificationCounter(notificationId)
return

# clean up after news notification is clicked away
$('.removeNewsNotification').on 'click', ->
notificationId = $(this).data('id')
console.log 'Hi'
Expand Down
6 changes: 5 additions & 1 deletion app/assets/stylesheets/colors.scss
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
.blue
{
background-color: blue;
background-color: blue;
}

.redtext {
color: red;
}

.darkblue {
color: #2251dd;
}

.white
{
background-color: white;
Expand Down
9 changes: 8 additions & 1 deletion app/helpers/announcements_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
module AnnouncementsHelper
# create text for notification about new announcement in notification dropdown
# menu
def announcement_notification_header(announcement)
def announcement_notification_item_header(announcement)
text = 'Neue Mitteilung '
if announcement.lecture.present?
return text + 'in ' + announcement.lecture.title_for_viewers
Expand All @@ -16,4 +16,11 @@ def news_card_color(announcement)
return 'bg-post-it-blue' if announcement.active?(current_user)
''
end

# create text for lecture announcement in notification card header
def announcement_notification_card_header(announcement)
link_to(announcement.lecture.title_for_viewers,
announcement.lecture.path(current_user),
class: 'text-dark')
end
end
20 changes: 17 additions & 3 deletions app/helpers/courses_helper.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
module CoursesHelper
# create text for notification about new course in notification dropdown menu
def course_notification_header(course)
text = 'Neues Modul' + course.title
def course_notification_item_header(course)
'Neues Modul ' + course.title
end

def course_notification_details(course)
# create text for notification card
def course_notification_item_details(course)
'Über Deine Profileinstellungen kannst Du es abonnieren.'
end

# create text for notification about new course in notification card
def course_notification_card_text(course)
'Neues Modul angelegt:' + tag(:br) + course.title
end

# create link for notification about new lecture in notification card
def course_notification_card_link
'Du kannst es über Deine ' +
link_to('Profileinstellungen', edit_profile_path,
class: 'darkblue') +
' abonnieren.'
end
end
19 changes: 17 additions & 2 deletions app/helpers/lectures_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,26 @@ def lecture_deletable?(lecture, inspection)
end

# create text for notification about new lecture in notification dropdown menu
def lecture_notification_header(lecture)
def lecture_notification_item_header(lecture)
text = 'Neue Vorlesung ' + lecture.title_for_viewers
end

def lecture_notification_details(lecture)
# create text for notification card
def lecture_notification_item_details(lecture)
'Über Deine Profileinstellungen kannst Du sie abonnieren.'
end

# create text for notification about new course in notification card
def lecture_notification_card_text(lecture)
'Neue Vorlesung angelegt:' + tag(:br) + lecture.course.title +
' (' + lecture.term.to_label + ', ' + lecture.teacher.name + ')'
end

# create link for notification about new course in notification card
def lecture_notification_card_link
'Du kannst sie über Deine ' +
link_to('Profileinstellungen', edit_profile_path,
class: 'darkblue') +
' abonnieren.'
end
end
18 changes: 16 additions & 2 deletions app/helpers/media_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,25 @@ def inspect_or_edit_medium_path(medium, inspection)
end

# create text for notification about new medium in notification dropdown menu
def medium_notification_header(medium)
def medium_notification_item_header(medium)
text = 'Neues Medium in ' + medium.teachable.media_scope.title_for_viewers
end

def medium_notification_details(medium)
def medium_notification_item_details(medium)
medium.local_title_for_viewers
end

# create text for notification about new medium in notification card
def medium_notification_card_header(medium)
link_to(medium.teachable.media_scope.title_for_viewers,
polymorphic_path(medium.teachable.media_scope),
class: 'text-dark')
end

# create link to medium in notification card
def medium_notification_card_link(medium)
link_to(medium.local_title_for_viewers,
medium,
class: 'darkblue')
end
end
101 changes: 43 additions & 58 deletions app/helpers/notifications_helper.rb
Original file line number Diff line number Diff line change
@@ -1,93 +1,78 @@
# Notifications Helper
module NotificationsHelper
# create text for notification in notification dropdown menu
def notification_menu_item_header(notification)
notifiable = notification.notifiable
return medium_notification_header(notifiable) if notification.medium?
return course_notification_header(notifiable) if notification.course?
return lecture_notification_header(notifiable) if notification.lecture?
announcement_notification_header(notifiable)
return medium_notification_item_header(notifiable) if notification.medium?
return course_notification_item_header(notifiable) if notification.course?
return lecture_notification_item_header(notifiable) if notification.lecture?
announcement_notification_item_header(notifiable)
end

# create text for notification details in notification dropdown menu
def notification_menu_item_details(notification)
notifiable = notification.notifiable
return medium_notification_details(notifiable) if notification.medium?
return course_notification_details(notifiable) if notification.course?
return lecture_notification_details(notifiable) if notification.lecture?
return medium_notification_item_details(notifiable) if notification.medium?
return course_notification_item_details(notifiable) if notification.course?
if notification.lecture?
return lecture_notification_item_details(notifiable)
end
''
end

# determine the color of a notification card
def notification_color(notification)
return 'bg-post-it-green' if notification.sesam?
return 'bg-post-it-pink' if notification.nuesse?
return 'bg-post-it-light-green' if notification.quiz?
return 'bg-post-it-blue' if notification.generic_announcement?
return 'bg-post-it-red' if notification.announcement?
return 'bg-post-it-orange' if notification.course? || notification.lecture?
'bg-post-it-yellow'
end

# provide text or link for header of notification card
def notification_header(notification)
notifiable = notification.notifiable
text = if notification.medium?
link_to(notifiable.teachable.media_scope.title_for_viewers,
polymorphic_path(notifiable.teachable.media_scope),
class: 'text-dark')
medium_notification_card_header(notifiable)
elsif notification.course? || notification.lecture?
'Kursangebot'
elsif notifiable.lecture.present?
link_to(notifiable.lecture.title_for_viewers,
notifiable.lecture.path(current_user),
class: 'text-dark')
elsif notification.lecture_announcement?
announcement_notification_card_header(notifiable)
else
link_to 'MaMpf-News', news_path, class: 'text-dark'
end
text.html_safe
end

def notification_color(notification)
notifiable = notification.notifiable
if notifiable.class.to_s == 'Medium' && notifiable.sort == 'Sesam'
return 'bg-post-it-green'
elsif notifiable.class.to_s == 'Medium' && notifiable.sort == 'Nuesse'
return 'bg-post-it-pink'
elsif notifiable.class.to_s == 'Medium' && notifiable.sort == 'KeksQuiz'
return 'bg-post-it-light-green'
elsif notifiable.class.to_s == 'Announcement' && notifiable.lecture.nil?
return 'bg-post-it-blue'
elsif notifiable.class.to_s == 'Announcement'
return 'bg-post-it-red'
elsif notifiable.class.to_s.in?(['Lecture', 'Course', 'Announcement'])
return 'bg-post-it-orange'
end
'bg-post-it-yellow'
end

# provide text for body of notification card
def notification_text(notification)
notifiable = notification.notifiable
return unless notifiable.class.to_s
.in?(Notification.allowed_notifiable_types)
text = if notifiable.class.to_s == 'Medium'
text = if notification.medium?
'Neues Medium angelegt:'
elsif notifiable.class.to_s == 'Course'
'Neues Modul angelegt:' + tag(:br) + notifiable.course.title
elsif notifiable.class.to_s == 'Lecture'
'Neue Vorlesung angelegt:' + tag(:br) + notifiable.course.title +
' (' + notifiable.term.to_label + ', ' +
notifiable.teacher.name + ')'
elsif notification.course?
course_notification_card_text(notifiable)
elsif notification.lecture?
lecture_notification_card_text(notifiable)
else
'Neue Mitteilung:'
end
text.html_safe
end

# provide link for body of notification card
def notification_link(notification)
notifiable = notification.notifiable
if notifiable.class.to_s == 'Medium'
return link_to(notifiable.local_title_for_viewers, notifiable,
style: 'color: #2251dd;')
elsif notifiable.class.to_s.in?(['Lecture', 'Course'])
return ('Du kannst sie über Deine ' +
link_to('Profileinstellungen', edit_profile_path,
style: 'color: #2251dd;') +
' abonnieren.').html_safe
else
notifiable.details
end
end

def delete_notification_href(announcement, user)
notification = user.matching_notification(announcement)
return '' unless notification.present?
'href=' + notification_path(notification)
text = if notification.medium?
medium_notification_card_link(notifiable)
elsif notification.course?
course_notification_card_link
elsif notification.lecture?
lecture_notification_card_link
else
notifiable.details
end
text.html_safe
end
end
28 changes: 28 additions & 0 deletions app/models/notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Notification < ApplicationRecord
include Rails.application.routes.url_helpers

belongs_to :recipient, class_name: 'User'

paginates_per 12

# retrieve notifiable defined by notifiable_type and notifiable_id
Expand Down Expand Up @@ -33,6 +34,8 @@ def self.allowed_notifiable_types
['Medium', 'Course', 'Lecture', 'Announcement']
end

# the next methods are for the determination which kind of notification it is

def medium?
return unless notifiable.present?
notifiable.class.to_s == 'Medium'
Expand All @@ -47,4 +50,29 @@ def lecture?
return unless notifiable.present?
notifiable.class.to_s == 'Lecture'
end

def announcement?
return unless notifiable.present?
notifiable.class.to_s == 'Announcement'
end

def sesam?
medium? && notifiable.sort == 'Sesam'
end

def nuesse?
medium? && notifiable.sort == 'Nuesse'
end

def quiz?
medium? && notifiable.sort == 'KeksQuiz'
end

def generic_announcement?
announcement? && notifiable.lecture.nil?
end

def lecture_announcement?
announcement? && notifiable.lecture.present?
end
end
Loading

0 comments on commit 2f5eac9

Please sign in to comment.