-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from andela-iamadi/refactor_code
Refactor code
- Loading branch information
Showing
14 changed files
with
43 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,5 +20,3 @@ def action_on_comment | |
comment_id.present? | ||
end | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,20 @@ | ||
class VotesController < ApplicationController | ||
before_action :current_user, :custom_initializer | ||
attr_reader :user_id, :question_id, :answer_id, :comment_id, :id | ||
|
||
include Common | ||
|
||
def upvote | ||
vote = Vote.act_on_vote("plus", Question, question_id, user_id) if action_on_question | ||
vote = Vote.act_on_vote("plus", Answer, answer_id, user_id) if action_on_answer | ||
vote = Vote.act_on_vote("plus", Comment, comment_id, user_id) if action_on_comment | ||
resource_to_upvote = vote_params[:resource_name].singularize.camelize.constantize | ||
vote = Vote.act_on_vote('plus', resource_to_upvote, vote_params[: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 | ||
vote = Vote.act_on_vote("minus", Question, question_id, user_id) if action_on_question | ||
vote = Vote.act_on_vote("minus", Answer, answer_id, user_id) if action_on_answer | ||
vote = Vote.act_on_vote("minus", Comment, comment_id, user_id) if action_on_comment | ||
resource_to_upvote = vote_params[:resource_name].singularize.camelize.constantize | ||
vote = Vote.act_on_vote('minus', resource_to_upvote, vote_params[: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(:question_id, :answer_id, :comment_id, :id) | ||
end | ||
|
||
def custom_initializer | ||
set_vars(vote_params) | ||
end | ||
def vote_params | ||
params.permit(:resource_name, :id) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,3 @@ | ||
class AnswerSerializer < ActiveModel::Serializer | ||
attributes :id, :user, :content, :question_id, :votes_count, :created_at, :updated_at, :comments_count | ||
|
||
def user | ||
{ | ||
id: object.user.id, | ||
name: object.user.name, | ||
email: object.user.points, | ||
image: object.user.social_providers.first.try(:profile_picture), | ||
points: object.user.points | ||
} | ||
end | ||
class AnswerSerializer < MainSerializer | ||
attributes :question_id, :comments_count | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
class CommentSerializer < ActiveModel::Serializer | ||
attributes :id, :user, :content, :votes_count, :created_at, :updated_at | ||
class CommentSerializer < MainSerializer | ||
|
||
belongs_to :user | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class MainSerializer < ActiveModel::Serializer | ||
attributes :id, :content, :votes_count, :voted, :created_at, :updated_at | ||
belongs_to :user | ||
|
||
def voted | ||
object.votes.voted?(object.class.to_s, object.id, object.user) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters