Commit 9e77f7f1282817f52c1bc12de1a0b8b6845516e4
1 parent
7809944b
Exists in
web_steps_improvements
and in
9 other branches
blocks: fix rendering content when inheritance is involved
Showing
7 changed files
with
27 additions
and
30 deletions
Show diff stats
app/helpers/boxes_helper.rb
| @@ -87,41 +87,38 @@ module BoxesHelper | @@ -87,41 +87,38 @@ module BoxesHelper | ||
| 87 | box_decorator == DontMoveBlocks | 87 | box_decorator == DontMoveBlocks |
| 88 | end | 88 | end |
| 89 | 89 | ||
| 90 | - def render_block_content(block) | ||
| 91 | - template_name = block.class.name.underscore.gsub('_block', '') | ||
| 92 | - template_filename = "#{template_name}.html.erb" | ||
| 93 | - if File.exists? Rails.root.join('app', 'views', 'blocks', template_filename) | ||
| 94 | - render :file => "blocks/#{template_name}", :locals => { :block => block } | ||
| 95 | - else | ||
| 96 | - nil | 90 | + def render_block block, prefix = nil, klass = block.class |
| 91 | + template_name = klass.name.underscore.sub '_block', '' | ||
| 92 | + begin | ||
| 93 | + render template: "blocks/#{prefix}#{template_name}", locals: { block: block } | ||
| 94 | + rescue ActionView::MissingTemplate | ||
| 95 | + return if klass.superclass === Block | ||
| 96 | + render_block block, prefix, klass.superclass | ||
| 97 | end | 97 | end |
| 98 | end | 98 | end |
| 99 | 99 | ||
| 100 | - def render_block_footer(block) | ||
| 101 | - template_name = block.class.name.underscore.gsub('_block', '') | ||
| 102 | - template_filename = "#{template_name}.html.erb" | ||
| 103 | - if File.exists? Rails.root.join('app', 'views', 'blocks', 'footers', template_filename) | ||
| 104 | - render :file => "blocks/footers/#{template_name}", :locals => { :block => block } | ||
| 105 | - else | ||
| 106 | - nil | ||
| 107 | - end | 100 | + def render_block_content block |
| 101 | + # FIXME: this conditional should be removed after all | ||
| 102 | + # block footer from plugins methods get refactored into helpers and views. | ||
| 103 | + # They are a failsafe until all of them are done. | ||
| 104 | + return block.content if block.method(:content).owner != Block | ||
| 105 | + render_block block | ||
| 106 | + end | ||
| 107 | + | ||
| 108 | + def render_block_footer block | ||
| 109 | + return block.footer if block.method(:footer).owner != Block | ||
| 110 | + render_block block, 'footers/' | ||
| 108 | end | 111 | end |
| 109 | 112 | ||
| 110 | def display_block_content(block, main_content = nil) | 113 | def display_block_content(block, main_content = nil) |
| 111 | - # FIXME: these conditionals should be removed after all block footer from plugins methods get refactored into helpers and views. They are a failsafe until all of them are done. | ||
| 112 | content = nil | 114 | content = nil |
| 113 | if block.main? | 115 | if block.main? |
| 114 | content = wrap_main_content(main_content) | 116 | content = wrap_main_content(main_content) |
| 115 | else | 117 | else |
| 116 | - if(block.method(:content).owner != Block) | ||
| 117 | - content = block.content() | ||
| 118 | - else | ||
| 119 | - content = render_block_content(block) | ||
| 120 | - end | 118 | + content = render_block_content block |
| 121 | end | 119 | end |
| 122 | result = extract_block_content(content) | 120 | result = extract_block_content(content) |
| 123 | - # FIXME: this ternary conditional should be removed after all block footer from plugins methods get refactored into helpers and views | ||
| 124 | - footer_content = extract_block_content(block.method(:footer).owner != Block ? block.footer : render_block_footer(block)) | 121 | + footer_content = extract_block_content(render_block_footer block) |
| 125 | unless footer_content.blank? | 122 | unless footer_content.blank? |
| 126 | footer_content = content_tag('div', footer_content, :class => 'block-footer-content' ) | 123 | footer_content = content_tag('div', footer_content, :class => 'block-footer-content' ) |
| 127 | end | 124 | end |
test/unit/categories_block_test.rb
| @@ -22,7 +22,7 @@ class CategoriesBlockTest < ActiveSupport::TestCase | @@ -22,7 +22,7 @@ class CategoriesBlockTest < ActiveSupport::TestCase | ||
| 22 | should 'display category block' do | 22 | should 'display category block' do |
| 23 | block = CategoriesBlock.new | 23 | block = CategoriesBlock.new |
| 24 | 24 | ||
| 25 | - self.expects(:render).with(:file => 'blocks/categories', :locals => { :block => block}) | 25 | + self.expects(:render).with(template: 'blocks/categories', locals: {block: block}) |
| 26 | render_block_content(block) | 26 | render_block_content(block) |
| 27 | end | 27 | end |
| 28 | 28 |
test/unit/featured_products_block_test.rb
| @@ -109,7 +109,7 @@ class FeaturedProductsBlockTest < ActiveSupport::TestCase | @@ -109,7 +109,7 @@ class FeaturedProductsBlockTest < ActiveSupport::TestCase | ||
| 109 | should 'display feature products block' do | 109 | should 'display feature products block' do |
| 110 | block = FeaturedProductsBlock.new | 110 | block = FeaturedProductsBlock.new |
| 111 | 111 | ||
| 112 | - self.expects(:render).with(:file => 'blocks/featured_products', :locals => { :block => block}) | 112 | + self.expects(:render).with(template: 'blocks/featured_products', locals: {block: block}) |
| 113 | render_block_content(block) | 113 | render_block_content(block) |
| 114 | end | 114 | end |
| 115 | 115 |
test/unit/highlights_block_test.rb
| @@ -84,7 +84,7 @@ class HighlightsBlockTest < ActiveSupport::TestCase | @@ -84,7 +84,7 @@ class HighlightsBlockTest < ActiveSupport::TestCase | ||
| 84 | 84 | ||
| 85 | should 'display highlights block' do | 85 | should 'display highlights block' do |
| 86 | block = HighlightsBlock.new | 86 | block = HighlightsBlock.new |
| 87 | - self.expects(:render).with(:file => 'blocks/highlights', :locals => { :block => block}) | 87 | + self.expects(:render).with(template: 'blocks/highlights', locals: {block: block}) |
| 88 | 88 | ||
| 89 | render_block_content(block) | 89 | render_block_content(block) |
| 90 | end | 90 | end |
test/unit/profile_image_block_test.rb
| @@ -11,7 +11,7 @@ class ProfileImageBlockTest < ActiveSupport::TestCase | @@ -11,7 +11,7 @@ class ProfileImageBlockTest < ActiveSupport::TestCase | ||
| 11 | should 'display profile image' do | 11 | should 'display profile image' do |
| 12 | block = ProfileImageBlock.new | 12 | block = ProfileImageBlock.new |
| 13 | 13 | ||
| 14 | - self.expects(:render).with(:file => 'blocks/profile_image', :locals => { :block => block }) | 14 | + self.expects(:render).with(template: 'blocks/profile_image', locals: { block: block }) |
| 15 | render_block_content(block) | 15 | render_block_content(block) |
| 16 | end | 16 | end |
| 17 | 17 |
test/unit/profile_info_block_test.rb
| @@ -19,7 +19,7 @@ class ProfileInfoBlockTest < ActiveSupport::TestCase | @@ -19,7 +19,7 @@ class ProfileInfoBlockTest < ActiveSupport::TestCase | ||
| 19 | include BoxesHelper | 19 | include BoxesHelper |
| 20 | 20 | ||
| 21 | should 'display profile information' do | 21 | should 'display profile information' do |
| 22 | - self.expects(:render).with(:file => 'blocks/profile_info', :locals => { :block => block }) | 22 | + self.expects(:render).with(template: 'blocks/profile_info', locals: { block: block }) |
| 23 | render_block_content(block) | 23 | render_block_content(block) |
| 24 | end | 24 | end |
| 25 | 25 |
test/unit/profile_search_block_test.rb
| @@ -18,14 +18,14 @@ class ProfileSearchBlockTest < ActiveSupport::TestCase | @@ -18,14 +18,14 @@ class ProfileSearchBlockTest < ActiveSupport::TestCase | ||
| 18 | block = ProfileSearchBlock.new | 18 | block = ProfileSearchBlock.new |
| 19 | block.stubs(:owner).returns(person) | 19 | block.stubs(:owner).returns(person) |
| 20 | 20 | ||
| 21 | - self.expects(:render).with(:file => 'blocks/profile_search', :locals => { :block => block }) | 21 | + self.expects(:render).with(template: 'blocks/profile_search', locals: { block: block }) |
| 22 | render_block_content(block) | 22 | render_block_content(block) |
| 23 | end | 23 | end |
| 24 | 24 | ||
| 25 | should 'provide view_title' do | 25 | should 'provide view_title' do |
| 26 | person = fast_create(Person) | 26 | person = fast_create(Person) |
| 27 | person.boxes << Box.new | 27 | person.boxes << Box.new |
| 28 | - block = ProfileSearchBlock.new(:title => 'Title from block') | 28 | + block = ProfileSearchBlock.new(title: 'Title from block') |
| 29 | person.boxes.first.blocks << block | 29 | person.boxes.first.blocks << block |
| 30 | block.save! | 30 | block.save! |
| 31 | assert_equal 'Title from block', block.view_title | 31 | assert_equal 'Title from block', block.view_title |