Commit bce11413dbb1c9da0f5b7dffe539bed6e9b499a8

Authored by Victor Costa
1 parent d0561132

Added a way to display blocks only to logged/not logged users

app/helpers/boxes_helper.rb
... ... @@ -65,7 +65,7 @@ module BoxesHelper
65 65 end
66 66  
67 67 def display_box_content(box, main_content)
68   - context = { :article => @page, :request_path => request.path, :locale => locale, :params => request.params }
  68 + context = { :article => @page, :request_path => request.path, :locale => locale, :params => request.params, :user => user }
69 69 box_decorator.select_blocks(box.blocks.includes(:box), context).map { |item| display_block(item, main_content) }.join("\n") + box_decorator.block_target(box)
70 70 end
71 71  
... ...
app/models/block.rb
... ... @@ -22,11 +22,13 @@ class Block < ActiveRecord::Base
22 22 #
23 23 # * <tt>:article</tt>: the article being viewed currently
24 24 # * <tt>:language</tt>: in which language the block will be displayed
  25 + # * <tt>:user</tt>: the logged user
25 26 def visible?(context = nil)
26 27 return false if display == 'never'
27 28  
28 29 if context
29 30 return false if language != 'all' && language != context[:locale]
  31 + return false unless display_to_user?(context[:user])
30 32  
31 33 begin
32 34 return self.send("display_#{display}", context)
... ... @@ -38,6 +40,10 @@ class Block &lt; ActiveRecord::Base
38 40 true
39 41 end
40 42  
  43 + def display_to_user?(user)
  44 + display_user == 'all' || (user.nil? && display_user == 'not_logged') || (user && display_user == 'logged')
  45 + end
  46 +
41 47 def display_always(context)
42 48 true
43 49 end
... ... @@ -68,6 +74,14 @@ class Block &lt; ActiveRecord::Base
68 74 # the homepage of its owner.
69 75 settings_items :display, :type => :string, :default => 'always'
70 76  
  77 +
  78 + # The condition for displaying a block to users. It can assume the following values:
  79 + #
  80 + # * <tt>'all'</tt>: the block is always displayed
  81 + # * <tt>'logged'</tt>: the block is displayed to logged users only
  82 + # * <tt>'not_logged'</tt>: the block is displayed only to not logged users
  83 + settings_items :display_user, :type => :string, :default => 'all'
  84 +
71 85 # The block can be configured to be displayed in all languages or in just one language. It can assume any locale of the environment:
72 86 #
73 87 # * <tt>'all'</tt>: the block is always displayed
... ... @@ -179,6 +193,14 @@ class Block &lt; ActiveRecord::Base
179 193 DISPLAY_OPTIONS[option]
180 194 end
181 195  
  196 + def display_user_options
  197 + @display_user_options ||= {
  198 + 'all' => __('All users'),
  199 + 'logged' => __('Logged'),
  200 + 'not_logged' => __('Not logged'),
  201 + }
  202 + end
  203 +
182 204 def duplicate
183 205 duplicated_block = self.clone
184 206 duplicated_block.display = 'never'
... ...
app/views/box_organizer/edit.rhtml
... ... @@ -15,6 +15,10 @@
15 15 <br/>
16 16 <% end %>
17 17 </div>
  18 + <div class="display_user">
  19 + <%= labelled_form_field _('Display to users:'), '' %>
  20 + <%= select_tag('block[display_user]', options_from_collection_for_select(@block.display_user_options, :first, :last, @block.display_user)) %>
  21 + </div>
18 22  
19 23 <%= labelled_form_field(_('Show for:'), select(:block, :language, [ [ _('all languages'), 'all']] + environment.locales.map {|key, value| [value, key]} )) %>
20 24  
... ...
test/unit/block_test.rb
... ... @@ -222,4 +222,50 @@ class BlockTest &lt; ActiveSupport::TestCase
222 222 assert block.visible?(2)
223 223 assert !block.visible?(3)
224 224 end
  225 +
  226 + should 'default value for display_user is all' do
  227 + block = Block.new
  228 + assert_equal 'all', block.display_user
  229 + end
  230 +
  231 + should 'display block to not logged users for display_user = all' do
  232 + block = Block.new
  233 + assert block.display_to_user?(nil)
  234 + end
  235 +
  236 + should 'display block to logged users for display_user = all' do
  237 + block = Block.new
  238 + assert block.display_to_user?(User.new)
  239 + end
  240 +
  241 + should 'display block to logged users for display_user = logged' do
  242 + block = Block.new
  243 + block.display_user = 'logged'
  244 + assert block.display_to_user?(User.new)
  245 + end
  246 +
  247 + should 'do not display block to logged users for display_user = not_logged' do
  248 + block = Block.new
  249 + block.display_user = 'not_logged'
  250 + assert !block.display_to_user?(User.new)
  251 + end
  252 +
  253 + should 'do not display block to not logged users for display_user = logged' do
  254 + block = Block.new
  255 + block.display_user = 'logged'
  256 + assert !block.display_to_user?(nil)
  257 + end
  258 +
  259 + should 'display block to not logged users for display_user = not_logged' do
  260 + block = Block.new
  261 + block.display_user = 'not_logged'
  262 + assert block.display_to_user?(nil)
  263 + end
  264 +
  265 + should 'not be visible if display_to_user? is false' do
  266 + block = Block.new
  267 + block.expects(:display_to_user?).once.returns(false)
  268 + assert !block.visible?({})
  269 + end
  270 +
225 271 end
... ...