From 2f5eac992efd5e7948e8f135e7216e315d8c626c Mon Sep 17 00:00:00 2001 From: fosterfarrell9 <28628554+fosterfarrell9@users.noreply.github.com> Date: Sat, 5 Jan 2019 17:48:00 +0100 Subject: [PATCH] refactor notification helper --- LICENSE | 2 +- app/assets/javascripts/notifications.coffee | 7 +- app/assets/stylesheets/colors.scss | 6 +- app/helpers/announcements_helper.rb | 9 +- app/helpers/courses_helper.rb | 20 +- app/helpers/lectures_helper.rb | 19 +- app/helpers/media_helper.rb | 18 +- app/helpers/notifications_helper.rb | 101 ++++----- app/models/notification.rb | 28 +++ app/views/media/_medium.html.erb | 218 ++++++++++---------- app/views/shared/_footer.html.erb | 2 +- erd.pdf | Bin 39748 -> 44610 bytes 12 files changed, 252 insertions(+), 178 deletions(-) diff --git a/LICENSE b/LICENSE index da477041d..8e20f5503 100644 --- a/LICENSE +++ b/LICENSE @@ -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 diff --git a/app/assets/javascripts/notifications.coffee b/app/assets/javascripts/notifications.coffee index 4a42c0d5f..aaead74e7 100644 --- a/app/assets/javascripts/notifications.coffee +++ b/app/assets/javascripts/notifications.coffee @@ -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() @@ -18,11 +19,13 @@ adjustNotificationCounter = (notificationId) -> document.title = 'MaMpf' # this is only relevant for index page $('#notificationCardRow') - .append('
Es liegen keine neuen Benachrichtigungen für Dich vor.
') + .append('
Es liegen keine neuen Benachrichtigungen + für Dich vor.
') return $(document).on 'turbolinks:load', -> + # clean up after lecture notification is clicked away $('.removeLectureNotification').on 'click', -> notificationId = $(this).data('id') lectureId = $(this).data('lecture') @@ -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 @@ -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' diff --git a/app/assets/stylesheets/colors.scss b/app/assets/stylesheets/colors.scss index 078fa7470..6134e8d5c 100644 --- a/app/assets/stylesheets/colors.scss +++ b/app/assets/stylesheets/colors.scss @@ -1,12 +1,16 @@ .blue { - background-color: blue; + background-color: blue; } .redtext { color: red; } +.darkblue { + color: #2251dd; +} + .white { background-color: white; diff --git a/app/helpers/announcements_helper.rb b/app/helpers/announcements_helper.rb index 4016eb614..6097c0fd3 100644 --- a/app/helpers/announcements_helper.rb +++ b/app/helpers/announcements_helper.rb @@ -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 @@ -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 diff --git a/app/helpers/courses_helper.rb b/app/helpers/courses_helper.rb index 75d925b1b..b556d8ce1 100644 --- a/app/helpers/courses_helper.rb +++ b/app/helpers/courses_helper.rb @@ -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 diff --git a/app/helpers/lectures_helper.rb b/app/helpers/lectures_helper.rb index ea4ba5bcd..d248f6ee7 100644 --- a/app/helpers/lectures_helper.rb +++ b/app/helpers/lectures_helper.rb @@ -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 diff --git a/app/helpers/media_helper.rb b/app/helpers/media_helper.rb index 2c8911686..380fdf349 100644 --- a/app/helpers/media_helper.rb +++ b/app/helpers/media_helper.rb @@ -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 diff --git a/app/helpers/notifications_helper.rb b/app/helpers/notifications_helper.rb index a6e7d66c3..e2efb1148 100644 --- a/app/helpers/notifications_helper.rb +++ b/app/helpers/notifications_helper.rb @@ -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 diff --git a/app/models/notification.rb b/app/models/notification.rb index bdd865701..924f63c01 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -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 @@ -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' @@ -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 diff --git a/app/views/media/_medium.html.erb b/app/views/media/_medium.html.erb index 3af126355..0dcacee47 100644 --- a/app/views/media/_medium.html.erb +++ b/app/views/media/_medium.html.erb @@ -11,127 +11,129 @@ <% end %> - - <% if medium.screenshot.present? %> - <%= image_tag(medium.screenshot_url, - alt: "Card image cap", - class: "card-img-top") %> - <% elsif medium.manuscript.present? %> - <%= image_tag(medium.manuscript_screenshot_url, - alt: "Card image cap", - class: "card-img-top manuscript-thumbnail") %> - <% end %> -
- <% unless medium.caption.nil? %> -
-
- <%= medium.caption %> -
-
- <% end %> - <% if tags %> -
- <% medium.tags.each do |t| %> - <%= link_to t.short_title(30), - t, - class: "badge badge-light", - title: t.title, - data: { toggle: 'tooltip', placement: 'bottom'} %> - <% end %> -
+ <% cache medium do %> + + <% if medium.screenshot.present? %> + <%= image_tag(medium.screenshot_url, + alt: "Card image cap", + class: "card-img-top") %> + <% elsif medium.manuscript.present? %> + <%= image_tag(medium.manuscript_screenshot_url, + alt: "Card image cap", + class: "card-img-top manuscript-thumbnail") %> <% end %> -
-