Skip to content

Commit

Permalink
Refactor comments presenter to a view component
Browse files Browse the repository at this point in the history
We've decided to use view components in favor of presenters.

Fixes #11559
  • Loading branch information
krauselukas authored and danidoni committed Mar 17, 2022
1 parent 4ffe659 commit 9744099
Show file tree
Hide file tree
Showing 6 changed files with 35 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

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

This file was deleted.

0 comments on commit 9744099

Please sign in to comment.