diff --git a/app/helpers/boxes_helper.rb b/app/helpers/boxes_helper.rb
index 9285a2b..b165249 100644
--- a/app/helpers/boxes_helper.rb
+++ b/app/helpers/boxes_helper.rb
@@ -65,7 +65,7 @@ module BoxesHelper
end
def display_box_content(box, main_content)
- context = { :article => @page, :request_path => request.path }
+ context = { :article => @page, :request_path => request.path, :locale => locale }
box_decorator.select_blocks(box.blocks, context).map { |item| display_block(item, main_content) }.join("\n") + box_decorator.block_target(box)
end
diff --git a/app/models/block.rb b/app/models/block.rb
index b25d364..cb5e492 100644
--- a/app/models/block.rb
+++ b/app/models/block.rb
@@ -19,15 +19,21 @@ class Block < ActiveRecord::Base
# may contain the following keys:
#
# * :article: the article being viewed currently
+ # * :language: in which language the block will be displayed
def visible?(context = nil)
if display == 'never'
return false
end
- if context && display == 'home_page_only'
- if context[:article]
- return context[:article] == owner.home_page
- else
- return context[:request_path] == '/'
+ if context
+ if language != 'all' && language != context[:locale]
+ return false
+ end
+ if display == 'home_page_only'
+ if context[:article]
+ return context[:article] == owner.home_page
+ else
+ return context[:request_path] == '/'
+ end
end
end
true
@@ -41,6 +47,11 @@ class Block < ActiveRecord::Base
# homepage of its owner.
settings_items :display, :type => :string, :default => 'always'
+ # The block can be configured to be displayed in all languages or in just one language. It can assume any locale of the environment:
+ #
+ # * 'all': the block is always displayed
+ settings_items :language, :type => :string, :default => 'all'
+
# returns the description of the block, used when the user sees a list of
# blocks to choose one to include in the design.
#
diff --git a/app/views/box_organizer/edit.rhtml b/app/views/box_organizer/edit.rhtml
index 165d6d4..3bc3b20 100644
--- a/app/views/box_organizer/edit.rhtml
+++ b/app/views/box_organizer/edit.rhtml
@@ -18,6 +18,8 @@
<%= label_tag('block_display_never', _("Don't display")) %>
+ <%= labelled_form_field(_('Show for:'), select(:block, :language, [ [ _('all languages'), 'all']] + Noosfero.locales.map {|key, value| [value, key]} )) %>
+
<% button_bar do %>
<%= submit_button(:save, _('Save')) %>
<%= lightbox_close_button(_('Cancel')) %>
diff --git a/features/edit_language_block.feature b/features/edit_language_block.feature
new file mode 100644
index 0000000..8e8f9ed
--- /dev/null
+++ b/features/edit_language_block.feature
@@ -0,0 +1,38 @@
+Feature: edit language of block
+ As a profile owner
+ I want to configure in which language a block will be displayed
+
+ Background:
+ Given the following users
+ | login | name |
+ | joaosilva | Jose Silva |
+ And the following blocks
+ | owner | type |
+ | joaosilva | ArticleBlock |
+ | joaosilva | LinkListBlock |
+ And I am logged in as "joaosilva"
+
+ Scenario: display in all languages
+ Given I go to edit ArticleBlock of joaosilva
+ And I fill in "Custom title for this block" with "Block displayed"
+ And I select "all languages"
+ And I press "Save"
+ When I go to Jose Silva's homepage
+ Then I should see "Block displayed"
+
+ Scenario: display in the selected language
+ Given I go to edit LinkListBlock of joaosilva
+ And I fill in "Custom title for this block" with "Block displayed"
+ And I select "Português"
+ And I press "Save"
+ And my browser prefers Portuguese
+ When I go to Jose Silva's homepage
+ Then I should see "Block displayed"
+
+ Scenario: not display in a not selected language
+ Given I go to edit LinkListBlock of joaosilva
+ And I fill in "Custom title for this block" with "Block not displayed"
+ And I select "Português"
+ And I press "Save"
+ When I go to Jose Silva's homepage
+ Then I should not see "Block displayed"
diff --git a/test/unit/block_test.rb b/test/unit/block_test.rb
index 1c69c1e..a6e859b 100644
--- a/test/unit/block_test.rb
+++ b/test/unit/block_test.rb
@@ -110,4 +110,30 @@ class BlockTest < Test::Unit::TestCase
assert_equal 'always', block.display
end
+ should 'display block in all languages by default' do
+ profile = Profile.new
+ block = Block.new
+ block.stubs(:owner).returns(profile)
+
+ assert_equal 'all', block.language
+ end
+
+ should 'be able to be displayed in all languages' do
+ profile = Profile.new
+ block = Block.new(:language => 'all')
+ block.stubs(:owner).returns(profile)
+
+ assert_equal true, block.visible?(:locale => 'pt')
+ assert_equal true, block.visible?(:locale => 'en')
+ end
+
+ should 'be able to be displayed only in the selected language' do
+ profile = Profile.new
+ block = Block.new(:language => 'pt')
+ block.stubs(:owner).returns(profile)
+
+ assert_equal true, block.visible?(:locale => 'pt')
+ assert_equal false, block.visible?(:locale => 'en')
+ end
+
end
diff --git a/test/unit/boxes_helper_test.rb b/test/unit/boxes_helper_test.rb
index 8a21f14..4e04090 100644
--- a/test/unit/boxes_helper_test.rb
+++ b/test/unit/boxes_helper_test.rb
@@ -50,6 +50,7 @@ class BoxesHelperTest < Test::Unit::TestCase
expects(:display_block).with(b, '')
expects(:request).returns(request)
stubs(:block_target).returns('')
+ expects(:locale).returns('en')
with_box_decorator self do
display_box_content(box, '')
end
@@ -67,6 +68,7 @@ class BoxesHelperTest < Test::Unit::TestCase
expects(:display_block).with(b, '').never
expects(:request).returns(request)
stubs(:block_target).returns('')
+ expects(:locale).returns('en')
display_box_content(box, '')
end
@@ -102,14 +104,15 @@ class BoxesHelperTest < Test::Unit::TestCase
assert block_css_classes(Block.new(:display => 'never')).split.any? { |item| item == 'invisible-block'}
end
- should 'fill context with the article and request_path' do
+ should 'fill context with the article, request_path and locale' do
request = mock()
box = mock()
box.expects(:blocks).returns([])
request.expects(:path).returns('/')
expects(:request).returns(request)
- box_decorator.expects(:select_blocks).with([], {:article => nil, :request_path => '/'}).returns([])
+ expects(:locale).returns('en')
+ box_decorator.expects(:select_blocks).with([], {:article => nil, :request_path => '/', :locale => 'en'}).returns([])
display_box_content(box, '')
end
--
libgit2 0.21.2