From 945bac1fe8879b1e142be14b7f2df9c8d3a13123 Mon Sep 17 00:00:00 2001 From: raluka Date: Wed, 9 Sep 2015 12:15:59 +0200 Subject: [PATCH] WIP modified CommentsController#index --- app/controllers/admin/comments_controller.rb | 12 +++++++++++- app/helpers/application_helper.rb | 3 +-- app/views/admin/comments/_posted_comments.html.haml | 10 +++++++++- app/views/admin/comments/_unread_comments.html.haml | 10 +++++++++- 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/app/controllers/admin/comments_controller.rb b/app/controllers/admin/comments_controller.rb index 7f1bc51b..55a3a8f3 100644 --- a/app/controllers/admin/comments_controller.rb +++ b/app/controllers/admin/comments_controller.rb @@ -3,7 +3,17 @@ module Admin load_and_authorize_resource def index - @comments = Comment.accessible_by(current_ability).joins('INNER JOIN events ON commentable_id = events.id').order('events.title', 'comments.created_at DESC').group_by{ |comment| comment.commentable.conference }.map {|conference, comments| [conference, comments.group_by{|comment| comment.commentable}]}.to_h + @comments = grouped_comments(accessible_ordered_comments) + @unread_comments = grouped_comments(accessible_ordered_comments.find_since_last_login(current_user)) + @posted_comments = grouped_comments(accessible_ordered_comments.find_comments_by_user(current_user)) + end + + def accessible_ordered_comments + Comment.accessible_by(current_ability).joins('INNER JOIN events ON commentable_id = events.id').order('events.title', 'comments.created_at DESC') + end + + def grouped_comments(comments) + comments.group_by{ |comment| comment.commentable.conference }.map {|conference, comments| [conference, comments.group_by{|comment| comment.commentable}]}.to_h end end end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 3498aaa2..417100fd 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -280,7 +280,6 @@ module ApplicationHelper end def unread_notifications(user) - available_conferences_ids = Conference.with_roles([:admin, :organizer, :cfp], user).pluck(:id) - Comment.find_since_last_login(user).where(commentable_type: 'Event', commentable_id: Event.where(conference_id: available_conferences_ids)) + Comment.accessible_by(current_ability).find_since_last_login(current_user).limit(5) end end diff --git a/app/views/admin/comments/_posted_comments.html.haml b/app/views/admin/comments/_posted_comments.html.haml index 7efd1295..dfba16c7 100644 --- a/app/views/admin/comments/_posted_comments.html.haml +++ b/app/views/admin/comments/_posted_comments.html.haml @@ -1,2 +1,10 @@ .panel.well - Some text + %ul + - @posted_comments.each do |conference, events| + %i.li= conference.title + - events.each do |event, comments| + %b.li= event.title + - comments.each do |comment| + %li= comment.body + %li= comment.user.name + %li= comment.created_at diff --git a/app/views/admin/comments/_unread_comments.html.haml b/app/views/admin/comments/_unread_comments.html.haml index 6a8abba3..26763eeb 100644 --- a/app/views/admin/comments/_unread_comments.html.haml +++ b/app/views/admin/comments/_unread_comments.html.haml @@ -1,2 +1,10 @@ .panel.well - Some text + %ul + - @unread_comments.each do |conference, events| + %i.li= conference.title + - events.each do |event, comments| + %b.li= event.title + - comments.each do |comment| + %li= comment.body + %li= comment.user.name + %li= comment.created_at