Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/Usuário pode publicar publicação programada #229

Merged
merged 1 commit into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class PostsController < ApplicationController
before_action :authenticate_user!, only: %w[new create edit pin]
before_action :set_post, only: %w[show edit update pin]
before_action :set_post, only: %w[show edit update pin publish]
before_action :authorize!, only: %w[edit update pin]
before_action :blocks_update, only: %w[update]
before_action :redirect_if_removed_content, only: %w[show edit update pin]
Expand Down Expand Up @@ -52,6 +52,11 @@ def pin
end
end

def publish
@post.published!
redirect_to post_path(@post), notice: t('.success')
end

private

def post_params
Expand Down
3 changes: 2 additions & 1 deletion app/jobs/post_scheduler_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ class PostSchedulerJob < ApplicationJob
queue_as :default

def perform(post)
return if post.published?

post.published!
post.update(published_at: Time.zone.now)
end
end
5 changes: 5 additions & 0 deletions app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ def first_image_attached
end
end

def published!
super
update(published_at: Time.current)
end

private

def create_notification_to_followers
Expand Down
4 changes: 3 additions & 1 deletion app/views/posts/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<div class="card text-muted">
<div class="card-body">
<h2 class="card-title"><%= @post.title %></h2>
<div class="my-4">
<div class="my-4 d-flex gap-1">
<% if current_user == @post.user %>
<%= link_to t('edit_btn'), edit_post_path(@post), class: 'btn btn-primary', data: { turbo: false } %>
<%= button_to t('publish_btn'), publish_post_path(@post), method: :patch,
class: 'btn btn-primary', data: { turbo: false } if @post.scheduled? %>
<%= Post.human_attribute_name @post.status %>
<% else %>
<%= link_to t('reports.report_btn'), new_report_path(reportable: @post, reportable_type: @post.class), class: 'btn btn-secondary btn-sm' %>
Expand Down
4 changes: 3 additions & 1 deletion app/views/profiles/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@

<div id="publications">
<h2 id="post-list-title"><%= Post.model_name.human(count: 2) %></h2>
<% if @profile.user.posts.count - @profile.user.posts.pinned.count < 1 %>
<% published_posts = @profile.user.posts.published %>

<% if published_posts.count - @profile.user.posts.pinned.count < 1 %>
<p><%= t('.nothing_here') %> <br> <%= t('.new_posts') %> :)</p>
<% end %>

Expand Down
3 changes: 2 additions & 1 deletion config/locales/buttons.pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ pt-BR:
pin_btn: Fixar
unpin_btn: Desafixar
return_btn: Voltar
send_btn: Enviar
send_btn: Enviar
publish_btn: Publicar
2 changes: 2 additions & 0 deletions config/locales/models/posts.pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pt-BR:
status_draft: Rascunho
status_published: Publicada
posts:
publish:
success: Publicada com sucesso!
redirect_alert:
invalid_user: Você não pode acessar este conteúdo ou realizar esta ação
create:
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
end

resources :posts, only: %i[] do
patch 'publish', on: :member
resources :likes, only: %i[create destroy], module: :posts

end
Expand Down
4 changes: 2 additions & 2 deletions db/schema.rb

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

12 changes: 12 additions & 0 deletions spec/models/post_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
expect(Post.get_sample(5).count).to eq 3
end
end

describe '#pinned' do
it 'e não altera o edited_at' do
user = create(:user)
Expand All @@ -70,4 +71,15 @@

expect(post.status).to eq 'published'
end

describe '#published!' do
it 'deve alterar status para published e atualizar published_at' do
post = create(:post, status: 'scheduled', published_at: 1.day.from_now)

post.published!

expect(post.reload).to be_published
expect(post.reload.published_at.to_date).to eq Time.current.to_date
end
end
end
16 changes: 16 additions & 0 deletions spec/system/posts/user_publish_scheduled_post_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'rails_helper'

describe 'Usuário publica um post agendado' do
it 'comm sucesso' do
post = create(:post, status: :scheduled, published_at: 1.day.from_now)

login_as post.user
visit post_path(post)
click_on 'Publicar'

expect(page).to have_content 'Publicada com sucesso'
expect(page).to have_content 'Publicada'
expect(post.reload).to be_published
expect(page).not_to have_button 'Publicar'
end
end
Loading