Change DevComment#render to utilize html.erb (#2554) [ci skip]
This commit is contained in:
parent
e90bb2bf53
commit
96a0235168
3 changed files with 43 additions and 68 deletions
|
|
@ -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
|
||||
|
|
|
|||
26
app/views/comments/_liquid.html.erb
Normal file
26
app/views/comments/_liquid.html.erb
Normal file
|
|
@ -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>
|
||||
|
|
@ -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) }
|
||||
|
||||
|
|
@ -12,24 +12,20 @@ RSpec.describe DevCommentTag, type: :liquid_template do
|
|||
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 }
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue