diff --git a/app/helpers/boxes_helper.rb b/app/helpers/boxes_helper.rb index 8045453..7741bbe 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, :locale => locale, :params => request.params } + context = { :article => @page, :request_path => request.path, :locale => locale, :params => request.params, :user => user } box_decorator.select_blocks(box.blocks.includes(:box), 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 1d51c1c..81991f0 100644 --- a/app/models/block.rb +++ b/app/models/block.rb @@ -22,11 +22,13 @@ class Block < ActiveRecord::Base # # * :article: the article being viewed currently # * :language: in which language the block will be displayed + # * :user: the logged user def visible?(context = nil) return false if display == 'never' if context return false if language != 'all' && language != context[:locale] + return false unless display_to_user?(context[:user]) begin return self.send("display_#{display}", context) @@ -38,6 +40,10 @@ class Block < ActiveRecord::Base true end + def display_to_user?(user) + display_user == 'all' || (user.nil? && display_user == 'not_logged') || (user && display_user == 'logged') + end + def display_always(context) true end @@ -68,6 +74,14 @@ class Block < ActiveRecord::Base # the homepage of its owner. settings_items :display, :type => :string, :default => 'always' + + # The condition for displaying a block to users. It can assume the following values: + # + # * 'all': the block is always displayed + # * 'logged': the block is displayed to logged users only + # * 'not_logged': the block is displayed only to not logged users + settings_items :display_user, :type => :string, :default => 'all' + # 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 @@ -179,6 +193,14 @@ class Block < ActiveRecord::Base DISPLAY_OPTIONS[option] end + def display_user_options + @display_user_options ||= { + 'all' => __('All users'), + 'logged' => __('Logged'), + 'not_logged' => __('Not logged'), + } + end + def duplicate duplicated_block = self.clone duplicated_block.display = 'never' diff --git a/app/views/box_organizer/edit.rhtml b/app/views/box_organizer/edit.rhtml index 4916980..530de37 100644 --- a/app/views/box_organizer/edit.rhtml +++ b/app/views/box_organizer/edit.rhtml @@ -15,6 +15,10 @@
<% end %> +
+ <%= labelled_form_field _('Display to users:'), '' %> + <%= select_tag('block[display_user]', options_from_collection_for_select(@block.display_user_options, :first, :last, @block.display_user)) %> +
<%= labelled_form_field(_('Show for:'), select(:block, :language, [ [ _('all languages'), 'all']] + environment.locales.map {|key, value| [value, key]} )) %> diff --git a/test/unit/block_test.rb b/test/unit/block_test.rb index e7d6957..4963c34 100644 --- a/test/unit/block_test.rb +++ b/test/unit/block_test.rb @@ -222,4 +222,50 @@ class BlockTest < ActiveSupport::TestCase assert block.visible?(2) assert !block.visible?(3) end + + should 'default value for display_user is all' do + block = Block.new + assert_equal 'all', block.display_user + end + + should 'display block to not logged users for display_user = all' do + block = Block.new + assert block.display_to_user?(nil) + end + + should 'display block to logged users for display_user = all' do + block = Block.new + assert block.display_to_user?(User.new) + end + + should 'display block to logged users for display_user = logged' do + block = Block.new + block.display_user = 'logged' + assert block.display_to_user?(User.new) + end + + should 'do not display block to logged users for display_user = not_logged' do + block = Block.new + block.display_user = 'not_logged' + assert !block.display_to_user?(User.new) + end + + should 'do not display block to not logged users for display_user = logged' do + block = Block.new + block.display_user = 'logged' + assert !block.display_to_user?(nil) + end + + should 'display block to not logged users for display_user = not_logged' do + block = Block.new + block.display_user = 'not_logged' + assert block.display_to_user?(nil) + end + + should 'not be visible if display_to_user? is false' do + block = Block.new + block.expects(:display_to_user?).once.returns(false) + assert !block.visible?({}) + end + end -- libgit2 0.21.2