From 38e8be8793dd9eb4045bd60c471cbdc9c0654f0d Mon Sep 17 00:00:00 2001 From: raluka Date: Fri, 17 Jul 2015 18:55:47 +0200 Subject: [PATCH] Issue#15: Added Comment Notifications: notification_emails and comments view --- app/controllers/admin/comments_controller.rb | 9 +++++ .../admin/conference_controller.rb | 1 - app/helpers/application_helper.rb | 4 ++ app/mailers/mailbot.rb | 16 ++++++++ app/models/ability.rb | 2 + app/models/comment.rb | 10 +++++ app/models/email_settings.rb | 4 +- app/models/user.rb | 3 ++ .../admin/comments/_all_comments.html.haml | 16 ++++++++ .../admin/comments/_posted_comments.html.haml | 14 +++++++ .../admin/comments/_unread_comments.html.haml | 16 ++++++++ app/views/admin/comments/index.html.haml | 23 ++++++++++++ .../admin/emails/comment_template.text.erb | 10 +++++ app/views/layouts/_navigation.html.haml | 37 +++++++++++++------ app/views/layouts/_unread_comment.html.haml | 2 + config/routes.rb | 1 + 16 files changed, 155 insertions(+), 13 deletions(-) create mode 100644 app/controllers/admin/comments_controller.rb create mode 100644 app/views/admin/comments/_all_comments.html.haml create mode 100644 app/views/admin/comments/_posted_comments.html.haml create mode 100644 app/views/admin/comments/_unread_comments.html.haml create mode 100644 app/views/admin/comments/index.html.haml create mode 100644 app/views/admin/emails/comment_template.text.erb create mode 100644 app/views/layouts/_unread_comment.html.haml diff --git a/app/controllers/admin/comments_controller.rb b/app/controllers/admin/comments_controller.rb new file mode 100644 index 00000000..990375cf --- /dev/null +++ b/app/controllers/admin/comments_controller.rb @@ -0,0 +1,9 @@ +module Admin + class CommentsController < Admin::BaseController + load_and_authorize_resource + + def index + @ordered_events = Event.order(:title).all + end + end +end diff --git a/app/controllers/admin/conference_controller.rb b/app/controllers/admin/conference_controller.rb index 89ad9970..25f60e12 100644 --- a/app/controllers/admin/conference_controller.rb +++ b/app/controllers/admin/conference_controller.rb @@ -27,7 +27,6 @@ module Admin @recent_users = User.limit(5).order(created_at: :desc) @recent_events = Event.limit(5).order(created_at: :desc) @recent_registrations = Registration.limit(5).order(created_at: :desc) - @top_submitter = Conference.get_top_submitter @submissions = {} diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 7e9f7652..9a0131f8 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -278,4 +278,8 @@ module ApplicationHelper new_user_registration_path end end + + def unread_notifications(user) + @unread_notifications = Comment.find_since_last_login(user) + end end diff --git a/app/mailers/mailbot.rb b/app/mailers/mailbot.rb index 9cf89b88..e8118382 100644 --- a/app/mailers/mailbot.rb +++ b/app/mailers/mailbot.rb @@ -80,6 +80,22 @@ class Mailbot < ActionMailer::Base end end + def send_notification_email_for_comment(comment) + @comment = comment + @event = @comment.commentable + @conference = @event.conference + recipients = User.comment_notifiable(@conference) # with scope + recipients.each do |user| + @user = user + mail(to: @user.email, + from: @conference.contact.email, + reply_to: @conference.contact.email, + template_path: 'admin/emails', + template_name: 'comment_template', + subject: "New comment has been posted for #{@event.title}") + end + end + def build_email(conference, to, subject, body) mail(to: to, from: conference.contact.email, diff --git a/app/models/ability.rb b/app/models/ability.rb index 0f89057e..e587cd08 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -137,6 +137,7 @@ class Ability can :manage, Sponsor, conference_id: conf_ids_for_organizer can :manage, SponsorshipLevel, conference_id: conf_ids_for_organizer can :manage, Ticket, conference_id: conf_ids_for_organizer + can :manage, Comment, conference_id: conf_ids_for_organizer end def signed_in_with_cfp_role(user) @@ -155,6 +156,7 @@ class Ability can :manage, CallForPaper, conference_id: conf_ids_for_cfp can :manage, Commercial, commercialable_type: 'Event', commercialable_id: Event.where(conference_id: conf_ids_for_cfp).pluck(:id) + can :manage, Comment, conference_id: conf_ids_for_cfp end def signed_in_with_info_desk_role(user) diff --git a/app/models/comment.rb b/app/models/comment.rb index 6196856e..f668afb3 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -3,6 +3,7 @@ class Comment < ActiveRecord::Base attr_accessible :commentable, :body, :user_id validates_presence_of :body validates_presence_of :user + after_create :send_notification # NOTE: install the acts_as_votable plugin if you # want user to vote on the quality of comments. @@ -40,9 +41,18 @@ class Comment < ActiveRecord::Base where(commentable_type: commentable_str.to_s, commentable_id: commentable_id).order('created_at DESC') } + scope :find_since_last_login, lambda { |user| + where(created_at: (user.last_sign_in_at..Time.now)).order(created_at: :desc) + } # Helper class method to look up a commentable object # given the commentable class name and id def self.find_commentable(commentable_str, commentable_id) commentable_str.constantize.find(commentable_id) end + + private + + def send_notification + Mailbot.delay.send_notification_email_for_comment(self) + end end diff --git a/app/models/email_settings.rb b/app/models/email_settings.rb index 11a7b628..5642b4da 100644 --- a/app/models/email_settings.rb +++ b/app/models/email_settings.rb @@ -49,7 +49,7 @@ class EmailSettings < ActiveRecord::Base h['registration_end_date'] = conference.registration_period.end_date end - if !event.nil? + if event h['eventtitle'] = event.title h['proposalslink'] = Rails.application.routes.url_helpers.conference_proposal_index_url( conference.short_title, host: CONFIG['url_for_emails']) @@ -67,6 +67,8 @@ class EmailSettings < ActiveRecord::Base parse_template(conf_update_template, values) end + private + def parse_template(text, values) values.each do |key, value| if value.kind_of?(Date) diff --git a/app/models/user.rb b/app/models/user.rb index 6214d07a..6ffd2059 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -11,6 +11,9 @@ class User < ActiveRecord::Base before_create :setup_role + # add scope + scope :comment_notifiable, ->(conference) {joins(:roles).where('roles.name IN (?)', [:organizer, :cfp]).where('roles.resource_id = ?', conference.id)} + # Include default devise modules. Others available are: # :token_authenticatable, :confirmable, # :lockable, :timeoutable and :omniauthable diff --git a/app/views/admin/comments/_all_comments.html.haml b/app/views/admin/comments/_all_comments.html.haml new file mode 100644 index 00000000..e7db8f0d --- /dev/null +++ b/app/views/admin/comments/_all_comments.html.haml @@ -0,0 +1,16 @@ +.panel.well + -@ordered_events.each do |event| + - if event.comment_threads.count > 0 + %panel + %panel.panel-header + %h4.title + = link_to event.title, admin_conference_event_path(event.conference.short_title, event) + %hr + -event.comment_threads.order(created_at: :desc).each do |comment| + %panel-body + %ul.list-unstyled + %li= comment.body + %ul.list-inline + %li Posted by: #{comment.user.name} + %li Created at: #{comment.created_at} + %hr diff --git a/app/views/admin/comments/_posted_comments.html.haml b/app/views/admin/comments/_posted_comments.html.haml new file mode 100644 index 00000000..5b41ff56 --- /dev/null +++ b/app/views/admin/comments/_posted_comments.html.haml @@ -0,0 +1,14 @@ +.panel.well + -@ordered_events.each do |event| + - if event.comment_threads.find_comments_by_user(current_user).count > 0 + %panel + %panel.panel-header + %h4.title + = link_to event.title, admin_conference_event_path(event.conference.short_title, event) + %hr + -event.comment_threads.find_comments_by_user(current_user).each do |comment| + %panel-body + %ul.list-unstyled + %li= comment.body + %li Created at: #{comment.created_at} + %hr diff --git a/app/views/admin/comments/_unread_comments.html.haml b/app/views/admin/comments/_unread_comments.html.haml new file mode 100644 index 00000000..a9960123 --- /dev/null +++ b/app/views/admin/comments/_unread_comments.html.haml @@ -0,0 +1,16 @@ +.panel.well + -@ordered_events.each do |event| + - if event.comment_threads.find_since_last_login(current_user).count > 0 + %panel + %panel.panel-header + %h4.title + = link_to event.title, admin_conference_event_path(event.conference.short_title, event) + %hr + -event.comment_threads.find_since_last_login(current_user).each do |comment| + %panel-body + %ul.list-unstyled + %li= comment.body + %ul.list-inline + %li Posted by: #{comment.user.name} + %li Created at: #{comment.created_at} + %hr diff --git a/app/views/admin/comments/index.html.haml b/app/views/admin/comments/index.html.haml new file mode 100644 index 00000000..96d942f3 --- /dev/null +++ b/app/views/admin/comments/index.html.haml @@ -0,0 +1,23 @@ +%h1 Comments +.row + .col-lg-12 + %ul.nav.nav-tabs#commentsTable + %li.active + %a{:href=>"#unread_comments", "data-toggle"=>"tab"} + %span.fa.fa-comment + Unread comments + %li + %a{:href=>"#all_comments", "data-toggle"=>"tab"} + %span.fa.fa-comments-o + Comments + %li + %a{:href=>"#posted_comments", "data-toggle"=>"tab"} + %span.fa.fa-pencil + Posted Comments + .tab-content + .tab-pane.active#unread_comments + = render partial: 'unread_comments' + .tab-pane#all_comments + = render partial: 'all_comments' + .tab-pane#posted_comments + = render partial: 'posted_comments' diff --git a/app/views/admin/emails/comment_template.text.erb b/app/views/admin/emails/comment_template.text.erb new file mode 100644 index 00000000..0ebd8ad4 --- /dev/null +++ b/app/views/admin/emails/comment_template.text.erb @@ -0,0 +1,10 @@ +Dear <%= @user.name %>, + +User <%= @comment.user.name %> posted a new comment for event <%= @event.title %> of <%= @conference.short_title %> . + +"<%= @comment.body %>" + +To reply to this comment, please go to <%= h( admin_conference_event_url(@conference.short_title, @event)) %> + +Best wishes, +<%= @conference.short_title %> Team diff --git a/app/views/layouts/_navigation.html.haml b/app/views/layouts/_navigation.html.haml index ea837d2c..66b4822c 100644 --- a/app/views/layouts/_navigation.html.haml +++ b/app/views/layouts/_navigation.html.haml @@ -13,17 +13,32 @@ %ul.nav.navbar-nav#splash-nav = content_for :splash_nav -if user_signed_in? - %ul.nav.navbar-nav.navbar-right - %li.dropdown - %a.dropdown-toggle{"data-toggle" => "dropdown", :href => "#", id: "current-user-detail"} - - if not current_user.name.blank? - #{current_user.name} - -else - #{current_user.email} - = image_tag(current_user.gravatar_url(size: '18'), title: "Yo #{current_user.name}!", :alt => '') - %b.caret - %ul.dropdown-menu - = render 'layouts/user_menu' + .btn-group.pull-right + %ul.nav.navbar-nav.navbar-right + %li.dropdown + %a.dropdown-toggle{"data-toggle" => "dropdown", :href => "#", id: "current-user-detail"} + - if not current_user.name.blank? + #{current_user.name} + -else + #{current_user.email} + = image_tag(current_user.gravatar_url(size: '18'), title: "Yo #{current_user.name}!", :alt => '') + %b.caret + %ul.dropdown-menu + = render 'layouts/user_menu' + - if can? :manage, Conference + %ul.nav.navbar-nav.navbar-right + %li.dropdown + %a.dropdown-toggle{"data-toggle" => "dropdown", :href => "#", id: "unread-comments-preview"} + - if unread_notifications(current_user) + Notifications (#{unread_notifications(current_user).length}) + %b.caret + %ul.dropdown-menu + - unread_notifications(current_user).limit(5).each do |unread_comment| + %li= link_to("New comment for: #{unread_comment.commentable.title}", admin_conference_event_path(unread_comment.commentable.conference.short_title, unread_comment.commentable_id)) + %li.divider + %li= link_to 'See comments', admin_comments_path + -else + No Notifications - else %ul.nav.navbar-nav.navbar-right - if CONFIG['authentication']['ichain']['enabled'] diff --git a/app/views/layouts/_unread_comment.html.haml b/app/views/layouts/_unread_comment.html.haml new file mode 100644 index 00000000..d337df5c --- /dev/null +++ b/app/views/layouts/_unread_comment.html.haml @@ -0,0 +1,2 @@ +%li= link_to("New comment for: #{unread_comment.commentable.title}", admin_conference_event_path(unread_comment.commentable.conference.short_title, unread_comment.commentable_id)) + diff --git a/config/routes.rb b/config/routes.rb index 5f8a59b6..f7d30750 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -15,6 +15,7 @@ Osem::Application.routes.draw do namespace :admin do resources :users resources :people + resources :comments resources :conference do member do get :roles