Skip to content

Commit

Permalink
Merge pull request #68 from zhishi-engine/include_users_vote_on_resou…
Browse files Browse the repository at this point in the history
…rces

Add the user's vote on the resources and add test for the '.voted' method
  • Loading branch information
0sc committed Apr 7, 2016
2 parents c113b78 + 23a2bbf commit 20fc57d
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 5 deletions.
1 change: 1 addition & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions app/models/concerns/votes_counter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 6 additions & 2 deletions app/models/question.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions app/views/answers/_answer.json.jbuilder
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions app/views/comments/_comment.json.jbuilder
Original file line number Diff line number Diff line change
@@ -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 }
1 change: 1 addition & 0 deletions app/views/questions/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 1 addition & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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

Expand Down
5 changes: 5 additions & 0 deletions docs/answers_endpoint.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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'
Expand All @@ -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'
Expand Down
1 change: 1 addition & 0 deletions docs/questions_endpoints.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ Status: 200
user_id: 24,
up_votes: 10
down_votes: 0
user_vote: 1,
comment: [
{
id: 1,
Expand Down
24 changes: 24 additions & 0 deletions spec/models/shared/shared_votes_counter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion spec/models/vote_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 20fc57d

Please sign in to comment.