From c7f445ed17d0e6792a54f86d4475225bacac518d Mon Sep 17 00:00:00 2001 From: Adoniran Date: Mon, 12 Feb 2024 18:26:01 -0300 Subject: [PATCH 01/24] Criando tabela post MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: André Pereira --- app/models/post.rb | 4 ++++ db/migrate/20240212192630_create_posts.rb | 12 ++++++++++++ db/schema.rb | 15 ++++++++++++++- spec/factories/posts.rb | 8 ++++++++ spec/models/post_spec.rb | 5 +++++ spec/system/forums/user_view_forum_spec.rb | 0 6 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 app/models/post.rb create mode 100644 db/migrate/20240212192630_create_posts.rb create mode 100644 spec/factories/posts.rb create mode 100644 spec/models/post_spec.rb create mode 100644 spec/system/forums/user_view_forum_spec.rb diff --git a/app/models/post.rb b/app/models/post.rb new file mode 100644 index 0000000..0614fb2 --- /dev/null +++ b/app/models/post.rb @@ -0,0 +1,4 @@ +class Post < ApplicationRecord + belongs_to :user_role + belongs_to :project +end diff --git a/db/migrate/20240212192630_create_posts.rb b/db/migrate/20240212192630_create_posts.rb new file mode 100644 index 0000000..8fdb72b --- /dev/null +++ b/db/migrate/20240212192630_create_posts.rb @@ -0,0 +1,12 @@ +class CreatePosts < ActiveRecord::Migration[7.1] + def change + create_table :posts do |t| + t.string :title + t.string :body + t.references :user_role, null: false, foreign_key: true + t.references :project, null: false, foreign_key: true + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 2d4fb6e..345c2b2 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_02_09_121744) do +ActiveRecord::Schema[7.1].define(version: 2024_02_12_192630) do create_table "active_storage_attachments", force: :cascade do |t| t.string "name", null: false t.string "record_type", null: false @@ -88,6 +88,17 @@ t.index ["user_role_id"], name: "index_meetings_on_user_role_id" end + create_table "posts", force: :cascade do |t| + t.string "title" + t.string "body" + t.integer "user_role_id", null: false + t.integer "project_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["project_id"], name: "index_posts_on_project_id" + t.index ["user_role_id"], name: "index_posts_on_user_role_id" + end + create_table "profiles", force: :cascade do |t| t.integer "user_id", null: false t.string "first_name" @@ -273,6 +284,8 @@ add_foreign_key "meeting_participants", "user_roles" add_foreign_key "meetings", "projects" add_foreign_key "meetings", "user_roles" + add_foreign_key "posts", "projects" + add_foreign_key "posts", "user_roles" add_foreign_key "profiles", "users" add_foreign_key "project_job_categories", "projects" add_foreign_key "projects", "users" diff --git a/spec/factories/posts.rb b/spec/factories/posts.rb new file mode 100644 index 0000000..18b18c1 --- /dev/null +++ b/spec/factories/posts.rb @@ -0,0 +1,8 @@ +FactoryBot.define do + factory :post do + title { "MyString" } + body { "MyString" } + user_role { nil } + project { nil } + end +end diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb new file mode 100644 index 0000000..f41774c --- /dev/null +++ b/spec/models/post_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Post, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/system/forums/user_view_forum_spec.rb b/spec/system/forums/user_view_forum_spec.rb new file mode 100644 index 0000000..e69de29 From ac4c68e061a57659c2e8d5d07c1daff0ffa6a9b9 Mon Sep 17 00:00:00 2001 From: Adoniran Date: Mon, 12 Feb 2024 19:12:36 -0300 Subject: [PATCH 02/24] Adiciona action index para forum MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: André Pereira --- app/controllers/forums_controller.rb | 5 +++++ app/views/forums/index.html.erb | 1 + app/views/shared/_project_header.html.erb | 18 +++++++++++------- config/locales/forum.pt-BR.yml | 3 +++ config/routes.rb | 2 ++ spec/system/forums/user_view_forum_spec.rb | 15 +++++++++++++++ 6 files changed, 37 insertions(+), 7 deletions(-) create mode 100644 app/controllers/forums_controller.rb create mode 100644 app/views/forums/index.html.erb create mode 100644 config/locales/forum.pt-BR.yml diff --git a/app/controllers/forums_controller.rb b/app/controllers/forums_controller.rb new file mode 100644 index 0000000..c3f6f40 --- /dev/null +++ b/app/controllers/forums_controller.rb @@ -0,0 +1,5 @@ +class ForumsController < ApplicationController + def index + @project = Project.find(params[:project_id]) + end +end diff --git a/app/views/forums/index.html.erb b/app/views/forums/index.html.erb new file mode 100644 index 0000000..a748492 --- /dev/null +++ b/app/views/forums/index.html.erb @@ -0,0 +1 @@ +<%= render 'shared/project_header', project: @project %> diff --git a/app/views/shared/_project_header.html.erb b/app/views/shared/_project_header.html.erb index 47d6552..1dc4009 100644 --- a/app/views/shared/_project_header.html.erb +++ b/app/views/shared/_project_header.html.erb @@ -28,15 +28,19 @@ class: "nav-item nav-link link-body-emphasis #{'active' if request.path.include?('/members') || request.path.include?('/user_roles') }" %> + <%= link_to t('forum.title'), + project_forum_path(project), + class: "nav-item nav-link link-body-emphasis + #{'active' if request.path.include?('/forum')}" %> <% if project.leader?(current_user) %> <%= link_to t(:search_users_btn), - search_project_portfoliorrr_profiles_path(project), - class: "nav-item nav-link link-body-emphasis - #{'active' if current_page?(search_project_portfoliorrr_profiles_path(project))}" %> - <%= link_to Proposal.model_name.human(count: 2), - project_proposals_path(project), - class: "nav-item nav-link link-body-emphasis - #{'active' if request.path.include?('/proposals')}" %> + search_project_portfoliorrr_profiles_path(project), + class: "nav-item nav-link link-body-emphasis + #{'active' if current_page?(search_project_portfoliorrr_profiles_path(project))}" %> + <%= link_to Proposal.model_name.human(count: 2), + project_proposals_path(project), + class: "nav-item nav-link link-body-emphasis + #{'active' if request.path.include?('/proposals')}" %> <% end %> diff --git a/config/locales/forum.pt-BR.yml b/config/locales/forum.pt-BR.yml new file mode 100644 index 0000000..5e642d8 --- /dev/null +++ b/config/locales/forum.pt-BR.yml @@ -0,0 +1,3 @@ +pt-BR: + forum: + title: Fórum diff --git a/config/routes.rb b/config/routes.rb index 4117583..c289a73 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -29,6 +29,8 @@ resources :user_roles, only: %i[edit update] resources :proposals, only: %i[index] resources :calendars, only: %i[index] + + get 'forum', to: 'forums#index' end resources :user_roles, only: [] do diff --git a/spec/system/forums/user_view_forum_spec.rb b/spec/system/forums/user_view_forum_spec.rb index e69de29..d6e477a 100644 --- a/spec/system/forums/user_view_forum_spec.rb +++ b/spec/system/forums/user_view_forum_spec.rb @@ -0,0 +1,15 @@ +require 'rails_helper' + +describe 'Usuário vê página de fórum de um projeto' do + it 'a partir da tela inicial' do + travel_to Time.zone.local(2024, 11, 24, 1, 4, 44) + user = create(:user) + project = create(:project, user:) + + login_as(user, scope: :user) + visit project_path(project) + click_on 'Reuniões' + + expect(page).to have_content 'Fórum' + end +end From 6f97dabf057a3e1d776a73fcb5de04922f2c3bae Mon Sep 17 00:00:00 2001 From: Adoniran Date: Mon, 12 Feb 2024 19:20:05 -0300 Subject: [PATCH 03/24] Configura factory de post Co-authored-by: Andre Pereira --- spec/factories/posts.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/factories/posts.rb b/spec/factories/posts.rb index 18b18c1..7c82922 100644 --- a/spec/factories/posts.rb +++ b/spec/factories/posts.rb @@ -1,7 +1,7 @@ FactoryBot.define do factory :post do - title { "MyString" } - body { "MyString" } + title { 'A arte de criar' } + body { 'Estive desenvolvendo um novo método para acelerar a resolução de tarefas do o projeto' } user_role { nil } project { nil } end From cb9411a436454d69a21c1ca3d71a84718f8d7253 Mon Sep 17 00:00:00 2001 From: decoporteira Date: Tue, 13 Feb 2024 11:16:22 -0300 Subject: [PATCH 04/24] =?UTF-8?q?WIP:=20implementa=20p=C3=A1gina=20com=20V?= =?UTF-8?q?ue?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Adoniran France --- app/javascript/application.js | 4 ++-- app/javascript/components/ForumVue.vue | 18 ++++++++++++++++++ app/javascript/components/forum_vue.js | 18 ++++++++++++++++++ app/javascript/components/hello_vue.js | 3 ++- app/views/forums/index.html.erb | 5 +++++ 5 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 app/javascript/components/ForumVue.vue create mode 100644 app/javascript/components/forum_vue.js diff --git a/app/javascript/application.js b/app/javascript/application.js index b3d564d..b1219a6 100644 --- a/app/javascript/application.js +++ b/app/javascript/application.js @@ -2,6 +2,6 @@ import "@hotwired/turbo-rails" import * as bootstrap from "bootstrap" import { createApp } from 'vue/dist/vue.esm-bundler.js' -import HelloComponent from './components/hello_vue.js' +import ForumComponent from './components/forum_vue.js' -createApp(HelloComponent).mount('#vue-app') +createApp(ForumComponent).mount('#vue-app') diff --git a/app/javascript/components/ForumVue.vue b/app/javascript/components/ForumVue.vue new file mode 100644 index 0000000..e287a09 --- /dev/null +++ b/app/javascript/components/ForumVue.vue @@ -0,0 +1,18 @@ + + + \ No newline at end of file diff --git a/app/javascript/components/forum_vue.js b/app/javascript/components/forum_vue.js new file mode 100644 index 0000000..4f87327 --- /dev/null +++ b/app/javascript/components/forum_vue.js @@ -0,0 +1,18 @@ +import { ref } from "vue/dist/vue.esm-bundler.js" + +export default { + data() { + return { + message: 'Hello!', + insertText: '', + project: 's' + } + }, + + + methods: { + insertMessage() { + return this.message = this.insertText; + } + } +} \ No newline at end of file diff --git a/app/javascript/components/hello_vue.js b/app/javascript/components/hello_vue.js index f53bfd6..325b665 100644 --- a/app/javascript/components/hello_vue.js +++ b/app/javascript/components/hello_vue.js @@ -4,7 +4,8 @@ export default { data() { return { message: 'Hello!', - insertText: '' + insertText: '', + project: 'Hello Projects' } }, diff --git a/app/views/forums/index.html.erb b/app/views/forums/index.html.erb index a748492..f48f5d8 100644 --- a/app/views/forums/index.html.erb +++ b/app/views/forums/index.html.erb @@ -1 +1,6 @@ <%= render 'shared/project_header', project: @project %> +
+ + {{ project }} + {{ data-project-title }} +
From 1c5c019c4e1bf8701aa7a22ec273d27c769759c5 Mon Sep 17 00:00:00 2001 From: Adoniran Date: Tue, 13 Feb 2024 11:48:56 -0300 Subject: [PATCH 05/24] =?UTF-8?q?Adiciona=20t=C3=ADtulo=20do=20projeto=20a?= =?UTF-8?q?=20index=20por=20vue?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: André Pereira --- app/javascript/components/forum_vue.js | 5 ++--- app/views/forums/index.html.erb | 10 ++++++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/app/javascript/components/forum_vue.js b/app/javascript/components/forum_vue.js index 4f87327..f9bcb14 100644 --- a/app/javascript/components/forum_vue.js +++ b/app/javascript/components/forum_vue.js @@ -5,14 +5,13 @@ export default { return { message: 'Hello!', insertText: '', - project: 's' + project: window.project } }, - methods: { insertMessage() { return this.message = this.insertText; } } -} \ No newline at end of file +} diff --git a/app/views/forums/index.html.erb b/app/views/forums/index.html.erb index f48f5d8..0da0bcd 100644 --- a/app/views/forums/index.html.erb +++ b/app/views/forums/index.html.erb @@ -1,6 +1,8 @@ <%= render 'shared/project_header', project: @project %> -
- - {{ project }} - {{ data-project-title }} +
+ {{project}}
+ + From e535b5f613fcf98f00a7fc0ec39d08e0791e53d8 Mon Sep 17 00:00:00 2001 From: Adoniran Date: Tue, 13 Feb 2024 12:17:52 -0300 Subject: [PATCH 06/24] Adiciona postagens do a index por vue MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: André Pereira --- app/controllers/forums_controller.rb | 1 + app/javascript/components/forum_vue.js | 9 ++++++--- app/models/project.rb | 1 + app/views/forums/index.html.erb | 10 +++++++++- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/app/controllers/forums_controller.rb b/app/controllers/forums_controller.rb index c3f6f40..b93a639 100644 --- a/app/controllers/forums_controller.rb +++ b/app/controllers/forums_controller.rb @@ -1,5 +1,6 @@ class ForumsController < ApplicationController def index @project = Project.find(params[:project_id]) + @posts = @project.posts end end diff --git a/app/javascript/components/forum_vue.js b/app/javascript/components/forum_vue.js index f9bcb14..798c135 100644 --- a/app/javascript/components/forum_vue.js +++ b/app/javascript/components/forum_vue.js @@ -5,13 +5,16 @@ export default { return { message: 'Hello!', insertText: '', - project: window.project + project: window.project, + posts: [] } }, - + mounted() { + this.posts = window.posts.map(item => ({ title: item.title, body: item.body })); + }, methods: { insertMessage() { - return this.message = this.insertText; + this.message = this.insertText; } } } diff --git a/app/models/project.rb b/app/models/project.rb index 9b6e588..6ea96c2 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -8,6 +8,7 @@ class Project < ApplicationRecord has_many :meetings, dependent: :destroy has_many :project_job_categories, dependent: :destroy has_many :proposals, dependent: :destroy + has_many :posts validates :title, :description, :category, presence: true diff --git a/app/views/forums/index.html.erb b/app/views/forums/index.html.erb index 0da0bcd..6ce6a5c 100644 --- a/app/views/forums/index.html.erb +++ b/app/views/forums/index.html.erb @@ -1,8 +1,16 @@ <%= render 'shared/project_header', project: @project %>
- {{project}} + {{ project }} +
    +
  • + {{ item.title }} + {{item.body}} +
  • +
+
From a4f88c9950bd48ffab2588703cc24dc5ef41468d Mon Sep 17 00:00:00 2001 From: Adoniran Date: Tue, 13 Feb 2024 13:01:10 -0300 Subject: [PATCH 07/24] =?UTF-8?q?Adiciona=20pesquisa=20por=20t=C3=ADtulo?= =?UTF-8?q?=20e=20corpo=20de=20postagem?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: André Pereira --- app/javascript/components/forum_vue.js | 22 ++++++++++++++++++++-- app/models/project.rb | 2 +- app/views/forums/index.html.erb | 18 ++++++++++++------ 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/app/javascript/components/forum_vue.js b/app/javascript/components/forum_vue.js index 798c135..7280ed9 100644 --- a/app/javascript/components/forum_vue.js +++ b/app/javascript/components/forum_vue.js @@ -3,15 +3,33 @@ import { ref } from "vue/dist/vue.esm-bundler.js" export default { data() { return { - message: 'Hello!', - insertText: '', + searchText: '', + selectedFilter: '', project: window.project, posts: [] } }, + mounted() { this.posts = window.posts.map(item => ({ title: item.title, body: item.body })); }, + + computed:{ + filteredProjects() { + const searchType = this.selectedFilter + return this.posts.filter(post => { + if (searchType === '') { + return ( + post.title.toLowerCase().includes(this.searchText.toLowerCase()) || + post.body.toLowerCase().includes(this.searchText.toLowerCase()) + ) + } else { + return post[searchType].toLowerCase().includes(this.searchText.toLowerCase()); + } + }) + } + }, + methods: { insertMessage() { this.message = this.insertText; diff --git a/app/models/project.rb b/app/models/project.rb index 6ea96c2..abd1eb9 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -8,7 +8,7 @@ class Project < ApplicationRecord has_many :meetings, dependent: :destroy has_many :project_job_categories, dependent: :destroy has_many :proposals, dependent: :destroy - has_many :posts + has_many :posts, dependent: :destroy validates :title, :description, :category, presence: true diff --git a/app/views/forums/index.html.erb b/app/views/forums/index.html.erb index 6ce6a5c..badc890 100644 --- a/app/views/forums/index.html.erb +++ b/app/views/forums/index.html.erb @@ -1,12 +1,18 @@ <%= render 'shared/project_header', project: @project %>
{{ project }} -
    -
  • - {{ item.title }} - {{item.body}} -
  • -
+
+
+ + +
+
    +
  • + {{ item.title }} + {{item.body}} +
  • +
+
From 5240ccf49fb0f4872f332e9eb35ef7b72f1c4cca Mon Sep 17 00:00:00 2001 From: Adoniran Date: Tue, 13 Feb 2024 14:18:44 -0300 Subject: [PATCH 08/24] =?UTF-8?q?Adiciona=20da=20data=20da=20cria=C3=A7?= =?UTF-8?q?=C3=A3o=20ou=20atualiza=C3=A7=C3=A3o=20da=20postagem?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: André Pereira --- app/javascript/components/forum_vue.js | 5 ++++- app/models/post.rb | 1 + app/views/forums/index.html.erb | 29 +++++++++++++++++--------- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/app/javascript/components/forum_vue.js b/app/javascript/components/forum_vue.js index 7280ed9..7b5cfb6 100644 --- a/app/javascript/components/forum_vue.js +++ b/app/javascript/components/forum_vue.js @@ -11,7 +11,10 @@ export default { }, mounted() { - this.posts = window.posts.map(item => ({ title: item.title, body: item.body })); + this.posts = window.posts.map(item => ({ title: item.title, + body: item.body, + author: item.user_name, + date: item.created_at })); }, computed:{ diff --git a/app/models/post.rb b/app/models/post.rb index 0614fb2..03d92dc 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -1,4 +1,5 @@ class Post < ApplicationRecord belongs_to :user_role belongs_to :project + delegate :user, to: :user_role end diff --git a/app/views/forums/index.html.erb b/app/views/forums/index.html.erb index badc890..9c61fac 100644 --- a/app/views/forums/index.html.erb +++ b/app/views/forums/index.html.erb @@ -1,22 +1,31 @@ <%= render 'shared/project_header', project: @project %>
{{ project }} -
-
+
+
- + + +
+
+
+
{{ item.title }}
+ {{ item.date }} +
+

{{ item.body }}

+ por {{ item.author }} +
+
-
    -
  • - {{ item.title }} - {{item.body}} -
  • -
From 8b8fe278a1246251221a6c69bb503352a5e63097 Mon Sep 17 00:00:00 2001 From: Adoniran Date: Tue, 13 Feb 2024 14:30:54 -0300 Subject: [PATCH 09/24] Remove ForumVue.vue MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: André Pereira --- app/javascript/components/ForumVue.vue | 18 ------------------ 1 file changed, 18 deletions(-) delete mode 100644 app/javascript/components/ForumVue.vue diff --git a/app/javascript/components/ForumVue.vue b/app/javascript/components/ForumVue.vue deleted file mode 100644 index e287a09..0000000 --- a/app/javascript/components/ForumVue.vue +++ /dev/null @@ -1,18 +0,0 @@ - - - \ No newline at end of file From 0d4de72397088daf10731f861587ba5dd907fcf9 Mon Sep 17 00:00:00 2001 From: Adoniran Date: Tue, 13 Feb 2024 18:21:18 -0300 Subject: [PATCH 10/24] =?UTF-8?q?WIP:=20Cria=20postagens=20por=20formul?= =?UTF-8?q?=C3=A1rio?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: André Pereira --- app/controllers/posts_controller.rb | 19 +++++++++++++++++++ app/javascript/components/forum_vue.js | 10 ++++++---- app/views/forums/index.html.erb | 20 ++++++++++++++++++-- app/views/layouts/application.html.erb | 1 + config/routes.rb | 1 + 5 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 app/controllers/posts_controller.rb diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb new file mode 100644 index 0000000..f96ebc1 --- /dev/null +++ b/app/controllers/posts_controller.rb @@ -0,0 +1,19 @@ +class PostsController < ApplicationController + def create + @project = Project.find(params[:project_id]) + @post = @project.posts.build(post_params) + @post.user_role = UserRole.find_by(user: current_user, project: @project) + + if @post.save! + redirect_to project_forum_path(@project), status: :created + else + redirect_to project_forum_path(@project), status: :unprocessable_entity + end + end + + private + + def post_params + params.require(:post).permit(:title, :body) + end +end diff --git a/app/javascript/components/forum_vue.js b/app/javascript/components/forum_vue.js index 7b5cfb6..ae29def 100644 --- a/app/javascript/components/forum_vue.js +++ b/app/javascript/components/forum_vue.js @@ -3,10 +3,10 @@ import { ref } from "vue/dist/vue.esm-bundler.js" export default { data() { return { - searchText: '', + searchText: 'Solução', selectedFilter: '', - project: window.project, - posts: [] + project: {}, + posts: [], } }, @@ -15,9 +15,11 @@ export default { body: item.body, author: item.user_name, date: item.created_at })); + this.project = { id: window.project.id, + title: window.project.title }; }, - computed:{ + computed: { filteredProjects() { const searchType = this.selectedFilter return this.posts.filter(post => { diff --git a/app/views/forums/index.html.erb b/app/views/forums/index.html.erb index 9c61fac..d0a9bb1 100644 --- a/app/views/forums/index.html.erb +++ b/app/views/forums/index.html.erb @@ -1,11 +1,27 @@ <%= render 'shared/project_header', project: @project %>
- {{ project }} + {{ project.title }}
+ <%= form_with(model: @post, url: project_posts_path, method: :post) do |form| %> +
+ <%= form.label :title, class: 'form-label' %> + <%= form.text_field :title, class: 'form-control' %> +
+ +
+ <%= form.label :body, class: 'form-label' %> + <%= form.text_area :body, class: 'form-control' %> +
+ +
+ <%= form.submit 'Postar', class: 'btn btn-primary' %> +
+ <% end %> +
@@ -22,7 +38,7 @@
diff --git a/app/views/shared/_project_header.html.erb b/app/views/shared/_project_header.html.erb index 1dc4009..325f14b 100644 --- a/app/views/shared/_project_header.html.erb +++ b/app/views/shared/_project_header.html.erb @@ -31,7 +31,8 @@ <%= link_to t('forum.title'), project_forum_path(project), class: "nav-item nav-link link-body-emphasis - #{'active' if request.path.include?('/forum')}" %> + #{'active' if request.path.include?('/forum')}", + data: { turbo: false } %> <% if project.leader?(current_user) %> <%= link_to t(:search_users_btn), search_project_portfoliorrr_profiles_path(project), From ae6e93188e47fe2b898a6b334b3abfc97b467a7c Mon Sep 17 00:00:00 2001 From: decoporteira Date: Thu, 15 Feb 2024 11:45:20 -0300 Subject: [PATCH 13/24] =?UTF-8?q?Conserta=20posts=20n=C3=A3o=20aparecendo?= =?UTF-8?q?=20na=20view.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Adoniran France --- app/controllers/forums_controller.rb | 13 +++- app/controllers/posts_controller.rb | 28 +++++++-- app/javascript/components/forum_vue.js | 34 +++++++---- app/views/forums/index.html.erb | 13 ++-- config/locales/forum.pt-BR.yml | 2 + config/locales/models/post.pt-BR.yml | 12 ++++ config/routes.rb | 2 +- spec/models/post_spec.rb | 22 ++++++- spec/requests/posts/user_creates_post_spec.rb | 31 ++++++++++ spec/requests/posts/user_edits_post_spec.rb | 59 +++++++++++++++++++ spec/system/forums/user_creates_forum_spec.rb | 21 +++++++ spec/system/forums/user_search_post_spec.rb | 19 ++++++ spec/system/forums/user_view_forum_spec.rb | 7 ++- 13 files changed, 234 insertions(+), 29 deletions(-) create mode 100644 config/locales/models/post.pt-BR.yml create mode 100644 spec/requests/posts/user_creates_post_spec.rb create mode 100644 spec/requests/posts/user_edits_post_spec.rb create mode 100644 spec/system/forums/user_creates_forum_spec.rb create mode 100644 spec/system/forums/user_search_post_spec.rb diff --git a/app/controllers/forums_controller.rb b/app/controllers/forums_controller.rb index 29743f7..b7507b7 100644 --- a/app/controllers/forums_controller.rb +++ b/app/controllers/forums_controller.rb @@ -1,6 +1,17 @@ class ForumsController < ApplicationController + before_action :set_project + before_action :check_contributor def index - @project = Project.find(params[:project_id]) @posts = @project.posts.order(created_at: :desc) end + + private + + def set_project + @project = Project.find(params[:project_id]) + end + + def check_contributor + redirect_to root_path, alert: t('.not_contributor') unless @project.member?(current_user) + end end diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index f96ebc1..4867a36 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -1,18 +1,38 @@ class PostsController < ApplicationController + before_action :set_project + before_action :check_contributor + def create - @project = Project.find(params[:project_id]) @post = @project.posts.build(post_params) @post.user_role = UserRole.find_by(user: current_user, project: @project) - if @post.save! - redirect_to project_forum_path(@project), status: :created + render status: :created, json: json_response(@post) if @post.save! + end + + def update + @post = Post.find.params(:id) + if @post.update(post_params) + redirect_to project_forum_path(@project), notice: t('.success') else - redirect_to project_forum_path(@project), status: :unprocessable_entity + flash.now[:alert] = t('.fail') + render :edit, status: :unprocessable_entity end end private + def json_response(post) + { id: post.id, title: post.title, body: post.body, author: post.user.full_name } + end + + def set_project + @project = Project.find(params[:project_id]) + end + + def check_contributor + redirect_to root_path, alert: t('.not_contributor') unless @project.member?(current_user) + end + def post_params params.require(:post).permit(:title, :body) end diff --git a/app/javascript/components/forum_vue.js b/app/javascript/components/forum_vue.js index 2a878cd..ad47418 100644 --- a/app/javascript/components/forum_vue.js +++ b/app/javascript/components/forum_vue.js @@ -6,24 +6,19 @@ export default { searchText: '', selectedFilter: '', project: {}, - posts: [{title: 'teste', body: 'Teste'}], + posts: ref([]), newPost: { title: '', body: '' - } + }, + errors: window.errors } }, async mounted() { - console.log(this.posts) - this.posts = await window.posts.map(item => ({ title: item.title, - body: item.body, - author: item.user_name, - date: item.created_at })); + this.loadPosts(); this.project = { id: window.project.id, title: window.project.title }; - - console.log(this.posts) }, computed: { @@ -43,6 +38,18 @@ export default { }, methods: { + loadPosts(){ + console.log(window.posts) + this.posts = window.posts.map(item => ({ id: item.id, + title: item.title, + body: item.body, + author: item.author, + date: item.date })); + + this.activePage = 'postsIndex' + + }, + async submitForm() { try { const csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content'); @@ -56,10 +63,11 @@ export default { body: JSON.stringify(this.newPost) }); - location.reload() - - this.newPost.title = ''; - this.newPost.body = ''; + const data = await response.json() + console.log(data) + this.posts.unshift(data) + this.newPost.title = '' + this.newPost.body = '' } catch (error) { console.error('Erro ao enviar o formulário:', error); } diff --git a/app/views/forums/index.html.erb b/app/views/forums/index.html.erb index 356a8bf..25db1bc 100644 --- a/app/views/forums/index.html.erb +++ b/app/views/forums/index.html.erb @@ -7,19 +7,19 @@
- +
- +
- +
-
+
{{ item.title }}
{{ item.date }} @@ -37,6 +37,7 @@ var posts = <%= @posts.map { |post| { id: post.id, title: post.title, body: post.body, - created_at: post.updated_at == post.created_at ? "Postado a #{time_ago_in_words(post.created_at)}" : "(Editado) #{time_ago_in_words(post.updated_at)}", - user_name: post.user.full_name } }.to_json.html_safe %>; + date: post.updated_at == post.created_at ? "Postado há #{time_ago_in_words(post.created_at)}" : "(Editado) #{time_ago_in_words(post.updated_at)}", + author: post.user.full_name } }.to_json.html_safe %>; + var errors = <%= { error: @errors }.to_json.html_safe %>; diff --git a/config/locales/forum.pt-BR.yml b/config/locales/forum.pt-BR.yml index 5e642d8..516d308 100644 --- a/config/locales/forum.pt-BR.yml +++ b/config/locales/forum.pt-BR.yml @@ -1,3 +1,5 @@ pt-BR: forum: title: Fórum + forums: + not_contributor: Você não é um colaborador desse projeto diff --git a/config/locales/models/post.pt-BR.yml b/config/locales/models/post.pt-BR.yml new file mode 100644 index 0000000..f036213 --- /dev/null +++ b/config/locales/models/post.pt-BR.yml @@ -0,0 +1,12 @@ +pt-BR: + activerecord: + models: + post: + one: Postagem + other: Postagens + attributes: + post: + title: Título + body: Corpo + posts: + not_contributor: Você não é um colaborador desse projeto. diff --git a/config/routes.rb b/config/routes.rb index d8cb6f8..df82d6f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -31,7 +31,7 @@ resources :calendars, only: %i[index] get 'forum', to: 'forums#index' - resources :posts, only: [:create] + resources :posts, only: %i[create update] end resources :user_roles, only: [] do diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index f41774c..ad61682 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -1,5 +1,25 @@ require 'rails_helper' RSpec.describe Post, type: :model do - pending "add some examples to (or delete) #{__FILE__}" + describe '#Valid?' do + context 'presence' do + it 'Título não pode ficar em branco' do + post = FactoryBot.build(:post, title: '') + + post.valid? + + expect(post.errors.full_messages).to include 'Título não pode ficar em branco' + expect(post.valid?).to eq false + end + + it 'Corpo não pode ficar em branco' do + post = FactoryBot.build(:post, body: '') + + post.valid? + + expect(post.errors.full_messages).to include 'Corpo não pode ficar em branco' + expect(post.valid?).to eq false + end + end + end end diff --git a/spec/requests/posts/user_creates_post_spec.rb b/spec/requests/posts/user_creates_post_spec.rb new file mode 100644 index 0000000..b46adc2 --- /dev/null +++ b/spec/requests/posts/user_creates_post_spec.rb @@ -0,0 +1,31 @@ +require 'rails_helper' + +describe 'Usuário cria postagem' do + it 'e é colaborador' do + user = create(:user) + project = create(:project, user:) + + login_as user, scope: :user + post project_posts_path(project), + params: { post: { title: 'Postagem inicial', body: 'Vamos abordar boas práticas' } } + + expect(Post.count).to eq 1 + expect(Post.last.title).to eq 'Postagem inicial' + expect(Post.last.body).to eq 'Vamos abordar boas práticas' + end + + it 'e não é colaborador' do + user = create(:user) + project = create(:project, user:) + not_colaborator = create(:user, cpf: '670.547.300-20') + create(:project, user: not_colaborator) + + login_as not_colaborator, scope: :user + post project_posts_path(project), + params: { post: { title: 'Postagem inicial', body: 'Vamos abordar boas práticas' } } + + expect(Post.count).to eq 0 + expect(response).to redirect_to root_path + expect(flash[:alert]).to eq 'Você não é um colaborador desse projeto.' + end +end diff --git a/spec/requests/posts/user_edits_post_spec.rb b/spec/requests/posts/user_edits_post_spec.rb new file mode 100644 index 0000000..e7cbb33 --- /dev/null +++ b/spec/requests/posts/user_edits_post_spec.rb @@ -0,0 +1,59 @@ +require 'rails_helper' + +describe 'Usuário edita postagem' do + it 'e é contribuinte autor da postagem' do + leader = create(:user) + project = create(:project, user: leader) + contributor = create(:user, email: 'contributor@email.com', cpf: '000.000.001-91') + contributor_role = project.user_roles.create!(user: contributor) + post = create(:post, project:, user_role: contributor_role, title: 'Postagem inicial') + + login_as contributor, scope: :user + patch(project_post_path(project, post), params: { post: { title: 'Postagem divertida' } }) + + expect(response).to redirect_to project_p_path(project, post) + expect(post.reload.title).to eq 'Postagem divertida' + end + + it 'e é contribuinte do projeto não autor da postagem' do + leader = create(:user) + project = create(:project, user: leader) + contributor = create(:user, email: 'contributor@email.com', cpf: '000.000.001-91') + project.user_roles.create!(user: contributor, role: :contributor) + post = create(:post, project:, user_role: leader.user_roles.first, title: 'Primeira postagem') + + login_as contributor, scope: :user + patch(project_post_path(project, post), params: { post: { title: 'Segunda postagem' } }) + + expect(post.reload.title).to eq 'Primeira postagem' + expect(response).to redirect_to project_project_path(project) + end + + it 'e é um líder não autor da postagem' do + leader = create(:user) + project = create(:project, user: leader) + contributor = create(:user, email: 'contributor@email.com', cpf: '000.000.001-91') + contributor_role = project.user_roles.create!(user: contributor) + post = create(:post, project:, user_role: contributor_role, title: 'Postagem inicial') + + login_as leader, scope: :user + patch(project_post_path(project, post), params: { post: { title: 'Postagem divertida' } }) + + expect(post.reload.title).to eq 'Postagem divertida' + expect(response).to redirect_to project_forum_path(project, post) + end + + it 'e é um administrador não autor da postagem' do + leader = create(:user) + project = create(:project, user: leader) + admin = create(:user, email: 'admin@email.com', cpf: '000.000.001-91') + project.user_roles.create!(user: admin, role: :admin) + post = create(:post, project:, user_role: leader.user_roles.first, title: 'Postagem inicial') + + login_as admin, scope: :user + patch(project_post_path(project, post), params: { post: { title: 'Postagem divertida' } }) + + expect(post.reload.title).to eq 'Postagem divertida' + expect(response).to redirect_to project_forum_path(project) + end +end diff --git a/spec/system/forums/user_creates_forum_spec.rb b/spec/system/forums/user_creates_forum_spec.rb new file mode 100644 index 0000000..b9559d5 --- /dev/null +++ b/spec/system/forums/user_creates_forum_spec.rb @@ -0,0 +1,21 @@ +require 'rails_helper' + +describe 'Usuário cria um novo tópico no forum' do + it 'a partir da tela do projeto' do + user = create(:user) + project = create(:project, user:) + + login_as(user, scope: :user) + visit project_path(project) + click_on 'Fórum' + fill_in 'title', with: 'Esse é meu jeito de viver' + fill_in 'body', with: 'De quem nunca que foi igual.' + click_on 'Postar' + + expect(Post.count).to eq 1 + expect(page).to have_content 'Esse é meu jeito de viver' + expect(page).to have_content 'De quem nunca que foi igual.' + expect(page).to have_content 'Postado a menos de um minuto' + expect(page).to have_content 'por Ash Ketchum' + end +end diff --git a/spec/system/forums/user_search_post_spec.rb b/spec/system/forums/user_search_post_spec.rb new file mode 100644 index 0000000..562ffb9 --- /dev/null +++ b/spec/system/forums/user_search_post_spec.rb @@ -0,0 +1,19 @@ +require 'rails_helper' + +describe 'Usuário vê página de fórum de um projeto' do + it 'a partir da tela inicial' do + user = create(:user) + project = create(:project, user:) + user_role = user.user_roles.first + create(:post, user_role:, project:, title: 'Postagem inicial') + create(:post, user_role:, project:, title: 'Como ajustar tarefas iniciadas') + + login_as(user, scope: :user) + visit project_path(project) + click_on 'Fórum' + fill_in 'Pesquisar', with: 'Como' + + expect(page).to have_content 'Como ajustar tarefas iniciadas' + expect(page).not_to have_content 'Postagem inicial' + end +end diff --git a/spec/system/forums/user_view_forum_spec.rb b/spec/system/forums/user_view_forum_spec.rb index d6e477a..16b7ae4 100644 --- a/spec/system/forums/user_view_forum_spec.rb +++ b/spec/system/forums/user_view_forum_spec.rb @@ -2,14 +2,15 @@ describe 'Usuário vê página de fórum de um projeto' do it 'a partir da tela inicial' do - travel_to Time.zone.local(2024, 11, 24, 1, 4, 44) user = create(:user) project = create(:project, user:) + user_role = user.user_roles.first + create(:post, user_role:, project:, title: 'Postagem inicial') login_as(user, scope: :user) visit project_path(project) - click_on 'Reuniões' + click_on 'Fórum' - expect(page).to have_content 'Fórum' + expect(page).to have_content 'Postagem inicial' end end From 695847db488e96f10499d5e4af74b8fff8d5f390 Mon Sep 17 00:00:00 2001 From: decoporteira Date: Thu, 15 Feb 2024 13:13:28 -0300 Subject: [PATCH 14/24] Implementa 'Postado agora' aos posts do forum. Co-authored-by: Adoniran France --- app/controllers/posts_controller.rb | 2 +- app/javascript/components/forum_vue.js | 1 - app/views/forums/index.html.erb | 4 ++-- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 4867a36..ebfecf6 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -22,7 +22,7 @@ def update private def json_response(post) - { id: post.id, title: post.title, body: post.body, author: post.user.full_name } + { id: post.id, title: post.title, body: post.body, author: post.user.full_name, date: 'Postado agora' } end def set_project diff --git a/app/javascript/components/forum_vue.js b/app/javascript/components/forum_vue.js index ad47418..e524850 100644 --- a/app/javascript/components/forum_vue.js +++ b/app/javascript/components/forum_vue.js @@ -39,7 +39,6 @@ export default { methods: { loadPosts(){ - console.log(window.posts) this.posts = window.posts.map(item => ({ id: item.id, title: item.title, body: item.body, diff --git a/app/views/forums/index.html.erb b/app/views/forums/index.html.erb index 25db1bc..c0d8e85 100644 --- a/app/views/forums/index.html.erb +++ b/app/views/forums/index.html.erb @@ -30,14 +30,14 @@
- + ss
From 3413e4425b5e2c4ea918f1296cff0a090cab51c8 Mon Sep 17 00:00:00 2001 From: decoporteira Date: Thu, 15 Feb 2024 15:11:26 -0300 Subject: [PATCH 15/24] =?UTF-8?q?Implementa=20testes=20de=20acesso=20de=20?= =?UTF-8?q?usu=C3=A1rios.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Adoniran France --- app/javascript/components/forum_vue.js | 2 +- app/views/forums/index.html.erb | 3 +- spec/requests/forum/user_view_forum_spec.rb | 49 +++++++++++++++ spec/requests/posts/user_edits_post_spec.rb | 59 ------------------- spec/system/forums/user_creates_forum_spec.rb | 1 + 5 files changed, 53 insertions(+), 61 deletions(-) create mode 100644 spec/requests/forum/user_view_forum_spec.rb delete mode 100644 spec/requests/posts/user_edits_post_spec.rb diff --git a/app/javascript/components/forum_vue.js b/app/javascript/components/forum_vue.js index e524850..25273ca 100644 --- a/app/javascript/components/forum_vue.js +++ b/app/javascript/components/forum_vue.js @@ -50,7 +50,7 @@ export default { }, async submitForm() { - try { + try { const csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content'); const response = await fetch(`/projects/${this.project.id}/posts`, { diff --git a/app/views/forums/index.html.erb b/app/views/forums/index.html.erb index c0d8e85..34a3a80 100644 --- a/app/views/forums/index.html.erb +++ b/app/views/forums/index.html.erb @@ -19,10 +19,11 @@
-
+
{{ item.title }}
{{ item.date }} + {{ index }}

{{ item.body }}

por {{ item.author }} diff --git a/spec/requests/forum/user_view_forum_spec.rb b/spec/requests/forum/user_view_forum_spec.rb new file mode 100644 index 0000000..0305a26 --- /dev/null +++ b/spec/requests/forum/user_view_forum_spec.rb @@ -0,0 +1,49 @@ +require 'rails_helper' + +describe 'Usuário acessa fórum' do + context 'e deve estar autenticado' do + it 'para acessar o index do forum' do + user = create(:user) + project = create(:project, user:) + + login_as user, scope: :user + + get project_forum_path(project) + + expect(response).to have_http_status :ok + end + + it 'e não está autenticado' do + user = create(:user) + project = create(:project, user:) + + get project_forum_path(project) + + expect(response).to redirect_to new_user_session_path + end + end + + context 'e deve ter acesso ao projeto' do + it 'para ver o index' do + leader = create(:user) + project = create(:project, user: leader) + + login_as(leader, scope: :user) + get project_forum_path(project) + + expect(response).to have_http_status :ok + end + + it 'e não é colaborador' do + leader = create(:user) + project = create(:project, user: leader) + not_contributor = create(:user, email: 'not_contributor@email.com', cpf: '000.000.001-91') + + login_as(not_contributor, scope: :user) + get project_forum_path(project) + + expect(response).to redirect_to root_path + expect(flash[:alert]).to eq 'Você não é um colaborador desse projeto' + end + end +end diff --git a/spec/requests/posts/user_edits_post_spec.rb b/spec/requests/posts/user_edits_post_spec.rb deleted file mode 100644 index e7cbb33..0000000 --- a/spec/requests/posts/user_edits_post_spec.rb +++ /dev/null @@ -1,59 +0,0 @@ -require 'rails_helper' - -describe 'Usuário edita postagem' do - it 'e é contribuinte autor da postagem' do - leader = create(:user) - project = create(:project, user: leader) - contributor = create(:user, email: 'contributor@email.com', cpf: '000.000.001-91') - contributor_role = project.user_roles.create!(user: contributor) - post = create(:post, project:, user_role: contributor_role, title: 'Postagem inicial') - - login_as contributor, scope: :user - patch(project_post_path(project, post), params: { post: { title: 'Postagem divertida' } }) - - expect(response).to redirect_to project_p_path(project, post) - expect(post.reload.title).to eq 'Postagem divertida' - end - - it 'e é contribuinte do projeto não autor da postagem' do - leader = create(:user) - project = create(:project, user: leader) - contributor = create(:user, email: 'contributor@email.com', cpf: '000.000.001-91') - project.user_roles.create!(user: contributor, role: :contributor) - post = create(:post, project:, user_role: leader.user_roles.first, title: 'Primeira postagem') - - login_as contributor, scope: :user - patch(project_post_path(project, post), params: { post: { title: 'Segunda postagem' } }) - - expect(post.reload.title).to eq 'Primeira postagem' - expect(response).to redirect_to project_project_path(project) - end - - it 'e é um líder não autor da postagem' do - leader = create(:user) - project = create(:project, user: leader) - contributor = create(:user, email: 'contributor@email.com', cpf: '000.000.001-91') - contributor_role = project.user_roles.create!(user: contributor) - post = create(:post, project:, user_role: contributor_role, title: 'Postagem inicial') - - login_as leader, scope: :user - patch(project_post_path(project, post), params: { post: { title: 'Postagem divertida' } }) - - expect(post.reload.title).to eq 'Postagem divertida' - expect(response).to redirect_to project_forum_path(project, post) - end - - it 'e é um administrador não autor da postagem' do - leader = create(:user) - project = create(:project, user: leader) - admin = create(:user, email: 'admin@email.com', cpf: '000.000.001-91') - project.user_roles.create!(user: admin, role: :admin) - post = create(:post, project:, user_role: leader.user_roles.first, title: 'Postagem inicial') - - login_as admin, scope: :user - patch(project_post_path(project, post), params: { post: { title: 'Postagem divertida' } }) - - expect(post.reload.title).to eq 'Postagem divertida' - expect(response).to redirect_to project_forum_path(project) - end -end diff --git a/spec/system/forums/user_creates_forum_spec.rb b/spec/system/forums/user_creates_forum_spec.rb index b9559d5..1c56e6c 100644 --- a/spec/system/forums/user_creates_forum_spec.rb +++ b/spec/system/forums/user_creates_forum_spec.rb @@ -10,6 +10,7 @@ click_on 'Fórum' fill_in 'title', with: 'Esse é meu jeito de viver' fill_in 'body', with: 'De quem nunca que foi igual.' + click_on 'Postar' expect(Post.count).to eq 1 From 6e66660c226a39df800cdce1dd8a8095a48c500c Mon Sep 17 00:00:00 2001 From: decoporteira Date: Thu, 15 Feb 2024 16:06:45 -0300 Subject: [PATCH 16/24] =?UTF-8?q?Implementa=20api=20para=20cria=C3=A7?= =?UTF-8?q?=C3=A3o=20de=20posts=20no=20f=C3=B3rum.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Adoniran France --- app/controllers/api/v1/posts_controller.rb | 39 +++++++++++++++++++ app/controllers/posts_controller.rb | 39 ------------------- app/javascript/components/forum_vue.js | 14 ++++--- app/views/forums/index.html.erb | 5 +-- config/routes.rb | 4 +- .../{ => api}/posts/user_creates_post_spec.rb | 9 +++-- spec/system/forums/user_creates_forum_spec.rb | 6 +-- 7 files changed, 60 insertions(+), 56 deletions(-) create mode 100644 app/controllers/api/v1/posts_controller.rb delete mode 100644 app/controllers/posts_controller.rb rename spec/requests/{ => api}/posts/user_creates_post_spec.rb (75%) diff --git a/app/controllers/api/v1/posts_controller.rb b/app/controllers/api/v1/posts_controller.rb new file mode 100644 index 0000000..de675d3 --- /dev/null +++ b/app/controllers/api/v1/posts_controller.rb @@ -0,0 +1,39 @@ +module Api + module V1 + class PostsController < Api::V1::ApiController + before_action :set_project + before_action :check_contributor + + def create + @post = @project.posts.build(post_params) + @post.user_role = UserRole.find_by(user: current_user, project: @project) + + return render status: :created, json: json_response(@post) if @post.save + + render json: { errors: @post.errors.full_messages }, status: :conflict + end + + private + + def json_response(post) + { id: post.id, title: post.title, body: post.body, author: post.user.full_name, date: 'Postado agora' } + end + + def set_project + @project = Project.find(params[:project_id]) + end + + def check_contributor + return if @project.member?(current_user) + + response = { error: 'Você não possui permissão.' } + + render status: :forbidden, json: response + end + + def post_params + params.require(:post).permit(:title, :body) + end + end + end +end diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb deleted file mode 100644 index ebfecf6..0000000 --- a/app/controllers/posts_controller.rb +++ /dev/null @@ -1,39 +0,0 @@ -class PostsController < ApplicationController - before_action :set_project - before_action :check_contributor - - def create - @post = @project.posts.build(post_params) - @post.user_role = UserRole.find_by(user: current_user, project: @project) - - render status: :created, json: json_response(@post) if @post.save! - end - - def update - @post = Post.find.params(:id) - if @post.update(post_params) - redirect_to project_forum_path(@project), notice: t('.success') - else - flash.now[:alert] = t('.fail') - render :edit, status: :unprocessable_entity - end - end - - private - - def json_response(post) - { id: post.id, title: post.title, body: post.body, author: post.user.full_name, date: 'Postado agora' } - end - - def set_project - @project = Project.find(params[:project_id]) - end - - def check_contributor - redirect_to root_path, alert: t('.not_contributor') unless @project.member?(current_user) - end - - def post_params - params.require(:post).permit(:title, :body) - end -end diff --git a/app/javascript/components/forum_vue.js b/app/javascript/components/forum_vue.js index 25273ca..2bbe3a2 100644 --- a/app/javascript/components/forum_vue.js +++ b/app/javascript/components/forum_vue.js @@ -11,7 +11,7 @@ export default { title: '', body: '' }, - errors: window.errors + errors: [] } }, @@ -51,22 +51,24 @@ export default { async submitForm() { try { - const csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content'); - const response = await fetch(`/projects/${this.project.id}/posts`, { + const response = await fetch(`/api/v1/projects/${this.project.id}/posts`, { method: 'POST', headers: { - 'Content-Type': 'application/json', - 'X-CSRF-Token': csrfToken + 'Content-Type': 'application/json' }, body: JSON.stringify(this.newPost) }); const data = await response.json() - console.log(data) + console.log(data.errors) this.posts.unshift(data) this.newPost.title = '' this.newPost.body = '' + if(data.errors.length > 0){ + this.errors = data.errors + } + } catch (error) { console.error('Erro ao enviar o formulário:', error); } diff --git a/app/views/forums/index.html.erb b/app/views/forums/index.html.erb index 34a3a80..06cc3b7 100644 --- a/app/views/forums/index.html.erb +++ b/app/views/forums/index.html.erb @@ -19,11 +19,10 @@
-
+
{{ item.title }}
{{ item.date }} - {{ index }}

{{ item.body }}

por {{ item.author }} @@ -31,7 +30,7 @@
- ss +
diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index e9e9efc..2d79f48 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -5,7 +5,6 @@ <%= csrf_meta_tags %> <%= csp_meta_tag %> - <%= csrf_meta_tags %> <%= stylesheet_link_tag "application", "data-turbo-track": "reload" %> <%= javascript_include_tag "application", "data-turbo-track": "reload", type: "module" %> <%= favicon_link_tag asset_path('favicon.png') %> diff --git a/config/locales/forum.pt-BR.yml b/config/locales/forum.pt-BR.yml index 516d308..89a36d1 100644 --- a/config/locales/forum.pt-BR.yml +++ b/config/locales/forum.pt-BR.yml @@ -3,3 +3,8 @@ pt-BR: title: Fórum forums: not_contributor: Você não é um colaborador desse projeto + search: Pesquisar + search_posts: Busque por postagens + post: Postar + posted_in: Postado há %{time} + no_posts: Não existem postagens. diff --git a/config/locales/models/post.pt-BR.yml b/config/locales/models/post.pt-BR.yml index f036213..fe7b6be 100644 --- a/config/locales/models/post.pt-BR.yml +++ b/config/locales/models/post.pt-BR.yml @@ -10,3 +10,5 @@ pt-BR: body: Corpo posts: not_contributor: Você não é um colaborador desse projeto. + posted_now: Postado agora + no_permission: Você não possui permissão. diff --git a/spec/system/forums/user_create_post_spec.rb b/spec/system/forums/user_create_post_spec.rb new file mode 100644 index 0000000..f2c63d8 --- /dev/null +++ b/spec/system/forums/user_create_post_spec.rb @@ -0,0 +1,25 @@ +require 'rails_helper' + +describe 'Usuário cria postagem' do + it 'com sucesso' do + user = create(:user) + project = create(:project, user:) + + login_as(user, scope: :user) + visit project_path(project) + page.refresh + click_on 'Fórum' + fill_in 'Título', with: 'Postagem inicial' + fill_in 'Corpo', with: 'Vamos falar sobre' + click_on 'Postar' + + page.refresh + + expect(page).to have_current_path project_forum_path(project) + expect(page).to have_content 'Fórum' + expect(page).to have_content 'Postagem inicial' + expect(page).to have_content 'Vamos falar sobre' + expect(page).to have_content "por #{user.full_name}" + expect(Post.count).to eq 1 + end +end From 61c48ebd856c0f288ca3f21127d5257b78bc2617 Mon Sep 17 00:00:00 2001 From: Adoniran Date: Fri, 16 Feb 2024 09:05:42 -0300 Subject: [PATCH 22/24] Altera status para unauthorized MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: André Pereira --- app/controllers/api/v1/posts_controller.rb | 2 +- spec/requests/api/posts/user_creates_post_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/v1/posts_controller.rb b/app/controllers/api/v1/posts_controller.rb index 29f3cf9..72d8a84 100644 --- a/app/controllers/api/v1/posts_controller.rb +++ b/app/controllers/api/v1/posts_controller.rb @@ -29,7 +29,7 @@ def check_contributor response = { error: I18n.t('posts.no_permission') } - render status: :forbidden, json: response + render status: :unauthorized, json: response end def post_params diff --git a/spec/requests/api/posts/user_creates_post_spec.rb b/spec/requests/api/posts/user_creates_post_spec.rb index 0e06caf..a607099 100644 --- a/spec/requests/api/posts/user_creates_post_spec.rb +++ b/spec/requests/api/posts/user_creates_post_spec.rb @@ -25,7 +25,7 @@ params: { post: { title: 'Postagem inicial', body: 'Vamos abordar boas práticas' } } expect(Post.count).to eq 0 - expect(response).to have_http_status :forbidden + expect(response).to have_http_status :unauthorized json_response = JSON.parse(response.body) expect(json_response['error']).to eq 'Você não possui permissão.' end From 86252837ae26c73421712f262a7db9b620e575df Mon Sep 17 00:00:00 2001 From: Adoniran Date: Fri, 16 Feb 2024 09:22:21 -0300 Subject: [PATCH 23/24] Refatora testes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: André Pereira --- spec/models/post_spec.rb | 8 ++----- spec/requests/forum/user_view_forum_spec.rb | 23 +++++---------------- spec/system/forums/user_search_post_spec.rb | 10 +++++---- 3 files changed, 13 insertions(+), 28 deletions(-) diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index ad61682..84cbed6 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -6,19 +6,15 @@ it 'Título não pode ficar em branco' do post = FactoryBot.build(:post, title: '') - post.valid? - - expect(post.errors.full_messages).to include 'Título não pode ficar em branco' expect(post.valid?).to eq false + expect(post.errors.full_messages).to include 'Título não pode ficar em branco' end it 'Corpo não pode ficar em branco' do post = FactoryBot.build(:post, body: '') - post.valid? - - expect(post.errors.full_messages).to include 'Corpo não pode ficar em branco' expect(post.valid?).to eq false + expect(post.errors.full_messages).to include 'Corpo não pode ficar em branco' end end end diff --git a/spec/requests/forum/user_view_forum_spec.rb b/spec/requests/forum/user_view_forum_spec.rb index 0305a26..bd61cdf 100644 --- a/spec/requests/forum/user_view_forum_spec.rb +++ b/spec/requests/forum/user_view_forum_spec.rb @@ -1,26 +1,13 @@ require 'rails_helper' describe 'Usuário acessa fórum' do - context 'e deve estar autenticado' do - it 'para acessar o index do forum' do - user = create(:user) - project = create(:project, user:) + it 'e não está autenticado' do + user = create(:user) + project = create(:project, user:) - login_as user, scope: :user + get project_forum_path(project) - get project_forum_path(project) - - expect(response).to have_http_status :ok - end - - it 'e não está autenticado' do - user = create(:user) - project = create(:project, user:) - - get project_forum_path(project) - - expect(response).to redirect_to new_user_session_path - end + expect(response).to redirect_to new_user_session_path end context 'e deve ter acesso ao projeto' do diff --git a/spec/system/forums/user_search_post_spec.rb b/spec/system/forums/user_search_post_spec.rb index 66d513c..04c2b5c 100644 --- a/spec/system/forums/user_search_post_spec.rb +++ b/spec/system/forums/user_search_post_spec.rb @@ -1,12 +1,13 @@ require 'rails_helper' -describe 'Usuário vê página de fórum de um projeto' do - it 'a partir da tela inicial' do +describe 'Usuário faz pesquisa' do + it 'e vê postagens filtradas' do user = create(:user) project = create(:project, user:) user_role = user.user_roles.first create(:post, user_role:, project:, title: 'Postagem inicial') - create(:post, user_role:, project:, title: 'Como ajustar tarefas iniciadas') + create(:post, user_role:, project:, title: '5 passos da reunião', body: 'Já se perguntou como iniciar?') + create(:post, user_role:, project:, title: 'Como ajustar tarefas iniciadas?') login_as(user, scope: :user) visit project_path(project) @@ -14,10 +15,11 @@ fill_in 'Pesquisar', with: 'Como' expect(page).to have_content 'Como ajustar tarefas iniciadas' + expect(page).to have_content '5 passos da reunião' expect(page).not_to have_content 'Postagem inicial' end - it 'a partir da tela inicial' do + it 'e não há postagem para pesquisa' do user = create(:user) project = create(:project, user:) user_role = user.user_roles.first From bb4baa213ad7fa3ca603ed4a5c8f489875c475f4 Mon Sep 17 00:00:00 2001 From: Adoniran Date: Fri, 16 Feb 2024 09:27:51 -0300 Subject: [PATCH 24/24] =?UTF-8?q?Resolve=20bug=20que=20antes=20n=C3=A3o=20?= =?UTF-8?q?incluia=20na=20pesquisa=20a=20postagem=20rec=C3=A9m=20criada?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: André Pereira --- app/javascript/components/forum_vue.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/javascript/components/forum_vue.js b/app/javascript/components/forum_vue.js index 4ad45c7..34cdff7 100644 --- a/app/javascript/components/forum_vue.js +++ b/app/javascript/components/forum_vue.js @@ -64,7 +64,7 @@ export default { const data = await response.json() if (response.ok) { - this.filteredPosts.unshift(data); + this.posts.unshift(data); this.newPost.title = '' this.newPost.body = '' } else {