Add comment functionality to tracks

This commit is contained in:
Rishabh Singh 2019-07-18 12:49:50 +05:30
parent 770a63e15c
commit 58208c73ac
19 changed files with 153 additions and 48 deletions

View file

@ -657,4 +657,4 @@ linters:
# Offense count: 38
InlineStyles:
enabled: false
enabled: false

View file

@ -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

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -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'

View file

@ -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/

View file

@ -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}

View file

@ -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}

View file

@ -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}

View file

@ -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

View file

@ -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}

View file

@ -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')

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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