Skip to content

Commit

Permalink
Use render partial to render a collection of comments
Browse files Browse the repository at this point in the history
Move model rendering to the view, lift some view code to a presenter
  • Loading branch information
vpereira committed Apr 25, 2019
1 parent 79bdd16 commit 6024dbb
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 52 deletions.
14 changes: 0 additions & 14 deletions src/api/app/models/comment.rb
Expand Up @@ -64,20 +64,6 @@ def involved_users
users.to_a
end

def to_xml(builder, include_commentable = false)
attrs = { who: user, when: created_at, id: id }
if include_commentable
attrs[commentable.class.name.downcase] = commentable.to_param
attrs['project'] = commentable.project if commentable.is_a?(Package)
end
attrs[:parent] = parent_id if parent_id
body.delete!("\u0000")

builder.comment_(attrs) do
builder.text(body)
end
end

def blank_or_destroy
if children.exists?
self.body = 'This comment has been deleted'
Expand Down
@@ -0,0 +1,30 @@
module CommentsControllerPresenters
class CommentPresenter
def initialize(comment, obj_is_user)
@comment = comment
@obj_is_user = obj_is_user
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
end
5 changes: 5 additions & 0 deletions src/api/app/views/comments/_comment.xml.builder
@@ -0,0 +1,5 @@
comment_presenter = CommentsControllerPresenters::CommentPresenter.new(comment, obj_is_user)

builder.comment_(comment_presenter.attributes) do
builder.text(comment_presenter.body)
end
6 changes: 2 additions & 4 deletions src/api/app/views/comments/index.xml.builder
@@ -1,6 +1,4 @@
obj_is_user = @obj.is_a?(User)
xml.comments(@header) do
@comments.each do |comment|
comment.to_xml(xml, obj_is_user)
end
render(partial: 'comment', collection: @comments, locals: { obj_is_user: @obj.is_a?(User),
builder: xml })
end
34 changes: 0 additions & 34 deletions src/api/spec/models/comment_spec.rb
Expand Up @@ -35,40 +35,6 @@
}
end

describe 'to_xml' do
let(:builder) { Nokogiri::XML::Builder.new }
let(:comment_element) { builder.doc.css('comment') }

context 'without parent' do
before do
comment_package.to_xml(builder)
end

it 'creates xml with correct attributes and content' do
expect(comment_element.attribute('id').value).to eq(comment_package.id.to_s)
expect(Time.parse(comment_element.attribute('when').value)).to eq(comment_package.created_at)
expect(comment_element.attribute('who').value).to eq(comment_package.user.login)

expect(comment_element.text).to match(/^#<Nokogiri::XML::Builder::NodeBuilder:0x\h+>$/)
end
end

context 'with parent' do
before do
comment_package_with_parent.to_xml(builder)
end

it 'creates xml with correct attributes and content' do
expect(comment_element.attribute('id').value).to eq(comment_package_with_parent.id.to_s)
expect(Time.parse(comment_element.attribute('when').value)).to eq(comment_package_with_parent.created_at)
expect(comment_element.attribute('who').value).to eq(comment_package_with_parent.user.login)
expect(comment_element.attribute('parent').value).to eq(comment_package_with_parent.parent_id.to_s)

expect(comment_element.text).to match(/^#<Nokogiri::XML::Builder::NodeBuilder:0x\h+>$/)
end
end
end

describe 'blank_or_destroy' do
context 'without children' do
before do
Expand Down
@@ -0,0 +1,27 @@
require 'rails_helper'

RSpec.describe ::CommentsControllerPresenters::CommentPresenter do
let(:user) { create(:confirmed_user, login: 'tom') }
let(:comment_project) { create(:comment_project, user: user) }
let(:comment_package) { create(:comment_package, user: user) }

describe 'Commenter is an User' do
context 'for a project' do
let(:comment_presenter) { CommentsControllerPresenters::CommentPresenter.new(comment_project, true) }
it { expect(comment_presenter.attributes).to include(who: 'tom') }
it { expect(comment_presenter.attributes).to include(id: comment_project.id) }
it { expect(comment_presenter.attributes).to include(:project) }
end

context 'for a package' do
let(:comment_presenter) { CommentsControllerPresenters::CommentPresenter.new(comment_package, true) }
it { expect(comment_presenter.attributes).to include(:project) }
it { expect(comment_presenter.attributes).to include(:package) }
end
end

describe 'Commenter isn\'t an user' do
let(:comment_presenter) { CommentsControllerPresenters::CommentPresenter.new(comment_package, false) }
it { expect(comment_presenter.attributes).not_to include(:project) }
end
end

0 comments on commit 6024dbb

Please sign in to comment.