From 58208c73ac855ae62d4a816e41f5ac0053c42179 Mon Sep 17 00:00:00 2001 From: Rishabh Singh Date: Thu, 18 Jul 2019 12:49:50 +0530 Subject: [PATCH] Add comment functionality to tracks --- .haml-lint_todo.yml | 2 +- app/controllers/admin/comments_controller.rb | 6 ++-- app/controllers/admin/tracks_controller.rb | 22 +++++++++++++- app/mailers/mailbot.rb | 20 ++++++++----- app/models/ability.rb | 10 ++++--- app/models/admin_ability.rb | 7 ++++- app/models/track.rb | 2 ++ .../admin/comments/_all_comments.html.haml | 9 ++++-- .../admin/comments/_posted_comments.html.haml | 9 ++++-- .../admin/comments/_unread_comments.html.haml | 9 ++++-- .../admin/tracks/_nested_comments.html.haml | 15 ++++++++++ app/views/admin/tracks/show.html.haml | 18 ++++++++++++ app/views/layouts/_user_menu.html.haml | 7 +++-- ...xt.erb => comment_event_template.text.erb} | 4 +-- .../mailbot/comment_track_template.text.erb | 10 +++++++ config/database.yml | 29 +++++++------------ config/routes.rb | 1 + ...0720062321_add_comments_count_to_tracks.rb | 10 +++++++ db/schema.rb | 11 +++++++ 19 files changed, 153 insertions(+), 48 deletions(-) create mode 100644 app/views/admin/tracks/_nested_comments.html.haml rename app/views/mailbot/{comment_template.text.erb => comment_event_template.text.erb} (61%) create mode 100644 app/views/mailbot/comment_track_template.text.erb create mode 100644 db/migrate/20190720062321_add_comments_count_to_tracks.rb diff --git a/.haml-lint_todo.yml b/.haml-lint_todo.yml index 7bcb33d4..9cc37000 100644 --- a/.haml-lint_todo.yml +++ b/.haml-lint_todo.yml @@ -657,4 +657,4 @@ linters: # Offense count: 38 InlineStyles: - enabled: false + enabled: false \ No newline at end of file diff --git a/app/controllers/admin/comments_controller.rb b/app/controllers/admin/comments_controller.rb index 16c07a6d..9e86fdbd 100644 --- a/app/controllers/admin/comments_controller.rb +++ b/app/controllers/admin/comments_controller.rb @@ -17,12 +17,12 @@ module Admin private - # Returning all available comments, ordered by created_at: :desc and by event.title + # Returning all available comments, ordered by created_at: :desc 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') + Comment.accessible_by(current_ability).order('comments.created_at DESC') end -# Grouping all comments by conference, and by event. It returns {:conference => {:event => [{comment_2}, {comment_1 }]}} +# Grouping all comments by conference, and by commentable obj. It returns {:conference => {:commentable => [{comment_2}, {comment_1 }]}} def grouped_comments(remarks) remarks.group_by{ |comment| comment.commentable.program.conference }.map {|conference, comments| [conference, comments.group_by{|comment| comment.commentable}]}.to_h end diff --git a/app/controllers/admin/tracks_controller.rb b/app/controllers/admin/tracks_controller.rb index a4f4c69e..7d793f88 100644 --- a/app/controllers/admin/tracks_controller.rb +++ b/app/controllers/admin/tracks_controller.rb @@ -30,6 +30,9 @@ module Admin end def show + @comments = @track.root_comments + @comment_count = @track.comment_threads.count + respond_to do |format| format.html { render } format.json { render json: @conference.tracks.to_json } @@ -53,7 +56,20 @@ module Admin end end - def edit; end + def edit + @comments = @track.root_comments + @comment_count = @track.comment_threads.count + end + + def comment + comment = Comment.new(comment_params) + comment.commentable = @track + comment.user_id = current_user.id + comment.save! + comment.move_to_child_of(params[:parent]) unless params[:parent].nil? + + redirect_to admin_conference_program_track_path(@conference.short_title, @track) + end def update if @track.update_attributes(track_params) @@ -139,6 +155,10 @@ module Admin params.require(:track).permit(:name, :description, :color, :short_name, :cfp_active, :start_date, :end_date, :room_id) end + def comment_params + params.require(:comment).permit(:commentable, :body, :user_id) + end + def update_state(transition, notice) errors = @track.update_state(transition) diff --git a/app/mailers/mailbot.rb b/app/mailers/mailbot.rb index 52e15b80..f3069ea3 100644 --- a/app/mailers/mailbot.rb +++ b/app/mailers/mailbot.rb @@ -128,13 +128,19 @@ class Mailbot < ActionMailer::Base def event_comment_mail(comment, user) @comment = comment - @event = @comment.commentable - @conference = @event.program.conference + @commentable = @comment.commentable + @conference = @commentable.program.conference @user = user - - mail(to: @user.email, - from: @conference.contact.email, - template_name: 'comment_template', - subject: "New comment has been posted for #{@event.title}") + if @comment.commentable_type == 'Event' + mail(to: @user.email, + from: @conference.contact.email, + template_name: 'comment_event_template', + subject: "New comment has been posted for #{@commentable.title}") + elsif @comment.commentable_type == 'Track' + mail(to: @user.email, + from: @conference.contact.email, + template_name: 'comment_track_template', + subject: "New comment has been posted for #{@commentable.short_name}") + end end end diff --git a/app/models/ability.rb b/app/models/ability.rb index caf8f2a4..e7916e82 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -155,15 +155,17 @@ class Ability can :manage, Registration, conference_id: conf_ids_for_organizer # To access conference/proposals can :manage, Event, program: { conference_id: conf_ids_for_organizer } + can :manage, Comment # To access comment link in menu bar - can :index, Comment, commentable_type: 'Event', - commentable_id: Event.where(program_id: Program.where(conference_id: conf_ids_for_organizer).pluck(:id)).pluck(:id) + # can :index, Comment, commentable_type: 'Event', + # commentable_id: Event.where(program_id: Program.where(conference_id: conf_ids_for_organizer).pluck(:id)).pluck(:id) end if conf_ids_for_cfp # To access comment link in menu bar - can :index, Comment, commentable_type: 'Event', - commentable_id: Event.where(program_id: Program.where(conference_id: conf_ids_for_cfp).pluck(:id)).pluck(:id) + can :manage, Comment + # can :index, Comment, commentable_type: 'Event', + # commentable_id: Event.where(program_id: Program.where(conference_id: conf_ids_for_cfp).pluck(:id)).pluck(:id) # To access conference/proposals can :manage, Event, program: { conference_id: conf_ids_for_cfp } end diff --git a/app/models/admin_ability.rb b/app/models/admin_ability.rb index 19d6c4d3..85580053 100644 --- a/app/models/admin_ability.rb +++ b/app/models/admin_ability.rb @@ -102,6 +102,7 @@ class AdminAbility signed_in_with_organizer_role(user, conf_ids_for_organization_admin) end + # rubocop:disable Metrics/AbcSize def signed_in_with_organizer_role(user, conf_ids_for_organization_admin = []) # ids of all the conferences for which the user has the 'organizer' role and # conferences that belong to organizations for which user is 'organization_admin' @@ -149,7 +150,8 @@ class AdminAbility end can :index, Comment, commentable_type: 'Event', commentable_id: Event.where(program_id: Program.where(conference_id: conf_ids).pluck(:id)).pluck(:id) - + can :index, Comment, commentable_type: 'Track', + commentable_id: Track.where(program_id: Program.where(conference_id: conf_ids).pluck(:id)).pluck(:id) # Abilities for Role (Conference resource) can [:index, :show], Role do |role| role.resource_type == 'Conference' || role.resource_type == 'Track' @@ -163,6 +165,7 @@ class AdminAbility can [:index, :revert_object, :revert_attribute], PaperTrail::Version, item_type: 'User' can [:index, :revert_object, :revert_attribute], PaperTrail::Version, conference_id: conf_ids end + # rubocop:enable Metrics/AbcSize def signed_in_with_cfp_role(user) # ids of all the conferences for which the user has the 'cfp' role @@ -189,6 +192,8 @@ class AdminAbility can :index, Comment, commentable_type: 'Event', commentable_id: Event.where(program_id: Program.where(conference_id: conf_ids_for_cfp).pluck(:id)).pluck(:id) + can :index, Comment, commentable_type: 'Track', + commentable_id: Track.where(program_id: Program.where(conference_id: conf_ids).pluck(:id)).pluck(:id) # Abilities for Role (Conference resource) can [:index, :show], Role do |role| role.resource_type == 'Conference' || role.resource_type == 'Track' diff --git a/app/models/track.rb b/app/models/track.rb index d34bfbef..7724e416 100644 --- a/app/models/track.rb +++ b/app/models/track.rb @@ -15,6 +15,8 @@ class Track < ApplicationRecord has_paper_trail ignore: [:updated_at], meta: { conference_id: :conference_id } + acts_as_commentable + before_create :generate_guid validates :name, presence: true validates :color, format: /\A#[0-9A-F]{6}\z/ diff --git a/app/views/admin/comments/_all_comments.html.haml b/app/views/admin/comments/_all_comments.html.haml index eb3a8093..4871de88 100644 --- a/app/views/admin/comments/_all_comments.html.haml +++ b/app/views/admin/comments/_all_comments.html.haml @@ -1,11 +1,14 @@ -- @comments.each do |conference, events| +- @comments.each do |conference, objects| .panel.panel-default .panel-heading %h4.title.panel-title= conference.title .panel-body - - events.each do |event, comments| + - objects.each do |obj, comments| .notifications - %h4.title= link_to event.title, admin_conference_program_event_path(event.program.conference.short_title, event) + - if obj.class.name == 'Event' + %h4.title= link_to obj.title, admin_conference_program_event_path(obj.program.conference.short_title, obj) + - if obj.class.name == 'Track' + %h4.title= link_to obj.name, admin_conference_program_track_path(obj.program.conference.short_title, obj) %hr - comments.each do |comment| %h5.strong Posted by: #{comment.user.name} | Created at: #{comment.created_at} diff --git a/app/views/admin/comments/_posted_comments.html.haml b/app/views/admin/comments/_posted_comments.html.haml index d274f929..1b2b952d 100644 --- a/app/views/admin/comments/_posted_comments.html.haml +++ b/app/views/admin/comments/_posted_comments.html.haml @@ -1,11 +1,14 @@ -- @posted_comments.each do |conference, events| +- @posted_comments.each do |conference, objects| .panel.panel-default .panel-heading %h4.title.panel-title= conference.title .panel-body - - events.each do |event, comments| + - objects.each do |obj, comments| .notifications - %h4.title= link_to event.title, admin_conference_program_event_path(event.program.conference.short_title, event) + - if obj.class.name == 'Event' + %h4.title= link_to obj.title, admin_conference_program_event_path(obj.program.conference.short_title, obj) + - if obj.class.name == 'Track' + %h4.title= link_to obj.name, admin_conference_program_track_path(obj.program.conference.short_title, obj) %hr - comments.each do |comment| %h5.strong Created at: #{comment.created_at} diff --git a/app/views/admin/comments/_unread_comments.html.haml b/app/views/admin/comments/_unread_comments.html.haml index 5facdc4b..8c5e8e11 100644 --- a/app/views/admin/comments/_unread_comments.html.haml +++ b/app/views/admin/comments/_unread_comments.html.haml @@ -1,11 +1,14 @@ -- @unread_comments.each do |conference, events| +- @unread_comments.each do |conference, objects| .panel.panel-default .panel-heading %h4.title.panel-title= conference.title .panel-body - - events.each do |event, comments| + - objects.each do |obj, comments| .notifications - %h4.title= link_to event.title, admin_conference_program_event_path(event.program.conference.short_title, event) + - if obj.class.name == 'Event' + %h4.title= link_to obj.title, admin_conference_program_event_path(obj.program.conference.short_title, obj) + - if obj.class.name == 'Track' + %h4.title= link_to obj.name, admin_conference_program_track_path(obj.program.conference.short_title, obj) %hr - comments.each do |comment| %h5.strong Posted by: #{comment.user.name} | Created at: #{comment.created_at} diff --git a/app/views/admin/tracks/_nested_comments.html.haml b/app/views/admin/tracks/_nested_comments.html.haml new file mode 100644 index 00000000..111fbd53 --- /dev/null +++ b/app/views/admin/tracks/_nested_comments.html.haml @@ -0,0 +1,15 @@ +%div{ style: "padding-left:#{padding}px" } + .well.comment-section + %strong= comment.user.name + %i= comment.created_at + %p.comment-body= comment.body + %div + %a.pull-right.comment-reply-link{ href: '#' } Reply + .comment-reply + = semantic_form_for :comment, url: comment_admin_conference_program_track_path(@conference.short_title, @track.short_name, comment.commentable_id), method: :post do |f| + = f.input :body + %input{ name: 'parent', type: 'hidden', value: comment.id } + %input{ name: 'authenticity_token', type: 'hidden', value: '#{form_authenticity_token}' } + %button.btn.btn-primary.pull-right{ name: 'button', type: 'submit' } Add Reply + - comment.children.each do |child| + = render 'nested_comments', comment: child, padding: 50 diff --git a/app/views/admin/tracks/show.html.haml b/app/views/admin/tracks/show.html.haml index a7bb61a4..6ebc8e3c 100644 --- a/app/views/admin/tracks/show.html.haml +++ b/app/views/admin/tracks/show.html.haml @@ -127,3 +127,21 @@ = event.state %td = event.time + + + + .row + = link_to "Comments (#{@comment_count})", '#', id: 'event-comment-link' + #comments-div + %hr + %ul.media + %div + .row-fluid + = semantic_form_for :comment, url: comment_admin_conference_program_track_path(@conference.short_title, @track.short_name), method: :post do |f| + = f.input :body + = f.submit 'Add Comment', class: 'btn btn-primary pull-right' + %br + %br + - @comments.each do |comment| + %div + = render partial: 'nested_comments', locals: { comment: comment, padding: 0} diff --git a/app/views/layouts/_user_menu.html.haml b/app/views/layouts/_user_menu.html.haml index 30e1d27f..0f00c281 100644 --- a/app/views/layouts/_user_menu.html.haml +++ b/app/views/layouts/_user_menu.html.haml @@ -4,8 +4,11 @@ Notifications (#{unread_notifications(current_user).length}) - 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_program_event_path(event.program.conference.short_title, event.id)) + - unread_notifications(current_user).limit(5).group_by{ |comment| comment.commentable}.each do |obj, comments| + - if obj.class.name === 'Event' + %li= link_to("#{obj.title}(#{comments.count})", admin_conference_program_event_path(obj.program.conference.short_title, obj.id)) + - if obj.class.name === 'Track' + %li= link_to("#{obj.name}(#{comments.count})", admin_conference_program_track_path(obj.program.conference.short_title, obj)) %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') diff --git a/app/views/mailbot/comment_template.text.erb b/app/views/mailbot/comment_event_template.text.erb similarity index 61% rename from app/views/mailbot/comment_template.text.erb rename to app/views/mailbot/comment_event_template.text.erb index 0e7a8c35..b9315a28 100644 --- a/app/views/mailbot/comment_template.text.erb +++ b/app/views/mailbot/comment_event_template.text.erb @@ -1,10 +1,10 @@ Dear <%= @user.name %>, -User <%= @comment.user.name %> posted a new comment for event <%= @event.title %> of <%= @conference.short_title %> . +User <%= @comment.user.name %> posted a new comment for event <%= @commentable.title %> of <%= @conference.short_title %> . "<%= @comment.body %>" -To reply to this comment, please go to <%= h(admin_conference_program_event_url(@conference.short_title, @event, only_path: false)) %> +To reply to this comment, please go to <%= h(admin_conference_program_event_url(@conference.short_title, @commentable, only_path: false)) %> Best wishes, <%= @conference.title %> Team diff --git a/app/views/mailbot/comment_track_template.text.erb b/app/views/mailbot/comment_track_template.text.erb new file mode 100644 index 00000000..c9cecc25 --- /dev/null +++ b/app/views/mailbot/comment_track_template.text.erb @@ -0,0 +1,10 @@ +Dear <%= @user.name %>, + +User <%= @comment.user.name %> posted a new comment for track <%= @commentable.short_name %> of <%= @conference.short_title %> . + +"<%= @comment.body %>" + +To reply to this comment, please go to <%= h(admin_conference_program_track_url(@conference.short_title, @commentable, only_path: false)) %> + +Best wishes, +<%= @conference.title %> Team diff --git a/config/database.yml b/config/database.yml index dd8ac693..d203295e 100644 --- a/config/database.yml +++ b/config/database.yml @@ -1,32 +1,25 @@ -<% - encoding = 'unicode' - if ENV['OSEM_DB_ADAPTER'] == 'mysql2' - encoding = 'utf8' - end -%> - - +# SQLite version 3.x +# gem install sqlite3 +# +# Ensure the SQLite 3 gem is defined in your Gemfile +# gem 'sqlite3' +# default: &default - adapter: <%= ENV['OSEM_DB_ADAPTER'] || 'postgresql' %> - encoding: <%= encoding %> - host: <%= ENV['OSEM_DB_HOST'] || 'database' %> - port: <%= ENV['OSEM_DB_PORT'] || '5432' %> - username: <%= ENV['OSEM_DB_USER'] || 'postgres' %> - password: <%= ENV['OSEM_DB_PASSWORD'] || 'mysecretpassword' %> - database: <%= ENV['OSEM_DB_NAME'] || 'postgres' %> + adapter: sqlite3 pool: 5 timeout: 5000 development: <<: *default - database: osem_development + database: db/development.sqlite3 # Warning: The database defined as "test" will be erased and -# re-generated when you run "rake". +# re-generated from your development database when you run "rake". # Do not set this db to the same as development or production. test: <<: *default - database: osem_test + database: db/test.sqlite3 production: <<: *default + database: db/production.sqlite3 \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 57bfa630..ddbdc362 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -81,6 +81,7 @@ Osem::Application.routes.draw do patch :toggle_cfp_inclusion patch :restart patch :to_accept + post :comment patch :accept patch :confirm patch :to_reject diff --git a/db/migrate/20190720062321_add_comments_count_to_tracks.rb b/db/migrate/20190720062321_add_comments_count_to_tracks.rb new file mode 100644 index 00000000..fc952618 --- /dev/null +++ b/db/migrate/20190720062321_add_comments_count_to_tracks.rb @@ -0,0 +1,10 @@ +class AddCommentsCountToTracks < ActiveRecord::Migration[5.1] + def change + add_column :tracks, :comments_count, :integer, default: 0, null: false + + Track.find_each do |track| + comments_count = track.comment_threads.count + track.update_attributes(:comments_count, comments_count) unless comments_count.zero? + end + end +end diff --git a/db/schema.rb b/db/schema.rb index b199367e..e05f019f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,11 @@ # # It's strongly recommended that you check this file into your version control system. +<<<<<<< HEAD ActiveRecord::Schema.define(version: 2019_06_03_143107) do +======= +ActiveRecord::Schema.define(version: 20190720062321) do +>>>>>>> Add comment functionality to tracks create_table "answers", force: :cascade do |t| t.string "title" @@ -549,6 +553,10 @@ ActiveRecord::Schema.define(version: 2019_06_03_143107) do t.date "end_date" t.text "relevance" t.integer "selected_schedule_id" +<<<<<<< HEAD +======= + t.integer "comments_count", default: 0, null: false +>>>>>>> Add comment functionality to tracks t.index ["room_id"], name: "index_tracks_on_room_id" t.index ["selected_schedule_id"], name: "index_tracks_on_selected_schedule_id" t.index ["submitter_id"], name: "index_tracks_on_submitter_id" @@ -587,6 +595,7 @@ ActiveRecord::Schema.define(version: 2019_06_03_143107) do t.boolean "is_admin", default: false t.string "username" t.boolean "is_disabled", default: false +<<<<<<< HEAD t.string "invitation_token" t.datetime "invitation_created_at" t.datetime "invitation_sent_at" @@ -595,6 +604,8 @@ ActiveRecord::Schema.define(version: 2019_06_03_143107) do t.string "invited_by_type" t.integer "invited_by_id" t.integer "invitations_count", default: 0 +======= +>>>>>>> Add comment functionality to tracks t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true t.index ["email"], name: "index_users_on_email", unique: true t.index ["invitation_token"], name: "index_users_on_invitation_token", unique: true