Skip to content

Commit

Permalink
Merge pull request #31 from andela-iamadi/refactor_code
Browse files Browse the repository at this point in the history
Refactor code
  • Loading branch information
0sc committed Feb 15, 2016
2 parents be71927 + 695d1d7 commit bb036ea
Show file tree
Hide file tree
Showing 14 changed files with 43 additions and 59 deletions.
2 changes: 1 addition & 1 deletion app/controllers/answers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def create

def update
if @answer.update(answer_params)
head :no_content
render json: @answer, status: 200
else
invalid_request(error_msg)
end
Expand Down
1 change: 0 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ class ApplicationController < ActionController::API
include ActionController::HttpAuthentication::Token::ControllerMethods
include ActionController::Serialization


attr_reader :current_user
before_action :authenticate_user

Expand Down
2 changes: 0 additions & 2 deletions app/controllers/concerns/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,3 @@ def action_on_comment
comment_id.present?
end
end


26 changes: 7 additions & 19 deletions app/controllers/votes_controller.rb
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
1 change: 1 addition & 0 deletions app/models/answer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class Answer < ActiveRecord::Base
has_many :comments, as: :comment_on
has_many :votes, as: :voteable
belongs_to :user
validates :user, presence: true
belongs_to :question, counter_cache: true

validates :content, presence: true
Expand Down
1 change: 1 addition & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ class Comment < ActiveRecord::Base
has_many :votes, as: :voteable
belongs_to :comment_on, polymorphic: true, counter_cache: true
belongs_to :user
validates :user, presence: true
validates :content, presence: true
end
1 change: 1 addition & 0 deletions app/models/question.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Question < ActiveRecord::Base
belongs_to :user
validates :title, presence: true
validates :content, presence: true
validates :user, presence: true

include Modify
include ActionView::Helpers::DateHelper
Expand Down
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class User < ActiveRecord::Base
has_many :answers
has_many :social_providers
has_many :tokens
has_many :votes
EMAIL_FORMAT= /(?<email>[.\w]+@andela).co[m]?\z/

def self.from_omniauth(auth, user=nil)
Expand Down
25 changes: 13 additions & 12 deletions app/models/vote.rb
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
class Vote < ActiveRecord::Base
belongs_to :voteable, polymorphic: true
validates :user_id, presence: true
belongs_to :user
validates :user, presence: true

class << self

def act_on_vote(operation, subject, subject_id, user_id)
def act_on_vote(operation, subject, subject_id, user)
value = 1 if operation == "plus"
value = -1 if operation == "minus"
process_vote(subject, subject_id, user_id, value)
process_vote(subject, subject_id, user, value)
end

def process_vote(subject, subject_id, user_id, value)
def process_vote(subject, subject_id, user, value)
if subject_exists?(subject, subject_id)
store_vote(subject, subject_id, user_id, value)
store_vote(subject, subject_id, user, value)
return total_votes(subject, subject_id)
end
end

def store_vote(subject, subject_id, user_id, value)
if voted?(subject, subject_id, user_id, value) || voted?(subject, subject_id, user_id)
subject_of_vote(subject, subject_id).votes.find_by(user_id: user_id).
def store_vote(subject, subject_id, user, value)
if voted?(subject, subject_id, user, value) || voted?(subject, subject_id, user)
subject_of_vote(subject, subject_id).votes.find_by(user: user).
update_attribute("value", value)
else
subject_of_vote(subject, subject_id).votes.
create(user_id: user_id, value: value)
create(user: user, value: value)
end
end

def voted?(subject, subject_id, user_id, value = nil)
return exists?(user_id: user_id, voteable_type: "#{subject}", voteable_id: subject_id) unless value
exists?(user_id: user_id, voteable_type: "#{subject}", voteable_id: subject_id, value: value)
def voted?(subject, subject_id, user, value = nil)
return exists?(user: user, voteable_type: "#{subject}", voteable_id: subject_id) unless value
exists?(user: user, voteable_type: "#{subject}", voteable_id: subject_id, value: value)
end

def subject_exists?(subject, subject_id)
Expand Down
14 changes: 2 additions & 12 deletions app/serializers/answer_serializer.rb
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
4 changes: 1 addition & 3 deletions app/serializers/comment_serializer.rb
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
8 changes: 8 additions & 0 deletions app/serializers/main_serializer.rb
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
5 changes: 2 additions & 3 deletions app/serializers/question_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
class QuestionSerializer < ActiveModel::Serializer
attributes :id, :title,:content, :votes_count, :tags, :created_at, :updated_at, :answers_count, :comments_count, :views
belongs_to :user
class QuestionSerializer < MainSerializer
attributes :title, :tags, :answers_count, :comments_count, :views

def attributes(*args)
class_eval { has_many :answers } if instance_options[:include_answers]
Expand Down
11 changes: 5 additions & 6 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
Rails.application.routes.draw do
post "questions/:question_id/upvote" => "votes#upvote"
post "answers/:answer_id/upvote" => "votes#upvote"
post "comments/:comment_id/upvote" => "votes#upvote"
post "questions/:question_id/downvote" => "votes#downvote"
post "answers/:answer_id/downvote" => "votes#downvote"
post "comments/:comment_id/downvote" => "votes#downvote"
scope '/:resource_name/:id', constraints: { resource_name: /(questions|answers|comments)/ } do
post '/upvote', to: 'votes#upvote'
post '/downvote', to: 'votes#downvote'
end

post '/validate_token', to: 'tokens#validate'

resources :questions, except: [:new, :edit] do
Expand Down

0 comments on commit bb036ea

Please sign in to comment.