diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 725bcfb..8e07c15 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -2,6 +2,7 @@ class ApplicationController < ActionController::API include ActionController::HttpAuthentication::Token::ControllerMethods attr_reader :current_user + helper_method :current_user before_action :authenticate_user def resource_not_found diff --git a/app/models/concerns/votes_counter.rb b/app/models/concerns/votes_counter.rb index e03a870..6116e36 100644 --- a/app/models/concerns/votes_counter.rb +++ b/app/models/concerns/votes_counter.rb @@ -30,4 +30,9 @@ def votes_alternative sum + vote.value end end + + def vote_by(user) + vote = votes.find { |vote| vote.user_id == user.id } + vote ? vote.value : nil + end end diff --git a/app/models/question.rb b/app/models/question.rb index c6b17e6..7e06523 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -25,8 +25,12 @@ def time_updated end def self.with_associations - eager_load(:votes).eager_load(answers: [{comments: [{user: [:social_providers]}, :votes]}, {user: [:social_providers]}, :votes]). - eager_load(user: [:social_providers]).eager_load(comments: [{user: [:social_providers]}, :votes]).eager_load(:tags).order('answers.accepted DESC') + eager_load(:votes) + .eager_load(answers: [{comments: [{user: [:social_providers]}, :votes]}, {user: [:social_providers]}, :votes]) + .eager_load(user: [:social_providers]) + .eager_load(comments: [{user: [:social_providers]}, :votes]) + .eager_load(:tags) + .order('answers.accepted DESC') end diff --git a/app/views/answers/_answer.json.jbuilder b/app/views/answers/_answer.json.jbuilder index 8c9b561..fad6904 100644 --- a/app/views/answers/_answer.json.jbuilder +++ b/app/views/answers/_answer.json.jbuilder @@ -1,4 +1,5 @@ json.extract! answer, :question_id, :comments_count, :id, :content, :votes_count, :accepted, :created_at, :updated_at +json.user_vote answer.vote_by(current_user) json.user { json.partial! 'users/user', user: answer.user } json.comments(answer.comments) do |comment| json.partial! 'comments/comment', comment: comment diff --git a/app/views/comments/_comment.json.jbuilder b/app/views/comments/_comment.json.jbuilder index 7f7f115..f840519 100644 --- a/app/views/comments/_comment.json.jbuilder +++ b/app/views/comments/_comment.json.jbuilder @@ -1,2 +1,3 @@ json.extract! comment, :id, :content, :votes_count, :created_at, :updated_at, :comment_on_id, :comment_on_type +json.user_vote comment.vote_by(current_user) json.user { json.partial! 'users/user', user: comment.user } diff --git a/app/views/questions/show.json.jbuilder b/app/views/questions/show.json.jbuilder index eddaed2..f3aefee 100644 --- a/app/views/questions/show.json.jbuilder +++ b/app/views/questions/show.json.jbuilder @@ -1,5 +1,6 @@ json.partial! 'questions/question', question: @question json.partial! 'tags/tag', tags: @question.tags +json.user_vote @question.vote_by(current_user) json.answers(@question.sort_answers) do |answer| json.partial! 'answers/answer', answer: answer end diff --git a/db/schema.rb b/db/schema.rb index e083316..3dc5a1f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -65,9 +65,9 @@ t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "token" + t.string "profile_picture" t.string "profile_url" t.string "email" - t.string "profile_picture" end add_index "social_providers", ["user_id"], name: "index_social_providers_on_user_id" @@ -76,7 +76,6 @@ t.string "name" t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.integer "parent_id" t.integer "representative_id" end diff --git a/docs/answers_endpoint.md b/docs/answers_endpoint.md index 73f590d..fdd3a88 100644 --- a/docs/answers_endpoint.md +++ b/docs/answers_endpoint.md @@ -32,6 +32,7 @@ Status: 200 date_created: 'Wed, 24TH Nov, 2017 10:00AM', updated: false, score: 8, + user_vote: -1, user: { id: 2, name: 'Oscar Laide' @@ -43,6 +44,7 @@ Status: 200 date_created: 'Wed, 25TH Nov, 2017 12:00PM', updated: true, score: 9, + user_vote: nil, user: { id: 3, name: 'Bayo Owoade' @@ -85,6 +87,7 @@ Status: 201 updated_at: '2016-03-24T13:57:29.219Z', updated: true, score: 9, + user_vote: nil, user: { "id": 1, "name": "Emmanuel Science", @@ -162,6 +165,7 @@ Status: 200 date_created: 'Wed, 25TH Nov, 2017 12:00PM', updated: true, score: 9, + user_vote: 1, user: { id: 3, name: 'Bayo Owoade' @@ -173,6 +177,7 @@ Status: 200 date_created: 'Wed, 24TH Nov, 2017 10:00AM', updated: false, score: 8, + vote: 0, user: { id: 2, name: 'Oscar Laide' diff --git a/docs/questions_endpoints.md b/docs/questions_endpoints.md index db5922d..2cc1b2f 100644 --- a/docs/questions_endpoints.md +++ b/docs/questions_endpoints.md @@ -132,6 +132,7 @@ Status: 200 user_id: 24, up_votes: 10 down_votes: 0 + user_vote: 1, comment: [ { id: 1, diff --git a/spec/models/shared/shared_votes_counter.rb b/spec/models/shared/shared_votes_counter.rb index 80973f0..b2a331c 100644 --- a/spec/models/shared/shared_votes_counter.rb +++ b/spec/models/shared/shared_votes_counter.rb @@ -79,4 +79,28 @@ before { 6.times { create(:vote, value: 1, voteable: subject) } } it { expect(subject.votes_count).to eq 6 } end + + describe "#vote_by" do + let(:user) { create(:user) } + let(:vote) { subject.votes.first } + let(:resource) { vote.voteable } + + context "when user has voted_on_resource" do + before(:each) do + vote.update!(user: user) + end + + + it "returns what the given user voted on the resource" do + expect(resource.vote_by(user)).to be_in([1, -1]) + expect(resource.vote_by(user)).not_to be_nil + end + end + + context "when user has not voted" do + it "returns what the given user voted on the resource" do + expect(resource.vote_by(user)).to be_nil + end + end + end end diff --git a/spec/models/vote_spec.rb b/spec/models/vote_spec.rb index ba83786..deae731 100644 --- a/spec/models/vote_spec.rb +++ b/spec/models/vote_spec.rb @@ -125,7 +125,7 @@ describe ".evaluate_reward" do context "when user has NOT voted before" do let(:new_vote) { true } - + context "when voting an Answer" do let(:resource) { Answer } context "when making an upvote" do