Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12046 from krauselukas/refactoring/retire_comment…
…_presenter Refactor comments presenter to a view component
- Loading branch information
Showing
8 changed files
with
99 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
@builder.comment_(attributes) do | ||
@builder.text(body) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
30 changes: 0 additions & 30 deletions
30
src/api/app/presenters/comments_controller_presenters/comment_presenter.rb
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
30 changes: 0 additions & 30 deletions
30
src/api/spec/presenters/comments_controller_presenters/comment_presenter_spec.rb
This file was deleted.
Oops, something went wrong.