-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#10] Setup vote model which belongs to a user a votable (which can e…
…ither be an article or comment). Added upvote/downvote methods in article controller.
- Loading branch information
1 parent
b6ef777
commit 4078c6f
Showing
11 changed files
with
144 additions
and
2 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
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,6 @@ | ||
class Vote < ApplicationRecord | ||
belongs_to :user | ||
belongs_to :votable, polymorphic: true | ||
|
||
validates :value, inclusion: {in: [-1, 0, 1]} | ||
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[7.1] | ||
def change | ||
create_table :votes do |t| | ||
t.references :user, null: false, foreign_key: true | ||
t.references :votable, polymorphic: true, null: false | ||
t.integer :value | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,7 @@ | ||
FactoryBot.define do | ||
factory :vote do | ||
association :user, factory: :user | ||
association :votable, factory: :article | ||
value { 0 } | ||
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,37 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe Vote, type: :model do | ||
let(:user) { create(:user) } | ||
let(:article) { create(:article, user: user) } | ||
let(:comment) { create(:comment, article: article, user: user) } | ||
let(:vote) {create(:vote, votable: article, user: user)} | ||
let(:comment_vote) {create(:vote, votable: comment, user: user)} | ||
|
||
it "should be valid with valid attributes" do | ||
expect(vote.valid?).to be(true) | ||
end | ||
|
||
it "should be valid with comment as votable" do | ||
expect(comment_vote.valid?).to be(true) | ||
end | ||
|
||
it "value should be present" do | ||
vote.value = nil | ||
expect(vote.valid?).to be(false) | ||
end | ||
|
||
it "should not be valid with a value other than -1, 0, or 1" do | ||
vote.value = 2 | ||
expect(vote.valid?).to be(false) | ||
end | ||
|
||
it "should belong to a user" do | ||
vote.user = nil | ||
expect(vote.valid?).to be(false) | ||
end | ||
|
||
it "should belong to a votable" do | ||
vote.votable = nil | ||
expect(vote.valid?).to be(false) | ||
end | ||
end |