Commit f4d329dee52a3de7048d7db3875372c322ca71c3

Authored by AntonioTerceiro
1 parent 5660d7f4

r237@sede: terceiro | 2007-07-28 19:39:59 -0300

ActionItem0: finishing Design:Helper, It's generating code beautifully!
 


git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@241 3f533792-8f58-4932-b0fe-aaf55b0a4547
vendor/plugins/design/lib/design/helper.rb
@@ -2,7 +2,8 @@ module Design @@ -2,7 +2,8 @@ module Design
2 2
3 module Helper 3 module Helper
4 4
5 - # proxies calls to controller's design method 5 + # proxies calls to controller's design method to get the design information
  6 + # holder object
6 def design 7 def design
7 @controller.send(:design) 8 @controller.send(:design)
8 end 9 end
@@ -46,13 +47,36 @@ module Design @@ -46,13 +47,36 @@ module Design
46 # 47 #
47 def design_display_blocks(box, content = "") 48 def design_display_blocks(box, content = "")
48 blocks = box.blocks_sort_by_position 49 blocks = box.blocks_sort_by_position
49 - content_tag(:div,  
50 - blocks.map { |b|  
51 - content_tag(:div, b.main? ? content : self.send('list_content', b.content), :class =>"block_item_box_#{box.number}" , :id => "block_#{b.id}" )  
52 - }.join("\n"), :id => "frame_#{box.number}"  
53 - ) 50 + blocks.map do |block|
  51 + # FIXME: should that actually be list_content?
  52 + text = block.main? ? content : design_block_content(block)
  53 + content_tag(:div, text, :class => "block" , :id => "block_#{block.id}" )
  54 + end.join("\n")
  55 + end
  56 +
  57 + # Displays the content of a block. See plugin README for details about the
  58 + # possibilities.
  59 + def design_block_content(block)
  60 + content = block.content
  61 + case content
  62 + when Proc
  63 + self.instance_eval(&content)
  64 + when Array
  65 + content_tag(
  66 + 'ul',
  67 + content.map do |c|
  68 + content_tag(
  69 + 'li',
  70 + c
  71 + )
  72 + end
  73 + )
  74 + else
  75 + content.to_s
  76 + end
54 end 77 end
55 78
  79 + # TODO: test stuff below this comment
56 80
57 ######################################################## 81 ########################################################
58 # Template 82 # Template
@@ -136,244 +160,6 @@ module Design @@ -136,244 +160,6 @@ module Design
136 files_path.map{|f| f.gsub(/.*#{remove_until}/, '')} 160 files_path.map{|f| f.gsub(/.*#{remove_until}/, '')}
137 end 161 end
138 162
139 -  
140 - end # END SHOW TEMPLATE HELPER  
141 -  
142 - module EditTemplateHelper  
143 -  
144 -  
145 - # Symbol dictionary used on select when we add or edit a block.  
146 - # This method has the responsability of translate a Block class in a humam name  
147 - # By default the class "MainBlock" has the human name "Main Block". Other classes  
148 - # defined by user are not going to display in a human name format until de method  
149 - # flexible_template_block_dict be redefined in a controller by user  
150 -  
151 -#TODO define the method flexible_template_block_dict if not defined by helper  
152 -# if !self.public_instance_methods.include? "flexible_template_block_dict"  
153 -# define_method('flexible_template_block_dict') do |str|  
154 -# {  
155 -# 'MainBlock' => _("Main Block")  
156 -# }[str] || str  
157 -# end  
158 -# end  
159 -  
160 -  
161 - def flexible_template_block_helper_dict(str)  
162 - {  
163 - 'plain_content' => _('Plain Content') ,  
164 - 'list_content' => _('List Content')  
165 - }[str] || str  
166 - end  
167 -  
168 -  
169 - private  
170 -  
171 - #################################################  
172 - # TEMPLATES METHODS RELATED  
173 - #################################################  
174 -  
175 - # Shows the blocks as defined in <tt>show_blocks</tt> adding the sortable and draggable elements.  
176 - # In this case the layout can be manipulated  
177 - def edit_blocks(box, main_content = "")  
178 - blocks = box.blocks_sort_by_position  
179 - [  
180 - content_tag(  
181 - :ul,[  
182 - box.name,  
183 - link_to_active_sort(box),  
184 - link_to_add_block(box),  
185 - blocks.map {|b|  
186 - [content_tag(  
187 - :li,  
188 - b.name + link_to_destroy_block(b),  
189 - :class =>"block_item_box_#{box.number}" , :id => "block_#{b.id}"  
190 - ),  
191 - draggable("block_#{b.id}")].join("\n")  
192 - }.join("\n")].join("\n"), :id => "sort_#{box.number}"  
193 - ),  
194 - drag_drop_items(box)].join("\n")  
195 - end  
196 -  
197 - def link_to_active_sort(box)  
198 - link_to_remote(_('Sort'),  
199 - {:update => "sort_#{box.number}", :url => {:action => 'flexible_template_set_sort_mode', :box_id => box.id }},  
200 - :class => 'sort_button')  
201 - end  
202 -  
203 - def link_to_add_block(box)  
204 - link_to_remote(_('Add Block'),  
205 - {:update => "sort_#{box.number}", :url => {:action => 'flexible_template_new_block', :box_id => box.id }},  
206 - :class => 'add_block_button')  
207 - end  
208 -  
209 - def link_to_destroy_block(block)  
210 - link_to_remote(_('Remove'),  
211 - {:update => "sort_#{block.box.number}", :url => {:action => 'flexible_template_destroy_block', :block_id => block.id }},  
212 - :class => 'destroy_block_button')  
213 - end  
214 -  
215 -  
216 - # Allows the biven box to have sortable elements  
217 - def sortable_block(box_number)  
218 - sortable_element "sort_#{box_number}",  
219 - :url => {:action => 'flexible_template_sort_box', :box_number => box_number },  
220 - :complete => visual_effect(:highlight, "sort_#{box_number}")  
221 - end  
222 -  
223 - # Allows an element item to be draggable  
224 - def draggable(item)  
225 - draggable_element(item, :ghosting => true, :revert => true)  
226 - end  
227 -  
228 - # Allows an draggable element change between diferrents boxes  
229 - def drag_drop_items(box)  
230 - boxes = @ft_config[:boxes].reject{|b| b.id == box.id}  
231 -  
232 - boxes.map{ |b|  
233 - drop_receiving_element("box_#{box.number}",  
234 - :accept => "block_item_box_#{b.number}",  
235 - :complete => "$('spinner').hide();",  
236 - :before => "$('spinner').show();",  
237 - :hoverclass => 'hover',  
238 - :with => "'block=' + encodeURIComponent(element.id.split('_').last())",  
239 - :url => {:action=>:flexible_template_change_box, :box_id => box.id})  
240 - }.to_s  
241 - end  
242 -  
243 -  
244 - # Generate a select option to choose one of the available templates.  
245 - # The available templates are those in 'public/templates'  
246 - def select_template  
247 - available_templates = @ft_config[:available_templates]  
248 -  
249 - template_options = options_for_select(available_templates.map{|template| [template, template] }, @ft_config[:template])  
250 - [ select_tag('template_name', template_options ),  
251 - change_template].join("\n")  
252 - end  
253 -  
254 - # Generate a observer to reload a page when a template is selected  
255 - def change_template  
256 - observe_field( 'template_name',  
257 - :url => {:action => 'set_default_template'},  
258 - :with =>"'template_name=' + escape(value) + '&object_id=' + escape(#{@ft_config[:owner].id})",  
259 - :complete => "document.location.reload();"  
260 - )  
261 - end  
262 -  
263 - def available_blocks  
264 -#TODO check if are valids blocks  
265 - h = {  
266 - 'MainBlock' => _("Main Block"),  
267 - }  
268 - h.merge!(controller.class::FLEXIBLE_TEMPLATE_AVAILABLE_BLOCKS) if controller.class.constants.include? "FLEXIBLE_TEMPLATE_AVAILABLE_BLOCKS"  
269 - h  
270 - end  
271 -  
272 - def block_helpers  
273 -#TODO check if are valids helpers  
274 - h = {  
275 - 'plain_content' => _("Plain Content"),  
276 - 'list_content' => _("Simple List Content"),  
277 - }  
278 - h.merge!(controller.class::FLEXIBLE_TEMPLATE_BLOCK_HELPER) if controller.class.constants.include? "FLEXIBLE_TEMPLATE_BLOCK_HELPER"  
279 - h  
280 - end  
281 -  
282 - def new_block_form(box)  
283 - type_block_options = options_for_select(available_blocks.collect{|k,v| [v,k] })  
284 - type_block_helper_options = options_for_select(block_helpers.collect{|k,v| [v,k] })  
285 - @block = Block.new  
286 - @block.box = box  
287 -  
288 - _("Adding block on %s") % box.name +  
289 - [  
290 - form_remote_tag(:url => {:action => 'flexible_template_create_block'}, :update => "sort_#{box.number}"),  
291 - hidden_field('block', 'box_id'),  
292 - content_tag(  
293 - :p,  
294 - [  
295 - content_tag(  
296 - :label, _('Name:')  
297 - ),  
298 - text_field('block', 'name')  
299 - ].join("\n")  
300 - ),  
301 - content_tag(  
302 - :p,  
303 - [  
304 - content_tag(  
305 - :label, _('Title:')  
306 - ),  
307 - text_field('block', 'title')  
308 - ].join("\n")  
309 - ),  
310 - content_tag(  
311 - :p,  
312 - [  
313 - content_tag(  
314 - :label, _('Type:')  
315 - ),  
316 - select_tag('block[type]', type_block_options)  
317 - ].join("\n")  
318 - ),  
319 - content_tag(  
320 - :p,  
321 - [  
322 - content_tag(  
323 - :label, _('Visualization Mode:')  
324 - ),  
325 - select_tag('block[helper]', type_block_helper_options)  
326 - ].join("\n")  
327 - ),  
328 - submit_tag( _('Submit')),  
329 - end_form_tag  
330 - ].join("\n")  
331 -  
332 - end  
333 -  
334 - # Generate a select option to choose one of the available themes.  
335 - # The available themes are those in 'public/themes'  
336 - def select_theme  
337 - available_themes = @ft_config[:available_themes]  
338 - theme_options = options_for_select(available_themes.map{|theme| [theme, theme] }, @ft_config[:theme])  
339 - [ select_tag('theme_name', theme_options ),  
340 - change_theme].join("\n")  
341 - end  
342 -  
343 - # Generate a observer to reload a page when a theme is selected  
344 - def change_theme  
345 - observe_field( 'theme_name',  
346 - :url => {:action => 'set_default_theme'},  
347 - :with =>"'theme_name=' + escape(value) + '&object_id=' + escape(#{@ft_config[:owner].id})",  
348 - :complete => "document.location.reload();"  
349 - )  
350 - end  
351 -  
352 -  
353 - #################################################  
354 - #ICONS THEMES RELATED  
355 - #################################################  
356 -  
357 - # Generate a select option to choose one of the available icons themes.  
358 - # The available icons themes are those in 'public/icons'  
359 - def select_icon_theme  
360 - available_icon_themes = @ft_config[:available_icon_themes]  
361 - icon_theme_options = options_for_select(available_icon_themes.map{|icon_theme| [icon_theme, icon_theme] }, @ft_config[:icon_theme])  
362 - [ select_tag('icon_theme_name', icon_theme_options ),  
363 - change_icon_theme].join("\n")  
364 - end  
365 -  
366 - # Generate a observer to reload a page when a icons theme is selected  
367 - def change_icon_theme  
368 - observe_field( 'icon_theme_name',  
369 - :url => {:action => 'set_default_icon_theme'},  
370 - :with =>"'icon_theme_name=' + escape(value) + '&object_id=' + escape(#{@ft_config[:owner].id})",  
371 - :complete => "document.location.reload();"  
372 - )  
373 - end  
374 -  
375 -  
376 - end # END OF module HElper  
377 - 163 + end # END OF module Helper
378 164
379 end #END OF module Design 165 end #END OF module Design
vendor/plugins/design/test/design_helper_test.rb
@@ -3,9 +3,30 @@ require File.join(File.dirname(__FILE__), &#39;test_helper&#39;) @@ -3,9 +3,30 @@ require File.join(File.dirname(__FILE__), &#39;test_helper&#39;)
3 class DesignHelperTestController < ActionController::Base 3 class DesignHelperTestController < ActionController::Base
4 4
5 box1 = Design::Box.new(:number => 1) 5 box1 = Design::Box.new(:number => 1)
  6 +
  7 + proc_block = Design::Block.new(:position => 1)
  8 + def proc_block.content
  9 + lambda do
  10 + link_to 'test link', :controller => 'controller_linked_from_block'
  11 + end
  12 + end
  13 + box1.blocks << proc_block
  14 +
  15 + array_block = Design::Block.new(:position => 2)
  16 + def array_block.content
  17 + [ 'item1', 'item2', 'item3' ]
  18 + end
  19 + box1.blocks << array_block
  20 +
6 box2 = Design::Box.new(:number => 2) 21 box2 = Design::Box.new(:number => 2)
7 - box2.blocks << Design::MainBlock.new 22 + box2.blocks << Design::MainBlock.new(:position => 1)
  23 +
8 box3 = Design::Box.new(:number => 3) 24 box3 = Design::Box.new(:number => 3)
  25 + fixed_text_block = Design::Block.new(:position => 1)
  26 + def fixed_text_block.content
  27 + "this is a fixed content block hacked for testing"
  28 + end
  29 + box3.blocks << fixed_text_block
9 30
10 design :fixed => { 31 design :fixed => {
11 :template => 'default', 32 :template => 'default',
@@ -27,11 +48,34 @@ class DesignHelperTest &lt; Test::Unit::TestCase @@ -27,11 +48,34 @@ class DesignHelperTest &lt; Test::Unit::TestCase
27 @response = ActionController::TestResponse.new 48 @response = ActionController::TestResponse.new
28 end 49 end
29 50
30 - def test_should_generate_template 51 + def test_should_generate_all_boxes
31 get :index 52 get :index
32 assert_tag :tag => 'div', :attributes => { :id => 'box_1' } 53 assert_tag :tag => 'div', :attributes => { :id => 'box_1' }
33 assert_tag :tag => 'div', :attributes => { :id => 'box_2' } 54 assert_tag :tag => 'div', :attributes => { :id => 'box_2' }
34 assert_tag :tag => 'div', :attributes => { :id => 'box_3' } 55 assert_tag :tag => 'div', :attributes => { :id => 'box_3' }
35 end 56 end
36 57
  58 + def test_should_render_array_as_list
  59 + get :index
  60 + assert_tag :tag => 'ul', :descendant => {
  61 + :tag => 'li', :content => 'item1'
  62 + }
  63 + assert_tag :tag => 'ul', :descendant => {
  64 + :tag => 'li', :content => 'item2'
  65 + }
  66 + assert_tag :tag => 'ul', :descendant => {
  67 + :tag => 'li', :content => 'item3'
  68 + }
  69 + end
  70 +
  71 + def test_should_process_block_returned_as_content
  72 + get :index
  73 + assert_tag :tag => 'a', :attributes => { :href => /controller_linked_from_block/ }
  74 + end
  75 +
  76 + def test_should_put_string_as_is
  77 + get :index
  78 + assert_tag :tag => 'div', :content => "this is a fixed content block hacked for testing", :attributes => { :class => 'block' }
  79 + end
  80 +
37 end 81 end