Skip to content

Commit

Permalink
Merge pull request #33 from andela-iamadi/ft-refactor-comments-contro…
Browse files Browse the repository at this point in the history
…ller

Ft refactor comments controller
  • Loading branch information
Oreoluwa committed Feb 19, 2016
2 parents d1fe165 + 1bc6fb5 commit 6fcfe9b
Show file tree
Hide file tree
Showing 11 changed files with 311 additions and 159 deletions.
2 changes: 1 addition & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def resource_not_found
render json: {errors: not_found}, status: 404
end

def invalid_request(message)
def invalid_request(message = error_msg)
render json: {errors: message}, status: 400
end

Expand Down
74 changes: 27 additions & 47 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -1,76 +1,56 @@
class CommentsController < ApplicationController
before_action :custom_initializer
attr_reader :user_id, :question_id, :answer_id, :id, :content

include Common
before_action :set_resource
before_action :set_comment, only: [:show, :update, :destroy]
include OwnershipConcern

def index
question = Question.find_by(id: question_id) if action_on_question
answer = Answer.find_by(id: answer_id) if action_on_answer
comments = question.comments if question
comments = answer.comments if answer
render json: comments, status: 200 unless comments.nil?
render json: { error: false }, status: 404 if comments.nil?
render json: @resource_comments, status: 200
end

def show
comments = Question.find_question_comment(question_id, id) if action_on_question
comments = Answer.find_answer_comment(answer_id, id) if action_on_answer
render json: comments , status: 200
rescue
render json: { error: false }, status: 404
render json: @comment, status: 200
end

def create
unless content.nil? || content == ""
if action_on_question
comments = Question.add_comment_to_question(question_id, user_id, content)
elsif action_on_answer
comments = Answer.add_comment_to_answer(answer_id, user_id, content)
end
render json: comments, root: false
comment = @resource_comments.new(content: comment_params[:content])
comment.user = current_user
if comment.save
render json: comment, status: 201
else
render json: { error: "Comment body can not be empty!" }, status: 403
invalid_request("Comment body can not be empty!")
end
end

def update
status = update_subject(id, user_id, "content", content) if content && content != ""
message = { response: status ? true : false }
render json: message
rescue
render json: { error: false }, status: 403
if @comment.update(content: comment_params[:content])
render json: @comment, status: 200
else
invalid_request("Comment body can not be empty!")
end
end

def destroy
if action_on_question
deleted if Question.delete_question_comment(id, user_id, question_id)
elsif action_on_answer
deleted if Answer.delete_answer_comment(id, user_id, answer_id)
if @comment.try(:destroy)
render json: :head, status: 204
else
invalid_request
end
rescue
render json: { error: false }, status: 403
end

private

def comment_params
params.permit(:question_id, :answer_id, :id, :content, :downvote, :upvote)
params.permit(:resource_name, :resource_id, :content, :id)
end

def custom_initializer
set_vars(comment_params)
def set_resource
resource = comment_params[:resource_name].singularize.camelize.constantize.find_by(id: comment_params[:resource_id])
resource_not_found && return unless resource
@resource_comments = resource.comments
end

def deleted
render json: { response: "Comment deleted." }, status: 410
end

def update_subject(id, user_id, attribute, value = nil)
if action_on_question
comments = Question.update_question_comment(id, user_id, question_id, attribute, value)
elsif action_on_answer
comments = Answer.update_answer_comment(id, user_id, answer_id, attribute, value)
end
def set_comment
@comment = @resource_comments.find_by(id: comment_params[:id])
resource_not_found && return unless @comment
end
end
22 changes: 0 additions & 22 deletions app/controllers/concerns/common.rb

This file was deleted.

6 changes: 3 additions & 3 deletions app/controllers/votes_controller.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
class VotesController < ApplicationController
def upvote
resource_to_upvote = vote_params[:resource_name].singularize.camelize.constantize
vote = Vote.act_on_vote('plus', resource_to_upvote, vote_params[:id], current_user)
vote = Vote.act_on_vote('plus', resource_to_upvote, vote_params[:resource_id], current_user)
render json: { response: vote }, status: 200 unless vote.nil?
render json: { error: "Invalid vote!" }, status: 403 if vote.nil?
end

def downvote
resource_to_upvote = vote_params[:resource_name].singularize.camelize.constantize
vote = Vote.act_on_vote('minus', resource_to_upvote, vote_params[:id], current_user)
vote = Vote.act_on_vote('minus', resource_to_upvote, vote_params[:resource_id], current_user)
render json: { response: vote }, status: 200 unless vote.nil?
render json: { error: "Invalid vote!" }, status: 403 if vote.nil?
end

private
def vote_params
params.permit(:resource_name, :id)
params.permit(:resource_name, :resource_id)
end
end
27 changes: 0 additions & 27 deletions app/models/answer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,4 @@ class Answer < ActiveRecord::Base
belongs_to :question, counter_cache: true

validates :content, presence: true

include Modify

class << self
def add_comment_to_answer(answer_id, user_id, content)
answer = find_by(id: answer_id)
if answer
answer.comments.create(user_id: user_id, content: content)
end
end

def find_answer_comment(answer_id, id)
answer = find_by(id: answer_id)
if answer
answer.comments.where(id: id)
end
end

def delete_answer_comment(id, user_id, answer_id)
Modify::Updater.affected_record(answer_id, self, user_id).destroy(id)
end

def update_answer_comment(id, user_id, answer_id, attribute, value)
Modify::Updater.update_subject_comment(id, user_id, answer_id, self, attribute, value)
end
end

end
16 changes: 0 additions & 16 deletions app/models/concerns/modify.rb

This file was deleted.

30 changes: 2 additions & 28 deletions app/models/question.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class Question < ActiveRecord::Base
validates :content, presence: true
validates :user, presence: true

include Modify
include ActionView::Helpers::DateHelper

def time_updated
Expand All @@ -27,32 +26,7 @@ def increment_views
update(views: views + 1)
end

class << self
def with_answers
includes(:answers)
end


def add_comment_to_question(question_id, user_id, content)
question = find_by(id: question_id)
if question
question.comments.create(user_id: user_id, content: content)
end
end

def find_question_comment(question_id, id)
question = find_by(id: question_id)
if question
question.comments.where(id: id)
end
end

def delete_question_comment(id, user_id, question_id)
Modify::Updater.affected_record(question_id, self, user_id).destroy(id)
end

def update_question_comment(id, user_id, question_id, attribute, value)
Modify::Updater.update_subject_comment(id, user_id, question_id, self, attribute, value)
end
def self.with_answers
includes(:answers)
end
end
3 changes: 2 additions & 1 deletion app/serializers/comment_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
class CommentSerializer < MainSerializer

attributes :user
belongs_to :user
end
10 changes: 2 additions & 8 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
Rails.application.routes.draw do
scope '/:resource_name/:id', constraints: { resource_name: /(questions|answers|comments)/ } do
scope '/:resource_name/:resource_id', constraints: { resource_name: /(questions|answers|comments)/ } do
post '/upvote', to: 'votes#upvote'
post '/downvote', to: 'votes#downvote'
resources :comments, except: [:new, :edit], resource_name: /(?!comments).*/
end

post '/validate_token', to: 'tokens#validate'
Expand All @@ -10,18 +11,11 @@
get "recent_answers"
get "popular_answers"

resources :comments, except: [:new, :edit]
resources :answers, except: [:new, :edit]
end

get "top_questions" => "questions#top_questions"

resources :answers, except: [:index, :show, :create, :destroy, :update, :new, :edit] do
resources :comments, except: [:new, :edit]
end

resources :comments, only: [:update, :delete]

post "users/logout" => "user#logout"
get 'users/renew_token' => 'users#renew_token'
get "users" => "users#index"
Expand Down
Loading

0 comments on commit 6fcfe9b

Please sign in to comment.