Skip to content

Commit

Permalink
Merge branch 'main' into refact/likes-controller
Browse files Browse the repository at this point in the history
Co-authored-by: Paulo Henrique Meneses <[email protected]>
  • Loading branch information
DanSmaR and paulohenrique-gh committed Feb 13, 2024
2 parents 7807902 + 0c3b229 commit 32e22ea
Show file tree
Hide file tree
Showing 20 changed files with 267 additions and 82 deletions.
1 change: 1 addition & 0 deletions app/assets/images/thumbs-up-regular.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions app/assets/images/thumbs-up-solid.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions app/assets/stylesheets/application.bootstrap.scss
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,24 @@ input[type="checkbox"]:checked {

.categories{
width: 35% !important;
}

.highlighted {
transform: scale(1.01s);
background-color: hsla(256, 85%, 82%, 0.2);
box-shadow: 0 0 5px 5px rgba(0, 0, 0, 0.2);
transition:
transform 1.5s ease-in-out,
box-shadow 1.5s ease-in-out,
background-color 1.5s ease-in-out;
}

[id^='comment_'] {
transform: reset;
background-color: reset;
box-shadow: reset;
transition:
transform 1s ease-in-out,
box-shadow 1s ease-in-out,
background-color 1s ease-in-out;
}
9 changes: 9 additions & 0 deletions app/jobs/post_interest_notification_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class PostInterestNotificationJob < ApplicationJob
queue_as :default

def perform(comment)
post = comment.post
users = post.comments.map(&:user).uniq.excluding(comment.user, post.user)
users.each { |user| Notification.create!(profile: user.profile, notifiable: comment) }
end
end
7 changes: 6 additions & 1 deletion app/models/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ class Comment < ApplicationRecord
belongs_to :user
has_many :likes, as: :likeable, dependent: :destroy
has_many :reports, as: :reportable, dependent: :destroy
has_one :notification, as: :notifiable, dependent: :destroy
has_many :notifications, as: :notifiable, dependent: :destroy

after_create :notify_interested_users
after_create :create_notification

private

def notify_interested_users
PostInterestNotificationJob.perform_later(self)
end

def create_notification
comment_author = user.profile
return if comment_author == post.user.profile
Expand Down
2 changes: 1 addition & 1 deletion app/models/profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def self.advanced_search(search_query)
left_outer_joins(:job_categories, :personal_info, :user).where(
'job_categories.name LIKE :term OR
personal_infos.city LIKE :term OR
users.full_name LIKE :term',
users.full_name LIKE :term OR users.search_name LIKE :term',
{ term: "%#{sanitize_sql_like(search_query)}%" }
).public_profile.active.uniq
end
Expand Down
5 changes: 5 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class User < ApplicationRecord

after_create :'create_profile!'
after_create :subscribe_likes_mailer_job
after_create :update_search_name

def description
if admin?
Expand Down Expand Up @@ -105,4 +106,8 @@ def cloned_user
clone.save!
clone
end

def update_search_name
update(search_name: I18n.transliterate(full_name, locale: :en))
end
end
64 changes: 30 additions & 34 deletions app/views/invitations/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,39 +1,35 @@
<div class="container">
<div class="row">
<section>
<h2 class="text-center">Convite</h2>
<h2 class="text-center">Convite</h2>

<div class="col-md-6 offset-md-3">
<div class="card shadow rounded">
<div class="card-body">
<h4 class="text-break text-primary"><%= @invitation.project_title %></h4>
<div class="col-md-6 offset-md-3">
<div class="card shadow rounded">
<div class="card-body">
<h4 class="text-break text-primary"><%= @invitation.project_title %></h4>

<p class="card-text"><%= @invitation.project_description %></p>
<p class="card-subtitle mb-2 text-muted">Categoria: <%= @invitation.project_category %></p>
<% if @invitation.message.present? %>
<p class="card-subtitle mb-2 text-muted">Mensagem: <%= @invitation.message %></p>
<% end %>
<% if @invitation.expiration_date.present? %>
<p class="card-subtitle mb-2 text-muted">Expira dia: <%= I18n.l(@invitation.expiration_date) %></p>
<% end %>
<p class="card-text"><%= @invitation.project_description %></p>
<p class="card-subtitle mb-2 text-muted">Categoria: <%= @invitation.project_category %></p>

<% if @invitation.pending? %>
<p class="card-subtitle mb-2 text-muted">Expira dia: <%= I18n.l(@invitation.expiration_date) %></p>

<div class="btn-group">
<div>
<%= link_to 'Aceitar', 'http://localhost:4000', method: :patch, class: 'btn btn-primary me-4' %>
</div>
<div>
<%= button_to 'Recusar', decline_invitation_path(@invitation), method: :patch, class: 'btn btn-secondary' %>
</div>
</div>

<p class="card-subtitle mb-2 text-muted">Mensagem: <%= @invitation.message %></p>
<% end %>

<p class="card-subtitle mb-2 text-success"><%= 'Aceito' if @invitation.accepted? %></p>
<p class="card-subtitle mb-2 text-danger"><%= 'Expirado' if @invitation.expired? %></p>
<p class="card-subtitle mb-2 text-danger"><%= 'Recusado' if @invitation.declined? %></p>
<p class="card-subtitle mb-2 text-dark"><%= 'Cancelado' if @invitation.cancelled? %></p>
<p class="card-subtitle mb-2 text-info"> <%= 'Processando' if @invitation.processing? %></p>
<% if @invitation.pending? %>
<div class="btn-group">
<div>
<%= link_to 'Aceitar', 'http://localhost:3000', class: 'btn btn-primary me-4' %>
</div>
<div>
<%= button_to 'Recusar', decline_invitation_path(@invitation), method: :patch, class: 'btn btn-secondary' %>
</div>
</div>
</div>
</section>
<% end %>

<p class="card-subtitle mb-2 text-success"><%= 'Aceito' if @invitation.accepted? %></p>
<p class="card-subtitle mb-2 text-danger"><%= 'Expirado' if @invitation.expired? %></p>
<p class="card-subtitle mb-2 text-danger"><%= 'Recusado' if @invitation.declined? %></p>
<p class="card-subtitle mb-2 text-dark"><%= 'Cancelado' if @invitation.cancelled? %></p>
<p class="card-subtitle mb-2 text-info"> <%= 'Processando' if @invitation.processing? %></p>
</div>
</div>
</div>
</div>
12 changes: 11 additions & 1 deletion app/views/notifications/_comment.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
<p><%= link_to notification.notifiable.user.full_name, profile_path(notification.notifiable.user.profile) %> comentou em sua publicação
<% if notification.notifiable.post.user != current_user %>
<p>
<%= link_to notification.notifiable.user.full_name,
profile_path(notification.notifiable.user.profile) %>
<%= t('notifications.index.commented_on_post') %>
"<%= link_to notification.notifiable.post.title,
post_path(notification.notifiable.post, anchor: "comment_#{notification.notifiable.id}") %>"
<% else %>
<p>
<%= link_to notification.notifiable.user.full_name, profile_path(notification.notifiable.user.profile) %> <%= t('notifications.index.commented_on_your_post') %>
<% end %>
2 changes: 1 addition & 1 deletion app/views/notifications/_connection.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<p><%= link_to notification.notifiable.follower.user.full_name, profile_path(notification.notifiable.follower) %> começou a te seguir
<p><%= link_to notification.notifiable.follower.user.full_name, profile_path(notification.notifiable.follower) %> começou a te seguir
5 changes: 4 additions & 1 deletion app/views/notifications/_invitation.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
<p>Você recebeu um convite para <%= link_to notification.notifiable.project_title, invitation_path(notification.notifiable) %>
<p>
Você recebeu um convite para
<%= link_to notification.notifiable.project_title,
invitation_path(notification.notifiable) %>
24 changes: 12 additions & 12 deletions app/views/notifications/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<div class="container d-flex flex-column align-items-center">
<h2>Notificações</h2>
<h2><%= Notification.model_name.human(count: @notifications.count) %></h2>
<% if @notifications&.any? %>
<% @notifications.each do |notification| %>
<div class="list-group">
<%= render(
partial: "#{notification.notifiable.class.name.downcase}" ,
locals: { notification: notification }
)%>
<%= distance_of_time_in_words_to_now(notification.notifiable.created_at) %></p>
</div>
<% end %>
<% @notifications.each do |notification| %>
<div class="list-group">
<%= render(
partial: "#{notification.notifiable.class.name.downcase}" ,
locals: { notification: notification }
)%>
<%= t('.time_ago', time: time_ago_in_words(notification.notifiable.created_at)) %>
</div>
<% end %>
<% else %>
<p>Nenhuma notificação encontrada</p>
<p><%= t('.no_notifications') %></p>
<% end %>
</div>
</div>
61 changes: 44 additions & 17 deletions app/views/posts/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,17 @@
</p>

<div class="btn-group">
<div class="me-2">
<div class="me-2 mt-2">
<%= @likes_count %> <%= Like.model_name.human(count: @likes_count) %>
</div>
<% if @liked %>
<%= button_to 'Descurtir', post_like_path(@post, @liked), method: :delete, class: 'btn btn-danger btn-sm' %>
<%= button_to post_like_path(@post, @liked), method: :delete, class: 'btn btn-sm', id: 'unlike' do %>
<%= image_tag 'thumbs-up-solid', width: '20rem' %>
<% end %>
<% else %>
<%= button_to 'Curtir', post_likes_path(@post), method: :post, class: 'btn btn-primary btn-sm' %>
<%= button_to post_likes_path(@post), method: :post, class: 'btn btn-sm', id: 'like' do %>
<%= image_tag 'thumbs-up-regular', width: '20rem' %>
<% end %>
<% end %>
</div>
</div>
Expand All @@ -65,7 +69,7 @@
<%= @post.comments.count %> <%= Comment.model_name.human(count: @post.comments.count) %>
</div>
<% @post.comments.each do |comment| %>
<div class="card-body" id="comment-<%= comment.id%>">
<div class="card-body" id="<%= dom_id(comment) %>">
<blockquote class="blockquote mb-0">
<p><%= comment.message %></p>
<footer class="blockquote-footer">
Expand All @@ -74,19 +78,25 @@
</blockquote>

<% if comment.user.deleted_at.nil? %>
<div class="btn-group">
<div class="me-2">
<%= comment.likes.count %> <%= Like.model_name.human(count: comment.likes.count) %>
</div>
<div class="btn-group flex-column">
<div class="d-flex">
<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 'Descurtir', comment_like_path(comment, like), method: :delete, class: 'btn btn-danger btn-sm' %>
<% else %>
<%= button_to 'Curtir', comment_likes_path(comment), method: :post, class: 'btn btn-primary btn-sm' %>
<% end %>
</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' %>
<% 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 %>
</div>
</div>

<% if current_user != comment.user %>
<div class="report-link-wrapper">
Expand All @@ -97,4 +107,21 @@
<% end %>
</div>
<% end %>
</div>
</div>

<script>
document.addEventListener('turbo:load', () => {
if (window.location.pathname === "/posts/<%= @post.id%>") {
const anchor_tag = window.location.hash;
if (anchor_tag) {
const comment = document.querySelector(anchor_tag);
if (comment) {
comment.classList.add('highlighted');
setTimeout(function() {
comment.classList.remove('highlighted');
}, 4000);
}
}
};
});
</script>
7 changes: 6 additions & 1 deletion config/locales/notifications.pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ pt-BR:

notifications:
new_follower_mail: '%{follower_name} te seguiu'
new_notificaiton: 'Você recebeu uma nova notificação!'
new_notificaiton: 'Você recebeu uma nova notificação!'
index:
no_notifications: 'Nenhuma notificação encontrada'
commented_on_post: 'comentou no post'
commented_on_your_post: 'comentou em sua publicação'
time_ago: 'há %{time}'
2 changes: 1 addition & 1 deletion config/solid_queue.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ default: &default
- queues: "*"
threads: 1
processes: 1
polling_interval: 0.1
polling_interval: 1

development:
<<: *default
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20240212172630_add_search_name_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddSearchNameToUsers < ActiveRecord::Migration[7.1]
def change
add_column :users, :search_name, :string
end
end
5 changes: 3 additions & 2 deletions db/schema.rb

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

Loading

0 comments on commit 32e22ea

Please sign in to comment.