Skip to content

Commit

Permalink
Merge pull request #224 from TreinaDev/feat/responder-comentrio
Browse files Browse the repository at this point in the history
Feat/responder comentário
  • Loading branch information
caiquedv authored Feb 15, 2024
2 parents 143231c + 670865f commit a0ca345
Show file tree
Hide file tree
Showing 23 changed files with 376 additions and 44 deletions.
28 changes: 27 additions & 1 deletion app/assets/stylesheets/application.bootstrap.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ $theme-colors: (
"primary": #a130fd,
"secondary": #515253,
"success": #2abd4cde,
"info": #68a1f7,
"info": #8fd0f5,
"warning": #ffc107,
"danger": #dc3545,
"light": rgba(255, 251, 251, 0.603),
Expand Down Expand Up @@ -98,3 +98,29 @@ input[type="checkbox"]:checked {
box-shadow 1s ease-in-out,
background-color 1s ease-in-out;
}

.comment-message, .reply-form{
margin-left: 5.2rem !important;
}

.comment-actions{
margin-left: 5.0rem !important;
}

.reply-content{
margin-left: 4.5rem !important;
}

.reply-form{
min-width: 350px;
}

.reply-collapser{
margin-left: 4.8rem !important;
color: #065fd4 !important;
font-size: large;
}

.reply-content, .comment-message{
font-size: large;
}
1 change: 1 addition & 0 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def show
@comment = Comment.new
@likes_count = @post.likes.count
@liked = Like.find_by(user: current_user, likeable: @post)
@reply = Reply.new
end

def edit; end
Expand Down
12 changes: 12 additions & 0 deletions app/controllers/replies/likes_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Replies
class LikesController < LikesController
before_action :set_likeable

private

def set_likeable
@likeable = Reply.find(params[:reply_id])
@post = @likeable.comment.post
end
end
end
21 changes: 21 additions & 0 deletions app/controllers/replies_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class RepliesController < ApplicationController
before_action :authenticate_user!
def create
comment = Comment.find(params[:comment_id])
@reply = comment.replies.build(reply_params)

if @reply.save
redirect_to post_path(comment.post), notice: t('.success')
else
redirect_to post_path(comment.post), alert: t('.error')
end
end

private

def reply_params
reply_params = params.require(:reply).permit(:message)
reply_params[:user_id] = current_user.id
reply_params
end
end
9 changes: 6 additions & 3 deletions app/controllers/reports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def set_reportable_for_new
@reportable = Post.find(reportable_id) if params[:reportable_type] == 'Post'
@reportable = Profile.find(reportable_id) if params[:reportable_type] == 'Profile'
@reportable = Comment.find(reportable_id) if params[:reportable_type] == 'Comment'
@reportable = Reply.find(reportable_id) if params[:reportable_type] == 'Reply'
end

def set_reportable_for_create
Expand Down Expand Up @@ -91,9 +92,11 @@ def authorize!
end

def redirect_if_self_report
return if @reportable.is_a?(Profile) && @reportable != current_user.profile
return if @reportable.is_a?(Comment) && @reportable.user != current_user
return if @reportable.is_a?(Post) && @reportable.user != current_user
reportable_classes = [Profile, Comment, Post, Reply]

return if reportable_classes.any? do |klass|
@reportable.is_a?(klass) && @reportable.user != current_user
end

redirect_to root_path, alert: t('.self_report')
end
Expand Down
1 change: 1 addition & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class Comment < ApplicationRecord
belongs_to :post
belongs_to :user
has_many :likes, as: :likeable, dependent: :destroy
has_many :replies, dependent: :destroy
has_many :reports, as: :reportable, dependent: :destroy
has_many :notifications, as: :notifiable, dependent: :destroy

Expand Down
6 changes: 6 additions & 0 deletions app/models/reply.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Reply < ApplicationRecord
belongs_to :user
belongs_to :comment
has_many :likes, as: :likeable, dependent: :destroy
validates :message, presence: true
end
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class User < ApplicationRecord
has_one :subscription, dependent: :destroy
has_many :posts, dependent: :nullify
has_many :likes, dependent: :destroy
has_many :replies, dependent: :destroy
has_many :comments, dependent: :nullify
has_one :personal_info, through: :profile
has_many :professional_infos, through: :profile
Expand Down
2 changes: 1 addition & 1 deletion app/views/home/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<aside class="col-md-3">
<h2> <%= t('.most_followed') %> </h2>

<div class="py-2" style="width: 18rem;">
<div style="width: 18rem;">
<% @most_followed.each do |profile| %>
<div class="card mb-3 bg-light-subtle text-center">
<div class="card-body">
Expand Down
137 changes: 102 additions & 35 deletions app/views/posts/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@
</p>

<div class="btn-group">
<div class="me-2 mt-2">
<%= @likes_count %> <%= Like.model_name.human(count: @likes_count) %>
</div>
<% if @liked %>
<%= button_to post_like_path(@post, @liked), method: :delete, class: 'btn btn-sm', id: 'unlike' do %>
<%= image_tag 'thumbs-up-solid', width: '20rem' %>
Expand All @@ -56,6 +53,9 @@
<%= image_tag 'thumbs-up-regular', width: '20rem' %>
<% end %>
<% end %>
<div class="me-2 mt-2">
<%= @likes_count %> <%= Like.model_name.human(count: @likes_count) %>
</div>
</div>
</div>
</div>
Expand All @@ -66,52 +66,114 @@
<% end %>


<div id="comments", class="card mt-3 mb-3 text-muted">
<div id="comments", class="card mt-3 mb-3">
<div class="card-header">
<%= @post.comments.count %> <%= Comment.model_name.human(count: @post.comments.count) %>
</div>
<% @post.comments.each do |comment| %>
<div class="card-body comment" id="<%= dom_id(comment) %>">
<% if comment.removed?%>
<p><%= t('comments.removed_content') %> </p>
<% else %>
<blockquote class="blockquote mb-0">
<p><%= comment.message %></p>
<footer class="blockquote-footer">
<% if comment.removed?%>
<p><%= t('comments.removed_content') %> </p>
<% else %>
<div class="card-body" id="<%= dom_id(comment) %>">
<footer>
<h5>
<% if comment.user.profile.photo.present? %>
<%= image_tag comment.user.profile.photo, alt: 'Foto de perfil', width: '35rem', class: 'rounded-circle mx-3' %>
<% else %>
<%= image_tag 'default_portfoliorrr_photo.png', alt: 'Foto de perfil', width: '35rem', class: 'rounded-circle mx-3' %>
<% end %>
<%= link_to comment.user.full_name, comment.user.profile %> <%= '(autor)' if comment.user == @post.user %>
</footer>
</blockquote>

</h5>
<p class="comment-message"><%= comment.message %></p>
</footer>
<% if comment.user.deleted_at.nil? %>
<div class="btn-group flex-column">
<div class="d-flex">
<div>
<div class="mt-2 me-2">
<%= comment.likes.count %> <%= Like.model_name.human(count: comment.likes.count) %>
</div>

<div class="me-2">
<% if user_signed_in? && comment.likes.where(user_id: current_user.id).any? %>
<% like = comment.likes.find_by(user_id: current_user.id) %>
<%= button_to comment_like_path(comment, like), method: :delete, class: 'btn btn-sm', id: 'unlike' do %>
<%= image_tag 'thumbs-up-solid', width: '20rem', class: 'mb-4' %>
<div class="d-flex align-items-center comment-actions">
<% if user_signed_in? && comment.likes.where(user_id: current_user.id).any? %>
<% like = comment.likes.find_by(user_id: current_user.id) %>
<%= button_to comment_like_path(comment, like), method: :delete, class: 'btn btn-sm', id: 'unlike' do %>
<%= image_tag 'thumbs-up-solid', width: '20rem', class: 'mb-2' %>
<% end %>
<% else %>
<%= button_to comment_likes_path(comment), method: :post, class: 'btn btn-sm', id: 'like' do %>
<%= image_tag 'thumbs-up-regular', width: '20rem', class: 'mb-2' %>
<% end %>
<% end %>
<span><%= comment.likes.count %> <%= Like.model_name.human(count: comment.likes.count) %></span>
<button onClick="showForm(<%= comment.id %>)" class="btn btn-outline-primary rounded-5 border-0 mx-2"><%= t('reply')%></button>
<% if current_user != comment.user %>
<div class="report-link-wrapper mt-3">
<p><%= link_to t('reports.report_btn'), new_report_path(reportable: comment, reportable_type: comment.class), class: 'btn btn-secondary btn-sm' %></p>
</div>
<% end %>
<% else %>
<%= button_to comment_likes_path(comment), method: :post, class: 'btn btn-sm', id: 'like' do %>
<%= image_tag 'thumbs-up-regular', width: '20rem', class: 'mb-2' %>
</div>
<div class="reply-form">
<%= form_with model: [comment, @reply], method: :post, class:'d-none row align-items-center mt-3', id:"comment-#{comment.id}" do |form| %>
<div class="col-md-12">
<%= form.label :message, class: 'd-none' %>
<%= form.text_area :message, placeholder: 'Adicione uma resposta...', class: 'form-control' %>
</div>
<div class="col-md-6 mt-2">
<%= form.submit t('replies.send'), class: 'btn btn-primary' %>
</div>
<% end %>
<% end %>
</div>
<% if comment.replies.any? %>
<button class="btn btn-outline-info border-0 rounded-5 mt-2 dropdown-toggle reply-collapser"
type="button"
data-bs-toggle="collapse"
data-bs-target="#collapseReplies<%= comment.id %>"
aria-expanded="false"
aria-controls="collapseReplies<%= comment.id %>">
<%= comment.replies.count %> <%= Reply.model_name.human(count: comment.replies.count) %>
</button>
<div class="collapse mx-5 mt-3 reply-content" id="collapseReplies<%= comment.id %>">
<% comment.replies.each do |reply| %>
<h6 class="mt-2">
<% if reply.user.profile.photo.present? %>
<%= image_tag reply.user.profile.photo, alt: 'Foto de perfil', width: '30rem', class: 'rounded-circle mx-3' %>
<% else %>
<%= image_tag 'default_portfoliorrr_photo.png', alt: 'Foto de perfil', width: '30rem', class: 'rounded-circle mx-3' %>
<% end %>
<%= link_to reply.user.full_name, reply.user.profile %> <%= '(autor)' if reply.user == @post.user %>
<small datetime="<%= reply.created_at.to_datetime %>">
<%= distance_of_time_in_words(Time.now, reply.created_at) %>
</small>
</h6>
<div class="reply-content text-dark">
<p class="mx-2"><%= reply.message %></p>
<div class="btn-group d-flex align-items-center mt-4">
<% if user_signed_in? && reply.likes.where(user_id: current_user.id).any? %>
<% like = reply.likes.find_by(user_id: current_user.id) %>
<%= button_to reply_like_path(reply, like), method: :delete, class: 'btn btn-sm', id: 'unlike' do %>
<%= image_tag 'thumbs-up-solid', width: '20rem' %>
<% end %>
<% else %>
<%= button_to reply_likes_path(reply), method: :post, class: 'btn btn-sm', id: 'like' do %>
<%= image_tag 'thumbs-up-regular', width: '20rem' %>
<% end %>
<% end %>
<div class="me-2 mt-2">
<%= reply.likes.count %> <%= Like.model_name.human(count: reply.likes.count) %>
</div>
<% if current_user != reply.user %>
<div class="report-link-wrapper mt-3">
<p><%= link_to t('reports.report_btn'), new_report_path(reportable: reply, reportable_type: reply.class), class: 'btn btn-secondary btn-sm' %></p>
</div>
<% end %>
</div>
</div>
<% end %>
</div>
<%end%>
</div>
</div>

<% if current_user != comment.user %>
<div class="report-link-wrapper">
<%= link_to t('reports.report_btn'), new_report_path(reportable: comment, reportable_type: comment.class), class: 'btn btn-secondary btn-sm' %>
</div>
<% end %>
</div>
<% end %>
<% end %>
</div>
</div>
<% end %>
<% end %>
</div>

Expand All @@ -130,4 +192,9 @@
}
};
});

function showForm(id){
form = document.getElementById(`comment-${id}`)
form.classList.toggle('d-none')
}
</script>
2 changes: 1 addition & 1 deletion app/views/reports/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<tr>
<td><%= report.offence_type %></td>
<td><%= report.truncated_message %></td>
<td><%= I18n.t (report.reportable_type) %></td>
<td><%= report.reportable.class.model_name.human %></td>
<td><%= link_to t('.action'), report_path(report), class: "see_more btn btn-sm btn-secondary"%></td>
</tr>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/shared/_navbar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<% if current_user.profile.photo_attachment %>
<%= image_tag current_user.profile.photo_attachment, width: '55rem', alt: 'Foto de perfil', class: 'rounded rounded-circle px-2' %>
<% else %>
<%= image_tag 'default_portfoliorrr_photo.png', width: '55rem', alt: 'Foto de perfil', class: 'rounded rounded-circle px-2' %>
<%= image_tag 'default_portfoliorrr_photo.png', width: '55rem', alt: 'Foto de perfil', class: 'rounded rounded-circle px-2' %>
<% end %>
</button>
<ul class="dropdown-menu dropdown-menu-white">
Expand Down
1 change: 1 addition & 0 deletions config/locales/pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pt-BR:
published: 'criada'
draft: 'criada'
archived: 'criada'
reply: 'Responder'
Post: Publicação
Comment: Comentário
Profile: Perfil
Expand Down
14 changes: 14 additions & 0 deletions config/locales/replies.pt-BR.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pt-BR:
activerecord:
models:
reply:
one: Resposta
other: Respostas
attributes:
reply:
message: 'Resposta'
replies:
send: 'Enviar Resposta'
create:
success: 'Resposta enviada com sucesso!'
error: 'Não foi possível enviar sua resposta'
10 changes: 8 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
resources :notifications, only: %i[index update]

resources :posts, only: %i[new create] do
resources :comments, only: %i[create]
resources :comments, shallow: true, only: %i[create] do
resources :replies, only: %i[create]
end
post 'pin', on: :member
end

Expand All @@ -34,12 +36,16 @@
resources :posts, only: %i[] do
patch 'publish', on: :member
resources :likes, only: %i[create destroy], module: :posts
end

end
resources :comments, only: %i[] do
resources :likes, only: %i[create destroy], module: :comments
end

resources :replies, only: %i[] do
resources :likes, only: %i[create destroy], module: :replies
end

resources :users, only: [] do
resources :posts, shallow: true, only: %i[show edit update]
resources :profiles, shallow: true, only: %i[edit show update] do
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20240213185205_create_replies.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateReplies < ActiveRecord::Migration[7.1]
def change
create_table :replies do |t|
t.references :user, null: false, foreign_key: true
t.references :comment, null: false, foreign_key: true
t.text :message

t.timestamps
end
end
end
Loading

0 comments on commit a0ca345

Please sign in to comment.