Commit 55d5a0062c1d3cdb8c8f0a2ee8c9033a1b5fe9ca

Authored by Victor Costa
1 parent ce0d03ab

Added a block that works like a container of blocks

plugins/container_block/lib/container_block.rb 0 → 100644
... ... @@ -0,0 +1,48 @@
  1 +class ContainerBlock < Block
  2 +
  3 + include Noosfero::Plugin::HotSpot
  4 +
  5 + after_create :create_box
  6 +
  7 + settings_items :container_box_id, :type => Integer, :default => nil
  8 +
  9 + def self.description
  10 + _('Container')
  11 + end
  12 +
  13 + def help
  14 + _('This block acts as a container for another blocks')
  15 + end
  16 +
  17 + def create_box
  18 + box = Box.create!(:owner => self)
  19 + settings[:container_box_id] = box.id
  20 + save!
  21 + end
  22 +
  23 + def container_box
  24 + Box.find(container_box_id)
  25 + end
  26 +
  27 + def block_classes=(classes)
  28 + classes.each { |c| block = c.constantize.create!(:box => container_box) } if classes
  29 + end
  30 +
  31 + def blocks
  32 + container_box.blocks
  33 + end
  34 +
  35 + #FIXME needed?
  36 + def layout_template
  37 + 'default2'
  38 + end
  39 +
  40 + #FIXME controller test
  41 + def content(args={})
  42 + block = self
  43 + lambda do
  44 + render :file => 'blocks/container.rhtml', :locals => {:block => block}
  45 + end
  46 + end
  47 +
  48 +end
... ...
plugins/container_block/lib/container_block_array.rb 0 → 100644
... ... @@ -0,0 +1,14 @@
  1 +module ContainerBlockArray
  2 +
  3 + def blocks_with_container_block_plugin
  4 + blocks = blocks_without_container_block_plugin
  5 + blocks.each { |block| blocks.concat(block.blocks) if block.kind_of?(ContainerBlock) }
  6 + end
  7 +
  8 + def self.included(base)
  9 + base.class_eval do
  10 + alias_method_chain :blocks, :container_block_plugin
  11 + end
  12 + end
  13 +
  14 +end
... ...
plugins/container_block/lib/container_block_plugin.rb 0 → 100644
... ... @@ -0,0 +1,19 @@
  1 +class ContainerBlockPlugin < Noosfero::Plugin
  2 +
  3 + def self.plugin_name
  4 + "Container Block Plugin"
  5 + end
  6 +
  7 + def self.plugin_description
  8 + _("A plugin that add a container block.")
  9 + end
  10 +
  11 + def self.extra_blocks
  12 + { ContainerBlock => {} }
  13 + end
  14 +
  15 + def stylesheet?
  16 + true
  17 + end
  18 +
  19 +end
... ...
plugins/container_block/lib/ext/block.rb 0 → 100644
... ... @@ -0,0 +1,15 @@
  1 +require_dependency 'block'
  2 +
  3 +class Block
  4 +
  5 + def box_with_container_block_plugin
  6 + box = box_without_container_block_plugin
  7 + if box && box.owner.kind_of?(ContainerBlock)
  8 + box = box.owner.box
  9 + end
  10 + box
  11 + end
  12 +
  13 + alias_method_chain :box, :container_block_plugin
  14 +
  15 +end
... ...
plugins/container_block/lib/ext/environment.rb 0 → 100644
... ... @@ -0,0 +1,7 @@
  1 +require_dependency 'environment'
  2 +
  3 +class Environment
  4 +
  5 + include ContainerBlockArray
  6 +
  7 +end
... ...
plugins/container_block/lib/ext/profile.rb 0 → 100644
... ... @@ -0,0 +1,7 @@
  1 +require_dependency 'profile'
  2 +
  3 +class Profile
  4 +
  5 + include ContainerBlockArray
  6 +
  7 +end
... ...
plugins/container_block/public/style.css 0 → 100644
... ... @@ -0,0 +1,11 @@
  1 +#content .boxes .container-block .block, #content .boxes .container-block .block-target {
  2 + float: left;
  3 +}
  4 +
  5 +#content .boxes .container-block .block-target {
  6 + width: 20px;
  7 +}
  8 +
  9 +#content .boxes .container-block .block .icon-down, #content .boxes .container-block .block .icon-down-disabled, #content .boxes .container-block .block .icon-up, #content .boxes .container-block .block .icon-up-disabled {
  10 + display: none;
  11 +}
... ...
plugins/container_block/test/functional/container_block_environment_design_controller_test.rb 0 → 100644
... ... @@ -0,0 +1,34 @@
  1 +require File.dirname(__FILE__) + '/../test_helper'
  2 +
  3 +# Re-raise errors caught by the controller.
  4 +class EnvironmentDesignController; def rescue_action(e) raise e end; end
  5 +
  6 +class EnvironmentDesignControllerTest < ActionController::TestCase
  7 +
  8 + def setup
  9 + Environment.delete_all
  10 + @environment = Environment.new(:name => 'testenv', :is_default => true)
  11 + @environment.enabled_plugins = ['ContainerBlock']
  12 + @environment.save!
  13 +
  14 + user = create_user('testinguser')
  15 + @environment.add_admin(user.person)
  16 + login_as(user.login)
  17 +
  18 + box = Box.create!(:owner => @environment)
  19 + @block = ContainerBlock.create!(:box => box)
  20 + end
  21 +
  22 + should 'be able to edit ContainerBlock' do
  23 + get :edit, :id => @block.id
  24 + assert_tag :tag => 'input', :attributes => { :id => 'block_title' }
  25 + end
  26 +
  27 + should 'be able to save ContainerBlock' do
  28 + get :edit, :id => @block.id
  29 + post :save, :id => @block.id, :block => {:title => 'Container' }
  30 + @block.reload
  31 + assert_equal 'Container', @block.title
  32 + end
  33 +
  34 +end
... ...
plugins/container_block/test/functional/container_block_home_controller_test.rb 0 → 100644
... ... @@ -0,0 +1,34 @@
  1 +require File.dirname(__FILE__) + '/../test_helper'
  2 +
  3 +# Re-raise errors caught by the controller.
  4 +class HomeController
  5 + append_view_path File.join(File.dirname(__FILE__) + '/../../views')
  6 + def rescue_action(e)
  7 + raise e
  8 + end
  9 +end
  10 +
  11 +class HomeControllerTest < ActionController::TestCase
  12 +
  13 + def setup
  14 + Environment.delete_all
  15 + @environment = Environment.new(:name => 'testenv', :is_default => true)
  16 + @environment.enabled_plugins = ['ContainerBlock']
  17 + @environment.save!
  18 +
  19 + user = create_user('testinguser')
  20 + @environment.add_admin(user.person)
  21 + login_as(user.login)
  22 +
  23 + box = Box.create!(:owner => @environment)
  24 + @block = ContainerBlock.create!(:box => box)
  25 +
  26 + @environment.boxes = [box]
  27 + end
  28 +
  29 + should 'display ContainerBlock' do
  30 + get :index
  31 + assert_tag :div, :attributes => { :class => 'block container-block' }
  32 + end
  33 +
  34 +end
... ...
plugins/container_block/test/test_helper.rb 0 → 100644
... ... @@ -0,0 +1 @@
  1 +require File.dirname(__FILE__) + '/../../../test/test_helper'
... ...
plugins/container_block/test/unit/block_test.rb 0 → 100644
... ... @@ -0,0 +1,24 @@
  1 +require File.dirname(__FILE__) + '/../test_helper'
  2 +
  3 +class BlockTest < ActiveSupport::TestCase
  4 +
  5 + def setup
  6 + @environment = Environment.new
  7 +
  8 + @box = Box.new(:owner => @environment)
  9 + @block = Block.new(:box => @box)
  10 +
  11 + @container_box = Box.new(:owner => @environment)
  12 + @container = ContainerBlock.new(:box => @container_box)
  13 + end
  14 +
  15 + should 'return block box if block owner is not a ContainerBlock' do
  16 + assert_equal @box, @block.box
  17 + end
  18 +
  19 + should 'return container box if block onwer is a ContainerBlock' do
  20 + @box.owner = @container
  21 + assert_equal @container_box, @block.box
  22 + end
  23 +
  24 +end
... ...
plugins/container_block/test/unit/container_block_array_test.rb 0 → 100644
... ... @@ -0,0 +1,29 @@
  1 +require File.dirname(__FILE__) + '/../test_helper'
  2 +
  3 +class ContainerBlockArrayTest < ActiveSupport::TestCase
  4 +
  5 + attr_reader :blocks
  6 +
  7 + include ContainerBlockArray
  8 +
  9 + def setup
  10 + @blocks = []
  11 +
  12 + @environment = fast_create(Environment)
  13 + @container_box = Box.new(:owner => @environment)
  14 + @container = ContainerBlock.new(:box => @container_box)
  15 + end
  16 +
  17 + should 'return blocks as usual' do
  18 + @blocks << Block.new
  19 + assert_equal @blocks, blocks_without_container_block_plugin
  20 + end
  21 +
  22 + should 'return blocks and container block children' do
  23 + @container.save!
  24 + @container_box.blocks << Block.new
  25 + @blocks.concat([Block.new, @container])
  26 + assert_equal @blocks + @container.blocks, blocks_without_container_block_plugin
  27 + end
  28 +
  29 +end
... ...
plugins/container_block/test/unit/container_block_plugin_test.rb 0 → 100644
... ... @@ -0,0 +1,25 @@
  1 +require File.dirname(__FILE__) + '/../test_helper'
  2 +
  3 +class ContainerBlockPluginTest < ActiveSupport::TestCase
  4 +
  5 + def setup
  6 + @plugin = ContainerBlockPlugin.new
  7 + end
  8 +
  9 + should 'has a name' do
  10 + assert !ContainerBlockPlugin.plugin_name.blank?
  11 + end
  12 +
  13 + should 'has a description' do
  14 + assert !ContainerBlockPlugin.plugin_description.blank?
  15 + end
  16 +
  17 + should 'add a block' do
  18 + assert_equal [ContainerBlock], ContainerBlockPlugin.extra_blocks.keys
  19 + end
  20 +
  21 + should 'has stylesheet' do
  22 + assert @plugin.stylesheet?
  23 + end
  24 +
  25 +end
... ...
plugins/container_block/test/unit/container_block_test.rb 0 → 100644
... ... @@ -0,0 +1,56 @@
  1 +require File.dirname(__FILE__) + '/../test_helper'
  2 +
  3 +class ContainerBlockTest < ActiveSupport::TestCase
  4 +
  5 + def setup
  6 + @block = ContainerBlock.new
  7 + end
  8 +
  9 + should 'describe yourself' do
  10 + assert !ContainerBlock.description.blank?
  11 + end
  12 +
  13 + should 'has a help' do
  14 + assert !@block.help.blank?
  15 + end
  16 +
  17 + should 'create a box on save' do
  18 + @block.save!
  19 + assert @block.container_box_id
  20 + end
  21 +
  22 + should 'return created box' do
  23 + @block.save!
  24 + assert @block.container_box
  25 + end
  26 +
  27 + should 'create new blocks when receive block classes' do
  28 + Block.destroy_all
  29 + @block.save!
  30 + @block.block_classes = ['Block']
  31 + assert_equal 2, Block.count
  32 + assert_equal Block, Block.last.class
  33 + end
  34 +
  35 + should 'do not create blocks when nothing is passed as block classes' do
  36 + Block.destroy_all
  37 + @block.save!
  38 + @block.block_classes = []
  39 + assert_equal 1, Block.count
  40 + end
  41 +
  42 + should 'do not create blocks when nil is passed as block classes' do
  43 + Block.destroy_all
  44 + @block.save!
  45 + @block.block_classes = nil
  46 + assert_equal 1, Block.count
  47 + end
  48 +
  49 + should 'return a list of blocks associated with the container block' do
  50 + Block.destroy_all
  51 + @block.save!
  52 + @block.block_classes = ['Block', 'Block']
  53 + assert_equal [Block, Block], @block.blocks.map(&:class)
  54 + end
  55 +
  56 +end
... ...
plugins/container_block/test/unit/environment_test.rb 0 → 100644
... ... @@ -0,0 +1,29 @@
  1 +require File.dirname(__FILE__) + '/../test_helper'
  2 +
  3 +class EnvironmentTest < ActiveSupport::TestCase
  4 +
  5 + def setup
  6 + @environment = fast_create(Environment)
  7 +
  8 + @box = Box.create!(:owner => @environment)
  9 + @block = Block.create!(:box => @box)
  10 +
  11 + @container_box = Box.create!(:owner => @environment)
  12 + @container = ContainerBlock.create!(:box => @container_box)
  13 + end
  14 +
  15 + should 'return blocks as usual' do
  16 + assert_equal [@block, @container], @environment.blocks
  17 + end
  18 +
  19 + should 'return block with id at find method' do
  20 + assert_equal @block, @environment.blocks.find(@block.id)
  21 + end
  22 +
  23 + should 'return child block with id at find method' do
  24 + block = Block.create!(:box => @container_box)
  25 + @container.save!
  26 + assert_equal @block, @environment.blocks.find(@block.id)
  27 + end
  28 +
  29 +end
... ...
plugins/container_block/test/unit/profile_test.rb 0 → 100644
... ... @@ -0,0 +1,29 @@
  1 +require File.dirname(__FILE__) + '/../test_helper'
  2 +
  3 +class ProfileTest < ActiveSupport::TestCase
  4 +
  5 + def setup
  6 + @profile = fast_create(Profile)
  7 +
  8 + @box = Box.create!(:owner => @profile)
  9 + @block = Block.create!(:box => @box)
  10 +
  11 + @container_box = Box.create!(:owner => @profile)
  12 + @container = ContainerBlock.create!(:box => @container_box)
  13 + end
  14 +
  15 + should 'return blocks as usual' do
  16 + assert_equal [@block, @container], @profile.blocks
  17 + end
  18 +
  19 + should 'return block with id at find method' do
  20 + assert_equal @block, @profile.blocks.find(@block.id)
  21 + end
  22 +
  23 + should 'return child block with id at find method' do
  24 + block = Block.create!(:box => @container_box)
  25 + @container.save!
  26 + assert_equal @block, @profile.blocks.find(@block.id)
  27 + end
  28 +
  29 +end
... ...
plugins/container_block/views/blocks/container.rhtml 0 → 100644
... ... @@ -0,0 +1,4 @@
  1 +<% block.blocks.each do |block| %>
  2 + <%= display_block(block, '') %>
  3 +<% end %>
  4 +<div class="clear"></div>
... ...
plugins/container_block/views/box_organizer/_container_block.rhtml 0 → 100644
... ... @@ -0,0 +1,14 @@
  1 +<br/>
  2 +<ul id="links">
  3 + <% @block.blocks.each do |block| %>
  4 + <li>
  5 + <%= block.class.name %>
  6 + </li>
  7 + <% end %>
  8 +</ul>
  9 +
  10 +<%= link_to_function(_('New block'), nil, :class => 'button icon-add with-text') { |page|
  11 + page.insert_html :bottom, 'links', content_tag('li',select_tag('block[block_classes][]',
  12 + options_for_select( @controller.available_blocks.each_with_index.map {|b,i| [b.name, b.name] } ))) } %>
  13 +
  14 +<br/><br/>
... ...
plugins/container_block/views/environment_design 0 → 120000
... ... @@ -0,0 +1 @@
  1 +box_organizer
0 2 \ No newline at end of file
... ...
plugins/container_block/views/profile_design 0 → 120000
... ... @@ -0,0 +1 @@
  1 +box_organizer
0 2 \ No newline at end of file
... ...