From 96a0235168f8a4f2c85c9d3cbafb09a36caf0929 Mon Sep 17 00:00:00 2001 From: Mario See Date: Thu, 2 May 2019 12:05:55 -0400 Subject: [PATCH] Change DevComment#render to utilize html.erb (#2554) [ci skip] --- app/liquid_tags/dev_comment_tag.rb | 65 ++++-------------------- app/views/comments/_liquid.html.erb | 26 ++++++++++ spec/liquid_tags/dev_comment_tag_spec.rb | 20 +++----- 3 files changed, 43 insertions(+), 68 deletions(-) create mode 100644 app/views/comments/_liquid.html.erb diff --git a/app/liquid_tags/dev_comment_tag.rb b/app/liquid_tags/dev_comment_tag.rb index fe66103da..4579590c1 100644 --- a/app/liquid_tags/dev_comment_tag.rb +++ b/app/liquid_tags/dev_comment_tag.rb @@ -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 -
- -
- #{@comment.processed_html.html_safe} -
-
- 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 += "" \ - +image_tag("/assets/twitter-logo.svg", class: "icon-img", alt: "twitter") + \ - "" - end - return if @comment.user.github_username.blank? - - result + "" \ - +image_tag("/assets/github-logo.svg", class: "icon-img", alt: "github") + \ - "" - 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 diff --git a/app/views/comments/_liquid.html.erb b/app/views/comments/_liquid.html.erb new file mode 100644 index 000000000..27886835f --- /dev/null +++ b/app/views/comments/_liquid.html.erb @@ -0,0 +1,26 @@ +
+
+ + <%= comment.user.username %> profile image + + + <%= comment.user.name %> + + <% if comment.user.twitter_username.present? %> + + twitter + + <% end %> + <% if comment.user.github_username.present? %> + + github + + <% end %> + +
+
+ <%= comment.processed_html.html_safe %> +
+
diff --git a/spec/liquid_tags/dev_comment_tag_spec.rb b/spec/liquid_tags/dev_comment_tag_spec.rb index 89e35450d..c5ab7d237 100644 --- a/spec/liquid_tags/dev_comment_tag_spec.rb +++ b/spec/liquid_tags/dev_comment_tag_spec.rb @@ -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 }