Skip to content

Commit

Permalink
[#10] Added upvote/downvote buttons and scores of each article to hom…
Browse files Browse the repository at this point in the history
…e page. Added votes to seeds.rb.
  • Loading branch information
paulmckissock committed Jun 26, 2024
1 parent 4078c6f commit 70bb3e7
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
5 changes: 3 additions & 2 deletions app/controllers/articles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class ArticlesController < ApplicationController
before_action :set_article, only: [:show, :upvote, :downvote]
def index
@articles = Article.order(created_at: :desc).page(params[:page]).per(20)
@user_votes = Vote.where(user: current_user, votable: @articles.to_a).index_by(&:votable_id)
end

def show
Expand All @@ -12,12 +13,12 @@ def show

def upvote
vote(1)
redirect_to @article
redirect_to root_path
end

def downvote
vote(-1)
redirect_to @article
redirect_to root_path
end

def new
Expand Down
1 change: 0 additions & 1 deletion app/models/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ class Comment < ApplicationRecord
validates :text, presence: true
has_many :votes, as: :votable


belongs_to :parent, class_name: "Comment", optional: true
has_many :replies, class_name: "Comment", foreign_key: :parent_id, dependent: :destroy
scope :without_parent, -> { where(parent_id: nil) }
Expand Down
10 changes: 9 additions & 1 deletion app/views/articles/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@
<ul>
<% @articles.each do |article| %>
<li>
<%= link_to article.title, article.link %> - Posted by <%= link_to article.user.name, article.user %> on <%= article.created_at.strftime("%B %d, %Y at %I:%M %p") %>
<div style="display: flex; align-items: center;>
<%= link_to article.title, article_path(article) %> - Score: <%= article.score %>
<% user_vote = @user_votes[article.id] %>
<%= button_to "Upvote", upvote_article_path(article), method: :post,
class: "btn btn-sm #{'btn-success' if user_vote&.value == 1} btn-light"%>
<%= button_to "Downvote", downvote_article_path(article), method: :post,
class: "btn btn-sm #{'btn-danger' if user_vote&.value == -1} btn-light"%>
</div>
- Posted by <%= link_to article.user.name, user_path(article.user) %> on <%= article.created_at.strftime("%B %d, %Y at %I:%M %p") %>
<div>
<%= link_to "comment", article %>
</div>
Expand Down
10 changes: 10 additions & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,13 @@
parent_id: comment.id
)
end
# Generate votes for articles
articles.each do |article|
rand(1..5).times do # Random number of votes per article
Vote.create!(
votable: article,
user: users.sample,
value: [-1, 0, 1].sample # Randomly selects a vote value
)
end
end
4 changes: 2 additions & 2 deletions spec/models/vote_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
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)}
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)
Expand Down

0 comments on commit 70bb3e7

Please sign in to comment.