Skip to content

Commit

Permalink
[#966] Rewriting comments in React and refactoring comments services (#…
Browse files Browse the repository at this point in the history
…1143)

- Delete all jQuery code related to comments
- Rewrite frontend in React
- Refactor services for comments
  - comments_helper.rb: logic for the comments array that gets passed as a prop to the Comments React widget
  - comment_form_helper.rb: form props for form used in Comments React widget
  - comments_viewers_service.rb (previously comments_visibility.rb): service for determining which comments to display to users/ability to delete
  - comments_controller.rb: moved all comment create/delete logic from other controllers into one
- Update tests, write more tests (a lot of which were missing)
- Only display commenting if moment/strategy is published
  • Loading branch information
julianguyen authored Oct 14, 2018
1 parent 9259217 commit 882bd29
Show file tree
Hide file tree
Showing 49 changed files with 1,739 additions and 1,234 deletions.
88 changes: 0 additions & 88 deletions app/assets/javascripts/add_comment.js

This file was deleted.

12 changes: 0 additions & 12 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,6 @@

I18n.locale = Cookies.get("locale") || I18n.defaultLocale;

function isShow(forms) {
var result = false;
_.each(forms, function(form) {
if ($("body").hasClass(form + " show")) {
result = true;
return;
}
});

return result;
}

var onReadyApplication = function() {
$.ajaxSetup({
headers: {
Expand Down
30 changes: 0 additions & 30 deletions app/assets/javascripts/delete_comment.js

This file was deleted.

72 changes: 0 additions & 72 deletions app/assets/stylesheets/application/comments.scss

This file was deleted.

9 changes: 0 additions & 9 deletions app/assets/stylesheets/application/strategy.scss

This file was deleted.

7 changes: 2 additions & 5 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class ApplicationController < ActionController::Base
include ActionView::Helpers::DateHelper
include ActionView::Helpers::TextHelper
include CommentsHelper
include CommentFormHelper
include TagsHelper
include MomentsHelper

Expand Down Expand Up @@ -78,7 +79,7 @@ def configure_for_accept_invitation
helper_method :avatar_url, :viewer_of?,
:are_allies?, :get_uid, :most_focus,
:tag_usage, :can_notify, :if_not_signed_in,
:generate_comment, :moments_stats
:moments_stats

def if_not_signed_in
return if user_signed_in?
Expand Down Expand Up @@ -169,10 +170,6 @@ def redirect_to_path(path)
end
end

def respond_not_saved
respond_with_json(no_save: true)
end

def respond_with_json(reponse)
respond_to do |format|
format.html { render json: reponse }
Expand Down
59 changes: 59 additions & 0 deletions app/controllers/comment_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# frozen_string_literal: true
class CommentController < ApplicationController
def create
response = {
comment: generate_comments(
Comment.where(id: handle_create(params[:comment]))
).first
}
render json: response, status: :ok
rescue ActiveRecord::RecordInvalid
render json: {}, status: :bad_request
end

def delete
comment = Comment.where(id: params[:comment_id]).first
raise 'Comment does not exist' if comment.nil?

handle_delete(comment)
render json: { id: comment.id }, status: :ok
rescue TypeError, RuntimeError
render json: {}, status: :bad_request
end

private

def remove_meeting_notification!(comment_id)
CommentNotificationsService.remove(
comment_id: comment_id,
model_name: 'meeting'
)
end

def remove_meeting_notification(comment, meeting)
my_comment = comment.present? && (comment.comment_by == current_user.id)
return unless (my_comment && meeting.member?(current_user)) ||
meeting.led_by?(current_user)

remove_meeting_notification!(comment.id)
end

def handle_create(comment_params)
comment = Comment.create_from!(comment_params)
comment.notify_of_creation!(current_user)
comment.id
end

def handle_delete(comment)
if %w[moment strategy].include?(comment.commentable_type)
CommentViewersService.deletable?(comment, current_user)
CommentNotificationsService.remove(comment_id: comment.id,
model_name:
comment.commentable_type)
elsif comment.commentable_type == 'meeting'
meeting_id = comment.commentable_id
meeting = Meeting.find_by(id: meeting_id)
remove_meeting_notification(comment, meeting)
end
end
end
46 changes: 2 additions & 44 deletions app/controllers/meetings_controller.rb
Original file line number Diff line number Diff line change
@@ -1,41 +1,17 @@
# frozen_string_literal: true
# rubocop:disable ClassLength
class MeetingsController < ApplicationController
include CommentsHelper

before_action :set_meeting, only: %i[show edit update destroy]

# GET /meetings/1
def show
@meeting = Meeting.friendly.find(params[:id])
@is_member = @meeting.member?(current_user)
@is_leader = @meeting.led_by?(current_user)

if @is_member
@no_hide_page = true
@comment = Comment.new
@comments = @meeting.comments
if @meeting.member?(current_user)
@comments = generate_comments(@meeting.comments.order(created_at: :desc))
elsif !@meeting.group.member?(current_user)
redirect_to_path(groups_path)
end
end

def comment
params[:visibility] = 'all'
comment_for('meeting')
end

def delete_comment
comment = Comment.find_by(id: params[:commentid])

if comment.present?
meeting_id = comment.commentable_id
meeting = Meeting.find_by(id: meeting_id)
remove_notification(comment, meeting)
end
head :ok
end

# GET /meetings/new
def new
@group = Group.find_by(id: params[:group_id])
Expand Down Expand Up @@ -147,22 +123,4 @@ def send_notification(meeting, members, type)
members: members
)
end

def remove_notification!
CommentNotificationsService.remove(
comment_id: params[:commentid],
model_name: 'meeting'
)
end

def remove_notification(comment, meeting)
remove_notification! if (my_comment?(comment) && \
meeting.member?(current_user)) || \
meeting.led_by?(current_user)
end

def my_comment?(comment)
comment.present? && (comment.comment_by == current_user.id)
end
end
# rubocop:enable ClassLength
Loading

0 comments on commit 882bd29

Please sign in to comment.