Commit 6315c4a687f09bb86fac90a0fef64b663ba31ffe

Authored by Leandro Santos
1 parent ec28af31

refactoring preview images to works with plugins and themes

app/helpers/box_organizer_helper.rb
... ... @@ -23,11 +23,40 @@ module BoxOrganizerHelper
23 23 end
24 24  
25 25 def display_previews(block)
26   -# def self.previews_path
27   -# previews = Dir.glob(File.join(images_filesystem_path, 'previews/*')).map do |path|
28   -# File.join(images_base_url_path, 'previews', File.basename(path))
  26 + images_path = nil
  27 + plugin = @plugins.fetch_first_plugin(:has_block?, block)
  28 +
  29 + theme = Theme.new(environment.theme) # remove this
  30 +
  31 +# images_path = Dir.glob(File.join(theme.public_path, 'images', block.previews_path, '*'))
  32 +
  33 +
  34 + images_path = Dir.glob(File.join(theme.filesystem_path, 'images', block.preview_path, '*'))
  35 + images_path = images_path.map{|path| path.gsub(theme.filesystem_path, theme.public_path) } unless images_path.empty?
  36 +
  37 + images_path = Dir.glob(File.join(Rails.root, 'public', plugin.public_path, 'images', block.preview_path, '*')) if plugin && images_path.empty?
  38 + images_path = images_path.map{|path| path.gsub(File.join(Rails.root, 'public'), '') } unless images_path.empty?
  39 +
  40 + images_path = Dir.glob(File.join(Rails.root, 'public', 'images', block.preview_path, '*')) if images_path.empty?
  41 + images_path = images_path.map{|path| path.gsub(File.join(Rails.root, 'public', 'images'), '') } unless images_path.empty?
  42 +
  43 + images_path = 1.upto(3).map{block.default_preview_path} if images_path.empty?
  44 +
  45 +# if File.exists?(File.join(theme.filesystem_path, 'images', block.previews_path))
  46 +# images_path = Dir.glob(File.join(theme.public_path, 'images', block.previews_path, '*'))
  47 +# elsif plugin && File.exists?(File.join(Rails.root, 'public', plugin.public_path, 'images', block.previews_path))
  48 +# images_path = Dir.glob(File.join('/', plugin.public_path, 'images', block.previews_path, '*'))
  49 +# elsif File.exists?(File.join(Rails.root, 'public', 'images', block.previews_path))
  50 +# images_path = block.previews_path
  51 +# else
  52 +# images_path = block.default_previews_path
29 53 # end
30   - ''
  54 +
  55 + content_tag(:ul,
  56 + images_path.map do |preview|
  57 + content_tag(:li, image_tag(preview, height: '240', width: '384', alt: ''))
  58 + end.join("\n")
  59 + )
31 60 end
32 61  
33 62 def icon_selector(icon = 'no-ico')
... ...
app/helpers/layout_helper.rb
... ... @@ -27,6 +27,7 @@ module LayoutHelper
27 27 'thickbox',
28 28 'lightbox',
29 29 'colorbox',
  30 + 'block_store',
30 31 pngfix_stylesheet_path,
31 32 ] + tokeninput_stylesheets
32 33 plugins_stylesheets = @plugins.select(&:stylesheet?).map { |plugin| plugin.class.public_path('style.css') }
... ...
app/models/block.rb
... ... @@ -247,9 +247,9 @@ class Block < ActiveRecord::Base
247 247 duplicated_block
248 248 end
249 249  
250   - def self.previews_path
  250 + def self.preview_path
251 251 base_name = self.name.split('::').last.underscore
252   - Dir.glob(File.join('blocks', base_name,'previews/*'))
  252 + File.join('blocks', base_name,'previews')
253 253 end
254 254  
255 255 def self.icon_path
... ... @@ -265,5 +265,4 @@ class Block < ActiveRecord::Base
265 265 "block_preview.png"
266 266 end
267 267  
268   -
269 268 end
... ...
app/views/box_organizer/show_block_type_info.html.erb
1 1 <div id="block-info-container">
2 2  
3   - <div id="bs-block-header">
  3 + <div id="block-info-header">
4 4 <%= display_icon(@block) %>
5 5 <h1><%= @block.pretty_name %></h1>
6 6 <p><%= @block.short_description %></p>
... ... @@ -8,15 +8,7 @@
8 8  
9 9 <div id="block-info-images">
10 10 <div style="white-space: nowrap;">
11   - <% if @block.previews_path.empty? %>
12   - <% for i in 0..2 %>
13   - <%= image_tag(@block.default_preview, height: '240', width: '384', alt: '') %>
14   - <% end %>
15   - <% else %>
16   - <% @block.previews_path.each do |preview| %>
17   - <%= image_tag(preview, height: '240', width: '384', alt: '') %>
18   - <% end %>
19   - <% end %>
  11 + <%= display_previews(@block) %>
20 12 </div>
21 13 </div>
22 14  
... ...
public/stylesheets/block_store.css
... ... @@ -82,17 +82,26 @@ r,
82 82 }
83 83  
84 84 #block-info-container #block-info-header {
85   - display: inline-block;
  85 + float: left;
  86 +}
  87 +
  88 +#block-info-container #block-info-header .block-type-icon{
86 89 float: left;
87 90 }
88 91  
89 92 #block-info-container #block-info-header h1 {
90   - display: inline-block;
  93 + float: left;
91 94 margin: 0;
92   - font-weight: normal;
93   - font-family: "Arial Black", Liberation Sans, Arial, sans-serif;
  95 + font-weight: normal;
  96 + font-family: "Arial Black", Liberation Sans, Arial, sans-serif;
  97 + margin-left: 5px;
  98 +}
  99 +
  100 +#block-info-container #block-info-header p{
  101 + margin-left: 55px;
94 102 }
95 103  
  104 +
96 105 #block-info-container h2 {
97 106 margin: 0;
98 107 margin-top: 10px;
... ... @@ -110,6 +119,19 @@ r,
110 119 padding-top: 15px;
111 120 }
112 121  
  122 +#block-info-images ul{
  123 + margin: 0px;
  124 + padding: 0px;
  125 +}
  126 +#block-info-images li{
  127 + list-style: none;
  128 + display: inline;
  129 +
  130 +}
  131 +
  132 +
113 133 #block-info-description {
114 134 margin-top: 20px;
115 135 }
  136 +
  137 +
... ...
test/unit/box_organizer_helper_test.rb
... ... @@ -131,4 +131,188 @@ class BoxOrganizerHelperTest &lt; ActionView::TestCase
131 131 assert_match 'plugins/some/images/blocks/some_block/icon.png', display_icon(block)
132 132 end
133 133  
  134 +##########################################
  135 +
  136 + should 'display the default preview for block without previews images' do
  137 + class SomeBlock < Block; end
  138 + block = SomeBlock
  139 + @plugins = mock
  140 + @plugins.stubs(:fetch_first_plugin).with(:has_block?, block).returns(nil)
  141 +
  142 + doc = HTML::Document.new display_previews(block)
  143 + assert_select doc.root, 'li' do |elements|
  144 + assert_match /img.* src="\/images\/block_preview.png.*"/, elements[0].to_s
  145 + assert_match /img.* src="\/images\/block_preview.png.*"/, elements[1].to_s
  146 + assert_match /img.* src="\/images\/block_preview.png.*"/, elements[2].to_s
  147 + end
  148 + end
  149 +
  150 + should 'display the previews of block' do
  151 + class SomeBlock < Block; end
  152 + block = SomeBlock
  153 + @plugins = mock
  154 + @plugins.stubs(:fetch_first_plugin).with(:has_block?, block).returns(nil)
  155 +
  156 + Dir.stubs(:glob).returns([])
  157 + base_path = File.join(Rails.root, 'public', 'images', '/blocks/some_block/previews/')
  158 + Dir.stubs(:glob).with(File.join(base_path, '*')).returns([File.join(base_path, 'p1.png'), File.join(base_path, 'p2.png')])
  159 + doc = HTML::Document.new display_previews(block)
  160 + assert_select doc.root, 'li' do |elements|
  161 + assert_match /img.* src="\/blocks\/some_block\/previews\/p1.png"/, elements[0].to_s
  162 + assert_match /img.* src="\/blocks\/some_block\/previews\/p2.png"/, elements[1].to_s
  163 + end
  164 + end
  165 +
  166 + should 'display the plugin preview images of block' do
  167 + class SomeBlock < Block; end
  168 + block = SomeBlock
  169 + class SomePlugin < Noosfero::Plugin; end
  170 + SomePlugin.stubs(:name).returns('SomePlugin')
  171 + @plugins = mock
  172 + @plugins.stubs(:fetch_first_plugin).with(:has_block?, block).returns(SomePlugin)
  173 +
  174 +
  175 + Dir.stubs(:glob).returns([])
  176 + base_path = File.join(Rails.root, 'public', 'plugins/some/', 'images', '/blocks/some_block/previews/')
  177 + Dir.stubs(:glob).with(File.join(base_path, '*')).returns([File.join(base_path, 'p1.png'), File.join(base_path, 'p2.png')])
  178 + doc = HTML::Document.new display_previews(block)
  179 + assert_select doc.root, 'li' do |elements|
  180 + assert_match /img.* src="\/plugins\/some\/images\/blocks\/some_block\/previews\/p1.png"/, elements[0].to_s
  181 + assert_match /img.* src="\/plugins\/some\/images\/blocks\/some_block\/previews\/p2.png"/, elements[1].to_s
  182 + end
  183 +
  184 + end
  185 +
  186 + should 'display the theme previews of block' do
  187 + class SomeBlock < Block; end
  188 + block = SomeBlock
  189 +
  190 + @plugins = mock
  191 + @plugins.stubs(:fetch_first_plugin).with(:has_block?, block).returns(nil)
  192 +
  193 + @environment = mock
  194 + @environment.stubs(:theme).returns('some_theme')
  195 +
  196 +
  197 + Dir.stubs(:glob).returns([])
  198 + base_path = File.join(Rails.root, 'public', 'designs/themes/some_theme/', 'images', '/blocks/some_block/previews/')
  199 + Dir.stubs(:glob).with(File.join(base_path, '*')).returns([File.join(base_path, 'p1.png'), File.join(base_path, 'p2.png')])
  200 + doc = HTML::Document.new display_previews(block)
  201 + assert_select doc.root, 'li' do |elements|
  202 + assert_match /img.* src="\/designs\/themes\/some_theme\/images\/blocks\/some_block\/previews\/p1.png"/, elements[0].to_s
  203 + assert_match /img.* src="\/designs\/themes\/some_theme\/images\/blocks\/some_block\/previews\/p2.png"/, elements[1].to_s
  204 + end
  205 +
  206 + end
  207 +
  208 + should 'display the theme preview images of block instead of block preview images' do
  209 + class SomeBlock < Block; end
  210 + block = SomeBlock
  211 +
  212 + @plugins = mock
  213 + @plugins.stubs(:fetch_first_plugin).with(:has_block?, block).returns(nil)
  214 +
  215 + @environment = mock
  216 + @environment.stubs(:theme).returns('some_theme')
  217 +
  218 + Dir.stubs(:glob).returns([])
  219 + base_path = File.join(Rails.root, 'public', 'designs/themes/some_theme/', 'images', '/blocks/some_block/previews/')
  220 + Dir.stubs(:glob).with(File.join(base_path, '*')).returns([File.join(base_path, 'p1.png'), File.join(base_path, 'p2.png')])
  221 +
  222 + base_path = File.join(Rails.root, 'public', 'images', '/blocks/some_block/previews/')
  223 + Dir.stubs(:glob).with(File.join(base_path, '*')).returns([File.join(base_path, 'p1.png'), File.join(base_path, 'p2.png')])
  224 +
  225 + doc = HTML::Document.new display_previews(block)
  226 + assert_select doc.root, 'li' do |elements|
  227 + assert_match /img.* src="\/designs\/themes\/some_theme\/images\/blocks\/some_block\/previews\/p1.png"/, elements[0].to_s
  228 + assert_match /img.* src="\/designs\/themes\/some_theme\/images\/blocks\/some_block\/previews\/p2.png"/, elements[1].to_s
  229 + end
  230 + end
  231 +
  232 + should 'display the theme preview images of block instead of plugin preview images' do
  233 + class SomeBlock < Block; end
  234 + block = SomeBlock
  235 +
  236 + class SomePlugin < Noosfero::Plugin; end
  237 + SomePlugin.stubs(:name).returns('SomePlugin')
  238 + @plugins = mock
  239 + @plugins.stubs(:fetch_first_plugin).with(:has_block?, block).returns(SomePlugin)
  240 +
  241 + @environment = mock
  242 + @environment.stubs(:theme).returns('some_theme')
  243 +
  244 + Dir.stubs(:glob).returns([])
  245 + base_path = File.join(Rails.root, 'public', 'designs/themes/some_theme/', 'images', '/blocks/some_block/previews/')
  246 + Dir.stubs(:glob).with(File.join(base_path, '*')).returns([File.join(base_path, 'p1.png'), File.join(base_path, 'p2.png')])
  247 +
  248 + base_path = File.join(Rails.root, 'public', 'plugins/some/', 'images', '/blocks/some_block/previews/')
  249 + Dir.stubs(:glob).with(File.join(base_path, '*')).returns([File.join(base_path, 'p1.png'), File.join(base_path, 'p2.png')])
  250 +
  251 + doc = HTML::Document.new display_previews(block)
  252 + assert_select doc.root, 'li' do |elements|
  253 + assert_match /img.* src="\/designs\/themes\/some_theme\/images\/blocks\/some_block\/previews\/p1.png"/, elements[0].to_s
  254 + assert_match /img.* src="\/designs\/themes\/some_theme\/images\/blocks\/some_block\/previews\/p2.png"/, elements[1].to_s
  255 + end
  256 +
  257 + end
  258 +
  259 + should 'display the theme preview images of block instead of block previews and plugin previews' do
  260 + class SomeBlock < Block; end
  261 + block = SomeBlock
  262 +
  263 + class SomePlugin < Noosfero::Plugin; end
  264 + SomePlugin.stubs(:name).returns('SomePlugin')
  265 + @plugins = mock
  266 + @plugins.stubs(:fetch_first_plugin).with(:has_block?, block).returns(SomePlugin)
  267 +
  268 +
  269 + @environment = mock
  270 + @environment.stubs(:theme).returns('some_theme')
  271 +
  272 + Dir.stubs(:glob).returns([])
  273 + base_path = File.join(Rails.root, 'public', 'designs/themes/some_theme/', 'images', '/blocks/some_block/previews/')
  274 + Dir.stubs(:glob).with(File.join(base_path, '*')).returns([File.join(base_path, 'p1.png'), File.join(base_path, 'p2.png')])
  275 +
  276 + base_path = File.join(Rails.root, 'public', 'plugins/some/', 'images', '/blocks/some_block/previews/')
  277 + Dir.stubs(:glob).with(File.join(base_path, '*')).returns([File.join(base_path, 'p1.png'), File.join(base_path, 'p2.png')])
  278 +
  279 + base_path = File.join(Rails.root, 'public', 'images', '/blocks/some_block/previews/')
  280 + Dir.stubs(:glob).with(File.join(base_path, '*')).returns([File.join(base_path, 'p1.png'), File.join(base_path, 'p2.png')])
  281 +
  282 + doc = HTML::Document.new display_previews(block)
  283 + assert_select doc.root, 'li' do |elements|
  284 + assert_match /img.* src="\/designs\/themes\/some_theme\/images\/blocks\/some_block\/previews\/p1.png"/, elements[0].to_s
  285 + assert_match /img.* src="\/designs\/themes\/some_theme\/images\/blocks\/some_block\/previews\/p2.png"/, elements[1].to_s
  286 + end
  287 +
  288 + end
  289 +
  290 + should 'display the plugin preview images of block instead of block previews' do
  291 + class SomeBlock < Block; end
  292 + block = SomeBlock
  293 +
  294 + class SomePlugin < Noosfero::Plugin; end
  295 + SomePlugin.stubs(:name).returns('SomePlugin')
  296 + @plugins = mock
  297 + @plugins.stubs(:fetch_first_plugin).with(:has_block?, block).returns(SomePlugin)
  298 +
  299 + Dir.stubs(:glob).returns([])
  300 + base_path = File.join(Rails.root, 'public', 'designs/themes/some_theme/', 'images', '/blocks/some_block/previews/')
  301 + Dir.stubs(:glob).with(File.join(base_path, '*')).returns([File.join(base_path, 'p1.png'), File.join(base_path, 'p2.png')])
  302 +
  303 + base_path = File.join(Rails.root, 'public', 'plugins/some/', 'images', '/blocks/some_block/previews/')
  304 + Dir.stubs(:glob).with(File.join(base_path, '*')).returns([File.join(base_path, 'p1.png'), File.join(base_path, 'p2.png')])
  305 +
  306 + base_path = File.join(Rails.root, 'public', 'images', '/blocks/some_block/previews/')
  307 + Dir.stubs(:glob).with(File.join(base_path, '*')).returns([File.join(base_path, 'p1.png'), File.join(base_path, 'p2.png')])
  308 +
  309 + doc = HTML::Document.new display_previews(block)
  310 + assert_select doc.root, 'li' do |elements|
  311 + assert_match /img.* src="\/plugins\/some\/images\/blocks\/some_block\/previews\/p1.png"/, elements[0].to_s
  312 + assert_match /img.* src="\/plugins\/some\/images\/blocks\/some_block\/previews\/p2.png"/, elements[1].to_s
  313 + end
  314 +
  315 + end
  316 +
  317 +
134 318 end
... ...