Commit a994ce63a5e4e7cd61025fab3dc162a62c1b00ce

Authored by Braulio Bhavamitra
2 parents 0834f87c 203192d0

Merge branch 'refactor_display_content' into 'master'

Refactor DisplayContentPlugin

Removes HTML generation from class into views.

This is the last plugin requiring such refactors allowing backports to get removed.

See merge request !927
plugins/display_content/lib/display_content_block.rb
... ... @@ -118,69 +118,28 @@ class DisplayContentBlock < Block
118 118 holder.articles.where(type: types, parent_id: if parent.nil? then nil else parent end)
119 119 end
120 120  
121   - def content(args={})
122   - block = self
123   -
  121 + def docs
124 122 order_string = "published_at"
125 123 order_string += " DESC" if order_by_recent
126 124  
127 125 limit_final = [limit_to_show, 0].max
128 126  
129   - docs = owner.articles.order(order_string)
  127 + documents = owner.articles.order(order_string)
130 128 .where(articles: {type: self.types})
131 129 .includes(:profile, :image, :tags)
132 130 if nodes.present?
133 131 nodes_conditions = 'articles.id IN(:nodes)'
134 132 nodes_conditions << ' OR articles.parent_id IN(:nodes) ' if display_folder_children
135   - docs = docs.where nodes_conditions, nodes: nodes
  133 + documents = documents.where nodes_conditions, nodes: nodes
136 134 end
137   - docs = docs.limit limit_final if display_folder_children
  135 + documents = documents.limit limit_final if display_folder_children
138 136  
139 137 if content_with_translations
140   - docs = docs.native_translations
141   - docs.replace docs.map{ |p| p.get_translation_to(FastGettext.locale) }.compact
  138 + documents = documents.native_translations
  139 + documents.replace documents.map{ |p| p.get_translation_to(FastGettext.locale) }.compact
142 140 end
143 141  
144   - proc do
145   - block.block_title(block.title, block.subtitle) +
146   - content_tag('ul', docs.map {|item|
147   - if !item.folder? && item.class != RssFeed
148   - content_sections = ''
149   - read_more_section = ''
150   - tags_section = ''
151   -
152   - block.sections.select { |section|
153   - case section[:value]
154   - when 'publish_date'
155   - content_sections += (block.display_section?(section) ? (content_tag('div', show_date(item.published_at, false), :class => 'published-at') ) : '')
156   - when 'title'
157   - content_sections += (block.display_section?(section) ? (content_tag('div', link_to(h(item.title), item.url), :class => 'title') ) : '')
158   - when 'abstract'
159   - content_sections += (block.display_section?(section) ? (content_tag('div', item.abstract.html_safe , :class => 'lead')) : '' )
160   - if block.display_section?(section)
161   - read_more_section = content_tag('div', link_to(_('Read more'), item.url), :class => 'read_more')
162   - end
163   - when 'body'
164   - content_sections += (block.display_section?(section) ? (content_tag('div', item.body.html_safe ,:class => 'body')) : '' )
165   - when 'image'
166   - image_section = image_tag item.image.public_filename if item.image
167   - if !image_section.blank?
168   - content_sections += (block.display_section?(section) ? (content_tag('div', link_to( image_section, item.url ) ,:class => 'image')) : '' )
169   - end
170   - when 'tags'
171   - if !item.tags.empty?
172   - tags_section = item.tags.map { |t| content_tag('span', t.name) }.join("")
173   - content_sections += (block.display_section?(section) ? (content_tag('div', tags_section, :class => 'tags')) : '')
174   - end
175   - end
176   - }
177   -
178   - content_sections += read_more_section if !read_more_section.blank?
179   -#raise sections.inspect
180   - content_tag('li', content_sections.html_safe)
181   - end
182   - }.join(" ").html_safe)
183   - end
  142 + documents
184 143 end
185 144  
186 145 def url_params
... ...
plugins/display_content/test/unit/display_content_block_test.rb
... ... @@ -347,53 +347,6 @@ class DisplayContentBlockTest &lt; 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 }
... ...