From 6315c4a687f09bb86fac90a0fef64b663ba31ffe Mon Sep 17 00:00:00 2001 From: Leandro Nunes dos Santos Date: Mon, 25 Aug 2014 16:51:30 -0300 Subject: [PATCH] refactoring preview images to works with plugins and themes --- app/helpers/box_organizer_helper.rb | 37 +++++++++++++++++++++++++++++++++---- app/helpers/layout_helper.rb | 1 + app/models/block.rb | 5 ++--- app/views/box_organizer/show_block_type_info.html.erb | 12 ++---------- public/stylesheets/block_store.css | 30 ++++++++++++++++++++++++++---- test/unit/box_organizer_helper_test.rb | 184 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 248 insertions(+), 21 deletions(-) diff --git a/app/helpers/box_organizer_helper.rb b/app/helpers/box_organizer_helper.rb index 0756343..777a1cc 100644 --- a/app/helpers/box_organizer_helper.rb +++ b/app/helpers/box_organizer_helper.rb @@ -23,11 +23,40 @@ module BoxOrganizerHelper end def display_previews(block) -# def self.previews_path -# previews = Dir.glob(File.join(images_filesystem_path, 'previews/*')).map do |path| -# File.join(images_base_url_path, 'previews', File.basename(path)) + images_path = nil + plugin = @plugins.fetch_first_plugin(:has_block?, block) + + theme = Theme.new(environment.theme) # remove this + +# images_path = Dir.glob(File.join(theme.public_path, 'images', block.previews_path, '*')) + + + images_path = Dir.glob(File.join(theme.filesystem_path, 'images', block.preview_path, '*')) + images_path = images_path.map{|path| path.gsub(theme.filesystem_path, theme.public_path) } unless images_path.empty? + + images_path = Dir.glob(File.join(Rails.root, 'public', plugin.public_path, 'images', block.preview_path, '*')) if plugin && images_path.empty? + images_path = images_path.map{|path| path.gsub(File.join(Rails.root, 'public'), '') } unless images_path.empty? + + images_path = Dir.glob(File.join(Rails.root, 'public', 'images', block.preview_path, '*')) if images_path.empty? + images_path = images_path.map{|path| path.gsub(File.join(Rails.root, 'public', 'images'), '') } unless images_path.empty? + + images_path = 1.upto(3).map{block.default_preview_path} if images_path.empty? + +# if File.exists?(File.join(theme.filesystem_path, 'images', block.previews_path)) +# images_path = Dir.glob(File.join(theme.public_path, 'images', block.previews_path, '*')) +# elsif plugin && File.exists?(File.join(Rails.root, 'public', plugin.public_path, 'images', block.previews_path)) +# images_path = Dir.glob(File.join('/', plugin.public_path, 'images', block.previews_path, '*')) +# elsif File.exists?(File.join(Rails.root, 'public', 'images', block.previews_path)) +# images_path = block.previews_path +# else +# images_path = block.default_previews_path # end - '' + + content_tag(:ul, + images_path.map do |preview| + content_tag(:li, image_tag(preview, height: '240', width: '384', alt: '')) + end.join("\n") + ) end def icon_selector(icon = 'no-ico') diff --git a/app/helpers/layout_helper.rb b/app/helpers/layout_helper.rb index 64d0541..fcedc07 100644 --- a/app/helpers/layout_helper.rb +++ b/app/helpers/layout_helper.rb @@ -27,6 +27,7 @@ module LayoutHelper 'thickbox', 'lightbox', 'colorbox', + 'block_store', pngfix_stylesheet_path, ] + tokeninput_stylesheets plugins_stylesheets = @plugins.select(&:stylesheet?).map { |plugin| plugin.class.public_path('style.css') } diff --git a/app/models/block.rb b/app/models/block.rb index 82f1134..1b18776 100644 --- a/app/models/block.rb +++ b/app/models/block.rb @@ -247,9 +247,9 @@ class Block < ActiveRecord::Base duplicated_block end - def self.previews_path + def self.preview_path base_name = self.name.split('::').last.underscore - Dir.glob(File.join('blocks', base_name,'previews/*')) + File.join('blocks', base_name,'previews') end def self.icon_path @@ -265,5 +265,4 @@ class Block < ActiveRecord::Base "block_preview.png" end - end diff --git a/app/views/box_organizer/show_block_type_info.html.erb b/app/views/box_organizer/show_block_type_info.html.erb index 4674e10..ef194a3 100644 --- a/app/views/box_organizer/show_block_type_info.html.erb +++ b/app/views/box_organizer/show_block_type_info.html.erb @@ -1,6 +1,6 @@
-
+
<%= display_icon(@block) %>

<%= @block.pretty_name %>

<%= @block.short_description %>

@@ -8,15 +8,7 @@
- <% if @block.previews_path.empty? %> - <% for i in 0..2 %> - <%= image_tag(@block.default_preview, height: '240', width: '384', alt: '') %> - <% end %> - <% else %> - <% @block.previews_path.each do |preview| %> - <%= image_tag(preview, height: '240', width: '384', alt: '') %> - <% end %> - <% end %> + <%= display_previews(@block) %>
diff --git a/public/stylesheets/block_store.css b/public/stylesheets/block_store.css index 81cbc63..39f7c79 100644 --- a/public/stylesheets/block_store.css +++ b/public/stylesheets/block_store.css @@ -82,17 +82,26 @@ r, } #block-info-container #block-info-header { - display: inline-block; + float: left; +} + +#block-info-container #block-info-header .block-type-icon{ float: left; } #block-info-container #block-info-header h1 { - display: inline-block; + float: left; margin: 0; - font-weight: normal; - font-family: "Arial Black", Liberation Sans, Arial, sans-serif; + font-weight: normal; + font-family: "Arial Black", Liberation Sans, Arial, sans-serif; + margin-left: 5px; +} + +#block-info-container #block-info-header p{ + margin-left: 55px; } + #block-info-container h2 { margin: 0; margin-top: 10px; @@ -110,6 +119,19 @@ r, padding-top: 15px; } +#block-info-images ul{ + margin: 0px; + padding: 0px; +} +#block-info-images li{ + list-style: none; + display: inline; + +} + + #block-info-description { margin-top: 20px; } + + diff --git a/test/unit/box_organizer_helper_test.rb b/test/unit/box_organizer_helper_test.rb index 943da06..3002519 100644 --- a/test/unit/box_organizer_helper_test.rb +++ b/test/unit/box_organizer_helper_test.rb @@ -131,4 +131,188 @@ class BoxOrganizerHelperTest < ActionView::TestCase assert_match 'plugins/some/images/blocks/some_block/icon.png', display_icon(block) end +########################################## + + should 'display the default preview for block without previews images' do + class SomeBlock < Block; end + block = SomeBlock + @plugins = mock + @plugins.stubs(:fetch_first_plugin).with(:has_block?, block).returns(nil) + + doc = HTML::Document.new display_previews(block) + assert_select doc.root, 'li' do |elements| + assert_match /img.* src="\/images\/block_preview.png.*"/, elements[0].to_s + assert_match /img.* src="\/images\/block_preview.png.*"/, elements[1].to_s + assert_match /img.* src="\/images\/block_preview.png.*"/, elements[2].to_s + end + end + + should 'display the previews of block' do + class SomeBlock < Block; end + block = SomeBlock + @plugins = mock + @plugins.stubs(:fetch_first_plugin).with(:has_block?, block).returns(nil) + + Dir.stubs(:glob).returns([]) + base_path = File.join(Rails.root, 'public', 'images', '/blocks/some_block/previews/') + Dir.stubs(:glob).with(File.join(base_path, '*')).returns([File.join(base_path, 'p1.png'), File.join(base_path, 'p2.png')]) + doc = HTML::Document.new display_previews(block) + assert_select doc.root, 'li' do |elements| + assert_match /img.* src="\/blocks\/some_block\/previews\/p1.png"/, elements[0].to_s + assert_match /img.* src="\/blocks\/some_block\/previews\/p2.png"/, elements[1].to_s + end + end + + should 'display the plugin preview images of block' do + class SomeBlock < Block; end + block = SomeBlock + class SomePlugin < Noosfero::Plugin; end + SomePlugin.stubs(:name).returns('SomePlugin') + @plugins = mock + @plugins.stubs(:fetch_first_plugin).with(:has_block?, block).returns(SomePlugin) + + + Dir.stubs(:glob).returns([]) + base_path = File.join(Rails.root, 'public', 'plugins/some/', 'images', '/blocks/some_block/previews/') + Dir.stubs(:glob).with(File.join(base_path, '*')).returns([File.join(base_path, 'p1.png'), File.join(base_path, 'p2.png')]) + doc = HTML::Document.new display_previews(block) + assert_select doc.root, 'li' do |elements| + assert_match /img.* src="\/plugins\/some\/images\/blocks\/some_block\/previews\/p1.png"/, elements[0].to_s + assert_match /img.* src="\/plugins\/some\/images\/blocks\/some_block\/previews\/p2.png"/, elements[1].to_s + end + + end + + should 'display the theme previews of block' do + class SomeBlock < Block; end + block = SomeBlock + + @plugins = mock + @plugins.stubs(:fetch_first_plugin).with(:has_block?, block).returns(nil) + + @environment = mock + @environment.stubs(:theme).returns('some_theme') + + + Dir.stubs(:glob).returns([]) + base_path = File.join(Rails.root, 'public', 'designs/themes/some_theme/', 'images', '/blocks/some_block/previews/') + Dir.stubs(:glob).with(File.join(base_path, '*')).returns([File.join(base_path, 'p1.png'), File.join(base_path, 'p2.png')]) + doc = HTML::Document.new display_previews(block) + assert_select doc.root, 'li' do |elements| + assert_match /img.* src="\/designs\/themes\/some_theme\/images\/blocks\/some_block\/previews\/p1.png"/, elements[0].to_s + assert_match /img.* src="\/designs\/themes\/some_theme\/images\/blocks\/some_block\/previews\/p2.png"/, elements[1].to_s + end + + end + + should 'display the theme preview images of block instead of block preview images' do + class SomeBlock < Block; end + block = SomeBlock + + @plugins = mock + @plugins.stubs(:fetch_first_plugin).with(:has_block?, block).returns(nil) + + @environment = mock + @environment.stubs(:theme).returns('some_theme') + + Dir.stubs(:glob).returns([]) + base_path = File.join(Rails.root, 'public', 'designs/themes/some_theme/', 'images', '/blocks/some_block/previews/') + Dir.stubs(:glob).with(File.join(base_path, '*')).returns([File.join(base_path, 'p1.png'), File.join(base_path, 'p2.png')]) + + base_path = File.join(Rails.root, 'public', 'images', '/blocks/some_block/previews/') + Dir.stubs(:glob).with(File.join(base_path, '*')).returns([File.join(base_path, 'p1.png'), File.join(base_path, 'p2.png')]) + + doc = HTML::Document.new display_previews(block) + assert_select doc.root, 'li' do |elements| + assert_match /img.* src="\/designs\/themes\/some_theme\/images\/blocks\/some_block\/previews\/p1.png"/, elements[0].to_s + assert_match /img.* src="\/designs\/themes\/some_theme\/images\/blocks\/some_block\/previews\/p2.png"/, elements[1].to_s + end + end + + should 'display the theme preview images of block instead of plugin preview images' do + class SomeBlock < Block; end + block = SomeBlock + + class SomePlugin < Noosfero::Plugin; end + SomePlugin.stubs(:name).returns('SomePlugin') + @plugins = mock + @plugins.stubs(:fetch_first_plugin).with(:has_block?, block).returns(SomePlugin) + + @environment = mock + @environment.stubs(:theme).returns('some_theme') + + Dir.stubs(:glob).returns([]) + base_path = File.join(Rails.root, 'public', 'designs/themes/some_theme/', 'images', '/blocks/some_block/previews/') + Dir.stubs(:glob).with(File.join(base_path, '*')).returns([File.join(base_path, 'p1.png'), File.join(base_path, 'p2.png')]) + + base_path = File.join(Rails.root, 'public', 'plugins/some/', 'images', '/blocks/some_block/previews/') + Dir.stubs(:glob).with(File.join(base_path, '*')).returns([File.join(base_path, 'p1.png'), File.join(base_path, 'p2.png')]) + + doc = HTML::Document.new display_previews(block) + assert_select doc.root, 'li' do |elements| + assert_match /img.* src="\/designs\/themes\/some_theme\/images\/blocks\/some_block\/previews\/p1.png"/, elements[0].to_s + assert_match /img.* src="\/designs\/themes\/some_theme\/images\/blocks\/some_block\/previews\/p2.png"/, elements[1].to_s + end + + end + + should 'display the theme preview images of block instead of block previews and plugin previews' do + class SomeBlock < Block; end + block = SomeBlock + + class SomePlugin < Noosfero::Plugin; end + SomePlugin.stubs(:name).returns('SomePlugin') + @plugins = mock + @plugins.stubs(:fetch_first_plugin).with(:has_block?, block).returns(SomePlugin) + + + @environment = mock + @environment.stubs(:theme).returns('some_theme') + + Dir.stubs(:glob).returns([]) + base_path = File.join(Rails.root, 'public', 'designs/themes/some_theme/', 'images', '/blocks/some_block/previews/') + Dir.stubs(:glob).with(File.join(base_path, '*')).returns([File.join(base_path, 'p1.png'), File.join(base_path, 'p2.png')]) + + base_path = File.join(Rails.root, 'public', 'plugins/some/', 'images', '/blocks/some_block/previews/') + Dir.stubs(:glob).with(File.join(base_path, '*')).returns([File.join(base_path, 'p1.png'), File.join(base_path, 'p2.png')]) + + base_path = File.join(Rails.root, 'public', 'images', '/blocks/some_block/previews/') + Dir.stubs(:glob).with(File.join(base_path, '*')).returns([File.join(base_path, 'p1.png'), File.join(base_path, 'p2.png')]) + + doc = HTML::Document.new display_previews(block) + assert_select doc.root, 'li' do |elements| + assert_match /img.* src="\/designs\/themes\/some_theme\/images\/blocks\/some_block\/previews\/p1.png"/, elements[0].to_s + assert_match /img.* src="\/designs\/themes\/some_theme\/images\/blocks\/some_block\/previews\/p2.png"/, elements[1].to_s + end + + end + + should 'display the plugin preview images of block instead of block previews' do + class SomeBlock < Block; end + block = SomeBlock + + class SomePlugin < Noosfero::Plugin; end + SomePlugin.stubs(:name).returns('SomePlugin') + @plugins = mock + @plugins.stubs(:fetch_first_plugin).with(:has_block?, block).returns(SomePlugin) + + Dir.stubs(:glob).returns([]) + base_path = File.join(Rails.root, 'public', 'designs/themes/some_theme/', 'images', '/blocks/some_block/previews/') + Dir.stubs(:glob).with(File.join(base_path, '*')).returns([File.join(base_path, 'p1.png'), File.join(base_path, 'p2.png')]) + + base_path = File.join(Rails.root, 'public', 'plugins/some/', 'images', '/blocks/some_block/previews/') + Dir.stubs(:glob).with(File.join(base_path, '*')).returns([File.join(base_path, 'p1.png'), File.join(base_path, 'p2.png')]) + + base_path = File.join(Rails.root, 'public', 'images', '/blocks/some_block/previews/') + Dir.stubs(:glob).with(File.join(base_path, '*')).returns([File.join(base_path, 'p1.png'), File.join(base_path, 'p2.png')]) + + doc = HTML::Document.new display_previews(block) + assert_select doc.root, 'li' do |elements| + assert_match /img.* src="\/plugins\/some\/images\/blocks\/some_block\/previews\/p1.png"/, elements[0].to_s + assert_match /img.* src="\/plugins\/some\/images\/blocks\/some_block\/previews\/p2.png"/, elements[1].to_s + end + + end + + end -- libgit2 0.21.2