From 9507e81fe8546c5938886292101ff38ce619e570 Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Mon, 10 Mar 2014 16:45:46 -0300 Subject: [PATCH] rails3: return proc to render content of display_content_block --- plugins/display_content/lib/display_content_block.rb | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------------------------------- plugins/display_content/test/unit/display_content_block_test.rb | 20 +++++++++++--------- 2 files changed, 66 insertions(+), 62 deletions(-) diff --git a/plugins/display_content/lib/display_content_block.rb b/plugins/display_content/lib/display_content_block.rb index a5fb92b..b8638c1 100644 --- a/plugins/display_content/lib/display_content_block.rb +++ b/plugins/display_content/lib/display_content_block.rb @@ -68,50 +68,51 @@ class DisplayContentBlock < Block holder.articles.find(:all, :conditions => {:type => VALID_CONTENT, :parent_id => (parent.nil? ? nil : parent)}) end - include ActionView::Helpers - include Rails.application.routes.url_helpers def content(args={}) + block = self extra_condition = display_folder_children ? 'OR articles.parent_id IN(:nodes)':'' 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}]) - block_title(title) + - content_tag('ul', docs.map {|item| - if !item.folder? - content_sections = '' - read_more_section = '' - tags_section = '' - - sections.select { |section| - case section[:name] - when 'Publish date' - content_sections += (display_section?(section) ? (content_tag('div', show_date(item.published_at, false), :class => 'published-at') ) : '') - when 'Title' - content_sections += (display_section?(section) ? (content_tag('div', link_to(h(item.title), item.url), :class => 'title') ) : '') - when 'Abstract' - content_sections += (display_section?(section) ? (content_tag('div', item.abstract , :class => 'lead')) : '' ) - if display_section?(section) - read_more_section = content_tag('div', link_to(_('Read more'), item.url), :class => 'read_more') + proc do + block.block_title(block.title) + + content_tag('ul', docs.map {|item| + if !item.folder? + content_sections = '' + read_more_section = '' + tags_section = '' + + block.sections.select { |section| + case section[:name] + when 'Publish date' + content_sections += (block.display_section?(section) ? (content_tag('div', block.show_date(item.published_at, false), :class => 'published-at') ) : '') + when 'Title' + content_sections += (block.display_section?(section) ? (content_tag('div', link_to(h(item.title), item.url), :class => 'title') ) : '') + when 'Abstract' + content_sections += (block.display_section?(section) ? (content_tag('div', item.abstract , :class => 'lead')) : '' ) + if block.display_section?(section) + read_more_section = content_tag('div', link_to(_('Read more'), item.url), :class => 'read_more') + end + when 'Body' + content_sections += (block.display_section?(section) ? (content_tag('div', item.body ,:class => 'body')) : '' ) + when 'Image' + image_section = image_tag item.image.public_filename if item.image + if !image_section.blank? + content_sections += (block.display_section?(section) ? (content_tag('div', link_to( image_section, item.url ) ,:class => 'image')) : '' ) + end + when 'Tags' + if !item.tags.empty? + tags_section = item.tags.map { |t| content_tag('span', t.name) }.join("") + content_sections += (block.display_section?(section) ? (content_tag('div', tags_section, :class => 'tags')) : '') + end end - when 'Body' - content_sections += (display_section?(section) ? (content_tag('div', item.body ,:class => 'body')) : '' ) - when 'Image' - image_section = image_tag item.image.public_filename if item.image - if !image_section.blank? - content_sections += (display_section?(section) ? (content_tag('div', link_to( image_section, item.url ) ,:class => 'image')) : '' ) - end - when 'Tags' - if !item.tags.empty? - tags_section = item.tags.map { |t| content_tag('span', t.name) }.join("") - content_sections += (display_section?(section) ? (content_tag('div', tags_section, :class => 'tags')) : '') - end - end - } + } - content_sections += read_more_section if !read_more_section.blank? + content_sections += read_more_section if !read_more_section.blank? - content_tag('li', content_sections) - end - }.join(" ")) + content_tag('li', content_sections) + end + }.join(" ")) + end end def url_params @@ -129,6 +130,23 @@ class DisplayContentBlock < Block section[:checked] end + #FIXME: this should be in a helper + def show_date(date, use_numbers = false, year=true) + if date && use_numbers + date_format = year ? _('%{month}/%{day}/%{year}') : _('%{month}/%{day}') + date_format % { :day => date.day, :month => date.month, :year => date.year } + elsif date + date_format = year ? _('%{month_name} %{day}, %{year}') : _('%{month_name} %{day}') + date_format % { :day => date.day, :month_name => month_name(date.month), :year => date.year } + else + '' + end + end + + def month_name(n) + _(MONTHS[n-1]) + end + protected def holder @@ -151,20 +169,4 @@ class DisplayContentBlock < Block { :profile => [:article], :environment => [:article] } end - def show_date(date, use_numbers = false, year=true) - if date && use_numbers - date_format = year ? _('%{month}/%{day}/%{year}') : _('%{month}/%{day}') - date_format % { :day => date.day, :month => date.month, :year => date.year } - elsif date - date_format = year ? _('%{month_name} %{day}, %{year}') : _('%{month_name} %{day}') - date_format % { :day => date.day, :month_name => month_name(date.month), :year => date.year } - else - '' - end - end - - def month_name(n) - _(MONTHS[n-1]) - end - end diff --git a/plugins/display_content/test/unit/display_content_block_test.rb b/plugins/display_content/test/unit/display_content_block_test.rb index ce6f331..76584c6 100644 --- a/plugins/display_content/test/unit/display_content_block_test.rb +++ b/plugins/display_content/test/unit/display_content_block_test.rb @@ -352,8 +352,8 @@ class DisplayContentBlockTest < ActiveSupport::TestCase block.stubs(:box).returns(box) box.stubs(:owner).returns(profile) - assert_match /.*#{a1.title}<\/a>/, block.content - assert_match /.*#{a2.title}<\/a>/, block.content + assert_match /.*#{a1.title}<\/a>/, instance_eval(&block.content) + assert_match /.*#{a2.title}<\/a>/, instance_eval(&block.content) end should 'list content for all articles lead defined in nodes' do @@ -369,8 +369,8 @@ class DisplayContentBlockTest < ActiveSupport::TestCase block.stubs(:box).returns(box) box.stubs(:owner).returns(profile) - assert_match /
#{a1.lead}<\/div>/, block.content - assert_match /
#{a2.lead}<\/div>/, block.content + assert_match /
#{a1.lead}<\/div>/, instance_eval(&block.content) + assert_match /
#{a2.lead}<\/div>/, instance_eval(&block.content) end should 'not crash when referenced article is removed' do @@ -384,8 +384,10 @@ class DisplayContentBlockTest < ActiveSupport::TestCase box.stubs(:owner).returns(profile) Article.delete_all - assert_match /
    <\/ul>/, block.content + assert_match /
      <\/ul>/, instance_eval(&block.content) end + include ActionView::Helpers + include Rails.application.routes.url_helpers should 'url_params return myprofile url params if the owner is a profile' do profile = create_user('testuser').person @@ -421,7 +423,7 @@ class DisplayContentBlockTest < ActiveSupport::TestCase block.stubs(:box).returns(box) box.stubs(:owner).returns(profile) - assert_match /.*#{a.title}<\/a>/, block.content + assert_match /.*#{a.title}<\/a>/, instance_eval(&block.content) end should 'show abstract if defined by user' do @@ -435,7 +437,7 @@ class DisplayContentBlockTest < ActiveSupport::TestCase block.stubs(:box).returns(box) box.stubs(:owner).returns(profile) - assert_match /#{a.abstract}/, block.content + assert_match /#{a.abstract}/, instance_eval(&block.content) end should 'show body if defined by user' do @@ -449,7 +451,7 @@ class DisplayContentBlockTest < ActiveSupport::TestCase block.stubs(:box).returns(box) box.stubs(:owner).returns(profile) - assert_match /#{a.body}/, block.content + assert_match /#{a.body}/, instance_eval(&block.content) end should 'display_attribute be true for title by default' do @@ -490,7 +492,7 @@ class DisplayContentBlockTest < ActiveSupport::TestCase block.stubs(:box).returns(box) box.stubs(:owner).returns(profile) - assert_match /#{a.published_at}/, block.content + assert_match /#{a.published_at}/, instance_eval(&block.content) end should 'do not save children if a folder is checked' do -- libgit2 0.21.2