Skip to content

Commit

Permalink
Change DevComment#render to utilize html.erb (forem#2554) [ci skip]
Browse files Browse the repository at this point in the history
  • Loading branch information
mariocsee authored and maestromac committed May 2, 2019
1 parent e90bb2b commit 96a0235
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 68 deletions.
65 changes: 9 additions & 56 deletions app/liquid_tags/dev_comment_tag.rb
Original file line number Diff line number Diff line change
@@ -1,67 +1,20 @@
class DevCommentTag < LiquidTagBase
attr_reader :id_code, :comment
PARTIAL = "comments/liquid".freeze

def initialize(_tag_name, id_code, _tokens)
@id_code = parse_id(id_code)
@comment = find_comment
@comment = find_comment(id_code.strip)
end

def render(_context)
raise_error unless @comment
<<-HTML
<div class="liquid-comment">
<div class="details">
<a href="/#{@comment.user.username}">
<img class="profile-pic" src="#{ProfileImage.new(@comment.user).get(50)}"
alt="#{@comment.user.username} profile image"/>
</a>
<a href="/#{@comment.user.username}">
<span class="comment-username">#{@comment.user.name}</span>
</a>
<div class="comment-date" data-published-timestamp="#{@comment.decorate.published_timestamp}">
<a href="#{@comment.path}">#{@comment.readable_publish_date}</a>
</div>
</div>
<div class="body">
#{@comment.processed_html.html_safe}
</div>
</div>
HTML
ActionController::Base.new.render_to_string(
partial: PARTIAL,
locals: { comment: @comment },
)
end

def render_twitter_and_github
result = ""
if @comment.user.twitter_username.present?
result += "<a href=\"https://twitter.com/#{@comment.user.twitter_username}\">" \
+image_tag("/assets/twitter-logo.svg", class: "icon-img", alt: "twitter") + \
"</a>"
end
return if @comment.user.github_username.blank?

result + "<a href=\"https://github.com/#{@comment.user.github_username}\">" \
+image_tag("/assets/github-logo.svg", class: "icon-img", alt: "github") + \
"</a>"
end

private

def parse_id(id)
id_no_space = id.delete(" ")
raise_error unless valid_id?(id_no_space)
id_no_space
end

def find_comment
comment = Comment.find_by(id: @id_code.to_i(26))
raise_error unless comment
comment
end

def valid_id?(id)
id.length < 10 && id =~ /^[a-zA-Z0-9]*$/
end

def raise_error
def find_comment(id_code)
Comment.find(id_code.to_i(26))
rescue ActiveRecord::RecordNotFound
raise StandardError, "Invalid comment ID or comment does not exist"
end
end
Expand Down
26 changes: 26 additions & 0 deletions app/views/comments/_liquid.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<div class="liquid-comment">
<div class="details">
<a href="/<%= comment.user.username %>">
<img class="profile-pic" src="<%= ProfileImage.new(comment.user).get(50) %>" alt="<%= comment.user.username %> profile image"/>
</a>
<a href="/<%= comment.user.username %>">
<span class="comment-username"><%= comment.user.name %></span>
</a>
<% if comment.user.twitter_username.present? %>
<a href="https://twitter.com/<%= comment.user.twitter_username %>">
<img src="/assets/twitter-logo.svg" class="icon-img" alt="twitter"/>
</a>
<% end %>
<% if comment.user.github_username.present? %>
<a href="https://github.com/<%= comment.user.github_username %>">
<img src="/assets/github-logo.svg" class="icon-img" alt="github"/>
</a>
<% end %>
<div class="comment-date" data-published-timestamp="<%= comment.decorate.published_timestamp %>">
<a href="<%= comment.path %>"><%= comment.readable_publish_date %></a>
</div>
</div>
<div class="body">
<%= comment.processed_html.html_safe %>
</div>
</div>
20 changes: 8 additions & 12 deletions spec/liquid_tags/dev_comment_tag_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require "rails_helper"

RSpec.describe DevCommentTag, type: :liquid_template do
let(:user) { create(:user) }
let(:article) { create(:article, user_id: user.id) }
let(:comment) { create(:comment, user_id: user.id, commentable_id: article.id) }
let(:user) { create(:user, username: "DevCommentTagTest", name: "DevCommentTag Test") }
let(:article) { create(:article) }
let(:comment) { create(:comment, commentable: article, body_markdown: "DevCommentTagTest", user: user) }

setup { Liquid::Template.register_tag("devcomment", DevCommentTag) }

Expand All @@ -12,24 +12,20 @@ def generate_comment_tag(id_code)
end

context "when given valid id_code" do
it "fetches the target comment" do
it "fetches the target comment and render properly" do
liquid = generate_comment_tag(comment.id_code_generated)
expect(liquid.root.nodelist.first.comment).to eq(comment)
expect(liquid.render).to include(comment.body_markdown)
expect(liquid.render).to include(user.name)
end

it "raise error if comment does not exist" do
expect do
generate_comment_tag("this should fail")
liquid = generate_comment_tag("this will fail")
liquid.render
end.to raise_error(StandardError)
end
end

it "rejects invalid id_code" do
expect do
generate_comment_tag("this should fail")
end.to raise_error(StandardError)
end

context "when rendered" do
let(:rendered_tag) { generate_comment_tag(comment.id_code_generated).render }

Expand Down

0 comments on commit 96a0235

Please sign in to comment.