From 036bda3afa083d1d51f487c4a6e24a64c93d41ff Mon Sep 17 00:00:00 2001 From: Oleksii Kriuchykhin Date: Thu, 29 Sep 2016 16:51:27 +0200 Subject: [PATCH 1/3] Initial code for system notifications [SCI-444] --- app/helpers/notifications_helper.rb | 10 ++++++++++ app/models/user_notification.rb | 13 +++++++++++++ lib/tasks/notifications.rake | 10 ++++++++++ 3 files changed, 33 insertions(+) create mode 100644 app/helpers/notifications_helper.rb create mode 100644 lib/tasks/notifications.rake diff --git a/app/helpers/notifications_helper.rb b/app/helpers/notifications_helper.rb new file mode 100644 index 000000000..0e7216a79 --- /dev/null +++ b/app/helpers/notifications_helper.rb @@ -0,0 +1,10 @@ +module NotificationsHelper + + def create_system_notification(title, message) + users = User.where.not(confirmed_at: nil) + users.each do |u| + UserNotification.create_notification(u, title, message, :system_message) + end + end + +end diff --git a/app/models/user_notification.rb b/app/models/user_notification.rb index 89af38f84..4e451ea66 100644 --- a/app/models/user_notification.rb +++ b/app/models/user_notification.rb @@ -12,4 +12,17 @@ class UserNotification < ActiveRecord::Base def self.unseen_notification(user) where('user_id = ? AND checked = false', user.id).count end + + def self.create_notification(user, title, message, type) + notification = Notification.new + notification.transaction do + notification.title = title + notification.message = message + notification.type_of = type + notification.save! + usernotification = UserNotification.new(user: user, notification: notification, checked: false) + usernotification.save! + end + end + end diff --git a/lib/tasks/notifications.rake b/lib/tasks/notifications.rake new file mode 100644 index 000000000..3b688a6df --- /dev/null +++ b/lib/tasks/notifications.rake @@ -0,0 +1,10 @@ +namespace :notifications do + + desc 'Creates new system notification for all active users' + task :new_release => :environment do + include NotificationsHelper + puts 'Creation of system notification for all active users with link to release notes' + create_system_notification('New release', 'http://scinote.net/docs/release-notes/') + end + +end From f5cc2fe2254474b7ecb34d100b0cf375b524cead Mon Sep 17 00:00:00 2001 From: Oleksii Kriuchykhin Date: Fri, 30 Sep 2016 09:34:58 +0200 Subject: [PATCH 2/3] Code style fixes --- app/helpers/notifications_helper.rb | 2 -- app/models/user_notification.rb | 4 ++-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/app/helpers/notifications_helper.rb b/app/helpers/notifications_helper.rb index 0e7216a79..1477ec717 100644 --- a/app/helpers/notifications_helper.rb +++ b/app/helpers/notifications_helper.rb @@ -1,10 +1,8 @@ module NotificationsHelper - def create_system_notification(title, message) users = User.where.not(confirmed_at: nil) users.each do |u| UserNotification.create_notification(u, title, message, :system_message) end end - end diff --git a/app/models/user_notification.rb b/app/models/user_notification.rb index 4e451ea66..d0192c596 100644 --- a/app/models/user_notification.rb +++ b/app/models/user_notification.rb @@ -20,9 +20,9 @@ class UserNotification < ActiveRecord::Base notification.message = message notification.type_of = type notification.save! - usernotification = UserNotification.new(user: user, notification: notification, checked: false) + usernotification = UserNotification + .new(user: user, notification: notification, checked: false) usernotification.save! end end - end From a39c948c5f762fa1ae3e3d4ef38fc742ab2af875 Mon Sep 17 00:00:00 2001 From: Oleksii Kriuchykhin Date: Mon, 3 Oct 2016 15:12:48 +0200 Subject: [PATCH 3/3] System notifications code rework --- app/helpers/notifications_helper.rb | 14 +++++++++++--- app/models/user_notification.rb | 13 ------------- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/app/helpers/notifications_helper.rb b/app/helpers/notifications_helper.rb index 1477ec717..19364cd8d 100644 --- a/app/helpers/notifications_helper.rb +++ b/app/helpers/notifications_helper.rb @@ -1,8 +1,16 @@ module NotificationsHelper def create_system_notification(title, message) - users = User.where.not(confirmed_at: nil) - users.each do |u| - UserNotification.create_notification(u, title, message, :system_message) + notification = Notification.new + notification.title = title + notification.message = message + notification.type_of = :system_message + notification.transaction do + User.where.not(confirmed_at: nil).find_each do |u| + UserNotification + .new(user: u, notification: notification, checked: false) + .save! + end + notification.save! end end end diff --git a/app/models/user_notification.rb b/app/models/user_notification.rb index d0192c596..89af38f84 100644 --- a/app/models/user_notification.rb +++ b/app/models/user_notification.rb @@ -12,17 +12,4 @@ class UserNotification < ActiveRecord::Base def self.unseen_notification(user) where('user_id = ? AND checked = false', user.id).count end - - def self.create_notification(user, title, message, type) - notification = Notification.new - notification.transaction do - notification.title = title - notification.message = message - notification.type_of = type - notification.save! - usernotification = UserNotification - .new(user: user, notification: notification, checked: false) - usernotification.save! - end - end end