Skip to content

Commit

Permalink
feat: adição da funcionalidade de comentarios nos artigos
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafazg committed Nov 20, 2024
1 parent 99505b1 commit 3e6415d
Show file tree
Hide file tree
Showing 13 changed files with 116 additions and 11 deletions.
9 changes: 6 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,15 @@ GEM
nio4r (2.7.4)
nokogiri (1.16.7-x64-mingw-ucrt)
racc (~> 1.4)
nokogiri (1.16.7-x86_64-linux)
racc (~> 1.4)
orm_adapter (0.5.0)
parallel (1.26.3)
parser (3.3.5.0)
ast (~> 2.4.1)
racc
pg (1.5.7)
pg (1.5.7-x64-mingw-ucrt)
psych (5.2.0)
pg (1.5.9-x64-mingw-ucrt)
psych (5.1.2)
stringio
public_suffix (6.0.1)
puma (6.4.3)
Expand Down Expand Up @@ -273,6 +274,7 @@ GEM
railties (>= 7.0.0)
tailwindcss-ruby
tailwindcss-ruby (3.4.14-x64-mingw-ucrt)
tailwindcss-ruby (3.4.14-x86_64-linux)
thor (1.3.2)
timeout (0.4.2)
turbo-rails (2.0.6)
Expand Down Expand Up @@ -302,6 +304,7 @@ GEM

PLATFORMS
x64-mingw-ucrt
x86_64-linux

DEPENDENCIES
bootsnap
Expand Down
24 changes: 24 additions & 0 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class CommentsController < ApplicationController
before_action :set_article

def create
@comment = @article.comments.new(comment_params)
@comment.user = current_user

if @comment.save
redirect_to @article, notice: 'Comentário criado com sucesso.'
else
redirect_to @article, alert: 'Erro ao criar comentário.'
end
end

private

def set_article
@article = Article.find(params[:article_id])
end

def comment_params
params.require(:comment).permit(:content)
end
end
2 changes: 2 additions & 0 deletions app/helpers/comments_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module CommentsHelper
end
1 change: 1 addition & 0 deletions app/models/article.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ class Article < ApplicationRecord

belongs_to :user
validates :category, presence: true, inclusion: { in: CATEGORIES }
has_many :comments, dependent: :destroy
end
4 changes: 4 additions & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Comment < ApplicationRecord
belongs_to :article
belongs_to :user
end
29 changes: 25 additions & 4 deletions app/views/articles/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,14 +1,35 @@
<div class="mx-auto md:w-2/3 w-full bg-gray-900 text-white p-6 rounded-lg shadow-lg">


<!-- Renderização do artigo -->
<%= render @article %>

<div class="mt-4 md:flex gap-4">
<div class="mt-4 md:flex gap-4">
<%= link_to "Voltar aos artigos", articles_path, class: "mb-2 rounded-lg py-2 px-4 bg-gray-700 text-white inline-block font-medium hover:bg-gray-600 focus:outline-none focus:ring-2 focus:ring-gray-500" %>
<% if @article.user == current_user %>
<%= link_to "Edite este artigo", edit_article_path(@article), class: "mb-2 rounded-lg py-2 px-4 bg-blue-600 text-white inline-block font-medium hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-500" %>
<%= button_to "Deletar artigo", @article, method: :delete, data: { confirm: "Are you sure you want to delete this article?" }, class: "rounded-lg py-2 px-4 bg-red-600 text-white inline-block font-medium hover:bg-red-700 focus:outline-none focus:ring-2 focus:ring-red-500" %>
<% end %>
</div>
</div>

<!-- Seção de comentários -->
<div class="mt-8">
<h2 class="text-2xl font-bold mb-4">Comentários</h2>

<!-- Formulário de comentário -->
<%= form_with(model: [ @article, Comment.new ], local: true) do |form| %>
<div class="mb-4">
<%= form.text_area :content, placeholder: "Escreva seu comentário...", class: "w-full p-4 bg-gray-700 text-white rounded-lg focus:outline-none focus:ring-2 focus:ring-lime-500" %>
</div>
<%= form.submit "Comentar", class: "bg-lime-600 text-white py-2 px-4 rounded-lg hover:bg-lime-700 focus:outline-none focus:ring-2 focus:ring-lime-500" %>
<% end %>

<!-- Lista de comentários -->
<div class="mt-6">
<% @article.comments.each do |comment| %>
<div class="bg-gray-800 p-4 rounded-lg mb-4">
<p class="text-sm text-gray-400">Comentado por: <%= comment.user.name %></p>
<p class="mt-2 text-white"><%= comment.content %></p>
</div>
<% end %>
</div>
</div>
</div>
2 changes: 1 addition & 1 deletion app/views/static_pages/home.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<section class="md:flex space-around items-center">
<section class="md:flex space-around items-center justify-center">
<div class="md:w-1/2 mx-5">
<h1 class="mb-4 text-4xl font-extrabold leading-none tracking-tight md:text-5xl lg:text-6xl">JUNTOS, VAMOS TRANSFORMAR CÓDIGOS EM CONQUISTAS!</h1>
<p class="mb-6 text-lg font-normal text-gray-400 lg:text-xl">Empoderando novos desenvolvedores a trilharem seu caminho no mundo da programação!</p>
Expand Down
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
Rails.application.routes.draw do
resources :articles
resources :articles do
resources :comments, only: [:create, :destroy]
end
devise_for :users
root "static_pages#home"
get "/courses" => "static_pages#courses"
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20241120193841_create_comments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateComments < ActiveRecord::Migration[8.0]
def change
create_table :comments do |t|
t.references :article, null: false, foreign_key: true
t.references :user, null: false, foreign_key: true
t.text :content

t.timestamps
end
end
end
16 changes: 14 additions & 2 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions test/controllers/comments_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "test_helper"

class CommentsControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end
11 changes: 11 additions & 0 deletions test/fixtures/comments.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
article: one
user: one
content: MyText

two:
article: two
user: two
content: MyText
7 changes: 7 additions & 0 deletions test/models/comment_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "test_helper"

class CommentTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit 3e6415d

Please sign in to comment.