Commit 9507e81fe8546c5938886292101ff38ce619e570

Authored by Victor Costa
1 parent f448634b

rails3: return proc to render content of display_content_block

plugins/display_content/lib/display_content_block.rb
... ... @@ -68,50 +68,51 @@ class DisplayContentBlock < Block
68 68 holder.articles.find(:all, :conditions => {:type => VALID_CONTENT, :parent_id => (parent.nil? ? nil : parent)})
69 69 end
70 70  
71   - include ActionView::Helpers
72   - include Rails.application.routes.url_helpers
73 71 def content(args={})
  72 + block = self
74 73 extra_condition = display_folder_children ? 'OR articles.parent_id IN(:nodes)':''
75 74 docs = nodes.blank? ? [] : owner.articles.find(:all, :conditions => ["(articles.id IN(:nodes) #{extra_condition}) AND articles.type IN(:types)", {:nodes => self.nodes, :types => VALID_CONTENT}])
76 75  
77   - block_title(title) +
78   - content_tag('ul', docs.map {|item|
79   - if !item.folder?
80   - content_sections = ''
81   - read_more_section = ''
82   - tags_section = ''
83   -
84   - sections.select { |section|
85   - case section[:name]
86   - when 'Publish date'
87   - content_sections += (display_section?(section) ? (content_tag('div', show_date(item.published_at, false), :class => 'published-at') ) : '')
88   - when 'Title'
89   - content_sections += (display_section?(section) ? (content_tag('div', link_to(h(item.title), item.url), :class => 'title') ) : '')
90   - when 'Abstract'
91   - content_sections += (display_section?(section) ? (content_tag('div', item.abstract , :class => 'lead')) : '' )
92   - if display_section?(section)
93   - read_more_section = content_tag('div', link_to(_('Read more'), item.url), :class => 'read_more')
  76 + proc do
  77 + block.block_title(block.title) +
  78 + content_tag('ul', docs.map {|item|
  79 + if !item.folder?
  80 + content_sections = ''
  81 + read_more_section = ''
  82 + tags_section = ''
  83 +
  84 + block.sections.select { |section|
  85 + case section[:name]
  86 + when 'Publish date'
  87 + content_sections += (block.display_section?(section) ? (content_tag('div', block.show_date(item.published_at, false), :class => 'published-at') ) : '')
  88 + when 'Title'
  89 + content_sections += (block.display_section?(section) ? (content_tag('div', link_to(h(item.title), item.url), :class => 'title') ) : '')
  90 + when 'Abstract'
  91 + content_sections += (block.display_section?(section) ? (content_tag('div', item.abstract , :class => 'lead')) : '' )
  92 + if block.display_section?(section)
  93 + read_more_section = content_tag('div', link_to(_('Read more'), item.url), :class => 'read_more')
  94 + end
  95 + when 'Body'
  96 + content_sections += (block.display_section?(section) ? (content_tag('div', item.body ,:class => 'body')) : '' )
  97 + when 'Image'
  98 + image_section = image_tag item.image.public_filename if item.image
  99 + if !image_section.blank?
  100 + content_sections += (block.display_section?(section) ? (content_tag('div', link_to( image_section, item.url ) ,:class => 'image')) : '' )
  101 + end
  102 + when 'Tags'
  103 + if !item.tags.empty?
  104 + tags_section = item.tags.map { |t| content_tag('span', t.name) }.join("")
  105 + content_sections += (block.display_section?(section) ? (content_tag('div', tags_section, :class => 'tags')) : '')
  106 + end
94 107 end
95   - when 'Body'
96   - content_sections += (display_section?(section) ? (content_tag('div', item.body ,:class => 'body')) : '' )
97   - when 'Image'
98   - image_section = image_tag item.image.public_filename if item.image
99   - if !image_section.blank?
100   - content_sections += (display_section?(section) ? (content_tag('div', link_to( image_section, item.url ) ,:class => 'image')) : '' )
101   - end
102   - when 'Tags'
103   - if !item.tags.empty?
104   - tags_section = item.tags.map { |t| content_tag('span', t.name) }.join("")
105   - content_sections += (display_section?(section) ? (content_tag('div', tags_section, :class => 'tags')) : '')
106   - end
107   - end
108   - }
  108 + }
109 109  
110   - content_sections += read_more_section if !read_more_section.blank?
  110 + content_sections += read_more_section if !read_more_section.blank?
111 111  
112   - content_tag('li', content_sections)
113   - end
114   - }.join(" "))
  112 + content_tag('li', content_sections)
  113 + end
  114 + }.join(" "))
  115 + end
115 116 end
116 117  
117 118 def url_params
... ... @@ -129,6 +130,23 @@ class DisplayContentBlock < Block
129 130 section[:checked]
130 131 end
131 132  
  133 + #FIXME: this should be in a helper
  134 + def show_date(date, use_numbers = false, year=true)
  135 + if date && use_numbers
  136 + date_format = year ? _('%{month}/%{day}/%{year}') : _('%{month}/%{day}')
  137 + date_format % { :day => date.day, :month => date.month, :year => date.year }
  138 + elsif date
  139 + date_format = year ? _('%{month_name} %{day}, %{year}') : _('%{month_name} %{day}')
  140 + date_format % { :day => date.day, :month_name => month_name(date.month), :year => date.year }
  141 + else
  142 + ''
  143 + end
  144 + end
  145 +
  146 + def month_name(n)
  147 + _(MONTHS[n-1])
  148 + end
  149 +
132 150 protected
133 151  
134 152 def holder
... ... @@ -151,20 +169,4 @@ class DisplayContentBlock < Block
151 169 { :profile => [:article], :environment => [:article] }
152 170 end
153 171  
154   - def show_date(date, use_numbers = false, year=true)
155   - if date && use_numbers
156   - date_format = year ? _('%{month}/%{day}/%{year}') : _('%{month}/%{day}')
157   - date_format % { :day => date.day, :month => date.month, :year => date.year }
158   - elsif date
159   - date_format = year ? _('%{month_name} %{day}, %{year}') : _('%{month_name} %{day}')
160   - date_format % { :day => date.day, :month_name => month_name(date.month), :year => date.year }
161   - else
162   - ''
163   - end
164   - end
165   -
166   - def month_name(n)
167   - _(MONTHS[n-1])
168   - end
169   -
170 172 end
... ...
plugins/display_content/test/unit/display_content_block_test.rb
... ... @@ -352,8 +352,8 @@ class DisplayContentBlockTest < ActiveSupport::TestCase
352 352 block.stubs(:box).returns(box)
353 353 box.stubs(:owner).returns(profile)
354 354  
355   - assert_match /.*<a.*>#{a1.title}<\/a>/, block.content
356   - assert_match /.*<a.*>#{a2.title}<\/a>/, block.content
  355 + assert_match /.*<a.*>#{a1.title}<\/a>/, instance_eval(&block.content)
  356 + assert_match /.*<a.*>#{a2.title}<\/a>/, instance_eval(&block.content)
357 357 end
358 358  
359 359 should 'list content for all articles lead defined in nodes' do
... ... @@ -369,8 +369,8 @@ class DisplayContentBlockTest &lt; ActiveSupport::TestCase
369 369 block.stubs(:box).returns(box)
370 370 box.stubs(:owner).returns(profile)
371 371  
372   - assert_match /<div class="lead">#{a1.lead}<\/div>/, block.content
373   - assert_match /<div class="lead">#{a2.lead}<\/div>/, block.content
  372 + assert_match /<div class="lead">#{a1.lead}<\/div>/, instance_eval(&block.content)
  373 + assert_match /<div class="lead">#{a2.lead}<\/div>/, instance_eval(&block.content)
374 374 end
375 375  
376 376 should 'not crash when referenced article is removed' do
... ... @@ -384,8 +384,10 @@ class DisplayContentBlockTest &lt; ActiveSupport::TestCase
384 384 box.stubs(:owner).returns(profile)
385 385  
386 386 Article.delete_all
387   - assert_match /<ul><\/ul>/, block.content
  387 + assert_match /<ul><\/ul>/, instance_eval(&block.content)
388 388 end
  389 + include ActionView::Helpers
  390 + include Rails.application.routes.url_helpers
389 391  
390 392 should 'url_params return myprofile url params if the owner is a profile' do
391 393 profile = create_user('testuser').person
... ... @@ -421,7 +423,7 @@ class DisplayContentBlockTest &lt; ActiveSupport::TestCase
421 423 block.stubs(:box).returns(box)
422 424 box.stubs(:owner).returns(profile)
423 425  
424   - assert_match /.*<a.*>#{a.title}<\/a>/, block.content
  426 + assert_match /.*<a.*>#{a.title}<\/a>/, instance_eval(&block.content)
425 427 end
426 428  
427 429 should 'show abstract if defined by user' do
... ... @@ -435,7 +437,7 @@ class DisplayContentBlockTest &lt; ActiveSupport::TestCase
435 437 block.stubs(:box).returns(box)
436 438 box.stubs(:owner).returns(profile)
437 439  
438   - assert_match /#{a.abstract}/, block.content
  440 + assert_match /#{a.abstract}/, instance_eval(&block.content)
439 441 end
440 442  
441 443 should 'show body if defined by user' do
... ... @@ -449,7 +451,7 @@ class DisplayContentBlockTest &lt; ActiveSupport::TestCase
449 451 block.stubs(:box).returns(box)
450 452 box.stubs(:owner).returns(profile)
451 453  
452   - assert_match /#{a.body}/, block.content
  454 + assert_match /#{a.body}/, instance_eval(&block.content)
453 455 end
454 456  
455 457 should 'display_attribute be true for title by default' do
... ... @@ -490,7 +492,7 @@ class DisplayContentBlockTest &lt; ActiveSupport::TestCase
490 492 block.stubs(:box).returns(box)
491 493 box.stubs(:owner).returns(profile)
492 494  
493   - assert_match /#{a.published_at}/, block.content
  495 + assert_match /#{a.published_at}/, instance_eval(&block.content)
494 496 end
495 497  
496 498 should 'do not save children if a folder is checked' do
... ...