From f09518bfaa8663f2283ac15be1eb7fc69a39636c Mon Sep 17 00:00:00 2001 From: Caique Date: Thu, 15 Feb 2024 00:02:46 -0400 Subject: [PATCH] =?UTF-8?q?Feat/Usu=C3=A1rio=20pode=20publicar=20publica?= =?UTF-8?q?=C3=A7=C3=A3o=20programada?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - altera metodo published! para alterar status e data de publicação - usuário pode clicar em publicar na view do post - garante que fila não aceitara posts já publicados --- app/controllers/posts_controller.rb | 7 ++++++- app/jobs/post_scheduler_job.rb | 3 ++- app/models/post.rb | 5 +++++ app/views/posts/show.html.erb | 4 +++- app/views/profiles/show.html.erb | 4 +++- config/locales/buttons.pt-BR.yml | 3 ++- config/locales/models/posts.pt-BR.yml | 2 ++ config/routes.rb | 1 + db/schema.rb | 4 ++-- spec/models/post_spec.rb | 12 ++++++++++++ .../posts/user_publish_scheduled_post_spec.rb | 16 ++++++++++++++++ 11 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 spec/system/posts/user_publish_scheduled_post_spec.rb diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 98f1aa8..48615ee 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -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] @@ -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 diff --git a/app/jobs/post_scheduler_job.rb b/app/jobs/post_scheduler_job.rb index 2b9fd17..0413671 100644 --- a/app/jobs/post_scheduler_job.rb +++ b/app/jobs/post_scheduler_job.rb @@ -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 diff --git a/app/models/post.rb b/app/models/post.rb index 832877d..51134ac 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -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 diff --git a/app/views/posts/show.html.erb b/app/views/posts/show.html.erb index 721ecee..935f2ac 100644 --- a/app/views/posts/show.html.erb +++ b/app/views/posts/show.html.erb @@ -1,9 +1,11 @@

<%= @post.title %>

-
+
<% 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' %> diff --git a/app/views/profiles/show.html.erb b/app/views/profiles/show.html.erb index 87b2c00..7c20876 100644 --- a/app/views/profiles/show.html.erb +++ b/app/views/profiles/show.html.erb @@ -12,7 +12,9 @@

<%= Post.model_name.human(count: 2) %>

- <% 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 %>

<%= t('.nothing_here') %>
<%= t('.new_posts') %> :)

<% end %> diff --git a/config/locales/buttons.pt-BR.yml b/config/locales/buttons.pt-BR.yml index 06cae11..b8949ae 100644 --- a/config/locales/buttons.pt-BR.yml +++ b/config/locales/buttons.pt-BR.yml @@ -9,4 +9,5 @@ pt-BR: pin_btn: Fixar unpin_btn: Desafixar return_btn: Voltar - send_btn: Enviar \ No newline at end of file + send_btn: Enviar + publish_btn: Publicar \ No newline at end of file diff --git a/config/locales/models/posts.pt-BR.yml b/config/locales/models/posts.pt-BR.yml index 6df4cea..414f0ad 100644 --- a/config/locales/models/posts.pt-BR.yml +++ b/config/locales/models/posts.pt-BR.yml @@ -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: diff --git a/config/routes.rb b/config/routes.rb index 04d1b4d..3a64372 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -30,6 +30,7 @@ end resources :posts, only: %i[] do + patch 'publish', on: :member resources :likes, only: %i[create destroy], module: :posts end diff --git a/db/schema.rb b/db/schema.rb index 02e02c2..c7de8d0 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -55,8 +55,8 @@ t.integer "user_id", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.text "old_message" t.integer "status", default: 0 + t.text "old_message" t.index ["post_id"], name: "index_comments_on_post_id" t.index ["user_id"], name: "index_comments_on_user_id" end @@ -162,7 +162,7 @@ t.datetime "created_at", null: false t.datetime "updated_at", null: false t.integer "pin", default: 0 - t.datetime "edited_at", default: "2024-02-13 03:11:57" + t.datetime "edited_at" t.integer "status", default: 0 t.datetime "published_at" t.string "old_status" diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index f26c77d..6738fb9 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -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) @@ -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 diff --git a/spec/system/posts/user_publish_scheduled_post_spec.rb b/spec/system/posts/user_publish_scheduled_post_spec.rb new file mode 100644 index 0000000..2f0f8a7 --- /dev/null +++ b/spec/system/posts/user_publish_scheduled_post_spec.rb @@ -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