diff --git a/vendor/plugins/design/lib/design/helper.rb b/vendor/plugins/design/lib/design/helper.rb index de00e42..8ef6e17 100644 --- a/vendor/plugins/design/lib/design/helper.rb +++ b/vendor/plugins/design/lib/design/helper.rb @@ -2,7 +2,8 @@ module Design module Helper - # proxies calls to controller's design method + # proxies calls to controller's design method to get the design information + # holder object def design @controller.send(:design) end @@ -46,13 +47,36 @@ module Design # def design_display_blocks(box, content = "") blocks = box.blocks_sort_by_position - content_tag(:div, - blocks.map { |b| - content_tag(:div, b.main? ? content : self.send('list_content', b.content), :class =>"block_item_box_#{box.number}" , :id => "block_#{b.id}" ) - }.join("\n"), :id => "frame_#{box.number}" - ) + blocks.map do |block| + # FIXME: should that actually be list_content? + text = block.main? ? content : design_block_content(block) + content_tag(:div, text, :class => "block" , :id => "block_#{block.id}" ) + end.join("\n") + end + + # Displays the content of a block. See plugin README for details about the + # possibilities. + def design_block_content(block) + content = block.content + case content + when Proc + self.instance_eval(&content) + when Array + content_tag( + 'ul', + content.map do |c| + content_tag( + 'li', + c + ) + end + ) + else + content.to_s + end end + # TODO: test stuff below this comment ######################################################## # Template @@ -136,244 +160,6 @@ module Design files_path.map{|f| f.gsub(/.*#{remove_until}/, '')} end - - end # END SHOW TEMPLATE HELPER - - module EditTemplateHelper - - - # Symbol dictionary used on select when we add or edit a block. - # This method has the responsability of translate a Block class in a humam name - # By default the class "MainBlock" has the human name "Main Block". Other classes - # defined by user are not going to display in a human name format until de method - # flexible_template_block_dict be redefined in a controller by user - -#TODO define the method flexible_template_block_dict if not defined by helper -# if !self.public_instance_methods.include? "flexible_template_block_dict" -# define_method('flexible_template_block_dict') do |str| -# { -# 'MainBlock' => _("Main Block") -# }[str] || str -# end -# end - - - def flexible_template_block_helper_dict(str) - { - 'plain_content' => _('Plain Content') , - 'list_content' => _('List Content') - }[str] || str - end - - - private - - ################################################# - # TEMPLATES METHODS RELATED - ################################################# - - # Shows the blocks as defined in show_blocks adding the sortable and draggable elements. - # In this case the layout can be manipulated - def edit_blocks(box, main_content = "") - blocks = box.blocks_sort_by_position - [ - content_tag( - :ul,[ - box.name, - link_to_active_sort(box), - link_to_add_block(box), - blocks.map {|b| - [content_tag( - :li, - b.name + link_to_destroy_block(b), - :class =>"block_item_box_#{box.number}" , :id => "block_#{b.id}" - ), - draggable("block_#{b.id}")].join("\n") - }.join("\n")].join("\n"), :id => "sort_#{box.number}" - ), - drag_drop_items(box)].join("\n") - end - - def link_to_active_sort(box) - link_to_remote(_('Sort'), - {:update => "sort_#{box.number}", :url => {:action => 'flexible_template_set_sort_mode', :box_id => box.id }}, - :class => 'sort_button') - end - - def link_to_add_block(box) - link_to_remote(_('Add Block'), - {:update => "sort_#{box.number}", :url => {:action => 'flexible_template_new_block', :box_id => box.id }}, - :class => 'add_block_button') - end - - def link_to_destroy_block(block) - link_to_remote(_('Remove'), - {:update => "sort_#{block.box.number}", :url => {:action => 'flexible_template_destroy_block', :block_id => block.id }}, - :class => 'destroy_block_button') - end - - - # Allows the biven box to have sortable elements - def sortable_block(box_number) - sortable_element "sort_#{box_number}", - :url => {:action => 'flexible_template_sort_box', :box_number => box_number }, - :complete => visual_effect(:highlight, "sort_#{box_number}") - end - - # Allows an element item to be draggable - def draggable(item) - draggable_element(item, :ghosting => true, :revert => true) - end - - # Allows an draggable element change between diferrents boxes - def drag_drop_items(box) - boxes = @ft_config[:boxes].reject{|b| b.id == box.id} - - boxes.map{ |b| - drop_receiving_element("box_#{box.number}", - :accept => "block_item_box_#{b.number}", - :complete => "$('spinner').hide();", - :before => "$('spinner').show();", - :hoverclass => 'hover', - :with => "'block=' + encodeURIComponent(element.id.split('_').last())", - :url => {:action=>:flexible_template_change_box, :box_id => box.id}) - }.to_s - end - - - # Generate a select option to choose one of the available templates. - # The available templates are those in 'public/templates' - def select_template - available_templates = @ft_config[:available_templates] - - template_options = options_for_select(available_templates.map{|template| [template, template] }, @ft_config[:template]) - [ select_tag('template_name', template_options ), - change_template].join("\n") - end - - # Generate a observer to reload a page when a template is selected - def change_template - observe_field( 'template_name', - :url => {:action => 'set_default_template'}, - :with =>"'template_name=' + escape(value) + '&object_id=' + escape(#{@ft_config[:owner].id})", - :complete => "document.location.reload();" - ) - end - - def available_blocks -#TODO check if are valids blocks - h = { - 'MainBlock' => _("Main Block"), - } - h.merge!(controller.class::FLEXIBLE_TEMPLATE_AVAILABLE_BLOCKS) if controller.class.constants.include? "FLEXIBLE_TEMPLATE_AVAILABLE_BLOCKS" - h - end - - def block_helpers -#TODO check if are valids helpers - h = { - 'plain_content' => _("Plain Content"), - 'list_content' => _("Simple List Content"), - } - h.merge!(controller.class::FLEXIBLE_TEMPLATE_BLOCK_HELPER) if controller.class.constants.include? "FLEXIBLE_TEMPLATE_BLOCK_HELPER" - h - end - - def new_block_form(box) - type_block_options = options_for_select(available_blocks.collect{|k,v| [v,k] }) - type_block_helper_options = options_for_select(block_helpers.collect{|k,v| [v,k] }) - @block = Block.new - @block.box = box - - _("Adding block on %s") % box.name + - [ - form_remote_tag(:url => {:action => 'flexible_template_create_block'}, :update => "sort_#{box.number}"), - hidden_field('block', 'box_id'), - content_tag( - :p, - [ - content_tag( - :label, _('Name:') - ), - text_field('block', 'name') - ].join("\n") - ), - content_tag( - :p, - [ - content_tag( - :label, _('Title:') - ), - text_field('block', 'title') - ].join("\n") - ), - content_tag( - :p, - [ - content_tag( - :label, _('Type:') - ), - select_tag('block[type]', type_block_options) - ].join("\n") - ), - content_tag( - :p, - [ - content_tag( - :label, _('Visualization Mode:') - ), - select_tag('block[helper]', type_block_helper_options) - ].join("\n") - ), - submit_tag( _('Submit')), - end_form_tag - ].join("\n") - - end - - # Generate a select option to choose one of the available themes. - # The available themes are those in 'public/themes' - def select_theme - available_themes = @ft_config[:available_themes] - theme_options = options_for_select(available_themes.map{|theme| [theme, theme] }, @ft_config[:theme]) - [ select_tag('theme_name', theme_options ), - change_theme].join("\n") - end - - # Generate a observer to reload a page when a theme is selected - def change_theme - observe_field( 'theme_name', - :url => {:action => 'set_default_theme'}, - :with =>"'theme_name=' + escape(value) + '&object_id=' + escape(#{@ft_config[:owner].id})", - :complete => "document.location.reload();" - ) - end - - - ################################################# - #ICONS THEMES RELATED - ################################################# - - # Generate a select option to choose one of the available icons themes. - # The available icons themes are those in 'public/icons' - def select_icon_theme - available_icon_themes = @ft_config[:available_icon_themes] - icon_theme_options = options_for_select(available_icon_themes.map{|icon_theme| [icon_theme, icon_theme] }, @ft_config[:icon_theme]) - [ select_tag('icon_theme_name', icon_theme_options ), - change_icon_theme].join("\n") - end - - # Generate a observer to reload a page when a icons theme is selected - def change_icon_theme - observe_field( 'icon_theme_name', - :url => {:action => 'set_default_icon_theme'}, - :with =>"'icon_theme_name=' + escape(value) + '&object_id=' + escape(#{@ft_config[:owner].id})", - :complete => "document.location.reload();" - ) - end - - - end # END OF module HElper - + end # END OF module Helper end #END OF module Design diff --git a/vendor/plugins/design/test/design_helper_test.rb b/vendor/plugins/design/test/design_helper_test.rb index 9e651ad..8dc3e4a 100644 --- a/vendor/plugins/design/test/design_helper_test.rb +++ b/vendor/plugins/design/test/design_helper_test.rb @@ -3,9 +3,30 @@ require File.join(File.dirname(__FILE__), 'test_helper') class DesignHelperTestController < ActionController::Base box1 = Design::Box.new(:number => 1) + + proc_block = Design::Block.new(:position => 1) + def proc_block.content + lambda do + link_to 'test link', :controller => 'controller_linked_from_block' + end + end + box1.blocks << proc_block + + array_block = Design::Block.new(:position => 2) + def array_block.content + [ 'item1', 'item2', 'item3' ] + end + box1.blocks << array_block + box2 = Design::Box.new(:number => 2) - box2.blocks << Design::MainBlock.new + box2.blocks << Design::MainBlock.new(:position => 1) + box3 = Design::Box.new(:number => 3) + fixed_text_block = Design::Block.new(:position => 1) + def fixed_text_block.content + "this is a fixed content block hacked for testing" + end + box3.blocks << fixed_text_block design :fixed => { :template => 'default', @@ -27,11 +48,34 @@ class DesignHelperTest < Test::Unit::TestCase @response = ActionController::TestResponse.new end - def test_should_generate_template + def test_should_generate_all_boxes get :index assert_tag :tag => 'div', :attributes => { :id => 'box_1' } assert_tag :tag => 'div', :attributes => { :id => 'box_2' } assert_tag :tag => 'div', :attributes => { :id => 'box_3' } end + def test_should_render_array_as_list + get :index + assert_tag :tag => 'ul', :descendant => { + :tag => 'li', :content => 'item1' + } + assert_tag :tag => 'ul', :descendant => { + :tag => 'li', :content => 'item2' + } + assert_tag :tag => 'ul', :descendant => { + :tag => 'li', :content => 'item3' + } + end + + def test_should_process_block_returned_as_content + get :index + assert_tag :tag => 'a', :attributes => { :href => /controller_linked_from_block/ } + end + + def test_should_put_string_as_is + get :index + assert_tag :tag => 'div', :content => "this is a fixed content block hacked for testing", :attributes => { :class => 'block' } + end + end -- libgit2 0.21.2