Fix Liquid Filters and Fix DevComment Tag (#608)

* Update standard filters

* Update comment tag and rename devcomment tag

* Update tests with new file name

* Refactor liquid tags into null tag

* Fix renaming issue and refactor

* Fix useless assignment via codeclimate

* Add test for null tag

* Update LiquidTags.scss with new file name
This commit is contained in:
Andy Zhao 2018-09-05 17:42:47 -04:00 committed by Ben Halpern
parent 9009a1a977
commit a4c3d97aa9
12 changed files with 90 additions and 53 deletions

View file

@ -2,7 +2,7 @@
@import 'TweetTag';
@import 'YoutubeTag';
@import 'GithubTag';
@import 'CommentTag';
@import 'DevCommentTag';
@import 'LinkTag';
@import 'PodcastTag';
@import 'UserTag';

View file

@ -1,7 +0,0 @@
class AssignTag < Liquid::Block
def initialize(_tag_name, _markup, _options)
raise StandardError.new("Liquid's Assign tag is disabled")
end
end
Liquid::Template.register_tag("assign".freeze, AssignTag)

View file

@ -1,7 +0,0 @@
class CaptureTag < Liquid::Block
def initialize(_tag_name, _markup, _options)
raise StandardError.new("Liquid's Capture tag is disabled")
end
end
Liquid::Template.register_tag("capture".freeze, CaptureTag)

View file

@ -1,7 +0,0 @@
class CycleTag < Liquid::Block
def initialize(_tag_name, _markup, _options)
raise StandardError.new("Liquid's Cycle tag is disabled")
end
end
Liquid::Template.register_tag("cycle".freeze, CycleTag)

View file

@ -1,4 +1,4 @@
class CommentTag < LiquidTagBase
class DevCommentTag < LiquidTagBase
attr_reader :id_code, :comment
def initialize(_tag_name, id_code, _tokens)
@ -8,24 +8,25 @@ class CommentTag < LiquidTagBase
def render(_context)
raise_error unless @comment
"<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>" \
"#{render_twitter_and_github}" \
"<div class=\"comment-date\">" \
"<a href=\"#{@comment.path}\">#{@comment.readable_publish_date}</a>" \
"</div>" \
"</div>" \
"<div class=\"body\">" \
+ @comment.processed_html.html_safe + \
"</div>" \
"</div>"
<<-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">
<a href="#{@comment.path}">#{@comment.readable_publish_date}</a>
</div>
</div>
<div class="body">
#{@comment.processed_html.html_safe}
</div>
</div>
HTML
end
def render_twitter_and_github
@ -65,4 +66,4 @@ class CommentTag < LiquidTagBase
end
end
Liquid::Template.register_tag("devcomment", CommentTag)
Liquid::Template.register_tag("devcomment", DevCommentTag)

View file

@ -1,7 +0,0 @@
class IncludeTag < Liquid::Block
def initialize(_tag_name, _markup, _options)
raise StandardError.new("Liquid's Include tag is disabled")
end
end
Liquid::Template.register_tag("include".freeze, IncludeTag)

View file

@ -0,0 +1,11 @@
class NullTag < Liquid::Block
def initialize(_tag_name, _markup, _options)
raise StandardError, "Liquid##{_tag_name} tag is disabled"
end
end
disabled_tags = %w(assign capture case comment cycle for if ifchanged include unless)
disabled_tags.each do |tag|
Liquid::Template.register_tag(tag, NullTag)
end

View file

@ -1,4 +1,8 @@
module StandardFilters
def append(_input)
raise StandardError, "Liquid#append filter is disabled"
end
def concat(_input)
raise StandardError, "Liquid#concat filter is disabled"
end
@ -15,6 +19,14 @@ module StandardFilters
raise StandardError, "Liquid#join filter is disabled"
end
def last(_input)
raise StandardError, "Liquid#last filter is disabled"
end
def map(_input, _property)
raise StandardError, "Liquid#map filter is disabled"
end
def prepend(_input, _string)
raise StandardError, "Liquid#prepend filter is disabled"
end
@ -27,6 +39,10 @@ module StandardFilters
raise StandardError, "Liquid#remove_first filter is disabled"
end
def reverse(_input)
raise StandardError, "Liquid#reverse filter is disabled"
end
def replace(_input)
raise StandardError, "Liquid#replace filter is disabled"
end
@ -35,9 +51,25 @@ module StandardFilters
raise StandardError, "Liquid#replace_first filter is disabled"
end
def slice(_input)
raise StandardError, "Liquid#slice filter is disabled"
end
def split(_input)
raise StandardError, "Liquid#split filter is disabled"
end
def truncate(_input)
raise StandardError, "Liquid#truncate filter is disabled"
end
def truncatewords(_input)
raise StandardError, "Liquid#truncatewords filter is disabled"
end
def uniq(_input, _property)
raise StandardError, "Liquid#uniq filter is disabled"
end
end
Liquid::Template.register_filter(StandardFilters)

View file

@ -167,8 +167,8 @@ class Comment < ApplicationRecord
end
def custom_css
MarkdownParser.new(body_markdown).tags_used.map do |t|
Rails.application.assets["ltags/#{t}.css"].to_s
MarkdownParser.new(body_markdown).tags_used.map do |tag|
Rails.application.assets["ltags/#{tag}.css"].to_s
end.join
end

View file

@ -1,11 +1,11 @@
require "rails_helper"
RSpec.describe CommentTag, type: :liquid_template do
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) }
setup { Liquid::Template.register_tag("devcomment", CommentTag) }
setup { Liquid::Template.register_tag("devcomment", DevCommentTag) }
def generate_comment_tag(id_code)
Liquid::Template.parse("{% devcomment #{id_code} %}")

View file

@ -0,0 +1,21 @@
require "rails_helper"
RSpec.describe NullTag, type: :liquid_template do
describe "#initialize" do
tags = %w(assign capture case comment cycle for if ifchanged include unless)
setup { tags.each { |tag| Liquid::Template.register_tag(tag, NullTag) } }
def generate_given_tag(tag)
Liquid::Template.parse("{% #{tag} %}")
end
context "when attempting the tags" do
it "prevents the tag from being used" do
tags.each do |tag|
expect { generate_given_tag(tag) }.to raise_error(StandardError)
end
end
end
end
end