Skip to content

Commit

Permalink
Add support for articles in cms bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Altmann committed Nov 30, 2015
1 parent fc222ce commit a3dc3f8
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 13 deletions.
17 changes: 17 additions & 0 deletions app/helpers/content_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = /<p>\[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
4 changes: 3 additions & 1 deletion app/views/articles/shared/_articles_grid.html.slim
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 9 additions & 0 deletions app/views/contents/_content.html.slim
Original file line number Diff line number Diff line change
@@ -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
6 changes: 1 addition & 5 deletions app/views/contents/clean_show.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 1 addition & 5 deletions app/views/contents/show.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -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'
1 change: 1 addition & 0 deletions config/locales/old/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ de:
index_pages: 'CMS-Seiten'
edit_page: 'CMS-Seite bearbeiten'
sure: 'Bist Du sicher?'
article_not_found_html: '<p><b>Fehler:</b> Mindestens ein Artikel konnte nicht gefunden werden.</p>'

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."
Expand Down
36 changes: 34 additions & 2 deletions test/features/contents_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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(' ')
"<p>[articles ids=\"#{articles1}\"]</p>
<p>[articles ids=\"#{articles2}\"]</p>"
end

scenario '[articles ids="a b c d"] is not expanded if articles cannot be found' do
content = FactoryGirl.create(:content, body: "<p>[articles ids=\"1 2 3 4\"]</p>")

visit content_path content

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

0 comments on commit a3dc3f8

Please sign in to comment.