2018-05-07 16:47:29 -07:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2015-07-17 18:55:47 +02:00
|
|
|
module Admin
|
|
|
|
|
class CommentsController < Admin::BaseController
|
|
|
|
|
load_and_authorize_resource
|
|
|
|
|
|
|
|
|
|
def index
|
2015-09-16 14:00:00 +02:00
|
|
|
# All available comments, grouped and sorted
|
2015-09-09 12:15:59 +02:00
|
|
|
@comments = grouped_comments(accessible_ordered_comments)
|
2015-09-16 14:00:00 +02:00
|
|
|
|
|
|
|
|
# Grouped, sorted, available comments, posted since current_user last login
|
2015-09-09 12:15:59 +02:00
|
|
|
@unread_comments = grouped_comments(accessible_ordered_comments.find_since_last_login(current_user))
|
2015-09-16 14:00:00 +02:00
|
|
|
|
|
|
|
|
# Grouped, sorted, available comments, posted by current_user
|
2015-09-09 12:15:59 +02:00
|
|
|
@posted_comments = grouped_comments(accessible_ordered_comments.find_comments_by_user(current_user))
|
|
|
|
|
end
|
|
|
|
|
|
2015-09-14 18:50:21 +02:00
|
|
|
private
|
|
|
|
|
|
2015-09-16 14:00:00 +02:00
|
|
|
# Returning all available comments, ordered by created_at: :desc and by event.title
|
2015-09-09 12:15:59 +02:00
|
|
|
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
|
|
|
|
|
|
2015-09-16 14:00:00 +02:00
|
|
|
# Grouping all comments by conference, and by event. It returns {:conference => {:event => [{comment_2}, {comment_1 }]}}
|
2015-09-09 16:50:07 +02:00
|
|
|
def grouped_comments(remarks)
|
2015-10-25 13:29:02 +02:00
|
|
|
remarks.group_by{ |comment| comment.commentable.program.conference }.map {|conference, comments| [conference, comments.group_by{|comment| comment.commentable}]}.to_h
|
2015-07-17 18:55:47 +02:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|