Skip to content

Commit

Permalink
Merge scope contraints for comments and votes routes
Browse files Browse the repository at this point in the history
  • Loading branch information
Osmond Oscar committed Feb 18, 2016
1 parent 0501f56 commit 1bc6fb5
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 8 deletions.
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
7 changes: 2 additions & 5 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,11 +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'
end

scope '/:resource_name/:resource_id', constraints: { resource_name: /(questions|answers)/} do
resources :comments, except: [:new, :edit]
resources :comments, except: [:new, :edit], resource_name: /(?!comments).*/
end

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

0 comments on commit 1bc6fb5

Please sign in to comment.