Skip to content

Commit

Permalink
Merge pull request #12046 from krauselukas/refactoring/retire_comment…
Browse files Browse the repository at this point in the history
…_presenter

Refactor comments presenter to a view component
  • Loading branch information
Dany Marcoux committed Mar 17, 2022
2 parents 4ffe659 + 919c849 commit 905729c
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 67 deletions.
31 changes: 31 additions & 0 deletions src/api/app/components/comment_component.rb
@@ -0,0 +1,31 @@
class CommentComponent < ApplicationComponent
def initialize(comment:, obj_is_user:, builder:)
super

@comment = comment
@obj_is_user = obj_is_user
@builder = builder
end

def user?
@obj_is_user
end

def attributes
attrs = { who: comment.user.login, when: comment.created_at, id: comment.id }
if user?
attrs[comment.commentable.class.name.downcase.to_sym] = comment.commentable.to_param
attrs[:project] = comment.commentable.project if comment.commentable.is_a?(Package)
end
attrs[:parent] = comment.parent_id if comment.parent_id
attrs
end

def body
comment.body.delete("\u0000")
end

private

attr_accessor :comment
end
3 changes: 3 additions & 0 deletions src/api/app/components/comment_component.xml.builder
@@ -0,0 +1,3 @@
@builder.comment_(attributes) do
@builder.text(body)
end
5 changes: 5 additions & 0 deletions src/api/app/components/comment_component_preview.rb
@@ -0,0 +1,5 @@
class CommentComponentPreview < ViewComponent::Preview
def with_default_content
render(CommentComponent.new(comment: create(:comment), obj_is_user: create(:confirmed_user)))
end
end

This file was deleted.

5 changes: 0 additions & 5 deletions src/api/app/views/comments/_comment.xml.builder

This file was deleted.

3 changes: 1 addition & 2 deletions src/api/app/views/comments/index.xml.builder
@@ -1,4 +1,3 @@
xml.comments(@header) do
render(partial: 'comment', collection: @comments, locals: { obj_is_user: @obj.is_a?(User),
builder: xml })
render(CommentComponent.with_collection(@comments, obj_is_user: @obj.is_a?(User), builder: xml))
end
59 changes: 59 additions & 0 deletions src/api/spec/components/comment_component_spec.rb
@@ -0,0 +1,59 @@
require 'rails_helper'

RSpec.describe CommentComponent, type: :component do
let(:builder) { Builder::XmlMarkup.new }

before do
render_inline(described_class.new(comment: comment, obj_is_user: true, builder: builder))
end

shared_examples 'rendering the body, who, when and the id' do
it "renders the comment's body" do
expect(builder).to have_text(comment.body)
end

it "renders who commented as an attribute of the comment's tag" do
expect(builder).to have_xpath("//comment_[contains(@who, '#{comment.user.login}')]")
end

it "renders when the comment was commented as an attribute of the comment's tag" do
expect(builder).to have_xpath("//comment_[contains(@when, '#{comment.created_at}')]")
end

it 'renders the id of the comment' do
expect(builder).to have_xpath("//comment_[contains(@id, '#{comment.id}')]")
end
end

context 'when the comment has a user, the comment creation date' do
context 'and the commentable is a Project' do
let(:comment) { create(:comment_project) }

it_behaves_like 'rendering the body, who, when and the id'

it 'renders the project of the comment' do
expect(builder).to have_xpath("//comment_[contains(@project, '#{comment.commentable.name}')]")
end
end

context 'and the commentable is a Package' do
let(:comment) { create(:comment_package) }

it_behaves_like 'rendering the body, who, when and the id'

it 'renders the package name of the commenter' do
expect(builder).to have_xpath("//comment_[contains(@package, '#{comment.commentable.name}')]")
end
end

context 'and the commentable is Request' do
let(:comment) { create(:comment_request) }

it_behaves_like 'rendering the body, who, when and the id'

it 'renders the request number of the commenter' do
expect(builder).to have_xpath("//comment_[contains(@bsrequest, '#{comment.commentable.number}')]")
end
end
end
end

This file was deleted.

0 comments on commit 905729c

Please sign in to comment.