Skip to content

Commit

Permalink
Fixed voting buttons on comments page. Changed seeds.rb comment/artic…
Browse files Browse the repository at this point in the history
…le names.
  • Loading branch information
paulmckissock committed Jul 3, 2024
1 parent dbd87a1 commit 1bfd5ad
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 34 deletions.
6 changes: 1 addition & 5 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ def create
comment.user = current_user

if comment.save
if comment.parent_id.present?
redirect_to article_comment_path(article, comment.parent_id)
else
redirect_to article
end
redirect_to article
else
render "articles/show", status: :unprocessable_entity
end
Expand Down
59 changes: 35 additions & 24 deletions app/helpers/comments_helper.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,40 @@
module CommentsHelper
def render_comment_tree(comment)
content_tag :li do
concat content_tag(:p, comment.text)
concat content_tag(:div, class: "subtext", style: "display: flex; align-items: center;") {
safe_concat button_to("Upvote", upvote_article_comment_path(comment.article, comment), method: :post, class: "btn btn-sm upvote-button", data: {votable_type: "Comment", votable_id: comment.id, article_id: comment.article.id})
safe_concat button_to("Downvote", downvote_article_comment_path(comment.article, comment), method: :post, class: "btn btn-sm downvote-button", data: {votable_type: "Comment", votable_id: comment.id, article_id: comment.article.id})

# concat button_to(upvote_article_comment_path(comment), method: :post, class: "btn btn-sm upvote-button", data: {votable_type: "Comment", votable_id: comment.id, article_id: comment.article.id}) do
# content_tag(:i, "", class: "fas fa-arrow-up")
# end
# concat button_to(downvote_article_comment_path(comment), method: :post, class: "btn btn-sm downvote-button", data: {votable_type: "Comment", votable_id: comment.id, article_id: comment.article.id}) do
# content_tag(:i, "", class: "fas fa-arrow-down")
# end

safe_concat content_tag(:span, "#{comment.score} points", id: "score-Comment-#{comment.id}", class: "score")
concat raw(" ")
safe_concat "by"
concat raw(" ")
safe_concat link_to(comment.user.name, comment.user)
concat raw(" ")
safe_concat "on #{comment.created_at.strftime("%B %d, %Y at %I:%M %p")}"
safe_concat " | "
safe_concat link_to(" reply", article_comment_path(comment.article, comment))
}
content_tag :div do
concat content_tag(:table, border: "0", cellpadding: "0", cellspacing: "0") {
concat content_tag(:tbody) {
concat content_tag(:tr) {
concat content_tag(:td, valign: "top", class: "votelinks") {
content_tag(:center) {
concat button_to(upvote_article_comment_path(comment), method: :post, class: "btn btn-sm upvote-button", data: { votable_type: 'Comment', votable_id: comment.id, article_id: comment.article.id }) {
content_tag(:i, '', class: "fas fa-arrow-up")
}
concat button_to(downvote_article_comment_path(comment), method: :post, class: "btn btn-sm downvote-button", data: { votable_type: 'Comment', votable_id: comment.id, article_id: comment.article.id }) {
content_tag(:i, '', class: "fas fa-arrow-down")
}
}
}
concat content_tag(:td, comment.text, valign: "top", style: "padding-left: 10px;")
}
concat content_tag(:tr) {
concat content_tag(:td, '', colspan: "2")
concat content_tag(:td, class: "subtext") {
safe_concat content_tag(:span, "#{comment.score} points", id: "score-Comment-#{comment.id}", class: "score")
concat raw(" ")
safe_concat "by"
concat raw(" ")
safe_concat link_to(comment.user.name, comment.user)
concat raw(" ")
safe_concat content_tag(:span, "on #{comment.created_at.strftime("%B %d, %Y at %I:%M %p")}")
safe_concat " | "
safe_concat link_to(" reply", article_comment_path(comment.article, comment))
}
}
concat content_tag(:tr, '', style: "height:5px")
}
}
if comment.replies.any?
concat(content_tag(:ul) do
concat(content_tag(:ul, style: "list-style-type: none; padding-left: 20px;") do
comment.replies.each do |reply|
concat render_comment_tree(reply)
end
Expand All @@ -33,3 +43,4 @@ def render_comment_tree(comment)
end
end
end

2 changes: 1 addition & 1 deletion app/views/comments/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<% end %>
</center>
</td>
<td valign="top" style="padding-left: 10px;">
<td valign="top">
<%= comment.text %>
</td>
</tr>
Expand Down
8 changes: 4 additions & 4 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,19 @@
comments = Comment.all
replies = []
# Create replies to comments
comments.each do |comment|
comments.each_with_index do |comment, i|
replies << Comment.create!(
text: "Sample Reply",
text: "Sample Reply #{i+1}",
article: comment.article,
user: users.sample,
parent_id: comment.id
)
end

# replies to replies
replies.each do |reply|
replies.each_with_index do |reply, i|
Comment.create!(
text: "Sample Reply",
text: "Sample Reply to Reply #{i+1}",
article: reply.article,
user: users.sample,
parent_id: reply.id
Expand Down

0 comments on commit 1bfd5ad

Please sign in to comment.