Commit 203192d0ddaa8c9b5a7ee20ed5a9a6e9d04b4915

Authored by Rafael Reggiani Manzo
1 parent 95bab8a2

Refactor DisplayContentBlock#content into views

This takes advantage of the structure at BoxesHelper for content
rendering. It improves MVC compliance.
plugins/display_content/lib/display_content_block.rb
... ... @@ -142,53 +142,6 @@ class DisplayContentBlock < Block
142 142 documents
143 143 end
144 144  
145   - def content(args={})
146   - block = self
147   -
148   - items = self.docs
149   -
150   - proc do
151   - block.block_title(block.title, block.subtitle) +
152   - content_tag('ul', items.map {|item|
153   - if !item.folder? && item.class != RssFeed
154   - content_sections = ''
155   - read_more_section = ''
156   - tags_section = ''
157   -
158   - block.sections.select { |section|
159   - case section[:value]
160   - when 'publish_date'
161   - content_sections += (block.display_section?(section) ? (content_tag('div', show_date(item.published_at, false), :class => 'published-at') ) : '')
162   - when 'title'
163   - content_sections += (block.display_section?(section) ? (content_tag('div', link_to(h(item.title), item.url), :class => 'title') ) : '')
164   - when 'abstract'
165   - content_sections += (block.display_section?(section) ? (content_tag('div', item.abstract.html_safe , :class => 'lead')) : '' )
166   - if block.display_section?(section)
167   - read_more_section = content_tag('div', link_to(_('Read more'), item.url), :class => 'read_more')
168   - end
169   - when 'body'
170   - content_sections += (block.display_section?(section) ? (content_tag('div', item.body.html_safe ,:class => 'body')) : '' )
171   - when 'image'
172   - image_section = image_tag item.image.public_filename if item.image
173   - if !image_section.blank?
174   - content_sections += (block.display_section?(section) ? (content_tag('div', link_to( image_section, item.url ) ,:class => 'image')) : '' )
175   - end
176   - when 'tags'
177   - if !item.tags.empty?
178   - tags_section = item.tags.map { |t| content_tag('span', t.name) }.join("")
179   - content_sections += (block.display_section?(section) ? (content_tag('div', tags_section, :class => 'tags')) : '')
180   - end
181   - end
182   - }
183   -
184   - content_sections += read_more_section if !read_more_section.blank?
185   -#raise sections.inspect
186   - content_tag('li', content_sections.html_safe)
187   - end
188   - }.join(" ").html_safe)
189   - end
190   - end
191   -
192 145 def url_params
193 146 params = {:block_id => self.id}
194 147 if self.owner.is_a?(Profile)
... ...
plugins/display_content/test/unit/display_content_block_test.rb
... ... @@ -347,53 +347,6 @@ class DisplayContentBlockTest < ActiveSupport::TestCase
347 347  
348 348 end
349 349  
350   - should 'list links for all articles title defined in nodes' do
351   - profile = create_user('testuser').person
352   - Article.delete_all
353   - a1 = fast_create(TextileArticle, :name => 'test article 1', :profile_id => profile.id)
354   - a2 = fast_create(TextileArticle, :name => 'test article 2', :profile_id => profile.id)
355   -
356   - block = DisplayContentBlock.new
357   - block.sections = [{:value => 'title', :checked => true}]
358   - block.nodes = [a1.id, a2.id]
359   - box = mock()
360   - block.stubs(:box).returns(box)
361   - box.stubs(:owner).returns(profile)
362   -
363   - assert_match /.*<a.*>#{a1.title}<\/a>/, instance_eval(&block.content)
364   - assert_match /.*<a.*>#{a2.title}<\/a>/, instance_eval(&block.content)
365   - end
366   -
367   - should 'list content for all articles lead defined in nodes' do
368   - profile = create_user('testuser').person
369   - Article.delete_all
370   - a1 = fast_create(TinyMceArticle, :name => 'test article 1', :profile_id => profile.id, :abstract => 'abstract article 1')
371   - a2 = fast_create(TinyMceArticle, :name => 'test article 2', :profile_id => profile.id, :abstract => 'abstract article 2')
372   -
373   - block = DisplayContentBlock.new
374   - block.sections = [{:value => 'abstract', :checked => true}]
375   - block.nodes = [a1.id, a2.id]
376   - box = mock()
377   - block.stubs(:box).returns(box)
378   - box.stubs(:owner).returns(profile)
379   -
380   - assert_match /<div class="lead">#{a1.lead}<\/div>/, instance_eval(&block.content)
381   - assert_match /<div class="lead">#{a2.lead}<\/div>/, instance_eval(&block.content)
382   - end
383   -
384   - should 'not crash when referenced article is removed' do
385   - profile = create_user('testuser').person
386   - a1 = fast_create(TextArticle, :name => 'test article 1', :profile_id => profile.id)
387   -
388   - block = DisplayContentBlock.new
389   - block.nodes = [a1.id]
390   - box = mock()
391   - block.stubs(:box).returns(box)
392   - box.stubs(:owner).returns(profile)
393   -
394   - Article.delete_all
395   - assert_match /<ul><\/ul>/, instance_eval(&block.content)
396   - end
397 350 include ActionView::Helpers
398 351 include Rails.application.routes.url_helpers
399 352  
... ... @@ -420,48 +373,6 @@ class DisplayContentBlockTest &lt; ActiveSupport::TestCase
420 373 assert_equal params, block.url_params
421 374 end
422 375  
423   - should 'show title if defined by user' do
424   - profile = create_user('testuser').person
425   - a = fast_create(TextileArticle, :name => 'test article 1', :profile_id => profile.id)
426   -
427   - block = DisplayContentBlock.new
428   - block.nodes = [a.id]
429   - block.sections = [{:value => 'title', :checked => true}]
430   - box = mock()
431   - block.stubs(:box).returns(box)
432   - box.stubs(:owner).returns(profile)
433   -
434   - assert_match /.*<a.*>#{a.title}<\/a>/, instance_eval(&block.content)
435   - end
436   -
437   - should 'show abstract if defined by user' do
438   - profile = create_user('testuser').person
439   - a = fast_create(TextileArticle, :name => 'test article 1', :profile_id => profile.id, :abstract => 'some abstract')
440   -
441   - block = DisplayContentBlock.new
442   - block.nodes = [a.id]
443   - block.sections = [{:value => 'abstract', :checked => true}]
444   - box = mock()
445   - block.stubs(:box).returns(box)
446   - box.stubs(:owner).returns(profile)
447   -
448   - assert_match /#{a.abstract}/, instance_eval(&block.content)
449   - end
450   -
451   - should 'show body if defined by user' do
452   - profile = create_user('testuser').person
453   - a = fast_create(TextileArticle, :name => 'test article 1', :profile_id => profile.id, :body => 'some body')
454   -
455   - block = DisplayContentBlock.new
456   - block.nodes = [a.id]
457   - block.sections = [{:value => 'body', :checked => true}]
458   - box = mock()
459   - block.stubs(:box).returns(box)
460   - box.stubs(:owner).returns(profile)
461   -
462   - assert_match /#{a.body}/, instance_eval(&block.content)
463   - end
464   -
465 376 should 'display_attribute be true for title by default' do
466 377 profile = create_user('testuser').person
467 378  
... ... @@ -489,20 +400,6 @@ class DisplayContentBlockTest &lt; ActiveSupport::TestCase
489 400 assert block.display_section?({:value => 'publish_date', :checked => true})
490 401 end
491 402  
492   - should 'show publishd date if defined by user' do
493   - profile = create_user('testuser').person
494   - a = fast_create(TextArticle, :name => 'test article 1', :profile_id => profile.id, :body => 'some body')
495   -
496   - block = DisplayContentBlock.new
497   - block.nodes = [a.id]
498   - block.sections = [{:value => 'publish_date', :checked => true}]
499   - box = mock()
500   - block.stubs(:box).returns(box)
501   - box.stubs(:owner).returns(profile)
502   -
503   - assert_match /#{a.published_at}/, instance_eval(&block.content)
504   - end
505   -
506 403 should 'do not save children if a folder is checked' do
507 404 profile = create_user('testuser').person
508 405 Article.delete_all
... ... @@ -648,6 +545,117 @@ class DisplayContentBlockTest &lt; ActiveSupport::TestCase
648 545 assert_equal [], block.parent_nodes
649 546 end
650 547  
  548 +end
  549 +
  550 +require 'boxes_helper'
  551 +
  552 +class DisplayContentBlockViewTest < ActionView::TestCase
  553 + include BoxesHelper
  554 +
  555 + should 'list links for all articles title defined in nodes' do
  556 + profile = create_user('testuser').person
  557 + Article.delete_all
  558 + a1 = fast_create(TextileArticle, :name => 'test article 1', :profile_id => profile.id)
  559 + a2 = fast_create(TextileArticle, :name => 'test article 2', :profile_id => profile.id)
  560 +
  561 + block = DisplayContentBlock.new
  562 + block.sections = [{:value => 'title', :checked => true}]
  563 + block.nodes = [a1.id, a2.id]
  564 + box = mock()
  565 + block.stubs(:box).returns(box)
  566 + box.stubs(:owner).returns(profile)
  567 +
  568 + assert_match /.*<a.*>#{a1.title}<\/a>/, render_block_content(block)
  569 + assert_match /.*<a.*>#{a2.title}<\/a>/, render_block_content(block)
  570 + end
  571 +
  572 + should 'list content for all articles lead defined in nodes' do
  573 + profile = create_user('testuser').person
  574 + Article.delete_all
  575 + a1 = fast_create(TinyMceArticle, :name => 'test article 1', :profile_id => profile.id, :abstract => 'abstract article 1')
  576 + a2 = fast_create(TinyMceArticle, :name => 'test article 2', :profile_id => profile.id, :abstract => 'abstract article 2')
  577 +
  578 + block = DisplayContentBlock.new
  579 + block.sections = [{:value => 'abstract', :checked => true}]
  580 + block.nodes = [a1.id, a2.id]
  581 + box = mock()
  582 + block.stubs(:box).returns(box)
  583 + box.stubs(:owner).returns(profile)
  584 +
  585 + assert_match /<div class="lead">#{a1.lead}<\/div>/, render_block_content(block)
  586 + assert_match /<div class="lead">#{a2.lead}<\/div>/, render_block_content(block)
  587 + end
  588 +
  589 + should 'not crash when referenced article is removed' do
  590 + profile = create_user('testuser').person
  591 + a1 = fast_create(TextArticle, :name => 'test article 1', :profile_id => profile.id)
  592 +
  593 + block = DisplayContentBlock.new
  594 + block.nodes = [a1.id]
  595 + box = mock()
  596 + block.stubs(:box).returns(box)
  597 + box.stubs(:owner).returns(profile)
  598 +
  599 + Article.delete_all
  600 + assert_match /<ul><\/ul>/, render_block_content(block)
  601 + end
  602 +
  603 + should 'show title if defined by user' do
  604 + profile = create_user('testuser').person
  605 + a = fast_create(TextileArticle, :name => 'test article 1', :profile_id => profile.id)
  606 +
  607 + block = DisplayContentBlock.new
  608 + block.nodes = [a.id]
  609 + block.sections = [{:value => 'title', :checked => true}]
  610 + box = mock()
  611 + block.stubs(:box).returns(box)
  612 + box.stubs(:owner).returns(profile)
  613 +
  614 + assert_match /.*<a.*>#{a.title}<\/a>/, render_block_content(block)
  615 + end
  616 +
  617 + should 'show abstract if defined by user' do
  618 + profile = create_user('testuser').person
  619 + a = fast_create(TextileArticle, :name => 'test article 1', :profile_id => profile.id, :abstract => 'some abstract')
  620 +
  621 + block = DisplayContentBlock.new
  622 + block.nodes = [a.id]
  623 + block.sections = [{:value => 'abstract', :checked => true}]
  624 + box = mock()
  625 + block.stubs(:box).returns(box)
  626 + box.stubs(:owner).returns(profile)
  627 +
  628 + assert_match /#{a.abstract}/, render_block_content(block)
  629 + end
  630 +
  631 + should 'show body if defined by user' do
  632 + profile = create_user('testuser').person
  633 + a = fast_create(TextileArticle, :name => 'test article 1', :profile_id => profile.id, :body => 'some body')
  634 +
  635 + block = DisplayContentBlock.new
  636 + block.nodes = [a.id]
  637 + block.sections = [{:value => 'body', :checked => true}]
  638 + box = mock()
  639 + block.stubs(:box).returns(box)
  640 + box.stubs(:owner).returns(profile)
  641 +
  642 + assert_match /#{a.body}/, render_block_content(block)
  643 + end
  644 +
  645 + should 'show publishd date if defined by user' do
  646 + profile = create_user('testuser').person
  647 + a = fast_create(TextArticle, :name => 'test article 1', :profile_id => profile.id, :body => 'some body')
  648 +
  649 + block = DisplayContentBlock.new
  650 + block.nodes = [a.id]
  651 + block.sections = [{:value => 'publish_date', :checked => true}]
  652 + box = mock()
  653 + block.stubs(:box).returns(box)
  654 + box.stubs(:owner).returns(profile)
  655 +
  656 + assert_match /#{a.published_at}/, render_block_content(block)
  657 + end
  658 +
651 659 should 'show articles in recent order' do
652 660 profile = create_user('testuser').person
653 661 Article.delete_all
... ... @@ -663,8 +671,8 @@ class DisplayContentBlockTest &lt; ActiveSupport::TestCase
663 671  
664 672 block.order_by_recent = true
665 673  
666   - a1_index = instance_eval(&block.content).index(a1.name)
667   - a2_index = instance_eval(&block.content).index(a2.name)
  674 + a1_index = render_block_content(block).index(a1.name)
  675 + a2_index = render_block_content(block).index(a2.name)
668 676  
669 677 assert a2_index < a1_index
670 678 end
... ... @@ -684,8 +692,8 @@ class DisplayContentBlockTest &lt; ActiveSupport::TestCase
684 692  
685 693 block.order_by_recent = false
686 694  
687   - a1_index = instance_eval(&block.content).index(a1.name)
688   - a2_index = instance_eval(&block.content).index(a2.name)
  695 + a1_index = render_block_content(block).index(a1.name)
  696 + a2_index = render_block_content(block).index(a2.name)
689 697  
690 698 assert a1_index < a2_index
691 699 end
... ... @@ -706,7 +714,7 @@ class DisplayContentBlockTest &lt; ActiveSupport::TestCase
706 714 block.order_by_recent = true
707 715 block.limit_to_show = 1
708 716  
709   - a1_index = instance_eval(&block.content).index(a1.name)
  717 + a1_index = render_block_content(block).index(a1.name)
710 718  
711 719 assert a1_index.nil?
712 720 end
... ... @@ -730,15 +738,15 @@ class DisplayContentBlockTest &lt; ActiveSupport::TestCase
730 738 block.stubs(:box).returns(box)
731 739 box.stubs(:owner).returns(profile)
732 740  
733   - assert_nil instance_eval(&block.content).index(en_article.name)
734   - assert_nil instance_eval(&block.content).index(en_article2.name)
735   - assert instance_eval(&block.content).index(pt_article.name).present?
  741 + assert_nil render_block_content(block).index(en_article.name)
  742 + assert_nil render_block_content(block).index(en_article2.name)
  743 + assert render_block_content(block).index(pt_article.name).present?
736 744  
737 745 FastGettext.stubs(:locale).returns('en')
738 746  
739   - assert instance_eval(&block.content).index(en_article.name)
740   - assert instance_eval(&block.content).index(en_article2.name)
741   - assert_nil instance_eval(&block.content).index(pt_article.name)
  747 + assert render_block_content(block).index(en_article.name)
  748 + assert render_block_content(block).index(en_article2.name)
  749 + assert_nil render_block_content(block).index(pt_article.name)
742 750 end
743 751  
744 752 should 'replace article with its translation' do
... ... @@ -758,12 +766,12 @@ class DisplayContentBlockTest &lt; ActiveSupport::TestCase
758 766 block.stubs(:box).returns(box)
759 767 box.stubs(:owner).returns(profile)
760 768  
761   - assert_nil instance_eval(&block.content).index(en_article.name)
762   - assert instance_eval(&block.content).index(pt_article.name).present?
  769 + assert_nil render_block_content(block).index(en_article.name)
  770 + assert render_block_content(block).index(pt_article.name).present?
763 771  
764 772 FastGettext.stubs(:locale).returns('en')
765 773  
766   - assert instance_eval(&block.content).index(en_article.name).present?
767   - assert_nil instance_eval(&block.content).index(pt_article.name)
  774 + assert render_block_content(block).index(en_article.name).present?
  775 + assert_nil render_block_content(block).index(pt_article.name)
768 776 end
769 777 end
... ...
plugins/display_content/views/blocks/_document.slim 0 → 100644
... ... @@ -0,0 +1,4 @@
  1 +li
  2 + - unless item.folder? || item.class == RssFeed
  3 + = render partial: 'blocks/section', collection: block.sections, locals: { block: block, item: item }
  4 + = render partial: 'blocks/read_more', locals: { item: item, abstract_section: block.sections.bsearch { |section| section[:value] == 'abstract' }, block: block }
0 5 \ No newline at end of file
... ...
plugins/display_content/views/blocks/_read_more.slim 0 → 100644
... ... @@ -0,0 +1,3 @@
  1 +- if !abstract_section.nil? && block.display_section?(abstract_section)
  2 + div class='read_more'
  3 + = link_to(_('Read more'), item.url)
... ...
plugins/display_content/views/blocks/_section.slim 0 → 100644
... ... @@ -0,0 +1,22 @@
  1 +- if block.display_section?(section)
  2 + - case section[:value]
  3 + - when 'publish_date'
  4 + div class='published-at'
  5 + = show_date(item.published_at, false)
  6 + - when 'title'
  7 + div class='title'
  8 + = link_to(h(item.title), item.url)
  9 + - when 'abstract'
  10 + div class='lead'
  11 + = item.abstract
  12 + - when 'body'
  13 + div class='body'
  14 + = item.body
  15 + - when 'image'
  16 + - unless item.image || item.image.public_filename
  17 + div class='image'
  18 + = link_to(image_tag(item.image.public_filename), item.url)
  19 + - when 'tags'
  20 + - unless item.tags.empty?
  21 + div class='tags'
  22 + = render partial: 'blocks/tag', collection: item.tags
... ...
plugins/display_content/views/blocks/_tag.slim 0 → 100644
... ... @@ -0,0 +1,2 @@
  1 +span
  2 + = tag.name
0 3 \ No newline at end of file
... ...
plugins/display_content/views/blocks/display_content.slim 0 → 100644
... ... @@ -0,0 +1,4 @@
  1 += block.block_title(block.title, block.subtitle)
  2 +
  3 +ul
  4 + = render partial: 'blocks/document', collection: block.docs, as: :item, locals: { block: block }
... ...