From a3dc3f861d9c6d0da5b5f26b4f6304f9e412e99e Mon Sep 17 00:00:00 2001 From: Marc Altmann Date: Fri, 27 Nov 2015 18:19:50 +0100 Subject: [PATCH] Add support for articles in cms bodies --- app/helpers/content_helper.rb | 17 +++++++++ .../articles/shared/_articles_grid.html.slim | 4 ++- app/views/contents/_content.html.slim | 9 +++++ app/views/contents/clean_show.html.slim | 6 +--- app/views/contents/show.html.slim | 6 +--- config/locales/old/de.yml | 1 + test/features/contents_test.rb | 36 +++++++++++++++++-- 7 files changed, 66 insertions(+), 13 deletions(-) create mode 100644 app/views/contents/_content.html.slim diff --git a/app/helpers/content_helper.rb b/app/helpers/content_helper.rb index 03441e3d89..438a348ceb 100644 --- a/app/helpers/content_helper.rb +++ b/app/helpers/content_helper.rb @@ -20,4 +20,21 @@ def tinycms_content_body_sanitized(key) content = tinycms_content_body(key) Sanitize.clean(content) end + + def expand_content_body(body) + body = body.dup + pattern = /

\[articles ids="(\d{1,10})\s(\d{1,10})\s(\d{1,10})\s(\d{1,10})"\]<\/p>/i + while body =~ pattern + begin + article_ids = Regexp.last_match[1..4].map(&:to_i) + articles = Article.find(article_ids) + rendered_articles = render 'articles/shared/articles_grid', articles: articles, + with_admin_section: false + body.sub!(pattern, rendered_articles) + rescue ActiveRecord::RecordNotFound + body.sub!(pattern, t('tinycms.content.article_not_found_html')) + end + end + body.html_safe + end end diff --git a/app/views/articles/shared/_articles_grid.html.slim b/app/views/articles/shared/_articles_grid.html.slim index b3a1a28fbd..59cbca3dc9 100644 --- a/app/views/articles/shared/_articles_grid.html.slim +++ b/app/views/articles/shared/_articles_grid.html.slim @@ -1,9 +1,11 @@ +- with_admin_section = true unless local_assigns.has_key?(:with_admin_section) + .Grid - articles.each do |article| .Grid-item = render '/articles/shared/show_article', article: article -- if User.is_admin?(current_user) +- if with_admin_section && User.is_admin?(current_user) .Grid-admin h2 Administration: h3 Sammlung befüllen diff --git a/app/views/contents/_content.html.slim b/app/views/contents/_content.html.slim new file mode 100644 index 0000000000..b55e183fa7 --- /dev/null +++ b/app/views/contents/_content.html.slim @@ -0,0 +1,9 @@ +/ Copyright (c) 2012-2015, Fairmondo eG. This file is +/ licensed under the GNU Affero General Public License version 3 or later. +/ See the COPYRIGHT file for details. + +.Content + = expand_content_body(content.body) + - if policy(content).admin? + = link_to t('tinycms.content.edit'), edit_content_url(content) + =< link_to t('tinycms.content.overview'), contents_path diff --git a/app/views/contents/clean_show.html.slim b/app/views/contents/clean_show.html.slim index 6a0b185edc..f073489a5a 100644 --- a/app/views/contents/clean_show.html.slim +++ b/app/views/contents/clean_show.html.slim @@ -3,8 +3,4 @@ / See the COPYRIGHT file for details. .content - .Content - = resource.body && resource.body.html_safe - - if policy(resource).admin? - = link_to t('tinycms.content.edit'), edit_content_url(resource) - = link_to t('tinycms.content.overview'), contents_path + = render resource diff --git a/app/views/contents/show.html.slim b/app/views/contents/show.html.slim index 34b43509dd..ec38adb265 100644 --- a/app/views/contents/show.html.slim +++ b/app/views/contents/show.html.slim @@ -3,10 +3,6 @@ / See the COPYRIGHT file for details. .content - .Content - = resource.body && resource.body.html_safe - - if policy(resource).admin? - = link_to t('tinycms.content.edit'), edit_content_url(resource) - =< link_to t('tinycms.content.overview'), contents_path + = render resource .content-share = render '/shared/take_part' diff --git a/config/locales/old/de.yml b/config/locales/old/de.yml index 721a14e341..2f7f29950f 100644 --- a/config/locales/old/de.yml +++ b/config/locales/old/de.yml @@ -170,6 +170,7 @@ de: index_pages: 'CMS-Seiten' edit_page: 'CMS-Seite bearbeiten' sure: 'Bist Du sicher?' + article_not_found_html: '

Fehler: Mindestens ein Artikel konnte nicht gefunden werden.

' meta_tags: description: "Kaufe, verkaufe, tausche und verleihe faire und nachhaltige Mode, Bücher, Lebensmittel, Spielzeug und Designartikel auf Fairmondo, dem Online Marktplatz für Alle." diff --git a/test/features/contents_test.rb b/test/features/contents_test.rb index 2505e2b6d5..ad9991b316 100644 --- a/test/features/contents_test.rb +++ b/test/features/contents_test.rb @@ -15,12 +15,12 @@ page.must_have_content content.body end - scenario 'admin visists a non exsisting page' do + scenario 'admin visits a non existing page' do login_as admin visit content_path 'not-there' current_path.must_equal new_content_path end - scenario 'guest visit non exsisting page' do + scenario 'guest visit non existing page' do -> { visit content_path 'not-there' }.must_raise ActiveRecord::RecordNotFound end @@ -68,4 +68,36 @@ click_link 'Löschen' end end + + scenario '[articles ids="a b c d"] is expanded to four article previews in a grid' do + user = FactoryGirl.create(:user) + article_ids = [] + (1..8).each do |n| + article_ids << FactoryGirl.create(:article, title: "Book #{n}", seller: user).id + end + body = create_body_with_articles(article_ids) + content = FactoryGirl.create(:content, body: body) + + visit content_path content + + (1..8).each do |n| + page.must_have_content "Book #{n}" + end + end + + def create_body_with_articles(article_ids) + articles1 = article_ids[0..3].join(' ') + articles2 = article_ids[4..7].join(' ') + "

[articles ids=\"#{articles1}\"]

+

[articles ids=\"#{articles2}\"]

" + end + + scenario '[articles ids="a b c d"] is not expanded if articles cannot be found' do + content = FactoryGirl.create(:content, body: "

[articles ids=\"1 2 3 4\"]

") + + visit content_path content + + page.must_have_content ActionController::Base.helpers + .strip_tags(I18n.t('tinycms.content.article_not_found_html')) + end end