Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Liquid Filters and Fix DevComment Tag #608

Merged
merged 8 commits into from Sep 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/assets/stylesheets/ltags/LiquidTags.scss
Expand Up @@ -2,7 +2,7 @@
@import 'TweetTag';
@import 'YoutubeTag';
@import 'GithubTag';
@import 'CommentTag';
@import 'DevCommentTag';
@import 'LinkTag';
@import 'PodcastTag';
@import 'UserTag';
Expand Down
7 changes: 0 additions & 7 deletions app/liquid_tags/assign_tag.rb

This file was deleted.

7 changes: 0 additions & 7 deletions app/liquid_tags/capture_tag.rb

This file was deleted.

7 changes: 0 additions & 7 deletions app/liquid_tags/cycle_tag.rb

This file was deleted.

@@ -1,4 +1,4 @@
class CommentTag < LiquidTagBase
class DevCommentTag < LiquidTagBase
attr_reader :id_code, :comment

def initialize(_tag_name, id_code, _tokens)
Expand All @@ -8,24 +8,25 @@ def initialize(_tag_name, id_code, _tokens)

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
Expand Down Expand Up @@ -65,4 +66,4 @@ def raise_error
end
end

Liquid::Template.register_tag("devcomment", CommentTag)
Liquid::Template.register_tag("devcomment", DevCommentTag)
7 changes: 0 additions & 7 deletions app/liquid_tags/include_tag.rb

This file was deleted.

11 changes: 11 additions & 0 deletions app/liquid_tags/null_tag.rb
@@ -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
32 changes: 32 additions & 0 deletions app/liquid_tags/standard_filters.rb
@@ -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
Expand All @@ -15,6 +19,14 @@ def join(_input)
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
Expand All @@ -27,6 +39,10 @@ def remove_first(_input)
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
Expand All @@ -35,9 +51,25 @@ def replace_first(_input)
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)
4 changes: 2 additions & 2 deletions app/models/comment.rb
Expand Up @@ -167,8 +167,8 @@ def activity_notify
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

Expand Down
@@ -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} %}")
Expand Down
21 changes: 21 additions & 0 deletions spec/liquid_tags/null_tag_spec.rb
@@ -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