Skip to content

Commit

Permalink
Implement error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Altmann committed Nov 27, 2015
1 parent a0a29b9 commit ce99f27
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
15 changes: 9 additions & 6 deletions app/helpers/content_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ def expand_content_body(body)
pattern =~ body
data = Regexp.last_match
if data && data.size == 5
article_ids = data[1..4].map { |str| str.to_i }
articles = article_ids.map { |id| Article.find(id) } # need rescue here, just return body
rendered_articles = render 'articles/shared/articles_grid', articles: articles
# the replace part
expanded_body = body.sub(pattern, rendered_articles)
expanded_body.html_safe
begin
article_ids = data[1..4].map { |str| str.to_i }
articles = Article.find(article_ids)
rendered_articles = render 'articles/shared/articles_grid', articles: articles
expanded_body = body.sub(pattern, rendered_articles)
expanded_body.html_safe
rescue ActiveRecord::RecordNotFound
body
end
else
body
end
Expand Down
11 changes: 9 additions & 2 deletions test/features/contents_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@

scenario 'content with article previews gets rendered correctly' do
# markup like
# [articles ids="1 2 3 4"]
# in content bodys is expanded to four article previews in a grid
# [articles ids="1 2 3 4"]
# in content bodies is expanded to four article previews in a grid
user = FactoryGirl.create(:user)
article_ids = 4.times.map { FactoryGirl.create(:article, seller: user).id }
content = FactoryGirl.create(:content,
Expand All @@ -83,4 +83,11 @@
page.must_have_content 'Book 3'
page.must_have_content 'Book 4'
end

scenario 'content with article previews does not render if articles are missing' do
content = FactoryGirl.create(:content,
body: "<p>[articles ids=\"1 2 3 4\"]</p>")
visit content_path content
page.must_have_content '[articles ids="1 2 3 4"]'
end
end

0 comments on commit ce99f27

Please sign in to comment.