From 38e8be8793dd9eb4045bd60c471cbdc9c0654f0d Mon Sep 17 00:00:00 2001 From: raluka Date: Fri, 17 Jul 2015 18:55:47 +0200 Subject: [PATCH 01/49] 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 From 878e6cc1f84b05420e13ac9c4d9e742ca8ab526b Mon Sep 17 00:00:00 2001 From: raluka Date: Mon, 24 Aug 2015 17:17:29 +0200 Subject: [PATCH 02/49] small changes --- app/views/layouts/_navigation.html.haml | 2 +- config/routes.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/views/layouts/_navigation.html.haml b/app/views/layouts/_navigation.html.haml index 66b4822c..543c7a8f 100644 --- a/app/views/layouts/_navigation.html.haml +++ b/app/views/layouts/_navigation.html.haml @@ -25,7 +25,7 @@ %b.caret %ul.dropdown-menu = render 'layouts/user_menu' - - if can? :manage, Conference + - if can? :manage, Comment %ul.nav.navbar-nav.navbar-right %li.dropdown %a.dropdown-toggle{"data-toggle" => "dropdown", :href => "#", id: "unread-comments-preview"} diff --git a/config/routes.rb b/config/routes.rb index f7d30750..05766f76 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -15,7 +15,7 @@ Osem::Application.routes.draw do namespace :admin do resources :users resources :people - resources :comments + resources :comments, only: [:index] resources :conference do member do get :roles From b6d726d40e4a26dd690f0733bcf510db5f73696e Mon Sep 17 00:00:00 2001 From: raluka Date: Mon, 24 Aug 2015 17:44:24 +0200 Subject: [PATCH 03/49] unread_notifications has no longer assigned instance variable --- app/helpers/application_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 9a0131f8..54d79425 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -280,6 +280,6 @@ module ApplicationHelper end def unread_notifications(user) - @unread_notifications = Comment.find_since_last_login(user) + Comment.find_since_last_login(user) end end From eab2894fad39b9db11b390a6cc7cd5ef387cefe1 Mon Sep 17 00:00:00 2001 From: raluka Date: Tue, 25 Aug 2015 14:41:09 +0200 Subject: [PATCH 04/49] created comments with FactoryGirl --- app/models/ability.rb | 7 +++++-- app/views/layouts/_navigation.html.haml | 2 +- spec/factories/comments.rb | 9 +++++++++ spec/factories/events.rb | 1 + spec/models/ability_spec.rb | 1 + 5 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 spec/factories/comments.rb diff --git a/app/models/ability.rb b/app/models/ability.rb index e587cd08..c6f0eb64 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -137,7 +137,9 @@ 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 + can [:read, :create], Comment, commentable_type: 'Event', + commentable_id: Event.where(conference_id: conf_ids_for_organizer).pluck(:id) + end def signed_in_with_cfp_role(user) @@ -156,7 +158,8 @@ 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 + can [:read, :create], Comment, commentable_type: 'Event', + commentable_id: Event.where(conference_id: conf_ids_for_cfp).pluck(:id) end def signed_in_with_info_desk_role(user) diff --git a/app/views/layouts/_navigation.html.haml b/app/views/layouts/_navigation.html.haml index 543c7a8f..000e46f9 100644 --- a/app/views/layouts/_navigation.html.haml +++ b/app/views/layouts/_navigation.html.haml @@ -25,7 +25,7 @@ %b.caret %ul.dropdown-menu = render 'layouts/user_menu' - - if can? :manage, Comment + - if can? :read, Comment %ul.nav.navbar-nav.navbar-right %li.dropdown %a.dropdown-toggle{"data-toggle" => "dropdown", :href => "#", id: "unread-comments-preview"} diff --git a/spec/factories/comments.rb b/spec/factories/comments.rb new file mode 100644 index 00000000..a424e23b --- /dev/null +++ b/spec/factories/comments.rb @@ -0,0 +1,9 @@ +# Read about factories at https://github.com/thoughtbot/factory_girl + +FactoryGirl.define do + factory :comment do + body 'Most interresting comment ever, created by a girl.' + user + association :commentable, factory: :event + end +end diff --git a/spec/factories/events.rb b/spec/factories/events.rb index 537dfecb..6dc89b22 100644 --- a/spec/factories/events.rb +++ b/spec/factories/events.rb @@ -47,6 +47,7 @@ FactoryGirl.define do event.difficulty_level = build(:difficulty_level, conference: event.conference) event.track = build(:track, conference: event.conference) event.room = build(:room, conference: event.conference) + event.comment_threads << build(:comment, commentable: event) end end diff --git a/spec/models/ability_spec.rb b/spec/models/ability_spec.rb index b3859748..1c390f87 100644 --- a/spec/models/ability_spec.rb +++ b/spec/models/ability_spec.rb @@ -141,6 +141,7 @@ describe 'User' do it{ should_not be_able_to(:manage, other_event.difficulty_level) } it{ should be_able_to(:manage, event.commercials.first) } it{ should_not be_able_to(:manage, other_event.commercials.first) } + it{ should be_able_to([:read, :create], event.comment_threads.first) } end context 'when user has the role cfp' do From 84cd952b570a9ff8be8e688d0c261b82c1305720 Mon Sep 17 00:00:00 2001 From: raluka Date: Wed, 26 Aug 2015 13:07:13 +0200 Subject: [PATCH 05/49] created tests for user ability --- app/models/ability.rb | 9 ++++----- app/views/layouts/_navigation.html.haml | 2 +- spec/models/ability_spec.rb | 9 ++++++++- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/app/models/ability.rb b/app/models/ability.rb index c6f0eb64..974f3993 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -137,9 +137,8 @@ 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 [:read, :create], Comment, commentable_type: 'Event', - commentable_id: Event.where(conference_id: conf_ids_for_organizer).pluck(:id) - + can [:index, :create], Comment, commentable_type: 'Event', + commentable_id: Event.where(conference_id: conf_ids_for_organizer).pluck(:id) end def signed_in_with_cfp_role(user) @@ -158,8 +157,8 @@ 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 [:read, :create], Comment, commentable_type: 'Event', - commentable_id: Event.where(conference_id: conf_ids_for_cfp).pluck(:id) + can [:index, :create], Comment, commentable_type: 'Event', + commentable_id: Event.where(conference_id: conf_ids_for_cfp).pluck(:id) end def signed_in_with_info_desk_role(user) diff --git a/app/views/layouts/_navigation.html.haml b/app/views/layouts/_navigation.html.haml index 000e46f9..60e32c12 100644 --- a/app/views/layouts/_navigation.html.haml +++ b/app/views/layouts/_navigation.html.haml @@ -25,7 +25,7 @@ %b.caret %ul.dropdown-menu = render 'layouts/user_menu' - - if can? :read, Comment + - if can? :index, Comment #TODO modify condition %ul.nav.navbar-nav.navbar-right %li.dropdown %a.dropdown-toggle{"data-toggle" => "dropdown", :href => "#", id: "unread-comments-preview"} diff --git a/spec/models/ability_spec.rb b/spec/models/ability_spec.rb index 1c390f87..30593a75 100644 --- a/spec/models/ability_spec.rb +++ b/spec/models/ability_spec.rb @@ -141,7 +141,8 @@ describe 'User' do it{ should_not be_able_to(:manage, other_event.difficulty_level) } it{ should be_able_to(:manage, event.commercials.first) } it{ should_not be_able_to(:manage, other_event.commercials.first) } - it{ should be_able_to([:read, :create], event.comment_threads.first) } + it{ should be_able_to(:create, event.comment_threads.first) } + it{ should be_able_to(:index, event.comment_threads.first) } end context 'when user has the role cfp' do @@ -199,6 +200,8 @@ describe 'User' do it{ should_not be_able_to(:manage, other_event.difficulty_level) } it{ should be_able_to(:manage, event.commercials.first) } it{ should_not be_able_to(:manage, other_event.commercials.first) } + it{ should be_able_to(:create, event.comment_threads.first) } + it{ should be_able_to(:index, event.comment_threads.first) } end context 'when user has the role info_desk' do @@ -256,6 +259,8 @@ describe 'User' do it{ should_not be_able_to(:manage, other_event.difficulty_level) } it{ should_not be_able_to(:manage, event.commercials.first) } it{ should_not be_able_to(:manage, other_event.commercials.first) } + it{ should_not be_able_to(:create, event.comment_threads.first) } + it{ should_not be_able_to(:index, event.comment_threads.first) } end context 'when user has the role volunteers_coordinator' do @@ -313,6 +318,8 @@ describe 'User' do it{ should_not be_able_to(:manage, other_event.difficulty_level) } it{ should_not be_able_to(:manage, event.commercials.first) } it{ should_not be_able_to(:manage, other_event.commercials.first) } + it{ should_not be_able_to(:create, event.comment_threads.first) } + it{ should_not be_able_to(:index, event.comment_threads.first) } it 'should be_able to :manage Vposition' it 'should be_able to :manage Vday' end From a3b6f204cb077ba5d6befc946dcee52f03f11c6e Mon Sep 17 00:00:00 2001 From: raluka Date: Wed, 26 Aug 2015 22:09:36 +0200 Subject: [PATCH 06/49] created can_manage_comments helper for rendering button --- app/helpers/application_helper.rb | 4 ++++ app/models/ability.rb | 4 ++-- app/models/user.rb | 2 +- app/views/layouts/_navigation.html.haml | 2 +- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 54d79425..7f24331e 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -263,6 +263,10 @@ module ApplicationHelper end end + def can_manage_comments(conference) + (current_user.has_role? :organizer, conference) || (current_user.has_role? :cfp, conference) + end + def sign_in_path if CONFIG['authentication']['ichain']['enabled'] new_user_ichain_session_path diff --git a/app/models/ability.rb b/app/models/ability.rb index 974f3993..28acd4f0 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -137,7 +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 [:index, :create], Comment, commentable_type: 'Event', + can :index, Comment, commentable_type: 'Event', commentable_id: Event.where(conference_id: conf_ids_for_organizer).pluck(:id) end @@ -157,7 +157,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 [:index, :create], Comment, commentable_type: 'Event', + can :index, Comment, commentable_type: 'Event', commentable_id: Event.where(conference_id: conf_ids_for_cfp).pluck(:id) end diff --git a/app/models/user.rb b/app/models/user.rb index 6ffd2059..cba11d58 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -29,7 +29,7 @@ class User < ActiveRecord::Base devise(*devise_modules) - has_and_belongs_to_many :roles + has_and_belongs_to_many :roles has_many :openids attr_accessible :email, :password, :password_confirmation, :remember_me, :role_id, :role_ids, diff --git a/app/views/layouts/_navigation.html.haml b/app/views/layouts/_navigation.html.haml index 60e32c12..1f66af8b 100644 --- a/app/views/layouts/_navigation.html.haml +++ b/app/views/layouts/_navigation.html.haml @@ -25,7 +25,7 @@ %b.caret %ul.dropdown-menu = render 'layouts/user_menu' - - if can? :index, Comment #TODO modify condition + - if can_manage_comments(@conference) %ul.nav.navbar-nav.navbar-right %li.dropdown %a.dropdown-toggle{"data-toggle" => "dropdown", :href => "#", id: "unread-comments-preview"} From 062762089b09bfc49ead95e485eb6e152dea9af2 Mon Sep 17 00:00:00 2001 From: raluka Date: Thu, 27 Aug 2015 12:36:35 +0200 Subject: [PATCH 07/49] changed wrong test for ability --- app/models/ability.rb | 4 ++-- spec/models/ability_spec.rb | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/models/ability.rb b/app/models/ability.rb index 28acd4f0..2b78cc06 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -138,7 +138,7 @@ class Ability can :manage, SponsorshipLevel, conference_id: conf_ids_for_organizer can :manage, Ticket, conference_id: conf_ids_for_organizer can :index, Comment, commentable_type: 'Event', - commentable_id: Event.where(conference_id: conf_ids_for_organizer).pluck(:id) + commentable_id: Event.where(conference_id: conf_ids_for_organizer).pluck(:id) end def signed_in_with_cfp_role(user) @@ -158,7 +158,7 @@ class Ability can :manage, Commercial, commercialable_type: 'Event', commercialable_id: Event.where(conference_id: conf_ids_for_cfp).pluck(:id) can :index, Comment, commentable_type: 'Event', - commentable_id: Event.where(conference_id: conf_ids_for_cfp).pluck(:id) + commentable_id: Event.where(conference_id: conf_ids_for_cfp).pluck(:id) end def signed_in_with_info_desk_role(user) diff --git a/spec/models/ability_spec.rb b/spec/models/ability_spec.rb index 30593a75..73812dc1 100644 --- a/spec/models/ability_spec.rb +++ b/spec/models/ability_spec.rb @@ -141,8 +141,8 @@ describe 'User' do it{ should_not be_able_to(:manage, other_event.difficulty_level) } it{ should be_able_to(:manage, event.commercials.first) } it{ should_not be_able_to(:manage, other_event.commercials.first) } - it{ should be_able_to(:create, event.comment_threads.first) } it{ should be_able_to(:index, event.comment_threads.first) } + it{ should_not be_able_to(:index, other_event.comment_threads.first) } end context 'when user has the role cfp' do @@ -200,8 +200,8 @@ describe 'User' do it{ should_not be_able_to(:manage, other_event.difficulty_level) } it{ should be_able_to(:manage, event.commercials.first) } it{ should_not be_able_to(:manage, other_event.commercials.first) } - it{ should be_able_to(:create, event.comment_threads.first) } it{ should be_able_to(:index, event.comment_threads.first) } + it{ should_not be_able_to(:index, other_event.comment_threads.first) } end context 'when user has the role info_desk' do @@ -259,8 +259,8 @@ describe 'User' do it{ should_not be_able_to(:manage, other_event.difficulty_level) } it{ should_not be_able_to(:manage, event.commercials.first) } it{ should_not be_able_to(:manage, other_event.commercials.first) } - it{ should_not be_able_to(:create, event.comment_threads.first) } it{ should_not be_able_to(:index, event.comment_threads.first) } + it{ should_not be_able_to(:index, other_event.comment_threads.first) } end context 'when user has the role volunteers_coordinator' do @@ -318,8 +318,8 @@ describe 'User' do it{ should_not be_able_to(:manage, other_event.difficulty_level) } it{ should_not be_able_to(:manage, event.commercials.first) } it{ should_not be_able_to(:manage, other_event.commercials.first) } - it{ should_not be_able_to(:create, event.comment_threads.first) } it{ should_not be_able_to(:index, event.comment_threads.first) } + it{ should_not be_able_to(:index, other_event.comment_threads.first) } it 'should be_able to :manage Vposition' it 'should be_able to :manage Vday' end From 5664b99dc40e585b13488c8c56fdf6369c1f75a2 Mon Sep 17 00:00:00 2001 From: raluka Date: Tue, 1 Sep 2015 13:01:57 +0200 Subject: [PATCH 08/49] changed #signed_in_with_roles(user) in ability.rb model --- app/controllers/admin/comments_controller.rb | 2 ++ app/helpers/application_helper.rb | 4 ---- app/models/ability.rb | 8 ++++---- app/views/layouts/_navigation.html.haml | 2 +- 4 files changed, 7 insertions(+), 9 deletions(-) diff --git a/app/controllers/admin/comments_controller.rb b/app/controllers/admin/comments_controller.rb index 990375cf..ae6623a1 100644 --- a/app/controllers/admin/comments_controller.rb +++ b/app/controllers/admin/comments_controller.rb @@ -1,7 +1,9 @@ module Admin class CommentsController < Admin::BaseController + load_and_authorize_resource :conference, find_by: :short_title load_and_authorize_resource + def index @ordered_events = Event.order(:title).all end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 7f24331e..54d79425 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -263,10 +263,6 @@ module ApplicationHelper end end - def can_manage_comments(conference) - (current_user.has_role? :organizer, conference) || (current_user.has_role? :cfp, conference) - end - def sign_in_path if CONFIG['authentication']['ichain']['enabled'] new_user_ichain_session_path diff --git a/app/models/ability.rb b/app/models/ability.rb index 2b78cc06..b71bee97 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -88,10 +88,10 @@ class Ability # Abilities from not_signed_in and signed_in are also inherited signed_in(user) - signed_in_with_organizer_role(user) - signed_in_with_cfp_role(user) - signed_in_with_info_desk_role(user) - signed_in_with_volunteers_coordinator_role(user) + signed_in_with_organizer_role(user) if user.has_role? :organizer, :any + signed_in_with_cfp_role(user) if user.has_role? :cfp, :any + signed_in_with_info_desk_role(user) if user.has_role? :info_desk, :any + signed_in_with_volunteers_coordinator_role(user) if user.has_role? :volunteer_coordinator, :any # for users with any role can [:show], Conference diff --git a/app/views/layouts/_navigation.html.haml b/app/views/layouts/_navigation.html.haml index 1f66af8b..a9a45ecc 100644 --- a/app/views/layouts/_navigation.html.haml +++ b/app/views/layouts/_navigation.html.haml @@ -25,7 +25,7 @@ %b.caret %ul.dropdown-menu = render 'layouts/user_menu' - - if can_manage_comments(@conference) + - if can? :index, Comment %ul.nav.navbar-nav.navbar-right %li.dropdown %a.dropdown-toggle{"data-toggle" => "dropdown", :href => "#", id: "unread-comments-preview"} From bcd8e416700b640489230be27839b63eba53d35d Mon Sep 17 00:00:00 2001 From: raluka Date: Thu, 3 Sep 2015 12:17:42 +0200 Subject: [PATCH 09/49] added logic for available conferences based on roles in comments_controller.rb --- app/controllers/admin/comments_controller.rb | 4 +-- app/models/user.rb | 2 +- .../admin/comments/_all_comments.html.haml | 30 ++++++++++--------- .../admin/comments/_posted_comments.html.haml | 26 ++++++++-------- .../admin/comments/_unread_comments.html.haml | 30 ++++++++++--------- 5 files changed, 48 insertions(+), 44 deletions(-) diff --git a/app/controllers/admin/comments_controller.rb b/app/controllers/admin/comments_controller.rb index ae6623a1..32ae0e85 100644 --- a/app/controllers/admin/comments_controller.rb +++ b/app/controllers/admin/comments_controller.rb @@ -1,11 +1,9 @@ module Admin class CommentsController < Admin::BaseController - load_and_authorize_resource :conference, find_by: :short_title load_and_authorize_resource - def index - @ordered_events = Event.order(:title).all + @conferences_available = Conference.with_roles([:admin, :organizer, :cfp], current_user) end end end diff --git a/app/models/user.rb b/app/models/user.rb index cba11d58..6ffd2059 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -29,7 +29,7 @@ class User < ActiveRecord::Base devise(*devise_modules) - has_and_belongs_to_many :roles + has_and_belongs_to_many :roles has_many :openids attr_accessible :email, :password, :password_confirmation, :remember_me, :role_id, :role_ids, diff --git a/app/views/admin/comments/_all_comments.html.haml b/app/views/admin/comments/_all_comments.html.haml index e7db8f0d..3ad58e89 100644 --- a/app/views/admin/comments/_all_comments.html.haml +++ b/app/views/admin/comments/_all_comments.html.haml @@ -1,16 +1,18 @@ .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) + -@conferences_available.each do |conference| + -conference.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) + = conference.title + %hr + -event.comment_threads.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 - -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 index 5b41ff56..3cdc2102 100644 --- a/app/views/admin/comments/_posted_comments.html.haml +++ b/app/views/admin/comments/_posted_comments.html.haml @@ -1,14 +1,16 @@ .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) + -@conferences_available.each do |conference| + -conference.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) + = conference.title + %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 - -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 index a9960123..8c58193e 100644 --- a/app/views/admin/comments/_unread_comments.html.haml +++ b/app/views/admin/comments/_unread_comments.html.haml @@ -1,16 +1,18 @@ .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) + -@conferences_available.each do |conference| + -conference.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) + = conference.title + %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 - -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 From f3fa68e371f7070ada3a45a63b31fa5528fbd7c7 Mon Sep 17 00:00:00 2001 From: raluka Date: Thu, 3 Sep 2015 14:04:16 +0200 Subject: [PATCH 10/49] fixed Administration link in user_menu dropdown --- app/views/layouts/_user_menu.html.haml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/views/layouts/_user_menu.html.haml b/app/views/layouts/_user_menu.html.haml index fe349b3b..5c22be72 100644 --- a/app/views/layouts/_user_menu.html.haml +++ b/app/views/layouts/_user_menu.html.haml @@ -21,7 +21,10 @@ = link_to(destroy_user_session_path, :method=>'delete') do %span.fa.fa-minus Sign out -- if can? :manage, Conference +- if current_user.is_admin || (current_user.has_any_role? :admin, { :name => :organizer, :resource => :any }, + { :name => :cfp, :resource => :any }, + { :name => :info_desk, :resource => :any }, + { :name => :volunteer_coordinator, :resource => :any }) %li.divider %li = link_to(admin_conference_index_path()) do From 55eddbfde7bea9fb990754a82ca143227b212773 Mon Sep 17 00:00:00 2001 From: raluka Date: Thu, 3 Sep 2015 14:20:11 +0200 Subject: [PATCH 11/49] changed comments helper to proper return unread comments available for current_user --- app/helpers/application_helper.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 54d79425..3498aaa2 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -280,6 +280,7 @@ module ApplicationHelper end def unread_notifications(user) - Comment.find_since_last_login(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)) end end From be55456c788a5f5e4550bbee70944a81236a4a74 Mon Sep 17 00:00:00 2001 From: raluka Date: Thu, 3 Sep 2015 17:03:05 +0200 Subject: [PATCH 12/49] event title and conference title in 2 different lines now in view --- app/views/admin/comments/_all_comments.html.haml | 5 +++-- app/views/admin/comments/_posted_comments.html.haml | 5 +++-- app/views/admin/comments/_unread_comments.html.haml | 5 +++-- app/views/layouts/_unread_comment.html.haml | 2 -- 4 files changed, 9 insertions(+), 8 deletions(-) delete mode 100644 app/views/layouts/_unread_comment.html.haml diff --git a/app/views/admin/comments/_all_comments.html.haml b/app/views/admin/comments/_all_comments.html.haml index 3ad58e89..004c0606 100644 --- a/app/views/admin/comments/_all_comments.html.haml +++ b/app/views/admin/comments/_all_comments.html.haml @@ -5,8 +5,9 @@ %panel %panel.panel-header %h4.title - = link_to event.title, admin_conference_event_path(event.conference.short_title, event) - = conference.title + %ul.list-unstyled + %li= link_to event.title, admin_conference_event_path(event.conference.short_title, event) + %li= conference.title %hr -event.comment_threads.each do |comment| %panel-body diff --git a/app/views/admin/comments/_posted_comments.html.haml b/app/views/admin/comments/_posted_comments.html.haml index 3cdc2102..3c49d82c 100644 --- a/app/views/admin/comments/_posted_comments.html.haml +++ b/app/views/admin/comments/_posted_comments.html.haml @@ -5,8 +5,9 @@ %panel %panel.panel-header %h4.title - = link_to event.title, admin_conference_event_path(event.conference.short_title, event) - = conference.title + %ul.list-unstyled + %li= link_to event.title, admin_conference_event_path(event.conference.short_title, event) + %li= conference.title %hr -event.comment_threads.find_comments_by_user(current_user).each do |comment| %panel-body diff --git a/app/views/admin/comments/_unread_comments.html.haml b/app/views/admin/comments/_unread_comments.html.haml index 8c58193e..e229aa78 100644 --- a/app/views/admin/comments/_unread_comments.html.haml +++ b/app/views/admin/comments/_unread_comments.html.haml @@ -5,8 +5,9 @@ %panel %panel.panel-header %h4.title - = link_to event.title, admin_conference_event_path(event.conference.short_title, event) - = conference.title + %ul.list-unstyled + %li= link_to event.title, admin_conference_event_path(event.conference.short_title, event) + %li= conference.title %hr -event.comment_threads.find_since_last_login(current_user).each do |comment| %panel-body diff --git a/app/views/layouts/_unread_comment.html.haml b/app/views/layouts/_unread_comment.html.haml deleted file mode 100644 index d337df5c..00000000 --- a/app/views/layouts/_unread_comment.html.haml +++ /dev/null @@ -1,2 +0,0 @@ -%li= link_to("New comment for: #{unread_comment.commentable.title}", admin_conference_event_path(unread_comment.commentable.conference.short_title, unread_comment.commentable_id)) - From 449d466f7d00b77a518666648b3965d19117e82b Mon Sep 17 00:00:00 2001 From: raluka Date: Mon, 7 Sep 2015 18:56:26 +0200 Subject: [PATCH 13/49] WIP changed CommentsController#index; temporary view for all_comments --- app/controllers/admin/comments_controller.rb | 3 ++- .../admin/conference_controller.rb | 1 + app/models/user.rb | 2 +- .../admin/comments/_all_comments.html.haml | 27 +++++++------------ .../admin/comments/_posted_comments.html.haml | 17 +----------- .../admin/comments/_unread_comments.html.haml | 19 +------------ 6 files changed, 15 insertions(+), 54 deletions(-) diff --git a/app/controllers/admin/comments_controller.rb b/app/controllers/admin/comments_controller.rb index 32ae0e85..ebcc9f92 100644 --- a/app/controllers/admin/comments_controller.rb +++ b/app/controllers/admin/comments_controller.rb @@ -3,7 +3,8 @@ module Admin load_and_authorize_resource def index - @conferences_available = Conference.with_roles([:admin, :organizer, :cfp], current_user) + @comments = Comment.accessible_by(current_ability).order(created_at: :desc).group_by { |comment| comment.commentable.conference } + @another_comments = Hash[@comments.map{|conference, comments| [conference, comments.group_by {|comment| comment.commentable}]}] end end end diff --git a/app/controllers/admin/conference_controller.rb b/app/controllers/admin/conference_controller.rb index 25f60e12..89ad9970 100644 --- a/app/controllers/admin/conference_controller.rb +++ b/app/controllers/admin/conference_controller.rb @@ -27,6 +27,7 @@ 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/models/user.rb b/app/models/user.rb index 6ffd2059..568a1c89 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -12,7 +12,7 @@ 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)} + scope :comment_notifiable, ->(conference) {joins(:roles).where('roles.name IN (?)', [:organizer, :cfp]).where('roles.resource_type = ? AND roles.resource_id = ?', 'Conference', conference.id)} # Include default devise modules. Others available are: # :token_authenticatable, :confirmable, diff --git a/app/views/admin/comments/_all_comments.html.haml b/app/views/admin/comments/_all_comments.html.haml index 004c0606..a08b6eb9 100644 --- a/app/views/admin/comments/_all_comments.html.haml +++ b/app/views/admin/comments/_all_comments.html.haml @@ -1,19 +1,10 @@ .panel.well - -@conferences_available.each do |conference| - -conference.events.each do |event| - - if event.comment_threads.count > 0 - %panel - %panel.panel-header - %h4.title - %ul.list-unstyled - %li= link_to event.title, admin_conference_event_path(event.conference.short_title, event) - %li= conference.title - %hr - -event.comment_threads.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 + %ul + - @another_comments.each do |conference, events| + %li= conference.title + - events.each do |event, comments| + %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/_posted_comments.html.haml b/app/views/admin/comments/_posted_comments.html.haml index 3c49d82c..7efd1295 100644 --- a/app/views/admin/comments/_posted_comments.html.haml +++ b/app/views/admin/comments/_posted_comments.html.haml @@ -1,17 +1,2 @@ .panel.well - -@conferences_available.each do |conference| - -conference.events.each do |event| - - if event.comment_threads.find_comments_by_user(current_user).count > 0 - %panel - %panel.panel-header - %h4.title - %ul.list-unstyled - %li= link_to event.title, admin_conference_event_path(event.conference.short_title, event) - %li= conference.title - %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 + Some text diff --git a/app/views/admin/comments/_unread_comments.html.haml b/app/views/admin/comments/_unread_comments.html.haml index e229aa78..6a8abba3 100644 --- a/app/views/admin/comments/_unread_comments.html.haml +++ b/app/views/admin/comments/_unread_comments.html.haml @@ -1,19 +1,2 @@ .panel.well - -@conferences_available.each do |conference| - -conference.events.each do |event| - - if event.comment_threads.find_since_last_login(current_user).count > 0 - %panel - %panel.panel-header - %h4.title - %ul.list-unstyled - %li= link_to event.title, admin_conference_event_path(event.conference.short_title, event) - %li= conference.title - %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 + Some text From b762b82e74775a57ecce33e731b609ad940675bc Mon Sep 17 00:00:00 2001 From: raluka Date: Tue, 8 Sep 2015 12:15:22 +0200 Subject: [PATCH 14/49] rewrite CommentsController#index to have everything in one call --- app/controllers/admin/comments_controller.rb | 3 +-- app/views/admin/comments/_all_comments.html.haml | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/app/controllers/admin/comments_controller.rb b/app/controllers/admin/comments_controller.rb index ebcc9f92..088d07f5 100644 --- a/app/controllers/admin/comments_controller.rb +++ b/app/controllers/admin/comments_controller.rb @@ -3,8 +3,7 @@ module Admin load_and_authorize_resource def index - @comments = Comment.accessible_by(current_ability).order(created_at: :desc).group_by { |comment| comment.commentable.conference } - @another_comments = Hash[@comments.map{|conference, comments| [conference, comments.group_by {|comment| comment.commentable}]}] + @comments = Comment.accessible_by(current_ability).order(created_at: :desc).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/views/admin/comments/_all_comments.html.haml b/app/views/admin/comments/_all_comments.html.haml index a08b6eb9..25f6b474 100644 --- a/app/views/admin/comments/_all_comments.html.haml +++ b/app/views/admin/comments/_all_comments.html.haml @@ -1,6 +1,6 @@ .panel.well %ul - - @another_comments.each do |conference, events| + - @comments.each do |conference, events| %li= conference.title - events.each do |event, comments| %li= event.title From 932cb9984f951690016654be2493b8336e22388f Mon Sep 17 00:00:00 2001 From: raluka Date: Tue, 8 Sep 2015 14:27:14 +0200 Subject: [PATCH 15/49] sorting comments after event title and created at in index --- app/controllers/admin/comments_controller.rb | 2 +- app/views/admin/comments/_all_comments.html.haml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/controllers/admin/comments_controller.rb b/app/controllers/admin/comments_controller.rb index 088d07f5..7f1bc51b 100644 --- a/app/controllers/admin/comments_controller.rb +++ b/app/controllers/admin/comments_controller.rb @@ -3,7 +3,7 @@ module Admin load_and_authorize_resource def index - @comments = Comment.accessible_by(current_ability).order(created_at: :desc).group_by { |comment| comment.commentable.conference }.map {|conference, comments| [conference, comments.group_by {|comment| comment.commentable}]}.to_h + @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 end end end diff --git a/app/views/admin/comments/_all_comments.html.haml b/app/views/admin/comments/_all_comments.html.haml index 25f6b474..7213f040 100644 --- a/app/views/admin/comments/_all_comments.html.haml +++ b/app/views/admin/comments/_all_comments.html.haml @@ -1,9 +1,9 @@ .panel.well %ul - @comments.each do |conference, events| - %li= conference.title + %i.li= conference.title - events.each do |event, comments| - %li= event.title + %b.li= event.title - comments.each do |comment| %li= comment.body %li= comment.user.name From e5c235ac32232d30b900d4bd399f0e7028422527 Mon Sep 17 00:00:00 2001 From: raluka Date: Wed, 9 Sep 2015 12:15:59 +0200 Subject: [PATCH 16/49] 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 From d365d5efa1c465eb2468c2b4d35c6988f3afbee1 Mon Sep 17 00:00:00 2001 From: raluka Date: Wed, 9 Sep 2015 16:50:07 +0200 Subject: [PATCH 17/49] added test file for comments_controller WIP --- app/controllers/admin/comments_controller.rb | 4 +-- app/helpers/application_helper.rb | 2 +- .../admin/comments_controller_spec.rb | 32 +++++++++++++++++++ 3 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 spec/controllers/admin/comments_controller_spec.rb diff --git a/app/controllers/admin/comments_controller.rb b/app/controllers/admin/comments_controller.rb index 55a3a8f3..aa8423fc 100644 --- a/app/controllers/admin/comments_controller.rb +++ b/app/controllers/admin/comments_controller.rb @@ -12,8 +12,8 @@ module Admin 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 + def grouped_comments(remarks) + remarks.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 417100fd..86f4c418 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -280,6 +280,6 @@ module ApplicationHelper end def unread_notifications(user) - Comment.accessible_by(current_ability).find_since_last_login(current_user).limit(5) + Comment.accessible_by(current_ability).find_since_last_login(user).limit(5) end end diff --git a/spec/controllers/admin/comments_controller_spec.rb b/spec/controllers/admin/comments_controller_spec.rb new file mode 100644 index 00000000..f202c423 --- /dev/null +++ b/spec/controllers/admin/comments_controller_spec.rb @@ -0,0 +1,32 @@ +require 'spec_helper' + +describe Admin::CommentsController do + +# some settings to be done before like creating objects used by tests + describe 'GET #index' do + context 'all comments' do + it 'populates a hash with conference, event, and comment objects' + it 'renders the :index template' + end + + context 'unread_comments' do + it 'populates a hash with conference, event, and comment objects created since last login of current_user' + it 'renders the :index template' + end + + context 'posted_comments' do + it 'populates a hash with conference, event, and comments posted by current_user' + it 'renders the :index template' + end + end + + describe 'accessible_ordered_comments' do + it 'returns comments' + it 'sorts comments by created_at and event title' + end + + describe 'grouped_comments(remarks)' do + it 'groups comments by conference and by event' + it 'returns a hash' + end +end From 81c4d1381c056a9336989c6f2ae12ac4f7fb2f30 Mon Sep 17 00:00:00 2001 From: raluka Date: Thu, 10 Sep 2015 11:46:49 +0200 Subject: [PATCH 18/49] added pretty view for comments --- .../admin/comments/_all_comments.html.haml | 24 ++++++++++++------- .../admin/comments/_posted_comments.html.haml | 24 ++++++++++++------- .../admin/comments/_unread_comments.html.haml | 24 ++++++++++++------- 3 files changed, 45 insertions(+), 27 deletions(-) diff --git a/app/views/admin/comments/_all_comments.html.haml b/app/views/admin/comments/_all_comments.html.haml index 7213f040..6a5aebfd 100644 --- a/app/views/admin/comments/_all_comments.html.haml +++ b/app/views/admin/comments/_all_comments.html.haml @@ -1,10 +1,16 @@ -.panel.well - %ul - - @comments.each do |conference, events| - %i.li= conference.title +- @comments.each do |conference, events| + .well + %p.h4= 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 + %h3.title + = link_to event.title, admin_conference_event_path(event.conference.short_title, event) + %body + - comments.each do |comment| + %hr + %ul.list-unstyled + %ul.list-inline + %li + %h5.strong Posted by: #{comment.user.name} + %li + %h5.strong Created at: #{comment.created_at} + %li= comment.body diff --git a/app/views/admin/comments/_posted_comments.html.haml b/app/views/admin/comments/_posted_comments.html.haml index dfba16c7..a0459d4a 100644 --- a/app/views/admin/comments/_posted_comments.html.haml +++ b/app/views/admin/comments/_posted_comments.html.haml @@ -1,10 +1,16 @@ -.panel.well - %ul - - @posted_comments.each do |conference, events| - %i.li= conference.title +- @posted_comments.each do |conference, events| + .well + %p.h4= 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 + %h3.title + = link_to event.title, admin_conference_event_path(event.conference.short_title, event) + %body + - comments.each do |comment| + %hr + %ul.list-unstyled + %ul.list-inline + %li + %h5.strong Posted by: #{comment.user.name} + %li + %h5.strong Created at: #{comment.created_at} + %li= comment.body diff --git a/app/views/admin/comments/_unread_comments.html.haml b/app/views/admin/comments/_unread_comments.html.haml index 26763eeb..24c5c9c5 100644 --- a/app/views/admin/comments/_unread_comments.html.haml +++ b/app/views/admin/comments/_unread_comments.html.haml @@ -1,10 +1,16 @@ -.panel.well - %ul - - @unread_comments.each do |conference, events| - %i.li= conference.title +- @unread_comments.each do |conference, events| + .well + %p.h4= 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 + %h3.title + = link_to event.title, admin_conference_event_path(event.conference.short_title, event) + %body + - comments.each do |comment| + %hr + %ul.list-unstyled + %ul.list-inline + %li + %h5.strong Posted by: #{comment.user.name} + %li + %h5.strong Created at: #{comment.created_at} + %li= comment.body From a8815aff5e298d7cce7e03abcdb2b9ad33c29174 Mon Sep 17 00:00:00 2001 From: raluka Date: Mon, 14 Sep 2015 18:50:21 +0200 Subject: [PATCH 19/49] WIP created tests for comments_controller.rb --- app/controllers/admin/comments_controller.rb | 2 + .../admin/comments_controller_spec.rb | 91 ++++++++++++++----- spec/factories/users.rb | 1 + 3 files changed, 73 insertions(+), 21 deletions(-) diff --git a/app/controllers/admin/comments_controller.rb b/app/controllers/admin/comments_controller.rb index aa8423fc..4fa39d32 100644 --- a/app/controllers/admin/comments_controller.rb +++ b/app/controllers/admin/comments_controller.rb @@ -8,6 +8,8 @@ module Admin @posted_comments = grouped_comments(accessible_ordered_comments.find_comments_by_user(current_user)) end + private + 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 diff --git a/spec/controllers/admin/comments_controller_spec.rb b/spec/controllers/admin/comments_controller_spec.rb index f202c423..b8140640 100644 --- a/spec/controllers/admin/comments_controller_spec.rb +++ b/spec/controllers/admin/comments_controller_spec.rb @@ -1,32 +1,81 @@ require 'spec_helper' -describe Admin::CommentsController do +describe Admin::CommentsController, type: :controller do -# some settings to be done before like creating objects used by tests - describe 'GET #index' do - context 'all comments' do - it 'populates a hash with conference, event, and comment objects' - it 'renders the :index template' - end + context 'not logged in user' do + describe 'GET #index' do + it 'renders the :index template' do + conference = create(:conference) + first_user = create(:user) + organizer_role = create(:role, name: 'organizer', resource: conference) + organizer = create(:user, role_ids: organizer_role.id) + event = create(:event, conference: conference) + comment = create(:comment, commentable_type: 'Event', commentable_id: event.id) - context 'unread_comments' do - it 'populates a hash with conference, event, and comment objects created since last login of current_user' - it 'renders the :index template' - end - - context 'posted_comments' do - it 'populates a hash with conference, event, and comments posted by current_user' - it 'renders the :index template' + get :index + expect(response).to redirect_to(user_session_path) + end end end - describe 'accessible_ordered_comments' do - it 'returns comments' - it 'sorts comments by created_at and event title' + context 'logged in as admin, organizer or cfp' do + describe 'GET #index' do + it 'populates a hash with comments' do + conference = create(:conference) + first_user = create(:user) + organizer_role = create(:role, name: 'organizer', resource: conference) + organizer = create(:user, role_ids: organizer_role.id) + event = create(:event, conference: conference) + comment = create(:comment, commentable_type: 'Event', commentable_id: event.id) + sign_in(organizer) + + get :index + expect(assigns(:comments)).to be_a(Hash) + # assigns(:comments).first returns an array of first pair key-value from hash. + # Calling again 'first' returns the key, meaning the Conference object. + expect(assigns(:comments).first.first.title).to eq(comment.commentable.conference.title) + end + it 'has status 200: OK' do + conference = create(:conference) + first_user = create(:user) + organizer_role = create(:role, name: 'organizer', resource: conference) + organizer = create(:user, role_ids: organizer_role.id) + event = create(:event, conference: conference) + comment = create(:comment, commentable_type: 'Event', commentable_id: event.id) + sign_in(organizer) + + get :index + expect(response).to have_http_status(:ok) + end + it 'renders the :index template' do + conference = create(:conference) + first_user = create(:user) + organizer_role = create(:role, name: 'organizer', resource: conference) + organizer = create(:user, role_ids: organizer_role.id) + event = create(:event, conference: conference) + comment = create(:comment, commentable_type: 'Event', commentable_id: event.id) + sign_in(organizer) + + get :index + expect(response).to render_template(:index) + end + end end - describe 'grouped_comments(remarks)' do - it 'groups comments by conference and by event' - it 'returns a hash' + context 'logged in with any other role or normal user' do + describe 'GET#index' do + it 'requires organizer privileges' do + conference = create(:conference) + first_user = create(:user) + participant = create(:user) + event = create(:event, conference: conference) + comment = create(:comment, commentable_type: 'Event', commentable_id: event.id) + sign_in(participant) + + get :index + expect(response).to redirect_to(root_path) + expect(flash[:alert]).to match('You are not authorized to access this area!') + end + end end end diff --git a/spec/factories/users.rb b/spec/factories/users.rb index e5c0b7c1..1f566252 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -6,6 +6,7 @@ FactoryGirl.define do sequence(:username) { |n| "username#{n}" } password 'changeme' password_confirmation 'changeme' + last_sign_in_at { Time.now - 10.days } confirmed_at { Time.now } biography <<-EOS Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus enim From 850782abee4be06c08491b8e33f7ad327b08bd19 Mon Sep 17 00:00:00 2001 From: raluka Date: Tue, 15 Sep 2015 12:39:36 +0200 Subject: [PATCH 20/49] refactored comments_controller tests; fixed access to Administration link into user_menu view --- app/models/ability.rb | 1 + app/views/layouts/_user_menu.html.haml | 5 +- .../admin/comments_controller_spec.rb | 54 ++++++------------- spec/factories/users.rb | 1 - 4 files changed, 19 insertions(+), 42 deletions(-) diff --git a/app/models/ability.rb b/app/models/ability.rb index b71bee97..bd30a8fd 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -94,6 +94,7 @@ class Ability signed_in_with_volunteers_coordinator_role(user) if user.has_role? :volunteer_coordinator, :any # for users with any role + can :access, Admin can [:show], Conference can :index, Commercial, commercialable_type: 'Conference' cannot [:edit, :update, :destroy], Question, global: true diff --git a/app/views/layouts/_user_menu.html.haml b/app/views/layouts/_user_menu.html.haml index 5c22be72..da55935a 100644 --- a/app/views/layouts/_user_menu.html.haml +++ b/app/views/layouts/_user_menu.html.haml @@ -21,10 +21,7 @@ = link_to(destroy_user_session_path, :method=>'delete') do %span.fa.fa-minus Sign out -- if current_user.is_admin || (current_user.has_any_role? :admin, { :name => :organizer, :resource => :any }, - { :name => :cfp, :resource => :any }, - { :name => :info_desk, :resource => :any }, - { :name => :volunteer_coordinator, :resource => :any }) +- if can? :access, Admin %li.divider %li = link_to(admin_conference_index_path()) do diff --git a/spec/controllers/admin/comments_controller_spec.rb b/spec/controllers/admin/comments_controller_spec.rb index b8140640..b3ff0914 100644 --- a/spec/controllers/admin/comments_controller_spec.rb +++ b/spec/controllers/admin/comments_controller_spec.rb @@ -2,16 +2,19 @@ require 'spec_helper' describe Admin::CommentsController, type: :controller do + # It is necessary to use bang version of let to build roles before user + let(:conference) { create(:conference) } + let!(:first_user) { create(:user) } + let!(:organizer_role) { create(:role, name: 'organizer', resource: conference) } + let(:organizer) { create(:user, role_ids: organizer_role.id, last_sign_in_at: Time.now - 1.day) } + let(:participant) { create(:user) } + let(:event) { create(:event, conference: conference) } + let(:comment) { create(:comment, commentable_type: 'Event', commentable_id: event.id) } + context 'not logged in user' do describe 'GET #index' do it 'renders the :index template' do - conference = create(:conference) - first_user = create(:user) - organizer_role = create(:role, name: 'organizer', resource: conference) - organizer = create(:user, role_ids: organizer_role.id) - event = create(:event, conference: conference) - comment = create(:comment, commentable_type: 'Event', commentable_id: event.id) - + comment get :index expect(response).to redirect_to(user_session_path) end @@ -19,16 +22,12 @@ describe Admin::CommentsController, type: :controller do end context 'logged in as admin, organizer or cfp' do + before :each do + sign_in(organizer) + end describe 'GET #index' do it 'populates a hash with comments' do - conference = create(:conference) - first_user = create(:user) - organizer_role = create(:role, name: 'organizer', resource: conference) - organizer = create(:user, role_ids: organizer_role.id) - event = create(:event, conference: conference) - comment = create(:comment, commentable_type: 'Event', commentable_id: event.id) - sign_in(organizer) - + comment get :index expect(assigns(:comments)).to be_a(Hash) # assigns(:comments).first returns an array of first pair key-value from hash. @@ -36,26 +35,12 @@ describe Admin::CommentsController, type: :controller do expect(assigns(:comments).first.first.title).to eq(comment.commentable.conference.title) end it 'has status 200: OK' do - conference = create(:conference) - first_user = create(:user) - organizer_role = create(:role, name: 'organizer', resource: conference) - organizer = create(:user, role_ids: organizer_role.id) - event = create(:event, conference: conference) - comment = create(:comment, commentable_type: 'Event', commentable_id: event.id) - sign_in(organizer) - + comment get :index expect(response).to have_http_status(:ok) end it 'renders the :index template' do - conference = create(:conference) - first_user = create(:user) - organizer_role = create(:role, name: 'organizer', resource: conference) - organizer = create(:user, role_ids: organizer_role.id) - event = create(:event, conference: conference) - comment = create(:comment, commentable_type: 'Event', commentable_id: event.id) - sign_in(organizer) - + comment get :index expect(response).to render_template(:index) end @@ -65,13 +50,8 @@ describe Admin::CommentsController, type: :controller do context 'logged in with any other role or normal user' do describe 'GET#index' do it 'requires organizer privileges' do - conference = create(:conference) - first_user = create(:user) - participant = create(:user) - event = create(:event, conference: conference) - comment = create(:comment, commentable_type: 'Event', commentable_id: event.id) sign_in(participant) - + comment get :index expect(response).to redirect_to(root_path) expect(flash[:alert]).to match('You are not authorized to access this area!') diff --git a/spec/factories/users.rb b/spec/factories/users.rb index 1f566252..e5c0b7c1 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -6,7 +6,6 @@ FactoryGirl.define do sequence(:username) { |n| "username#{n}" } password 'changeme' password_confirmation 'changeme' - last_sign_in_at { Time.now - 10.days } confirmed_at { Time.now } biography <<-EOS Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus enim From e6480d26762403478e2541ff167e6e4995913317 Mon Sep 17 00:00:00 2001 From: raluka Date: Wed, 16 Sep 2015 12:23:45 +0200 Subject: [PATCH 21/49] added extra condition for view Create Conference link --- app/views/layouts/_user_menu.html.haml | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/app/views/layouts/_user_menu.html.haml b/app/views/layouts/_user_menu.html.haml index da55935a..1764d9d0 100644 --- a/app/views/layouts/_user_menu.html.haml +++ b/app/views/layouts/_user_menu.html.haml @@ -24,11 +24,12 @@ - if can? :access, Admin %li.divider %li - = link_to(admin_conference_index_path()) do - - if Conference.any? + - if Conference.any? + = link_to(admin_conference_index_path()) do %span.fa.fa-home - Administration - - else + Administration + - if can? :create, Conference + =link_to(new_admin_conference_path) do %span.fa.fa-plus Create Conference -if @conference and @conference.id and can? :show, @conference From 4d745fb6e35b8b89fecd905418683218b868a8f9 Mon Sep 17 00:00:00 2001 From: raluka Date: Wed, 16 Sep 2015 13:29:52 +0200 Subject: [PATCH 22/49] moved comment instance into before block --- spec/controllers/admin/comments_controller_spec.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/spec/controllers/admin/comments_controller_spec.rb b/spec/controllers/admin/comments_controller_spec.rb index b3ff0914..ac75b189 100644 --- a/spec/controllers/admin/comments_controller_spec.rb +++ b/spec/controllers/admin/comments_controller_spec.rb @@ -24,10 +24,10 @@ describe Admin::CommentsController, type: :controller do context 'logged in as admin, organizer or cfp' do before :each do sign_in(organizer) + comment end describe 'GET #index' do it 'populates a hash with comments' do - comment get :index expect(assigns(:comments)).to be_a(Hash) # assigns(:comments).first returns an array of first pair key-value from hash. @@ -35,12 +35,10 @@ describe Admin::CommentsController, type: :controller do expect(assigns(:comments).first.first.title).to eq(comment.commentable.conference.title) end it 'has status 200: OK' do - comment get :index expect(response).to have_http_status(:ok) end it 'renders the :index template' do - comment get :index expect(response).to render_template(:index) end From 96e6adc71c7aa589ff795d619d54ea80dda2bd37 Mon Sep 17 00:00:00 2001 From: raluka Date: Wed, 16 Sep 2015 14:00:00 +0200 Subject: [PATCH 23/49] added comments to explain comments_controller --- app/controllers/admin/comments_controller.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/controllers/admin/comments_controller.rb b/app/controllers/admin/comments_controller.rb index 4fa39d32..874439df 100644 --- a/app/controllers/admin/comments_controller.rb +++ b/app/controllers/admin/comments_controller.rb @@ -3,17 +3,24 @@ module Admin load_and_authorize_resource def index + # All available comments, grouped and sorted @comments = grouped_comments(accessible_ordered_comments) + + # Grouped, sorted, available comments, posted since current_user last login @unread_comments = grouped_comments(accessible_ordered_comments.find_since_last_login(current_user)) + + # Grouped, sorted, available comments, posted by current_user @posted_comments = grouped_comments(accessible_ordered_comments.find_comments_by_user(current_user)) end private + # Returning all available comments, ordered by created_at: :desc and by event.title 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 +# Grouping all comments by conference, and by event. It returns {:conference => {:event => [{comment_2}, {comment_1 }]}} def grouped_comments(remarks) remarks.group_by{ |comment| comment.commentable.conference }.map {|conference, comments| [conference, comments.group_by{|comment| comment.commentable}]}.to_h end From 35714f350ee6194239a1f0c0f832d94670031cc3 Mon Sep 17 00:00:00 2001 From: raluka Date: Fri, 17 Jul 2015 18:55:47 +0200 Subject: [PATCH 24/49] 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 From aa1ff51b584e0550e421b4cd3a8731af4734cbb1 Mon Sep 17 00:00:00 2001 From: raluka Date: Mon, 24 Aug 2015 17:17:29 +0200 Subject: [PATCH 25/49] small changes --- app/views/layouts/_navigation.html.haml | 2 +- config/routes.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/views/layouts/_navigation.html.haml b/app/views/layouts/_navigation.html.haml index 66b4822c..543c7a8f 100644 --- a/app/views/layouts/_navigation.html.haml +++ b/app/views/layouts/_navigation.html.haml @@ -25,7 +25,7 @@ %b.caret %ul.dropdown-menu = render 'layouts/user_menu' - - if can? :manage, Conference + - if can? :manage, Comment %ul.nav.navbar-nav.navbar-right %li.dropdown %a.dropdown-toggle{"data-toggle" => "dropdown", :href => "#", id: "unread-comments-preview"} diff --git a/config/routes.rb b/config/routes.rb index f7d30750..05766f76 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -15,7 +15,7 @@ Osem::Application.routes.draw do namespace :admin do resources :users resources :people - resources :comments + resources :comments, only: [:index] resources :conference do member do get :roles From 6985493e5c167119fcce2a731bed567adba7502d Mon Sep 17 00:00:00 2001 From: raluka Date: Mon, 24 Aug 2015 17:44:24 +0200 Subject: [PATCH 26/49] unread_notifications has no longer assigned instance variable --- app/helpers/application_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 9a0131f8..54d79425 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -280,6 +280,6 @@ module ApplicationHelper end def unread_notifications(user) - @unread_notifications = Comment.find_since_last_login(user) + Comment.find_since_last_login(user) end end From b780996128604d48299841d0569466d7254cb57f Mon Sep 17 00:00:00 2001 From: raluka Date: Tue, 25 Aug 2015 14:41:09 +0200 Subject: [PATCH 27/49] created comments with FactoryGirl --- app/models/ability.rb | 7 +++++-- app/views/layouts/_navigation.html.haml | 2 +- spec/factories/comments.rb | 9 +++++++++ spec/factories/events.rb | 1 + spec/models/ability_spec.rb | 1 + 5 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 spec/factories/comments.rb diff --git a/app/models/ability.rb b/app/models/ability.rb index e587cd08..c6f0eb64 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -137,7 +137,9 @@ 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 + can [:read, :create], Comment, commentable_type: 'Event', + commentable_id: Event.where(conference_id: conf_ids_for_organizer).pluck(:id) + end def signed_in_with_cfp_role(user) @@ -156,7 +158,8 @@ 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 + can [:read, :create], Comment, commentable_type: 'Event', + commentable_id: Event.where(conference_id: conf_ids_for_cfp).pluck(:id) end def signed_in_with_info_desk_role(user) diff --git a/app/views/layouts/_navigation.html.haml b/app/views/layouts/_navigation.html.haml index 543c7a8f..000e46f9 100644 --- a/app/views/layouts/_navigation.html.haml +++ b/app/views/layouts/_navigation.html.haml @@ -25,7 +25,7 @@ %b.caret %ul.dropdown-menu = render 'layouts/user_menu' - - if can? :manage, Comment + - if can? :read, Comment %ul.nav.navbar-nav.navbar-right %li.dropdown %a.dropdown-toggle{"data-toggle" => "dropdown", :href => "#", id: "unread-comments-preview"} diff --git a/spec/factories/comments.rb b/spec/factories/comments.rb new file mode 100644 index 00000000..a424e23b --- /dev/null +++ b/spec/factories/comments.rb @@ -0,0 +1,9 @@ +# Read about factories at https://github.com/thoughtbot/factory_girl + +FactoryGirl.define do + factory :comment do + body 'Most interresting comment ever, created by a girl.' + user + association :commentable, factory: :event + end +end diff --git a/spec/factories/events.rb b/spec/factories/events.rb index 537dfecb..6dc89b22 100644 --- a/spec/factories/events.rb +++ b/spec/factories/events.rb @@ -47,6 +47,7 @@ FactoryGirl.define do event.difficulty_level = build(:difficulty_level, conference: event.conference) event.track = build(:track, conference: event.conference) event.room = build(:room, conference: event.conference) + event.comment_threads << build(:comment, commentable: event) end end diff --git a/spec/models/ability_spec.rb b/spec/models/ability_spec.rb index b3859748..1c390f87 100644 --- a/spec/models/ability_spec.rb +++ b/spec/models/ability_spec.rb @@ -141,6 +141,7 @@ describe 'User' do it{ should_not be_able_to(:manage, other_event.difficulty_level) } it{ should be_able_to(:manage, event.commercials.first) } it{ should_not be_able_to(:manage, other_event.commercials.first) } + it{ should be_able_to([:read, :create], event.comment_threads.first) } end context 'when user has the role cfp' do From e10a2491d3448ff8b02748d79a2226f8b12ac348 Mon Sep 17 00:00:00 2001 From: raluka Date: Wed, 26 Aug 2015 13:07:13 +0200 Subject: [PATCH 28/49] created tests for user ability --- app/models/ability.rb | 9 ++++----- app/views/layouts/_navigation.html.haml | 2 +- spec/models/ability_spec.rb | 9 ++++++++- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/app/models/ability.rb b/app/models/ability.rb index c6f0eb64..974f3993 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -137,9 +137,8 @@ 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 [:read, :create], Comment, commentable_type: 'Event', - commentable_id: Event.where(conference_id: conf_ids_for_organizer).pluck(:id) - + can [:index, :create], Comment, commentable_type: 'Event', + commentable_id: Event.where(conference_id: conf_ids_for_organizer).pluck(:id) end def signed_in_with_cfp_role(user) @@ -158,8 +157,8 @@ 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 [:read, :create], Comment, commentable_type: 'Event', - commentable_id: Event.where(conference_id: conf_ids_for_cfp).pluck(:id) + can [:index, :create], Comment, commentable_type: 'Event', + commentable_id: Event.where(conference_id: conf_ids_for_cfp).pluck(:id) end def signed_in_with_info_desk_role(user) diff --git a/app/views/layouts/_navigation.html.haml b/app/views/layouts/_navigation.html.haml index 000e46f9..60e32c12 100644 --- a/app/views/layouts/_navigation.html.haml +++ b/app/views/layouts/_navigation.html.haml @@ -25,7 +25,7 @@ %b.caret %ul.dropdown-menu = render 'layouts/user_menu' - - if can? :read, Comment + - if can? :index, Comment #TODO modify condition %ul.nav.navbar-nav.navbar-right %li.dropdown %a.dropdown-toggle{"data-toggle" => "dropdown", :href => "#", id: "unread-comments-preview"} diff --git a/spec/models/ability_spec.rb b/spec/models/ability_spec.rb index 1c390f87..30593a75 100644 --- a/spec/models/ability_spec.rb +++ b/spec/models/ability_spec.rb @@ -141,7 +141,8 @@ describe 'User' do it{ should_not be_able_to(:manage, other_event.difficulty_level) } it{ should be_able_to(:manage, event.commercials.first) } it{ should_not be_able_to(:manage, other_event.commercials.first) } - it{ should be_able_to([:read, :create], event.comment_threads.first) } + it{ should be_able_to(:create, event.comment_threads.first) } + it{ should be_able_to(:index, event.comment_threads.first) } end context 'when user has the role cfp' do @@ -199,6 +200,8 @@ describe 'User' do it{ should_not be_able_to(:manage, other_event.difficulty_level) } it{ should be_able_to(:manage, event.commercials.first) } it{ should_not be_able_to(:manage, other_event.commercials.first) } + it{ should be_able_to(:create, event.comment_threads.first) } + it{ should be_able_to(:index, event.comment_threads.first) } end context 'when user has the role info_desk' do @@ -256,6 +259,8 @@ describe 'User' do it{ should_not be_able_to(:manage, other_event.difficulty_level) } it{ should_not be_able_to(:manage, event.commercials.first) } it{ should_not be_able_to(:manage, other_event.commercials.first) } + it{ should_not be_able_to(:create, event.comment_threads.first) } + it{ should_not be_able_to(:index, event.comment_threads.first) } end context 'when user has the role volunteers_coordinator' do @@ -313,6 +318,8 @@ describe 'User' do it{ should_not be_able_to(:manage, other_event.difficulty_level) } it{ should_not be_able_to(:manage, event.commercials.first) } it{ should_not be_able_to(:manage, other_event.commercials.first) } + it{ should_not be_able_to(:create, event.comment_threads.first) } + it{ should_not be_able_to(:index, event.comment_threads.first) } it 'should be_able to :manage Vposition' it 'should be_able to :manage Vday' end From 74dcd75a8c2a4220d5bb23317c9f5deb55575128 Mon Sep 17 00:00:00 2001 From: raluka Date: Wed, 26 Aug 2015 22:09:36 +0200 Subject: [PATCH 29/49] created can_manage_comments helper for rendering button --- app/helpers/application_helper.rb | 4 ++++ app/models/ability.rb | 4 ++-- app/models/user.rb | 2 +- app/views/layouts/_navigation.html.haml | 2 +- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 54d79425..7f24331e 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -263,6 +263,10 @@ module ApplicationHelper end end + def can_manage_comments(conference) + (current_user.has_role? :organizer, conference) || (current_user.has_role? :cfp, conference) + end + def sign_in_path if CONFIG['authentication']['ichain']['enabled'] new_user_ichain_session_path diff --git a/app/models/ability.rb b/app/models/ability.rb index 974f3993..28acd4f0 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -137,7 +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 [:index, :create], Comment, commentable_type: 'Event', + can :index, Comment, commentable_type: 'Event', commentable_id: Event.where(conference_id: conf_ids_for_organizer).pluck(:id) end @@ -157,7 +157,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 [:index, :create], Comment, commentable_type: 'Event', + can :index, Comment, commentable_type: 'Event', commentable_id: Event.where(conference_id: conf_ids_for_cfp).pluck(:id) end diff --git a/app/models/user.rb b/app/models/user.rb index 6ffd2059..cba11d58 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -29,7 +29,7 @@ class User < ActiveRecord::Base devise(*devise_modules) - has_and_belongs_to_many :roles + has_and_belongs_to_many :roles has_many :openids attr_accessible :email, :password, :password_confirmation, :remember_me, :role_id, :role_ids, diff --git a/app/views/layouts/_navigation.html.haml b/app/views/layouts/_navigation.html.haml index 60e32c12..1f66af8b 100644 --- a/app/views/layouts/_navigation.html.haml +++ b/app/views/layouts/_navigation.html.haml @@ -25,7 +25,7 @@ %b.caret %ul.dropdown-menu = render 'layouts/user_menu' - - if can? :index, Comment #TODO modify condition + - if can_manage_comments(@conference) %ul.nav.navbar-nav.navbar-right %li.dropdown %a.dropdown-toggle{"data-toggle" => "dropdown", :href => "#", id: "unread-comments-preview"} From df29484317076861f6041727f402e2e300fc644e Mon Sep 17 00:00:00 2001 From: raluka Date: Thu, 27 Aug 2015 12:36:35 +0200 Subject: [PATCH 30/49] changed wrong test for ability --- app/models/ability.rb | 4 ++-- spec/models/ability_spec.rb | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/models/ability.rb b/app/models/ability.rb index 28acd4f0..2b78cc06 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -138,7 +138,7 @@ class Ability can :manage, SponsorshipLevel, conference_id: conf_ids_for_organizer can :manage, Ticket, conference_id: conf_ids_for_organizer can :index, Comment, commentable_type: 'Event', - commentable_id: Event.where(conference_id: conf_ids_for_organizer).pluck(:id) + commentable_id: Event.where(conference_id: conf_ids_for_organizer).pluck(:id) end def signed_in_with_cfp_role(user) @@ -158,7 +158,7 @@ class Ability can :manage, Commercial, commercialable_type: 'Event', commercialable_id: Event.where(conference_id: conf_ids_for_cfp).pluck(:id) can :index, Comment, commentable_type: 'Event', - commentable_id: Event.where(conference_id: conf_ids_for_cfp).pluck(:id) + commentable_id: Event.where(conference_id: conf_ids_for_cfp).pluck(:id) end def signed_in_with_info_desk_role(user) diff --git a/spec/models/ability_spec.rb b/spec/models/ability_spec.rb index 30593a75..73812dc1 100644 --- a/spec/models/ability_spec.rb +++ b/spec/models/ability_spec.rb @@ -141,8 +141,8 @@ describe 'User' do it{ should_not be_able_to(:manage, other_event.difficulty_level) } it{ should be_able_to(:manage, event.commercials.first) } it{ should_not be_able_to(:manage, other_event.commercials.first) } - it{ should be_able_to(:create, event.comment_threads.first) } it{ should be_able_to(:index, event.comment_threads.first) } + it{ should_not be_able_to(:index, other_event.comment_threads.first) } end context 'when user has the role cfp' do @@ -200,8 +200,8 @@ describe 'User' do it{ should_not be_able_to(:manage, other_event.difficulty_level) } it{ should be_able_to(:manage, event.commercials.first) } it{ should_not be_able_to(:manage, other_event.commercials.first) } - it{ should be_able_to(:create, event.comment_threads.first) } it{ should be_able_to(:index, event.comment_threads.first) } + it{ should_not be_able_to(:index, other_event.comment_threads.first) } end context 'when user has the role info_desk' do @@ -259,8 +259,8 @@ describe 'User' do it{ should_not be_able_to(:manage, other_event.difficulty_level) } it{ should_not be_able_to(:manage, event.commercials.first) } it{ should_not be_able_to(:manage, other_event.commercials.first) } - it{ should_not be_able_to(:create, event.comment_threads.first) } it{ should_not be_able_to(:index, event.comment_threads.first) } + it{ should_not be_able_to(:index, other_event.comment_threads.first) } end context 'when user has the role volunteers_coordinator' do @@ -318,8 +318,8 @@ describe 'User' do it{ should_not be_able_to(:manage, other_event.difficulty_level) } it{ should_not be_able_to(:manage, event.commercials.first) } it{ should_not be_able_to(:manage, other_event.commercials.first) } - it{ should_not be_able_to(:create, event.comment_threads.first) } it{ should_not be_able_to(:index, event.comment_threads.first) } + it{ should_not be_able_to(:index, other_event.comment_threads.first) } it 'should be_able to :manage Vposition' it 'should be_able to :manage Vday' end From 52a503afd2c831b97fe2d9979296e8dc0132415f Mon Sep 17 00:00:00 2001 From: raluka Date: Tue, 1 Sep 2015 13:01:57 +0200 Subject: [PATCH 31/49] changed #signed_in_with_roles(user) in ability.rb model --- app/controllers/admin/comments_controller.rb | 2 ++ app/helpers/application_helper.rb | 4 ---- app/models/ability.rb | 8 ++++---- app/views/layouts/_navigation.html.haml | 2 +- 4 files changed, 7 insertions(+), 9 deletions(-) diff --git a/app/controllers/admin/comments_controller.rb b/app/controllers/admin/comments_controller.rb index 990375cf..ae6623a1 100644 --- a/app/controllers/admin/comments_controller.rb +++ b/app/controllers/admin/comments_controller.rb @@ -1,7 +1,9 @@ module Admin class CommentsController < Admin::BaseController + load_and_authorize_resource :conference, find_by: :short_title load_and_authorize_resource + def index @ordered_events = Event.order(:title).all end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 7f24331e..54d79425 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -263,10 +263,6 @@ module ApplicationHelper end end - def can_manage_comments(conference) - (current_user.has_role? :organizer, conference) || (current_user.has_role? :cfp, conference) - end - def sign_in_path if CONFIG['authentication']['ichain']['enabled'] new_user_ichain_session_path diff --git a/app/models/ability.rb b/app/models/ability.rb index 2b78cc06..b71bee97 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -88,10 +88,10 @@ class Ability # Abilities from not_signed_in and signed_in are also inherited signed_in(user) - signed_in_with_organizer_role(user) - signed_in_with_cfp_role(user) - signed_in_with_info_desk_role(user) - signed_in_with_volunteers_coordinator_role(user) + signed_in_with_organizer_role(user) if user.has_role? :organizer, :any + signed_in_with_cfp_role(user) if user.has_role? :cfp, :any + signed_in_with_info_desk_role(user) if user.has_role? :info_desk, :any + signed_in_with_volunteers_coordinator_role(user) if user.has_role? :volunteer_coordinator, :any # for users with any role can [:show], Conference diff --git a/app/views/layouts/_navigation.html.haml b/app/views/layouts/_navigation.html.haml index 1f66af8b..a9a45ecc 100644 --- a/app/views/layouts/_navigation.html.haml +++ b/app/views/layouts/_navigation.html.haml @@ -25,7 +25,7 @@ %b.caret %ul.dropdown-menu = render 'layouts/user_menu' - - if can_manage_comments(@conference) + - if can? :index, Comment %ul.nav.navbar-nav.navbar-right %li.dropdown %a.dropdown-toggle{"data-toggle" => "dropdown", :href => "#", id: "unread-comments-preview"} From 800a41b3f9726c2a28d045e7c1624ed63e26b453 Mon Sep 17 00:00:00 2001 From: raluka Date: Thu, 3 Sep 2015 12:17:42 +0200 Subject: [PATCH 32/49] added logic for available conferences based on roles in comments_controller.rb --- app/controllers/admin/comments_controller.rb | 4 +-- app/models/user.rb | 2 +- .../admin/comments/_all_comments.html.haml | 30 ++++++++++--------- .../admin/comments/_posted_comments.html.haml | 26 ++++++++-------- .../admin/comments/_unread_comments.html.haml | 30 ++++++++++--------- 5 files changed, 48 insertions(+), 44 deletions(-) diff --git a/app/controllers/admin/comments_controller.rb b/app/controllers/admin/comments_controller.rb index ae6623a1..32ae0e85 100644 --- a/app/controllers/admin/comments_controller.rb +++ b/app/controllers/admin/comments_controller.rb @@ -1,11 +1,9 @@ module Admin class CommentsController < Admin::BaseController - load_and_authorize_resource :conference, find_by: :short_title load_and_authorize_resource - def index - @ordered_events = Event.order(:title).all + @conferences_available = Conference.with_roles([:admin, :organizer, :cfp], current_user) end end end diff --git a/app/models/user.rb b/app/models/user.rb index cba11d58..6ffd2059 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -29,7 +29,7 @@ class User < ActiveRecord::Base devise(*devise_modules) - has_and_belongs_to_many :roles + has_and_belongs_to_many :roles has_many :openids attr_accessible :email, :password, :password_confirmation, :remember_me, :role_id, :role_ids, diff --git a/app/views/admin/comments/_all_comments.html.haml b/app/views/admin/comments/_all_comments.html.haml index e7db8f0d..3ad58e89 100644 --- a/app/views/admin/comments/_all_comments.html.haml +++ b/app/views/admin/comments/_all_comments.html.haml @@ -1,16 +1,18 @@ .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) + -@conferences_available.each do |conference| + -conference.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) + = conference.title + %hr + -event.comment_threads.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 - -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 index 5b41ff56..3cdc2102 100644 --- a/app/views/admin/comments/_posted_comments.html.haml +++ b/app/views/admin/comments/_posted_comments.html.haml @@ -1,14 +1,16 @@ .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) + -@conferences_available.each do |conference| + -conference.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) + = conference.title + %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 - -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 index a9960123..8c58193e 100644 --- a/app/views/admin/comments/_unread_comments.html.haml +++ b/app/views/admin/comments/_unread_comments.html.haml @@ -1,16 +1,18 @@ .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) + -@conferences_available.each do |conference| + -conference.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) + = conference.title + %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 - -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 From 0c39590f2e3801226adc9bebf4692167284523fc Mon Sep 17 00:00:00 2001 From: raluka Date: Thu, 3 Sep 2015 14:04:16 +0200 Subject: [PATCH 33/49] fixed Administration link in user_menu dropdown --- app/views/layouts/_user_menu.html.haml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/views/layouts/_user_menu.html.haml b/app/views/layouts/_user_menu.html.haml index fe349b3b..5c22be72 100644 --- a/app/views/layouts/_user_menu.html.haml +++ b/app/views/layouts/_user_menu.html.haml @@ -21,7 +21,10 @@ = link_to(destroy_user_session_path, :method=>'delete') do %span.fa.fa-minus Sign out -- if can? :manage, Conference +- if current_user.is_admin || (current_user.has_any_role? :admin, { :name => :organizer, :resource => :any }, + { :name => :cfp, :resource => :any }, + { :name => :info_desk, :resource => :any }, + { :name => :volunteer_coordinator, :resource => :any }) %li.divider %li = link_to(admin_conference_index_path()) do From cd2a52023b5c8cc7890177878aed9827c864f591 Mon Sep 17 00:00:00 2001 From: raluka Date: Thu, 3 Sep 2015 14:20:11 +0200 Subject: [PATCH 34/49] changed comments helper to proper return unread comments available for current_user --- app/helpers/application_helper.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 54d79425..3498aaa2 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -280,6 +280,7 @@ module ApplicationHelper end def unread_notifications(user) - Comment.find_since_last_login(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)) end end From 7ba3cfd473eb7e9671e717ec23f1af5a7ffde3bb Mon Sep 17 00:00:00 2001 From: raluka Date: Thu, 3 Sep 2015 17:03:05 +0200 Subject: [PATCH 35/49] event title and conference title in 2 different lines now in view --- app/views/admin/comments/_all_comments.html.haml | 5 +++-- app/views/admin/comments/_posted_comments.html.haml | 5 +++-- app/views/admin/comments/_unread_comments.html.haml | 5 +++-- app/views/layouts/_unread_comment.html.haml | 2 -- 4 files changed, 9 insertions(+), 8 deletions(-) delete mode 100644 app/views/layouts/_unread_comment.html.haml diff --git a/app/views/admin/comments/_all_comments.html.haml b/app/views/admin/comments/_all_comments.html.haml index 3ad58e89..004c0606 100644 --- a/app/views/admin/comments/_all_comments.html.haml +++ b/app/views/admin/comments/_all_comments.html.haml @@ -5,8 +5,9 @@ %panel %panel.panel-header %h4.title - = link_to event.title, admin_conference_event_path(event.conference.short_title, event) - = conference.title + %ul.list-unstyled + %li= link_to event.title, admin_conference_event_path(event.conference.short_title, event) + %li= conference.title %hr -event.comment_threads.each do |comment| %panel-body diff --git a/app/views/admin/comments/_posted_comments.html.haml b/app/views/admin/comments/_posted_comments.html.haml index 3cdc2102..3c49d82c 100644 --- a/app/views/admin/comments/_posted_comments.html.haml +++ b/app/views/admin/comments/_posted_comments.html.haml @@ -5,8 +5,9 @@ %panel %panel.panel-header %h4.title - = link_to event.title, admin_conference_event_path(event.conference.short_title, event) - = conference.title + %ul.list-unstyled + %li= link_to event.title, admin_conference_event_path(event.conference.short_title, event) + %li= conference.title %hr -event.comment_threads.find_comments_by_user(current_user).each do |comment| %panel-body diff --git a/app/views/admin/comments/_unread_comments.html.haml b/app/views/admin/comments/_unread_comments.html.haml index 8c58193e..e229aa78 100644 --- a/app/views/admin/comments/_unread_comments.html.haml +++ b/app/views/admin/comments/_unread_comments.html.haml @@ -5,8 +5,9 @@ %panel %panel.panel-header %h4.title - = link_to event.title, admin_conference_event_path(event.conference.short_title, event) - = conference.title + %ul.list-unstyled + %li= link_to event.title, admin_conference_event_path(event.conference.short_title, event) + %li= conference.title %hr -event.comment_threads.find_since_last_login(current_user).each do |comment| %panel-body diff --git a/app/views/layouts/_unread_comment.html.haml b/app/views/layouts/_unread_comment.html.haml deleted file mode 100644 index d337df5c..00000000 --- a/app/views/layouts/_unread_comment.html.haml +++ /dev/null @@ -1,2 +0,0 @@ -%li= link_to("New comment for: #{unread_comment.commentable.title}", admin_conference_event_path(unread_comment.commentable.conference.short_title, unread_comment.commentable_id)) - From 1addae9cd992ef02243883266bc4561b36fa7e07 Mon Sep 17 00:00:00 2001 From: raluka Date: Mon, 7 Sep 2015 18:56:26 +0200 Subject: [PATCH 36/49] WIP changed CommentsController#index; temporary view for all_comments --- app/controllers/admin/comments_controller.rb | 3 ++- .../admin/conference_controller.rb | 1 + app/models/user.rb | 2 +- .../admin/comments/_all_comments.html.haml | 27 +++++++------------ .../admin/comments/_posted_comments.html.haml | 17 +----------- .../admin/comments/_unread_comments.html.haml | 19 +------------ 6 files changed, 15 insertions(+), 54 deletions(-) diff --git a/app/controllers/admin/comments_controller.rb b/app/controllers/admin/comments_controller.rb index 32ae0e85..ebcc9f92 100644 --- a/app/controllers/admin/comments_controller.rb +++ b/app/controllers/admin/comments_controller.rb @@ -3,7 +3,8 @@ module Admin load_and_authorize_resource def index - @conferences_available = Conference.with_roles([:admin, :organizer, :cfp], current_user) + @comments = Comment.accessible_by(current_ability).order(created_at: :desc).group_by { |comment| comment.commentable.conference } + @another_comments = Hash[@comments.map{|conference, comments| [conference, comments.group_by {|comment| comment.commentable}]}] end end end diff --git a/app/controllers/admin/conference_controller.rb b/app/controllers/admin/conference_controller.rb index 25f60e12..89ad9970 100644 --- a/app/controllers/admin/conference_controller.rb +++ b/app/controllers/admin/conference_controller.rb @@ -27,6 +27,7 @@ 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/models/user.rb b/app/models/user.rb index 6ffd2059..568a1c89 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -12,7 +12,7 @@ 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)} + scope :comment_notifiable, ->(conference) {joins(:roles).where('roles.name IN (?)', [:organizer, :cfp]).where('roles.resource_type = ? AND roles.resource_id = ?', 'Conference', conference.id)} # Include default devise modules. Others available are: # :token_authenticatable, :confirmable, diff --git a/app/views/admin/comments/_all_comments.html.haml b/app/views/admin/comments/_all_comments.html.haml index 004c0606..a08b6eb9 100644 --- a/app/views/admin/comments/_all_comments.html.haml +++ b/app/views/admin/comments/_all_comments.html.haml @@ -1,19 +1,10 @@ .panel.well - -@conferences_available.each do |conference| - -conference.events.each do |event| - - if event.comment_threads.count > 0 - %panel - %panel.panel-header - %h4.title - %ul.list-unstyled - %li= link_to event.title, admin_conference_event_path(event.conference.short_title, event) - %li= conference.title - %hr - -event.comment_threads.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 + %ul + - @another_comments.each do |conference, events| + %li= conference.title + - events.each do |event, comments| + %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/_posted_comments.html.haml b/app/views/admin/comments/_posted_comments.html.haml index 3c49d82c..7efd1295 100644 --- a/app/views/admin/comments/_posted_comments.html.haml +++ b/app/views/admin/comments/_posted_comments.html.haml @@ -1,17 +1,2 @@ .panel.well - -@conferences_available.each do |conference| - -conference.events.each do |event| - - if event.comment_threads.find_comments_by_user(current_user).count > 0 - %panel - %panel.panel-header - %h4.title - %ul.list-unstyled - %li= link_to event.title, admin_conference_event_path(event.conference.short_title, event) - %li= conference.title - %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 + Some text diff --git a/app/views/admin/comments/_unread_comments.html.haml b/app/views/admin/comments/_unread_comments.html.haml index e229aa78..6a8abba3 100644 --- a/app/views/admin/comments/_unread_comments.html.haml +++ b/app/views/admin/comments/_unread_comments.html.haml @@ -1,19 +1,2 @@ .panel.well - -@conferences_available.each do |conference| - -conference.events.each do |event| - - if event.comment_threads.find_since_last_login(current_user).count > 0 - %panel - %panel.panel-header - %h4.title - %ul.list-unstyled - %li= link_to event.title, admin_conference_event_path(event.conference.short_title, event) - %li= conference.title - %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 + Some text From 8dd0ac7c1d1ad50a324a4774594d8588a57ac8dd Mon Sep 17 00:00:00 2001 From: raluka Date: Tue, 8 Sep 2015 12:15:22 +0200 Subject: [PATCH 37/49] rewrite CommentsController#index to have everything in one call --- app/controllers/admin/comments_controller.rb | 3 +-- app/views/admin/comments/_all_comments.html.haml | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/app/controllers/admin/comments_controller.rb b/app/controllers/admin/comments_controller.rb index ebcc9f92..088d07f5 100644 --- a/app/controllers/admin/comments_controller.rb +++ b/app/controllers/admin/comments_controller.rb @@ -3,8 +3,7 @@ module Admin load_and_authorize_resource def index - @comments = Comment.accessible_by(current_ability).order(created_at: :desc).group_by { |comment| comment.commentable.conference } - @another_comments = Hash[@comments.map{|conference, comments| [conference, comments.group_by {|comment| comment.commentable}]}] + @comments = Comment.accessible_by(current_ability).order(created_at: :desc).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/views/admin/comments/_all_comments.html.haml b/app/views/admin/comments/_all_comments.html.haml index a08b6eb9..25f6b474 100644 --- a/app/views/admin/comments/_all_comments.html.haml +++ b/app/views/admin/comments/_all_comments.html.haml @@ -1,6 +1,6 @@ .panel.well %ul - - @another_comments.each do |conference, events| + - @comments.each do |conference, events| %li= conference.title - events.each do |event, comments| %li= event.title From bc67cac873ae4864826efd76dfc58cf90a87a77f Mon Sep 17 00:00:00 2001 From: raluka Date: Tue, 8 Sep 2015 14:27:14 +0200 Subject: [PATCH 38/49] sorting comments after event title and created at in index --- app/controllers/admin/comments_controller.rb | 2 +- app/views/admin/comments/_all_comments.html.haml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/controllers/admin/comments_controller.rb b/app/controllers/admin/comments_controller.rb index 088d07f5..7f1bc51b 100644 --- a/app/controllers/admin/comments_controller.rb +++ b/app/controllers/admin/comments_controller.rb @@ -3,7 +3,7 @@ module Admin load_and_authorize_resource def index - @comments = Comment.accessible_by(current_ability).order(created_at: :desc).group_by { |comment| comment.commentable.conference }.map {|conference, comments| [conference, comments.group_by {|comment| comment.commentable}]}.to_h + @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 end end end diff --git a/app/views/admin/comments/_all_comments.html.haml b/app/views/admin/comments/_all_comments.html.haml index 25f6b474..7213f040 100644 --- a/app/views/admin/comments/_all_comments.html.haml +++ b/app/views/admin/comments/_all_comments.html.haml @@ -1,9 +1,9 @@ .panel.well %ul - @comments.each do |conference, events| - %li= conference.title + %i.li= conference.title - events.each do |event, comments| - %li= event.title + %b.li= event.title - comments.each do |comment| %li= comment.body %li= comment.user.name From 945bac1fe8879b1e142be14b7f2df9c8d3a13123 Mon Sep 17 00:00:00 2001 From: raluka Date: Wed, 9 Sep 2015 12:15:59 +0200 Subject: [PATCH 39/49] 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 From 21c10166ec67b3e25a2e139f4c4e5a626be7b582 Mon Sep 17 00:00:00 2001 From: raluka Date: Wed, 9 Sep 2015 16:50:07 +0200 Subject: [PATCH 40/49] added test file for comments_controller WIP --- app/controllers/admin/comments_controller.rb | 4 +-- app/helpers/application_helper.rb | 2 +- .../admin/comments_controller_spec.rb | 32 +++++++++++++++++++ 3 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 spec/controllers/admin/comments_controller_spec.rb diff --git a/app/controllers/admin/comments_controller.rb b/app/controllers/admin/comments_controller.rb index 55a3a8f3..aa8423fc 100644 --- a/app/controllers/admin/comments_controller.rb +++ b/app/controllers/admin/comments_controller.rb @@ -12,8 +12,8 @@ module Admin 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 + def grouped_comments(remarks) + remarks.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 417100fd..86f4c418 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -280,6 +280,6 @@ module ApplicationHelper end def unread_notifications(user) - Comment.accessible_by(current_ability).find_since_last_login(current_user).limit(5) + Comment.accessible_by(current_ability).find_since_last_login(user).limit(5) end end diff --git a/spec/controllers/admin/comments_controller_spec.rb b/spec/controllers/admin/comments_controller_spec.rb new file mode 100644 index 00000000..f202c423 --- /dev/null +++ b/spec/controllers/admin/comments_controller_spec.rb @@ -0,0 +1,32 @@ +require 'spec_helper' + +describe Admin::CommentsController do + +# some settings to be done before like creating objects used by tests + describe 'GET #index' do + context 'all comments' do + it 'populates a hash with conference, event, and comment objects' + it 'renders the :index template' + end + + context 'unread_comments' do + it 'populates a hash with conference, event, and comment objects created since last login of current_user' + it 'renders the :index template' + end + + context 'posted_comments' do + it 'populates a hash with conference, event, and comments posted by current_user' + it 'renders the :index template' + end + end + + describe 'accessible_ordered_comments' do + it 'returns comments' + it 'sorts comments by created_at and event title' + end + + describe 'grouped_comments(remarks)' do + it 'groups comments by conference and by event' + it 'returns a hash' + end +end From 208478ff2d2bdd771e0df312414c03ed19b6022e Mon Sep 17 00:00:00 2001 From: raluka Date: Thu, 10 Sep 2015 11:46:49 +0200 Subject: [PATCH 41/49] added pretty view for comments --- .../admin/comments/_all_comments.html.haml | 24 ++++++++++++------- .../admin/comments/_posted_comments.html.haml | 24 ++++++++++++------- .../admin/comments/_unread_comments.html.haml | 24 ++++++++++++------- 3 files changed, 45 insertions(+), 27 deletions(-) diff --git a/app/views/admin/comments/_all_comments.html.haml b/app/views/admin/comments/_all_comments.html.haml index 7213f040..6a5aebfd 100644 --- a/app/views/admin/comments/_all_comments.html.haml +++ b/app/views/admin/comments/_all_comments.html.haml @@ -1,10 +1,16 @@ -.panel.well - %ul - - @comments.each do |conference, events| - %i.li= conference.title +- @comments.each do |conference, events| + .well + %p.h4= 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 + %h3.title + = link_to event.title, admin_conference_event_path(event.conference.short_title, event) + %body + - comments.each do |comment| + %hr + %ul.list-unstyled + %ul.list-inline + %li + %h5.strong Posted by: #{comment.user.name} + %li + %h5.strong Created at: #{comment.created_at} + %li= comment.body diff --git a/app/views/admin/comments/_posted_comments.html.haml b/app/views/admin/comments/_posted_comments.html.haml index dfba16c7..a0459d4a 100644 --- a/app/views/admin/comments/_posted_comments.html.haml +++ b/app/views/admin/comments/_posted_comments.html.haml @@ -1,10 +1,16 @@ -.panel.well - %ul - - @posted_comments.each do |conference, events| - %i.li= conference.title +- @posted_comments.each do |conference, events| + .well + %p.h4= 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 + %h3.title + = link_to event.title, admin_conference_event_path(event.conference.short_title, event) + %body + - comments.each do |comment| + %hr + %ul.list-unstyled + %ul.list-inline + %li + %h5.strong Posted by: #{comment.user.name} + %li + %h5.strong Created at: #{comment.created_at} + %li= comment.body diff --git a/app/views/admin/comments/_unread_comments.html.haml b/app/views/admin/comments/_unread_comments.html.haml index 26763eeb..24c5c9c5 100644 --- a/app/views/admin/comments/_unread_comments.html.haml +++ b/app/views/admin/comments/_unread_comments.html.haml @@ -1,10 +1,16 @@ -.panel.well - %ul - - @unread_comments.each do |conference, events| - %i.li= conference.title +- @unread_comments.each do |conference, events| + .well + %p.h4= 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 + %h3.title + = link_to event.title, admin_conference_event_path(event.conference.short_title, event) + %body + - comments.each do |comment| + %hr + %ul.list-unstyled + %ul.list-inline + %li + %h5.strong Posted by: #{comment.user.name} + %li + %h5.strong Created at: #{comment.created_at} + %li= comment.body From a9a43d726fb0b2f9c32e8d53914971531ddcbb48 Mon Sep 17 00:00:00 2001 From: raluka Date: Mon, 14 Sep 2015 18:50:21 +0200 Subject: [PATCH 42/49] WIP created tests for comments_controller.rb --- app/controllers/admin/comments_controller.rb | 2 + .../admin/comments_controller_spec.rb | 91 ++++++++++++++----- spec/factories/users.rb | 1 + 3 files changed, 73 insertions(+), 21 deletions(-) diff --git a/app/controllers/admin/comments_controller.rb b/app/controllers/admin/comments_controller.rb index aa8423fc..4fa39d32 100644 --- a/app/controllers/admin/comments_controller.rb +++ b/app/controllers/admin/comments_controller.rb @@ -8,6 +8,8 @@ module Admin @posted_comments = grouped_comments(accessible_ordered_comments.find_comments_by_user(current_user)) end + private + 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 diff --git a/spec/controllers/admin/comments_controller_spec.rb b/spec/controllers/admin/comments_controller_spec.rb index f202c423..b8140640 100644 --- a/spec/controllers/admin/comments_controller_spec.rb +++ b/spec/controllers/admin/comments_controller_spec.rb @@ -1,32 +1,81 @@ require 'spec_helper' -describe Admin::CommentsController do +describe Admin::CommentsController, type: :controller do -# some settings to be done before like creating objects used by tests - describe 'GET #index' do - context 'all comments' do - it 'populates a hash with conference, event, and comment objects' - it 'renders the :index template' - end + context 'not logged in user' do + describe 'GET #index' do + it 'renders the :index template' do + conference = create(:conference) + first_user = create(:user) + organizer_role = create(:role, name: 'organizer', resource: conference) + organizer = create(:user, role_ids: organizer_role.id) + event = create(:event, conference: conference) + comment = create(:comment, commentable_type: 'Event', commentable_id: event.id) - context 'unread_comments' do - it 'populates a hash with conference, event, and comment objects created since last login of current_user' - it 'renders the :index template' - end - - context 'posted_comments' do - it 'populates a hash with conference, event, and comments posted by current_user' - it 'renders the :index template' + get :index + expect(response).to redirect_to(user_session_path) + end end end - describe 'accessible_ordered_comments' do - it 'returns comments' - it 'sorts comments by created_at and event title' + context 'logged in as admin, organizer or cfp' do + describe 'GET #index' do + it 'populates a hash with comments' do + conference = create(:conference) + first_user = create(:user) + organizer_role = create(:role, name: 'organizer', resource: conference) + organizer = create(:user, role_ids: organizer_role.id) + event = create(:event, conference: conference) + comment = create(:comment, commentable_type: 'Event', commentable_id: event.id) + sign_in(organizer) + + get :index + expect(assigns(:comments)).to be_a(Hash) + # assigns(:comments).first returns an array of first pair key-value from hash. + # Calling again 'first' returns the key, meaning the Conference object. + expect(assigns(:comments).first.first.title).to eq(comment.commentable.conference.title) + end + it 'has status 200: OK' do + conference = create(:conference) + first_user = create(:user) + organizer_role = create(:role, name: 'organizer', resource: conference) + organizer = create(:user, role_ids: organizer_role.id) + event = create(:event, conference: conference) + comment = create(:comment, commentable_type: 'Event', commentable_id: event.id) + sign_in(organizer) + + get :index + expect(response).to have_http_status(:ok) + end + it 'renders the :index template' do + conference = create(:conference) + first_user = create(:user) + organizer_role = create(:role, name: 'organizer', resource: conference) + organizer = create(:user, role_ids: organizer_role.id) + event = create(:event, conference: conference) + comment = create(:comment, commentable_type: 'Event', commentable_id: event.id) + sign_in(organizer) + + get :index + expect(response).to render_template(:index) + end + end end - describe 'grouped_comments(remarks)' do - it 'groups comments by conference and by event' - it 'returns a hash' + context 'logged in with any other role or normal user' do + describe 'GET#index' do + it 'requires organizer privileges' do + conference = create(:conference) + first_user = create(:user) + participant = create(:user) + event = create(:event, conference: conference) + comment = create(:comment, commentable_type: 'Event', commentable_id: event.id) + sign_in(participant) + + get :index + expect(response).to redirect_to(root_path) + expect(flash[:alert]).to match('You are not authorized to access this area!') + end + end end end diff --git a/spec/factories/users.rb b/spec/factories/users.rb index e5c0b7c1..1f566252 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -6,6 +6,7 @@ FactoryGirl.define do sequence(:username) { |n| "username#{n}" } password 'changeme' password_confirmation 'changeme' + last_sign_in_at { Time.now - 10.days } confirmed_at { Time.now } biography <<-EOS Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus enim From 37339dccc138da3faccea2e6a825f09f2e76801e Mon Sep 17 00:00:00 2001 From: raluka Date: Tue, 15 Sep 2015 12:39:36 +0200 Subject: [PATCH 43/49] refactored comments_controller tests; fixed access to Administration link into user_menu view --- app/models/ability.rb | 1 + app/views/layouts/_user_menu.html.haml | 5 +- .../admin/comments_controller_spec.rb | 54 ++++++------------- spec/factories/users.rb | 1 - 4 files changed, 19 insertions(+), 42 deletions(-) diff --git a/app/models/ability.rb b/app/models/ability.rb index b71bee97..bd30a8fd 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -94,6 +94,7 @@ class Ability signed_in_with_volunteers_coordinator_role(user) if user.has_role? :volunteer_coordinator, :any # for users with any role + can :access, Admin can [:show], Conference can :index, Commercial, commercialable_type: 'Conference' cannot [:edit, :update, :destroy], Question, global: true diff --git a/app/views/layouts/_user_menu.html.haml b/app/views/layouts/_user_menu.html.haml index 5c22be72..da55935a 100644 --- a/app/views/layouts/_user_menu.html.haml +++ b/app/views/layouts/_user_menu.html.haml @@ -21,10 +21,7 @@ = link_to(destroy_user_session_path, :method=>'delete') do %span.fa.fa-minus Sign out -- if current_user.is_admin || (current_user.has_any_role? :admin, { :name => :organizer, :resource => :any }, - { :name => :cfp, :resource => :any }, - { :name => :info_desk, :resource => :any }, - { :name => :volunteer_coordinator, :resource => :any }) +- if can? :access, Admin %li.divider %li = link_to(admin_conference_index_path()) do diff --git a/spec/controllers/admin/comments_controller_spec.rb b/spec/controllers/admin/comments_controller_spec.rb index b8140640..b3ff0914 100644 --- a/spec/controllers/admin/comments_controller_spec.rb +++ b/spec/controllers/admin/comments_controller_spec.rb @@ -2,16 +2,19 @@ require 'spec_helper' describe Admin::CommentsController, type: :controller do + # It is necessary to use bang version of let to build roles before user + let(:conference) { create(:conference) } + let!(:first_user) { create(:user) } + let!(:organizer_role) { create(:role, name: 'organizer', resource: conference) } + let(:organizer) { create(:user, role_ids: organizer_role.id, last_sign_in_at: Time.now - 1.day) } + let(:participant) { create(:user) } + let(:event) { create(:event, conference: conference) } + let(:comment) { create(:comment, commentable_type: 'Event', commentable_id: event.id) } + context 'not logged in user' do describe 'GET #index' do it 'renders the :index template' do - conference = create(:conference) - first_user = create(:user) - organizer_role = create(:role, name: 'organizer', resource: conference) - organizer = create(:user, role_ids: organizer_role.id) - event = create(:event, conference: conference) - comment = create(:comment, commentable_type: 'Event', commentable_id: event.id) - + comment get :index expect(response).to redirect_to(user_session_path) end @@ -19,16 +22,12 @@ describe Admin::CommentsController, type: :controller do end context 'logged in as admin, organizer or cfp' do + before :each do + sign_in(organizer) + end describe 'GET #index' do it 'populates a hash with comments' do - conference = create(:conference) - first_user = create(:user) - organizer_role = create(:role, name: 'organizer', resource: conference) - organizer = create(:user, role_ids: organizer_role.id) - event = create(:event, conference: conference) - comment = create(:comment, commentable_type: 'Event', commentable_id: event.id) - sign_in(organizer) - + comment get :index expect(assigns(:comments)).to be_a(Hash) # assigns(:comments).first returns an array of first pair key-value from hash. @@ -36,26 +35,12 @@ describe Admin::CommentsController, type: :controller do expect(assigns(:comments).first.first.title).to eq(comment.commentable.conference.title) end it 'has status 200: OK' do - conference = create(:conference) - first_user = create(:user) - organizer_role = create(:role, name: 'organizer', resource: conference) - organizer = create(:user, role_ids: organizer_role.id) - event = create(:event, conference: conference) - comment = create(:comment, commentable_type: 'Event', commentable_id: event.id) - sign_in(organizer) - + comment get :index expect(response).to have_http_status(:ok) end it 'renders the :index template' do - conference = create(:conference) - first_user = create(:user) - organizer_role = create(:role, name: 'organizer', resource: conference) - organizer = create(:user, role_ids: organizer_role.id) - event = create(:event, conference: conference) - comment = create(:comment, commentable_type: 'Event', commentable_id: event.id) - sign_in(organizer) - + comment get :index expect(response).to render_template(:index) end @@ -65,13 +50,8 @@ describe Admin::CommentsController, type: :controller do context 'logged in with any other role or normal user' do describe 'GET#index' do it 'requires organizer privileges' do - conference = create(:conference) - first_user = create(:user) - participant = create(:user) - event = create(:event, conference: conference) - comment = create(:comment, commentable_type: 'Event', commentable_id: event.id) sign_in(participant) - + comment get :index expect(response).to redirect_to(root_path) expect(flash[:alert]).to match('You are not authorized to access this area!') diff --git a/spec/factories/users.rb b/spec/factories/users.rb index 1f566252..e5c0b7c1 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -6,7 +6,6 @@ FactoryGirl.define do sequence(:username) { |n| "username#{n}" } password 'changeme' password_confirmation 'changeme' - last_sign_in_at { Time.now - 10.days } confirmed_at { Time.now } biography <<-EOS Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus enim From f3996a9bfff7fa55516352eca685be35141e4e0e Mon Sep 17 00:00:00 2001 From: raluka Date: Wed, 16 Sep 2015 12:23:45 +0200 Subject: [PATCH 44/49] added extra condition for view Create Conference link --- app/views/layouts/_user_menu.html.haml | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/app/views/layouts/_user_menu.html.haml b/app/views/layouts/_user_menu.html.haml index da55935a..1764d9d0 100644 --- a/app/views/layouts/_user_menu.html.haml +++ b/app/views/layouts/_user_menu.html.haml @@ -24,11 +24,12 @@ - if can? :access, Admin %li.divider %li - = link_to(admin_conference_index_path()) do - - if Conference.any? + - if Conference.any? + = link_to(admin_conference_index_path()) do %span.fa.fa-home - Administration - - else + Administration + - if can? :create, Conference + =link_to(new_admin_conference_path) do %span.fa.fa-plus Create Conference -if @conference and @conference.id and can? :show, @conference From 1fb1071feb482282c7cb5fabfd9e08360c9b0967 Mon Sep 17 00:00:00 2001 From: raluka Date: Wed, 16 Sep 2015 13:29:52 +0200 Subject: [PATCH 45/49] moved comment instance into before block --- spec/controllers/admin/comments_controller_spec.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/spec/controllers/admin/comments_controller_spec.rb b/spec/controllers/admin/comments_controller_spec.rb index b3ff0914..ac75b189 100644 --- a/spec/controllers/admin/comments_controller_spec.rb +++ b/spec/controllers/admin/comments_controller_spec.rb @@ -24,10 +24,10 @@ describe Admin::CommentsController, type: :controller do context 'logged in as admin, organizer or cfp' do before :each do sign_in(organizer) + comment end describe 'GET #index' do it 'populates a hash with comments' do - comment get :index expect(assigns(:comments)).to be_a(Hash) # assigns(:comments).first returns an array of first pair key-value from hash. @@ -35,12 +35,10 @@ describe Admin::CommentsController, type: :controller do expect(assigns(:comments).first.first.title).to eq(comment.commentable.conference.title) end it 'has status 200: OK' do - comment get :index expect(response).to have_http_status(:ok) end it 'renders the :index template' do - comment get :index expect(response).to render_template(:index) end From f294316cbade7d3cd953889e27cfd898ff9ff6c3 Mon Sep 17 00:00:00 2001 From: raluka Date: Wed, 16 Sep 2015 14:00:00 +0200 Subject: [PATCH 46/49] added comments to explain comments_controller --- app/controllers/admin/comments_controller.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/controllers/admin/comments_controller.rb b/app/controllers/admin/comments_controller.rb index 4fa39d32..874439df 100644 --- a/app/controllers/admin/comments_controller.rb +++ b/app/controllers/admin/comments_controller.rb @@ -3,17 +3,24 @@ module Admin load_and_authorize_resource def index + # All available comments, grouped and sorted @comments = grouped_comments(accessible_ordered_comments) + + # Grouped, sorted, available comments, posted since current_user last login @unread_comments = grouped_comments(accessible_ordered_comments.find_since_last_login(current_user)) + + # Grouped, sorted, available comments, posted by current_user @posted_comments = grouped_comments(accessible_ordered_comments.find_comments_by_user(current_user)) end private + # Returning all available comments, ordered by created_at: :desc and by event.title 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 +# Grouping all comments by conference, and by event. It returns {:conference => {:event => [{comment_2}, {comment_1 }]}} def grouped_comments(remarks) remarks.group_by{ |comment| comment.commentable.conference }.map {|conference, comments| [conference, comments.group_by{|comment| comment.commentable}]}.to_h end From 1b6372844004a124d725b0123479a0acfee9bcd5 Mon Sep 17 00:00:00 2001 From: raluka Date: Thu, 17 Sep 2015 12:23:01 +0200 Subject: [PATCH 47/49] Grooming conference.rb to fix rubocop offenses --- app/models/conference.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/app/models/conference.rb b/app/models/conference.rb index 94e23149..3c6f20b2 100644 --- a/app/models/conference.rb +++ b/app/models/conference.rb @@ -114,17 +114,17 @@ class Conference < ActiveRecord::Base before_create :add_color def date_range_string - startstr = "Unknown - " - endstr = "Unknown" + startstr = 'Unknown - ' + endstr = 'Unknown' if start_date.month == end_date.month && start_date.year == end_date.year - startstr = start_date.strftime("%B %d - ") - endstr = end_date.strftime("%d, %Y") + startstr = start_date.strftime('%B %d - ') + endstr = end_date.strftime('%d, %Y') elsif start_date.month != end_date.month && start_date.year == end_date.year - startstr = start_date.strftime("%B %d - ") - endstr = end_date.strftime("%B %d, %Y") + startstr = start_date.strftime('%B %d - ') + endstr = end_date.strftime('%B %d, %Y') else - startstr = start_date.strftime("%B %d, %Y - ") - endstr = end_date.strftime("%B %d, %Y") + startstr = start_date.strftime('%B %d, %Y - ') + endstr = end_date.strftime('%B %d, %Y') end result = startstr + endstr From f7c70ba55caca4b7a2b54a9eb3f1bb9e4495801c Mon Sep 17 00:00:00 2001 From: raluka Date: Mon, 21 Sep 2015 15:56:57 +0200 Subject: [PATCH 48/49] updated comment layout from upstream; added grouped_by_event behaviour to comments_dropdown and helper --- app/assets/stylesheets/osem.css.scss | 46 +++++++++++-------- app/helpers/application_helper.rb | 2 +- .../admin/comments/_all_comments.html.haml | 24 ++++------ .../admin/comments/_posted_comments.html.haml | 24 ++++------ .../admin/comments/_unread_comments.html.haml | 24 ++++------ app/views/admin/comments/index.html.haml | 19 ++++---- app/views/layouts/_navigation.html.haml | 16 ++++--- 7 files changed, 78 insertions(+), 77 deletions(-) diff --git a/app/assets/stylesheets/osem.css.scss b/app/assets/stylesheets/osem.css.scss index 83a921a6..69f0fae0 100644 --- a/app/assets/stylesheets/osem.css.scss +++ b/app/assets/stylesheets/osem.css.scss @@ -1,13 +1,13 @@ html { - position: relative; - min-height: 100%; + position: relative; + min-height: 100%; } body { - /* Margin bottom by 2 times the footer height */ - margin-bottom: 60px; - /* Margin bottom by navbar height */ - padding-top: 60px; + /* Margin bottom by 2 times the footer height */ + margin-bottom: 60px; + /* Margin bottom by navbar height */ + padding-top: 60px; } #content { @@ -15,23 +15,23 @@ body { } #footer { - position: absolute; - bottom: 0; - width: 100%; - /* Set the fixed height of the footer here */ - height: 60px; - background-color: #f5f5f5; - .container { - padding: 15px; - } + position: absolute; + bottom: 0; + width: 100%; + /* Set the fixed height of the footer here */ + height: 60px; + background-color: #f5f5f5; + .container { + padding: 15px; + } } .nav-tabs { - margin-bottom: 15px; + margin-bottom: 15px; } .img-center { - margin: 0 auto; + margin: 0 auto; } /* centered columns styles */ @@ -71,4 +71,14 @@ p.comment-body { #account-already { font-size: 0.6em; -} \ No newline at end of file +} + +/* comments views pane: use padding-bottom before next comment box */ +.panel.panel-default .panel-body .box { + padding-bottom: 20px; +} + +/* comments views pane: use padding-bottom after each comment */ +.panel.panel-default .panel-body .box p { + padding-bottom: 20px; +} diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 86f4c418..4c72b3dc 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -280,6 +280,6 @@ module ApplicationHelper end def unread_notifications(user) - Comment.accessible_by(current_ability).find_since_last_login(user).limit(5) + Comment.accessible_by(current_ability).find_since_last_login(user) end end diff --git a/app/views/admin/comments/_all_comments.html.haml b/app/views/admin/comments/_all_comments.html.haml index 6a5aebfd..c7a78cf8 100644 --- a/app/views/admin/comments/_all_comments.html.haml +++ b/app/views/admin/comments/_all_comments.html.haml @@ -1,16 +1,12 @@ - @comments.each do |conference, events| - .well - %p.h4= conference.title - - events.each do |event, comments| - %h3.title - = link_to event.title, admin_conference_event_path(event.conference.short_title, event) - %body - - comments.each do |comment| + .panel.panel-default + .panel-heading + %h4.title.panel-title= conference.title + .panel-body + - events.each do |event, comments| + .box + %h4.title= link_to event.title, admin_conference_event_path(event.conference.short_title, event) %hr - %ul.list-unstyled - %ul.list-inline - %li - %h5.strong Posted by: #{comment.user.name} - %li - %h5.strong Created at: #{comment.created_at} - %li= comment.body + - comments.each do |comment| + %h5.strong Posted by: #{comment.user.name} | Created at: #{comment.created_at} + %p= comment.body diff --git a/app/views/admin/comments/_posted_comments.html.haml b/app/views/admin/comments/_posted_comments.html.haml index a0459d4a..45f3fe2e 100644 --- a/app/views/admin/comments/_posted_comments.html.haml +++ b/app/views/admin/comments/_posted_comments.html.haml @@ -1,16 +1,12 @@ - @posted_comments.each do |conference, events| - .well - %p.h4= conference.title - - events.each do |event, comments| - %h3.title - = link_to event.title, admin_conference_event_path(event.conference.short_title, event) - %body - - comments.each do |comment| + .panel.panel-default + .panel-heading + %h4.title.panel-title= conference.title + .panel-body + - events.each do |event, comments| + .box + %h4.title= link_to event.title, admin_conference_event_path(event.conference.short_title, event) %hr - %ul.list-unstyled - %ul.list-inline - %li - %h5.strong Posted by: #{comment.user.name} - %li - %h5.strong Created at: #{comment.created_at} - %li= comment.body + - comments.each do |comment| + %h5.strong Created at: #{comment.created_at} + %p= comment.body diff --git a/app/views/admin/comments/_unread_comments.html.haml b/app/views/admin/comments/_unread_comments.html.haml index 24c5c9c5..60ad80dc 100644 --- a/app/views/admin/comments/_unread_comments.html.haml +++ b/app/views/admin/comments/_unread_comments.html.haml @@ -1,16 +1,12 @@ - @unread_comments.each do |conference, events| - .well - %p.h4= conference.title - - events.each do |event, comments| - %h3.title - = link_to event.title, admin_conference_event_path(event.conference.short_title, event) - %body - - comments.each do |comment| + .panel.panel-default + .panel-heading + %h4.title.panel-title= conference.title + .panel-body + - events.each do |event, comments| + .box + %h4.title= link_to event.title, admin_conference_event_path(event.conference.short_title, event) %hr - %ul.list-unstyled - %ul.list-inline - %li - %h5.strong Posted by: #{comment.user.name} - %li - %h5.strong Created at: #{comment.created_at} - %li= comment.body + - comments.each do |comment| + %h5.strong Posted by: #{comment.user.name} | Created at: #{comment.created_at} + %p= comment.body diff --git a/app/views/admin/comments/index.html.haml b/app/views/admin/comments/index.html.haml index 96d942f3..9a141c85 100644 --- a/app/views/admin/comments/index.html.haml +++ b/app/views/admin/comments/index.html.haml @@ -1,23 +1,24 @@ %h1 Comments +%br .row - .col-lg-12 + .col-md-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 + Unread %li %a{:href=>"#posted_comments", "data-toggle"=>"tab"} %span.fa.fa-pencil - Posted Comments + Posted + %li + %a{:href=>"#all_comments", "data-toggle"=>"tab"} + %span.fa.fa-comments-o + All .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' + .tab-pane#all_comments + = render partial: 'all_comments' diff --git a/app/views/layouts/_navigation.html.haml b/app/views/layouts/_navigation.html.haml index a9a45ecc..cf2b3b44 100644 --- a/app/views/layouts/_navigation.html.haml +++ b/app/views/layouts/_navigation.html.haml @@ -28,17 +28,19 @@ - if can? :index, Comment %ul.nav.navbar-nav.navbar-right %li.dropdown - %a.dropdown-toggle{"data-toggle" => "dropdown", :href => "#", id: "unread-comments-preview"} + %a.dropdown-toggle{"data-toggle" => "dropdown", :href => "#"} - if unread_notifications(current_user) Notifications (#{unread_notifications(current_user).length}) + %span.fa.fa-comment %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 + - if unread_notifications(current_user).length > 0 + %li.dropdown-header Last 5 Comments for: + - unread_notifications(current_user).limit(5).group_by{ |comment| comment.commentable}.each do |event, comments| + %li= link_to("#{event.title}(#{comments.count})", admin_conference_event_path(event.conference.short_title, event.id)) + %li.divider + %li= link_to "See all unread Comments (#{unread_notifications(current_user).length})", admin_comments_path + %li= link_to 'See all Comments', admin_comments_path(anchor: 'all_comments') - else %ul.nav.navbar-nav.navbar-right - if CONFIG['authentication']['ichain']['enabled'] From 1cf877bf76e119fe0d9e3d524dce10810f6d6fc4 Mon Sep 17 00:00:00 2001 From: raluka Date: Mon, 5 Oct 2015 13:28:35 +0200 Subject: [PATCH 49/49] grooming style --- app/assets/stylesheets/osem.css.scss | 4 ++-- app/views/admin/comments/_all_comments.html.haml | 2 +- app/views/admin/comments/_posted_comments.html.haml | 2 +- app/views/admin/comments/_unread_comments.html.haml | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app/assets/stylesheets/osem.css.scss b/app/assets/stylesheets/osem.css.scss index 69f0fae0..03c221da 100644 --- a/app/assets/stylesheets/osem.css.scss +++ b/app/assets/stylesheets/osem.css.scss @@ -74,11 +74,11 @@ p.comment-body { } /* comments views pane: use padding-bottom before next comment box */ -.panel.panel-default .panel-body .box { +.panel.panel-default .panel-body .notifications { padding-bottom: 20px; } /* comments views pane: use padding-bottom after each comment */ -.panel.panel-default .panel-body .box p { +.panel.panel-default .panel-body .notifications p { padding-bottom: 20px; } diff --git a/app/views/admin/comments/_all_comments.html.haml b/app/views/admin/comments/_all_comments.html.haml index c7a78cf8..02ad15e8 100644 --- a/app/views/admin/comments/_all_comments.html.haml +++ b/app/views/admin/comments/_all_comments.html.haml @@ -4,7 +4,7 @@ %h4.title.panel-title= conference.title .panel-body - events.each do |event, comments| - .box + .notifications %h4.title= link_to event.title, admin_conference_event_path(event.conference.short_title, event) %hr - comments.each do |comment| diff --git a/app/views/admin/comments/_posted_comments.html.haml b/app/views/admin/comments/_posted_comments.html.haml index 45f3fe2e..89c90519 100644 --- a/app/views/admin/comments/_posted_comments.html.haml +++ b/app/views/admin/comments/_posted_comments.html.haml @@ -4,7 +4,7 @@ %h4.title.panel-title= conference.title .panel-body - events.each do |event, comments| - .box + .notifications %h4.title= link_to event.title, admin_conference_event_path(event.conference.short_title, event) %hr - comments.each do |comment| diff --git a/app/views/admin/comments/_unread_comments.html.haml b/app/views/admin/comments/_unread_comments.html.haml index 60ad80dc..9291aa83 100644 --- a/app/views/admin/comments/_unread_comments.html.haml +++ b/app/views/admin/comments/_unread_comments.html.haml @@ -4,7 +4,7 @@ %h4.title.panel-title= conference.title .panel-body - events.each do |event, comments| - .box + .notifications %h4.title= link_to event.title, admin_conference_event_path(event.conference.short_title, event) %hr - comments.each do |comment|