Skip to content

Commit 143231c

Browse files
authored
Merge pull request #229 from TreinaDev/feat/publicar-post-agendado
Feat/Usuário pode publicar publicação programada
2 parents 0f941aa + f09518b commit 143231c

File tree

11 files changed

+54
-7
lines changed

11 files changed

+54
-7
lines changed

app/controllers/posts_controller.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class PostsController < ApplicationController
22
before_action :authenticate_user!, only: %w[new create edit pin]
3-
before_action :set_post, only: %w[show edit update pin]
3+
before_action :set_post, only: %w[show edit update pin publish]
44
before_action :authorize!, only: %w[edit update pin]
55
before_action :blocks_update, only: %w[update]
66
before_action :redirect_if_removed_content, only: %w[show edit update pin]
@@ -52,6 +52,11 @@ def pin
5252
end
5353
end
5454

55+
def publish
56+
@post.published!
57+
redirect_to post_path(@post), notice: t('.success')
58+
end
59+
5560
private
5661

5762
def post_params

app/jobs/post_scheduler_job.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ class PostSchedulerJob < ApplicationJob
22
queue_as :default
33

44
def perform(post)
5+
return if post.published?
6+
57
post.published!
6-
post.update(published_at: Time.zone.now)
78
end
89
end

app/models/post.rb

+5
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ def first_image_attached
4040
end
4141
end
4242

43+
def published!
44+
super
45+
update(published_at: Time.current)
46+
end
47+
4348
private
4449

4550
def create_notification_to_followers

app/views/posts/show.html.erb

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<div class="card text-muted">
22
<div class="card-body">
33
<h2 class="card-title"><%= @post.title %></h2>
4-
<div class="my-4">
4+
<div class="my-4 d-flex gap-1">
55
<% if current_user == @post.user %>
66
<%= link_to t('edit_btn'), edit_post_path(@post), class: 'btn btn-primary', data: { turbo: false } %>
7+
<%= button_to t('publish_btn'), publish_post_path(@post), method: :patch,
8+
class: 'btn btn-primary', data: { turbo: false } if @post.scheduled? %>
79
<%= Post.human_attribute_name @post.status %>
810
<% else %>
911
<%= link_to t('reports.report_btn'), new_report_path(reportable: @post, reportable_type: @post.class), class: 'btn btn-secondary btn-sm' %>

app/views/profiles/show.html.erb

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212

1313
<div id="publications">
1414
<h2 id="post-list-title"><%= Post.model_name.human(count: 2) %></h2>
15-
<% if @profile.user.posts.count - @profile.user.posts.pinned.count < 1 %>
15+
<% published_posts = @profile.user.posts.published %>
16+
17+
<% if published_posts.count - @profile.user.posts.pinned.count < 1 %>
1618
<p><%= t('.nothing_here') %> <br> <%= t('.new_posts') %> :)</p>
1719
<% end %>
1820

config/locales/buttons.pt-BR.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ pt-BR:
99
pin_btn: Fixar
1010
unpin_btn: Desafixar
1111
return_btn: Voltar
12-
send_btn: Enviar
12+
send_btn: Enviar
13+
publish_btn: Publicar

config/locales/models/posts.pt-BR.yml

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ pt-BR:
2121
status_draft: Rascunho
2222
status_published: Publicada
2323
posts:
24+
publish:
25+
success: Publicada com sucesso!
2426
redirect_alert:
2527
invalid_user: Você não pode acessar este conteúdo ou realizar esta ação
2628
create:

config/routes.rb

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
end
3333

3434
resources :posts, only: %i[] do
35+
patch 'publish', on: :member
3536
resources :likes, only: %i[create destroy], module: :posts
3637

3738
end

db/schema.rb

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/models/post_spec.rb

+12
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
expect(Post.get_sample(5).count).to eq 3
5555
end
5656
end
57+
5758
describe '#pinned' do
5859
it 'e não altera o edited_at' do
5960
user = create(:user)
@@ -70,4 +71,15 @@
7071

7172
expect(post.status).to eq 'published'
7273
end
74+
75+
describe '#published!' do
76+
it 'deve alterar status para published e atualizar published_at' do
77+
post = create(:post, status: 'scheduled', published_at: 1.day.from_now)
78+
79+
post.published!
80+
81+
expect(post.reload).to be_published
82+
expect(post.reload.published_at.to_date).to eq Time.current.to_date
83+
end
84+
end
7385
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require 'rails_helper'
2+
3+
describe 'Usuário publica um post agendado' do
4+
it 'comm sucesso' do
5+
post = create(:post, status: :scheduled, published_at: 1.day.from_now)
6+
7+
login_as post.user
8+
visit post_path(post)
9+
click_on 'Publicar'
10+
11+
expect(page).to have_content 'Publicada com sucesso'
12+
expect(page).to have_content 'Publicada'
13+
expect(post.reload).to be_published
14+
expect(page).not_to have_button 'Publicar'
15+
end
16+
end

0 commit comments

Comments
 (0)