I need to show article's content based on this logic:
- if article's content has to be truncated (e.g for preview)
- if there's
[read more]
substring in the content- replace first
[read more]
with link to the article - truncate content to the first occurence of
[read more]
- replace first
- else
- truncate content to the first 500 characters
- if there's
- remove all
[read more]
from content.
I've written a presenter for this, how can it be refactored?
(_h
gives access to the helpers)
This is just one of the responsibilities that this presenter has, there are already a few methods (public & private), so... should I extract it into a helper?
class ArticlePresenter
# ... omitted
DEFAULT_LENGTH = 500
READ_MORE_REGEX = /\[read more\]/i
def content(truncate)
content = article.content
if truncate
index = content.index(READ_MORE_REGEX)
if index
url = _h.article_url(article)
content = _h.truncate(content, length: index, omission: " [Show more](#{url})")
else
content = _h.truncate(content, length: DEFAULT_LENGTH)
end
end
content.gsub!(READ_MORE_REGEX, '')
html = MarkdownService.instance.render content
content_tag :div, html
end