From 90c72cf3d9345b0cadfd642ea7be3c6202af01de Mon Sep 17 00:00:00 2001 From: Moises Almeida Date: Tue, 13 Feb 2024 15:57:06 -0300 Subject: [PATCH 01/15] WIP: Usuario visualiza comentario MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Mateus Cavedini Co-authored-by: Valéria Carneiro --- app/javascript/components/forum_vue.js | 6 +++-- app/models/comment.rb | 4 ++++ app/views/forums/index.html.erb | 5 +++-- db/migrate/20240213182535_create_comments.rb | 11 ++++++++++ db/schema.rb | 14 +++++++++++- db/seeds.rb | 4 ++++ spec/factories/comments.rb | 7 ++++++ spec/models/comment_spec.rb | 5 +++++ .../user_views_forum_post_comment_spec.rb | 22 +++++++++++++++++++ spec/system/forums/user_view_forum_spec.rb | 1 - 10 files changed, 73 insertions(+), 6 deletions(-) create mode 100644 app/models/comment.rb create mode 100644 db/migrate/20240213182535_create_comments.rb create mode 100644 spec/factories/comments.rb create mode 100644 spec/models/comment_spec.rb create mode 100644 spec/system/forums/comments/user_views_forum_post_comment_spec.rb diff --git a/app/javascript/components/forum_vue.js b/app/javascript/components/forum_vue.js index 7b5cfb6..3160732 100644 --- a/app/javascript/components/forum_vue.js +++ b/app/javascript/components/forum_vue.js @@ -18,7 +18,7 @@ export default { }, computed:{ - filteredProjects() { + filteredPosts() { const searchType = this.selectedFilter return this.posts.filter(post => { if (searchType === '') { @@ -36,6 +36,8 @@ export default { methods: { insertMessage() { this.message = this.insertText; - } + }, + + showPostDetails(){} } } diff --git a/app/models/comment.rb b/app/models/comment.rb new file mode 100644 index 0000000..73ff86a --- /dev/null +++ b/app/models/comment.rb @@ -0,0 +1,4 @@ +class Comment < ApplicationRecord + belongs_to :post + belongs_to :user_role +end diff --git a/app/views/forums/index.html.erb b/app/views/forums/index.html.erb index 9c61fac..ce3b527 100644 --- a/app/views/forums/index.html.erb +++ b/app/views/forums/index.html.erb @@ -7,9 +7,9 @@
-
+
-
{{ item.title }}
+
{{ item.title }}
{{ item.date }}

{{ item.body }}

@@ -21,6 +21,7 @@
+ From c0b1dbb6d5c65be5720e6f310fe4146ed6f620d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Val=C3=A9ria=20Carneiro?= Date: Tue, 13 Feb 2024 18:25:05 -0300 Subject: [PATCH 03/15] =?UTF-8?q?Adiciona=20exibi=C3=A7=C3=A3o=20de=20come?= =?UTF-8?q?nt=C3=A1rios?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/javascript/components/forum_vue.js | 21 +++++++++++++-------- app/models/user_role.rb | 1 + app/views/forums/index.html.erb | 22 +++++++++++++++++++++- db/schema.rb | 8 ++++++++ db/seeds.rb | 3 ++- 5 files changed, 45 insertions(+), 10 deletions(-) diff --git a/app/javascript/components/forum_vue.js b/app/javascript/components/forum_vue.js index ddc65a2..24b2154 100644 --- a/app/javascript/components/forum_vue.js +++ b/app/javascript/components/forum_vue.js @@ -6,17 +6,21 @@ export default { searchText: '', selectedFilter: '', selectedPostId: null, + selectedPost: null, project: window.project, posts: [], comments: [] } }, - mounted() { - this.posts = window.posts.map(item => ({ title: item.title, + async mounted() { + this.posts = window.posts.map(item => ({ id: item.id, + title: item.title, body: item.body, author: item.user_name, - date: item.created_at })); + date: item.created_at, + comments: item.comments })); + await showPostDetails; }, computed:{ @@ -34,18 +38,19 @@ export default { }) } }, + methods: { - insertMessage() { + async insertMessage() { this.message = this.insertText; }, - showPostDetails(post_id) { - this.selectedPostId = post_id - selectedPost = this.posts.filter(post => post.id == post_id) + async showPostDetails(id) { + this.selectedPostId = id; - const comments = this.comments.filter(comment => comment.post_id === post_id); + this.selectedPost = this.posts.find(post => post.id === id); + this.comments = this.selectedPost.comments } } } diff --git a/app/models/user_role.rb b/app/models/user_role.rb index 63e5c9d..42c4668 100644 --- a/app/models/user_role.rb +++ b/app/models/user_role.rb @@ -3,6 +3,7 @@ class UserRole < ApplicationRecord belongs_to :project has_many :meeting_participants, dependent: :destroy has_many :meetings, through: :meeting_participants + has_many :comments, dependent: :destroy enum role: { contributor: 1, admin: 5, leader: 9 } diff --git a/app/views/forums/index.html.erb b/app/views/forums/index.html.erb index 6779065..002d6d4 100644 --- a/app/views/forums/index.html.erb +++ b/app/views/forums/index.html.erb @@ -16,6 +16,20 @@ por {{ item.author }}
+ +
+
+
+
+
{{ item.content }}
+ {{ item.created_at }} +
+

{{ item.content }}

+ por {{ item.author }} +
+
+
+ @@ -27,7 +41,13 @@ var posts = <%= @posts.map { |post| { id: post.id, title: post.title, body: post.body, - comments: post.comments, + comments: post.comments.map { |comment| + { + id: comment.id, + content: comment.content, + author: comment.user_role.user.full_name, + } + }, created_at: post.updated_at == post.created_at ? "Postado em #{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 %>; diff --git a/db/schema.rb b/db/schema.rb index 1c81a13..1335fbc 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -61,6 +61,13 @@ t.index ["user_id"], name: "index_documents_on_user_id" end + create_table "forums", force: :cascade do |t| + t.integer "project_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["project_id"], name: "index_forums_on_project_id" + end + create_table "invitations", force: :cascade do |t| t.integer "project_id", null: false t.integer "profile_id", null: false @@ -291,6 +298,7 @@ add_foreign_key "comments", "user_roles" add_foreign_key "documents", "projects" add_foreign_key "documents", "users" + add_foreign_key "forums", "projects" add_foreign_key "invitations", "projects" add_foreign_key "meeting_participants", "meetings" add_foreign_key "meeting_participants", "user_roles" diff --git a/db/seeds.rb b/db/seeds.rb index 9920dae..c96277c 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -178,4 +178,5 @@ -FactoryBot.create(:post, user_role: ash.user_roles.find_by(project: pikachu_project), project: pikachu_project) +post = FactoryBot.create(:post, user_role: ash.user_roles.find_by(project: pikachu_project), project: pikachu_project) +FactoryBot.create(:comment, user_role: ash.user_roles.find_by(project: pikachu_project), content: 'Bom trabalho!!', post: post) From ae4881f7ce248957f0ffe776dcef840e9517634b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Val=C3=A9ria=20Carneiro?= Date: Wed, 14 Feb 2024 10:48:42 -0300 Subject: [PATCH 04/15] =?UTF-8?q?Refatora=20for=C3=BAm=20e=20adiciona=20no?= =?UTF-8?q?vos=20cen=C3=A1rios=20de=20teste?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Mateus Cavedini Co-authored-by: Moises Almeida --- app/javascript/components/forum_vue.js | 42 +++++++++++-------- app/views/forums/index.html.erb | 25 +++++++---- app/views/shared/_project_header.html.erb | 3 +- db/seeds.rb | 2 + .../user_views_forum_post_comment_spec.rb | 25 ++++++++++- 5 files changed, 69 insertions(+), 28 deletions(-) diff --git a/app/javascript/components/forum_vue.js b/app/javascript/components/forum_vue.js index 24b2154..eefc1da 100644 --- a/app/javascript/components/forum_vue.js +++ b/app/javascript/components/forum_vue.js @@ -5,26 +5,22 @@ export default { return { searchText: '', selectedFilter: '', - selectedPostId: null, selectedPost: null, project: window.project, posts: [], - comments: [] + comments: [], + activePage: 'postsIndex' } }, - async mounted() { - this.posts = window.posts.map(item => ({ id: item.id, - title: item.title, - body: item.body, - author: item.user_name, - date: item.created_at, - comments: item.comments })); - await showPostDetails; + mounted() { + this.loadPosts(); }, computed:{ filteredPosts() { + this.resetPostDetails(); + const searchType = this.selectedFilter return this.posts.filter(post => { if (searchType === '') { @@ -39,18 +35,30 @@ export default { } }, - methods: { - async insertMessage() { - this.message = this.insertText; - }, + loadPosts(){ + this.posts = window.posts.map(item => ({ id: item.id, + title: item.title, + body: item.body, + author: item.user_name, + date: item.created_at, + comments: item.comments })); - async showPostDetails(id) { - this.selectedPostId = id; + this.activePage = 'postsIndex' + }, + showPostDetails(id) { this.selectedPost = this.posts.find(post => post.id === id); this.comments = this.selectedPost.comments + + this.activePage = 'postDetails' + }, + + resetPostDetails(){ + this.selectedPostId = null; + + this.selectedPost = null; } } -} +} \ No newline at end of file diff --git a/app/views/forums/index.html.erb b/app/views/forums/index.html.erb index 002d6d4..49e0461 100644 --- a/app/views/forums/index.html.erb +++ b/app/views/forums/index.html.erb @@ -1,8 +1,8 @@ <%= render 'shared/project_header', project: @project %>
- {{ project }} +
-
+
@@ -16,20 +16,28 @@ por {{ item.author }}
+
+ +
+
+

{{ selectedPost.title }}

+

{{ selectedPost.body }}

+
-
-
+
+

Comentários:

-
{{ item.content }}
+

{{ item.content }}

+ {{ item.created_at }}
-

{{ item.content }}

por {{ item.author }}
+
+
-
@@ -46,9 +54,10 @@ id: comment.id, content: comment.content, author: comment.user_role.user.full_name, + created_at: comment.updated_at == comment.created_at ? "Postado em #{time_ago_in_words(comment.created_at)}" : "(Editado) #{time_ago_in_words(comment.updated_at)}", } }, created_at: post.updated_at == post.created_at ? "Postado em #{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 %>; - + \ No newline at end of file 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), diff --git a/db/seeds.rb b/db/seeds.rb index c96277c..81fc75f 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -180,3 +180,5 @@ post = FactoryBot.create(:post, user_role: ash.user_roles.find_by(project: pikachu_project), project: pikachu_project) FactoryBot.create(:comment, user_role: ash.user_roles.find_by(project: pikachu_project), content: 'Bom trabalho!!', post: post) +FactoryBot.create(:comment, user_role: ash.user_roles.find_by(project: pikachu_project), content: 'Muito bom!', post: post) + diff --git a/spec/system/forums/comments/user_views_forum_post_comment_spec.rb b/spec/system/forums/comments/user_views_forum_post_comment_spec.rb index 808ecb9..9efc5e5 100644 --- a/spec/system/forums/comments/user_views_forum_post_comment_spec.rb +++ b/spec/system/forums/comments/user_views_forum_post_comment_spec.rb @@ -15,8 +15,29 @@ click_on 'Fórum' click_on 'Post Top' - expect(page).to have_content 'Comentários' + expect(page).to have_content 'Comentários:' expect(page).to have_content 'Comentário top' - expect(page).to have_content "Autor: #{leader.full_name}" + expect(page).to have_content "por #{leader.full_name}" end + + it 'e não tem nenhum comentário' do + leader = create :user, email: 'leader@email.com' + project = create :project, user: leader, title: 'Projeto Top' + leader_role = UserRole.last + create :post, user_role: leader_role, project:, title: 'Post Top' + + login_as leader + visit root_path + click_on 'Projetos' + click_on 'Projeto Top' + click_on 'Fórum' + click_on 'Post Top' + + expect(page).to have_content 'Comentários:' + expect(page).to have_content 'Seja o primeiro a comentar' + expect(page).to have_button 'Comentar' + expect(page).to have_field 'Insira seu comentário' + end + + xit 'e não vê comentários de outras publicações' end From e2532327ec7f5464d40f3d1e95ece7007f555f32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Val=C3=A9ria=20Carneiro?= Date: Wed, 14 Feb 2024 11:55:31 -0300 Subject: [PATCH 05/15] =?UTF-8?q?Adiciona=20formul=C3=A1rio=20para=20cria?= =?UTF-8?q?=C3=A7=C3=A3o=20de=20coment=C3=A1rios?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Mateus Cavedini Co-authored-by: Moises Almeida --- app/javascript/components/forum_vue.js | 9 ++++++++- app/views/forums/index.html.erb | 20 +++++++++++++++---- .../user_views_forum_post_comment_spec.rb | 2 +- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/app/javascript/components/forum_vue.js b/app/javascript/components/forum_vue.js index eefc1da..7223810 100644 --- a/app/javascript/components/forum_vue.js +++ b/app/javascript/components/forum_vue.js @@ -9,7 +9,10 @@ export default { project: window.project, posts: [], comments: [], - activePage: 'postsIndex' + activePage: 'postsIndex', + newComment: { + content: '' + } } }, @@ -54,6 +57,10 @@ export default { this.activePage = 'postDetails' }, + + async createComment() { + + }, resetPostDetails(){ this.selectedPostId = null; diff --git a/app/views/forums/index.html.erb b/app/views/forums/index.html.erb index 49e0461..d7bca18 100644 --- a/app/views/forums/index.html.erb +++ b/app/views/forums/index.html.erb @@ -26,7 +26,7 @@

Comentários:

-
+

{{ item.content }}

@@ -34,9 +34,21 @@
por {{ item.author }}
-
-
- +
+

Seja o primeiro a comentar

+
+
+
+ + +
+
+ +
+
+
+ +
diff --git a/spec/system/forums/comments/user_views_forum_post_comment_spec.rb b/spec/system/forums/comments/user_views_forum_post_comment_spec.rb index 9efc5e5..5a9b706 100644 --- a/spec/system/forums/comments/user_views_forum_post_comment_spec.rb +++ b/spec/system/forums/comments/user_views_forum_post_comment_spec.rb @@ -36,7 +36,7 @@ expect(page).to have_content 'Comentários:' expect(page).to have_content 'Seja o primeiro a comentar' expect(page).to have_button 'Comentar' - expect(page).to have_field 'Insira seu comentário' + expect(page).to have_field 'Conteúdo:' end xit 'e não vê comentários de outras publicações' From 5d68fa9de4d764ce5375790635541c9cb42c2029 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Val=C3=A9ria=20Carneiro?= Date: Wed, 14 Feb 2024 17:55:44 -0300 Subject: [PATCH 06/15] =?UTF-8?q?Adiciona=20teste=20para=20cria=C3=A7?= =?UTF-8?q?=C3=A3o=20de=20coment=C3=A1rio?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Moises Almeida Co-authored-by: Mateus Cavedini --- app/controllers/comments_controller.rb | 3 +++ app/javascript/components/forum_vue.js | 26 ++++++++++++++++++ app/views/forums/index.html.erb | 3 +-- config/routes.rb | 1 + spec/factories/posts.rb | 2 +- .../comments/user_comments_forum_post_spec.rb | 27 +++++++++++++++++++ .../user_views_forum_post_comment_spec.rb | 15 ++++++++++- .../posts/user_views_post_details_spec.rb | 23 ++++++++++++++++ 8 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 app/controllers/comments_controller.rb create mode 100644 spec/system/forums/comments/user_comments_forum_post_spec.rb create mode 100644 spec/system/forums/posts/user_views_post_details_spec.rb diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb new file mode 100644 index 0000000..5014f1d --- /dev/null +++ b/app/controllers/comments_controller.rb @@ -0,0 +1,3 @@ +class CommentsController < ApplicationController + def create; end +end diff --git a/app/javascript/components/forum_vue.js b/app/javascript/components/forum_vue.js index 7223810..ae97c04 100644 --- a/app/javascript/components/forum_vue.js +++ b/app/javascript/components/forum_vue.js @@ -59,13 +59,39 @@ export default { }, async createComment() { + try { + const response = await fetch('/comments', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify(this.newComment) + }); + if (!response.ok){ + throw new Error('Erro ao criar comentário'); + } + + this.newComment = await response.json(); + + this.comments.push(newComment) + + this.newComment.content = '' + + console.log('Comentário criado com sucesso:', newComment); + } catch (error) { + console.error('Erro ao criar comentário:', error); + } }, resetPostDetails(){ this.selectedPostId = null; this.selectedPost = null; + }, + + createComment(){ + } } } \ No newline at end of file diff --git a/app/views/forums/index.html.erb b/app/views/forums/index.html.erb index d7bca18..8e13d60 100644 --- a/app/views/forums/index.html.erb +++ b/app/views/forums/index.html.erb @@ -40,7 +40,7 @@
- +
@@ -55,7 +55,6 @@
-