Refactor Dev's LinkTag (#2619)
This commit is contained in:
parent
d1549220ba
commit
95cb63fe04
3 changed files with 42 additions and 45 deletions
|
|
@ -1,52 +1,32 @@
|
|||
class LinkTag < LiquidTagBase
|
||||
include ActionView::Helpers
|
||||
attr_reader :article
|
||||
PARTIAL = "articles/liquid".freeze
|
||||
|
||||
def initialize(_tag_name, slug_or_path_or_url, _tokens)
|
||||
@article = parse_url_for_article(slug_or_path_or_url)
|
||||
@article = get_article(slug_or_path_or_url)
|
||||
@title = strip_tags(@article.title)
|
||||
end
|
||||
|
||||
def render(_context)
|
||||
tags = article.tag_list.map { |t| "<span class='ltag__link__tag'>##{t}</span>" }.join
|
||||
<<-HTML
|
||||
<div class='ltag__link'>
|
||||
<a href='#{article.user.path}' class='ltag__link__link'>
|
||||
<div class='ltag__link__pic'>
|
||||
<img src='#{ProfileImage.new(article.user).get(150)}' alt='#{article.user.username} image'/>
|
||||
</div></a>
|
||||
<a href='#{article.path}' class='ltag__link__link'>
|
||||
<div class='ltag__link__content'>
|
||||
<h2>#{strip_tags article.title}</h2>
|
||||
<h3>#{article.user.name}</h3>
|
||||
<div class='ltag__link__taglist'>#{tags}</div>
|
||||
</div>
|
||||
</a>
|
||||
</div>
|
||||
HTML
|
||||
ActionController::Base.new.render_to_string(
|
||||
partial: PARTIAL,
|
||||
locals: { article: @article, title: @title },
|
||||
)
|
||||
end
|
||||
|
||||
private
|
||||
def get_article(slug)
|
||||
slug = ActionController::Base.helpers.strip_tags(slug).strip
|
||||
article = find_article_by_user(article_hash(slug)) || find_article_by_org(article_hash(slug))
|
||||
raise StandardError, "Invalid link URL or link URL does not exist" unless article
|
||||
|
||||
def parse_url_for_article(slug_or_path_or_url)
|
||||
slug_or_path_or_url = ActionController::Base.helpers.strip_tags(slug_or_path_or_url).strip
|
||||
|
||||
hash = article_hash(slug_or_path_or_url)
|
||||
|
||||
raise_error if hash.nil?
|
||||
|
||||
article = find_article_by_user(hash) || find_article_by_org(hash)
|
||||
raise_error unless article
|
||||
article
|
||||
rescue StandardError
|
||||
raise_error
|
||||
end
|
||||
|
||||
def article_hash(slug_or_path_or_url)
|
||||
path = Addressable::URI.parse(slug_or_path_or_url).path
|
||||
def article_hash(slug)
|
||||
path = Addressable::URI.parse(slug).path
|
||||
path.slice!(0) if path.starts_with?("/") # remove leading slash if present
|
||||
path.slice!(-1) if path.ends_with?("/") # remove trailing slash if present
|
||||
template = Addressable::Template.new("{username}/{slug}")
|
||||
template.extract(path)&.symbolize_keys
|
||||
Addressable::Template.new("{username}/{slug}").extract(path)&.symbolize_keys
|
||||
end
|
||||
|
||||
def find_article_by_user(hash)
|
||||
|
|
@ -62,10 +42,6 @@ class LinkTag < LiquidTagBase
|
|||
|
||||
org.articles.where(slug: hash[:slug])&.first
|
||||
end
|
||||
|
||||
def raise_error
|
||||
raise StandardError, "Invalid link URL or link URL does not exist"
|
||||
end
|
||||
end
|
||||
|
||||
Liquid::Template.register_tag("link", LinkTag)
|
||||
|
|
|
|||
18
app/views/articles/_liquid.html.erb
Normal file
18
app/views/articles/_liquid.html.erb
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
<div class='ltag__link'>
|
||||
<a href='<%= article.user.path %>' class='ltag__link__link'>
|
||||
<div class='ltag__link__pic'>
|
||||
<img src='<%= ProfileImage.new(article.user).get(150) %>' alt='<%= article.user.username + " " + "image"%>'/>
|
||||
</div>
|
||||
</a>
|
||||
<a href='<%= article.path %>' class='ltag__link__link'>
|
||||
<div class='ltag__link__content'>
|
||||
<h2><%= title %></h2>
|
||||
<h3><%= article.user.name %></h3>
|
||||
<div class='ltag__link__taglist'>
|
||||
<% article.tag_list.each do |t| %>
|
||||
<span class='ltag__link__tag'>#<%= t %></span>
|
||||
<% end %>
|
||||
</div>
|
||||
</div>
|
||||
</a>
|
||||
</div>
|
||||
|
|
@ -19,18 +19,21 @@ RSpec.describe LinkTag, type: :liquid_template do
|
|||
|
||||
def correct_link_html(article)
|
||||
tags = article.tag_list.map { |t| "<span class='ltag__link__tag'>##{t}</span>" }.reverse.join
|
||||
<<-HTML
|
||||
<<~HTML
|
||||
<div class='ltag__link'>
|
||||
<a href='#{article.user.path}' class='ltag__link__link'>
|
||||
<div class='ltag__link__pic'>
|
||||
<img src='#{ProfileImage.new(article.user).get(150)}' alt='#{article.user.username} image'/>
|
||||
</div></a>
|
||||
<a href='#{article.path}' class='ltag__link__link'>
|
||||
<div class='ltag__link__content'>
|
||||
<h2>#{ActionController::Base.helpers.strip_tags(article.title)}</h2>
|
||||
<h3>#{article.user.name}</h3>
|
||||
<div class='ltag__link__taglist'>#{tags}</div>
|
||||
</div>
|
||||
</a>
|
||||
<a href='#{article.path}' class='ltag__link__link'>
|
||||
<div class='ltag__link__content'>
|
||||
<h2>#{ActionController::Base.helpers.strip_tags(article.title)}</h2>
|
||||
<h3>#{article.user.name}</h3>
|
||||
<div class='ltag__link__taglist'>
|
||||
#{tags}
|
||||
</div>
|
||||
</div>
|
||||
</a>
|
||||
</div>
|
||||
HTML
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue