[Git][noosfero/noosfero][master] blocks: fix rendering content when inheritance is involved
Bráulio Bhavamitra
gitlab at mg.gitlab.com
Thu Mar 17 20:22:55 BRT 2016
Bráulio Bhavamitra pushed to branch master at Noosfero / noosfero
Commits:
9e77f7f1 by Braulio Bhavamitra at 2016-03-17T20:22:05-03:00
blocks: fix rendering content when inheritance is involved
- - - - -
7 changed files:
- app/helpers/boxes_helper.rb
- test/unit/categories_block_test.rb
- test/unit/featured_products_block_test.rb
- test/unit/highlights_block_test.rb
- test/unit/profile_image_block_test.rb
- test/unit/profile_info_block_test.rb
- test/unit/profile_search_block_test.rb
Changes:
=====================================
app/helpers/boxes_helper.rb
=====================================
--- a/app/helpers/boxes_helper.rb
+++ b/app/helpers/boxes_helper.rb
@@ -87,41 +87,38 @@ module BoxesHelper
box_decorator == DontMoveBlocks
end
- def render_block_content(block)
- template_name = block.class.name.underscore.gsub('_block', '')
- template_filename = "#{template_name}.html.erb"
- if File.exists? Rails.root.join('app', 'views', 'blocks', template_filename)
- render :file => "blocks/#{template_name}", :locals => { :block => block }
- else
- nil
+ def render_block block, prefix = nil, klass = block.class
+ template_name = klass.name.underscore.sub '_block', ''
+ begin
+ render template: "blocks/#{prefix}#{template_name}", locals: { block: block }
+ rescue ActionView::MissingTemplate
+ return if klass.superclass === Block
+ render_block block, prefix, klass.superclass
end
end
- def render_block_footer(block)
- template_name = block.class.name.underscore.gsub('_block', '')
- template_filename = "#{template_name}.html.erb"
- if File.exists? Rails.root.join('app', 'views', 'blocks', 'footers', template_filename)
- render :file => "blocks/footers/#{template_name}", :locals => { :block => block }
- else
- nil
- end
+ def render_block_content block
+ # FIXME: this conditional 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.
+ return block.content if block.method(:content).owner != Block
+ render_block block
+ end
+
+ def render_block_footer block
+ return block.footer if block.method(:footer).owner != Block
+ render_block block, 'footers/'
end
def display_block_content(block, main_content = nil)
- # 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.
content = nil
if block.main?
content = wrap_main_content(main_content)
else
- if(block.method(:content).owner != Block)
- content = block.content()
- else
- content = render_block_content(block)
- end
+ content = render_block_content block
end
result = extract_block_content(content)
- # FIXME: this ternary conditional should be removed after all block footer from plugins methods get refactored into helpers and views
- footer_content = extract_block_content(block.method(:footer).owner != Block ? block.footer : render_block_footer(block))
+ footer_content = extract_block_content(render_block_footer block)
unless footer_content.blank?
footer_content = content_tag('div', footer_content, :class => 'block-footer-content' )
end
=====================================
test/unit/categories_block_test.rb
=====================================
--- a/test/unit/categories_block_test.rb
+++ b/test/unit/categories_block_test.rb
@@ -22,7 +22,7 @@ class CategoriesBlockTest < ActiveSupport::TestCase
should 'display category block' do
block = CategoriesBlock.new
- self.expects(:render).with(:file => 'blocks/categories', :locals => { :block => block})
+ self.expects(:render).with(template: 'blocks/categories', locals: {block: block})
render_block_content(block)
end
=====================================
test/unit/featured_products_block_test.rb
=====================================
--- a/test/unit/featured_products_block_test.rb
+++ b/test/unit/featured_products_block_test.rb
@@ -109,7 +109,7 @@ class FeaturedProductsBlockTest < ActiveSupport::TestCase
should 'display feature products block' do
block = FeaturedProductsBlock.new
- self.expects(:render).with(:file => 'blocks/featured_products', :locals => { :block => block})
+ self.expects(:render).with(template: 'blocks/featured_products', locals: {block: block})
render_block_content(block)
end
=====================================
test/unit/highlights_block_test.rb
=====================================
--- a/test/unit/highlights_block_test.rb
+++ b/test/unit/highlights_block_test.rb
@@ -84,7 +84,7 @@ class HighlightsBlockTest < ActiveSupport::TestCase
should 'display highlights block' do
block = HighlightsBlock.new
- self.expects(:render).with(:file => 'blocks/highlights', :locals => { :block => block})
+ self.expects(:render).with(template: 'blocks/highlights', locals: {block: block})
render_block_content(block)
end
=====================================
test/unit/profile_image_block_test.rb
=====================================
--- a/test/unit/profile_image_block_test.rb
+++ b/test/unit/profile_image_block_test.rb
@@ -11,7 +11,7 @@ class ProfileImageBlockTest < ActiveSupport::TestCase
should 'display profile image' do
block = ProfileImageBlock.new
- self.expects(:render).with(:file => 'blocks/profile_image', :locals => { :block => block })
+ self.expects(:render).with(template: 'blocks/profile_image', locals: { block: block })
render_block_content(block)
end
=====================================
test/unit/profile_info_block_test.rb
=====================================
--- a/test/unit/profile_info_block_test.rb
+++ b/test/unit/profile_info_block_test.rb
@@ -19,7 +19,7 @@ class ProfileInfoBlockTest < ActiveSupport::TestCase
include BoxesHelper
should 'display profile information' do
- self.expects(:render).with(:file => 'blocks/profile_info', :locals => { :block => block })
+ self.expects(:render).with(template: 'blocks/profile_info', locals: { block: block })
render_block_content(block)
end
=====================================
test/unit/profile_search_block_test.rb
=====================================
--- a/test/unit/profile_search_block_test.rb
+++ b/test/unit/profile_search_block_test.rb
@@ -18,14 +18,14 @@ class ProfileSearchBlockTest < ActiveSupport::TestCase
block = ProfileSearchBlock.new
block.stubs(:owner).returns(person)
- self.expects(:render).with(:file => 'blocks/profile_search', :locals => { :block => block })
+ self.expects(:render).with(template: 'blocks/profile_search', locals: { block: block })
render_block_content(block)
end
should 'provide view_title' do
person = fast_create(Person)
person.boxes << Box.new
- block = ProfileSearchBlock.new(:title => 'Title from block')
+ block = ProfileSearchBlock.new(title: 'Title from block')
person.boxes.first.blocks << block
block.save!
assert_equal 'Title from block', block.view_title
View it on GitLab: https://gitlab.com/noosfero/noosfero/commit/9e77f7f1282817f52c1bc12de1a0b8b6845516e4
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20160317/293def17/attachment.html>
More information about the Noosfero-dev
mailing list