-
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 #11 from andela-iamadi/ft-votes-resources
Ft votes resources
- Loading branch information
Showing
18 changed files
with
179 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
2.2.3 |
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,22 +1,24 @@ | ||
module Common | ||
|
||
def set_vars(allowed = {}) | ||
@question_id = allowed[:question_id] | ||
@answer_id = allowed[:answer_id] | ||
@comment_id = allowed[:comment_id] | ||
@id = allowed[:id] | ||
@content = allowed[:content] | ||
@downvote = allowed[:downvote] | ||
@upvote = allowed[:upvote] | ||
@user_id = current_user.try(:id) | ||
@user_id = current_user.id if current_user | ||
end | ||
|
||
def comment_of_question | ||
def action_on_question | ||
question_id && answer_id.nil? | ||
end | ||
|
||
def comment_of_answer | ||
def action_on_answer | ||
answer_id && question_id.nil? | ||
end | ||
|
||
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
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 | ||
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 | ||
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 | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
class Vote < ActiveRecord::Base | ||
belongs_to :voteable, polymorphic: true | ||
validates :user_id, presence: true | ||
|
||
class << self | ||
|
||
def act_on_vote(operation, subject, subject_id, user_id) | ||
value = 1 if operation == "plus" | ||
value = -1 if operation == "minus" | ||
process_vote(subject, subject_id, user_id, value) | ||
end | ||
|
||
def process_vote(subject, subject_id, user_id, value) | ||
if subject_exists?(subject, subject_id) | ||
store_vote(subject, subject_id, user_id, 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). | ||
update_attribute("value", value) | ||
else | ||
subject_of_vote(subject, subject_id).votes. | ||
create(user_id: user_id, 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) | ||
end | ||
|
||
def subject_exists?(subject, subject_id) | ||
subject.exists?(id: subject_id) | ||
end | ||
|
||
def subject_of_vote(subject, subject_id) | ||
subject.find_by(id: subject_id) | ||
end | ||
|
||
def total_votes(subject, subject_id) | ||
subject_of_vote(subject, subject_id).votes.sum("value") | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class CreateVotes < ActiveRecord::Migration | ||
def change | ||
create_table :votes do |t| | ||
t.integer :user_id, index: true | ||
t.integer :value | ||
t.references :voteable, polymorphic: true, index: true | ||
|
||
t.timestamps null: false | ||
end | ||
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class RemoveVotesFromOtherTables < ActiveRecord::Migration | ||
def change | ||
remove_column :questions, :votes, :integer | ||
remove_column :answers, :votes, :integer | ||
remove_column :comments, :votes, :integer | ||
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class RemoveValueFromVotes < ActiveRecord::Migration | ||
def change | ||
remove_column :votes, :value, :integer | ||
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddValueToVotes < ActiveRecord::Migration | ||
def change | ||
add_column :votes, :value, :integer | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe AnswersController, type: :controller do | ||
|
||
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 |
---|---|---|
|
@@ -50,5 +50,4 @@ | |
expect(response).to have_http_status(:success) | ||
end | ||
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe VotesController, type: :controller do | ||
|
||
describe "GET #upvote" do | ||
it "returns http success" do | ||
get :upvote | ||
expect(response).to have_http_status(:success) | ||
end | ||
end | ||
|
||
describe "GET #downvote" do | ||
it "returns http success" do | ||
get :downvote | ||
expect(response).to have_http_status(:success) | ||
end | ||
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe Vote, type: :model do | ||
pending "add some examples to (or delete) #{__FILE__}" | ||
end |