Commit 22d3d9d34034136b344cd6b58270a6b49bf61dbd

Authored by Victor Costa
1 parent bce11413

Added a filter to display blocks depending on user

app/helpers/boxes_helper.rb
... ... @@ -70,7 +70,18 @@ module BoxesHelper
70 70 end
71 71  
72 72 def select_blocks(arr, context)
73   - arr
  73 + filter = (context && context[:params] && context[:params][:filter]) || {}
  74 + user_filter(arr, filter)
  75 + end
  76 +
  77 + def user_filter(arr, filter)
  78 + if filter[:display_user]=='not_logged'
  79 + arr.select { |block| block.display_to_user?(nil) }
  80 + elsif filter[:display_user]=='logged'
  81 + arr.select { |block| block.display_to_user?(user) }
  82 + else
  83 + arr
  84 + end
74 85 end
75 86  
76 87 def display_block(block, main_content = nil)
... ...
app/views/box_organizer/index.rhtml
... ... @@ -4,3 +4,11 @@
4 4 <%= colorbox_button('add', _('Add a block'), { :action => 'add_block' }) %>
5 5 <%= button(:back, _('Back to control panel'), :controller => (profile.nil? ? 'admin_panel': 'profile_editor')) %>
6 6 <% end %>
  7 +
  8 +<% form_tag({:action => :index}) do %>
  9 + <div class="user-filter">
  10 + <span class="label"><%= _('Display blocks to:') %> </span>
  11 + <%= select_tag('filter[display_user]', options_from_collection_for_select(Block.new.display_user_options, :first, :last, params[:filter] ? params[:filter][:display_user] : 'all'), :onchange => "this.form.submit();") %>
  12 + </div>
  13 + <div style="clear: both"></div>
  14 +<% end %>
... ...
public/stylesheets/application.css
... ... @@ -6488,3 +6488,11 @@ ul.article-versions li {
6488 6488 font-size: 13px;
6489 6489 padding: 3px 0px;
6490 6490 }
  6491 +
  6492 +.user-filter {
  6493 + float: right;
  6494 +}
  6495 +
  6496 +.design-menu {
  6497 + float: left;
  6498 +}
... ...
test/functional/environment_design_controller_test.rb
... ... @@ -366,4 +366,16 @@ class EnvironmentDesignControllerTest &lt; ActionController::TestCase
366 366 assert @controller.instance_variable_get('@side_block_types').include?(CustomBlock8)
367 367 end
368 368  
  369 + should 'display user filter for blocks' do
  370 + login_as(create_admin_user(Environment.default))
  371 + get :index
  372 + assert_tag 'div', :attributes => {:class => 'user-filter'}
  373 + end
  374 +
  375 + should 'display user filter for blocks with selected value' do
  376 + login_as(create_admin_user(Environment.default))
  377 + get :index, :filter => {:display_user => 'logged'}
  378 + assert_tag 'select', :attributes => {:name => 'filter[display_user]'}, :descendant => {:tag => 'option', :attributes => {:value => 'logged', :selected => true}}
  379 + end
  380 +
369 381 end
... ...
test/functional/profile_design_controller_test.rb
... ... @@ -744,4 +744,9 @@ class ProfileDesignControllerTest &lt; ActionController::TestCase
744 744 end
745 745 end
746 746  
  747 + should 'display user filter for blocks' do
  748 + get :index, :profile => 'designtestuser'
  749 + assert_tag 'div', :attributes => {:class => 'user-filter'}
  750 + end
  751 +
747 752 end
... ...
test/unit/boxes_helper_test.rb
... ... @@ -54,6 +54,7 @@ class BoxesHelperTest &lt; ActiveSupport::TestCase
54 54 expects(:display_block).with(b, '')
55 55 stubs(:request).returns(request)
56 56 stubs(:block_target).returns('')
  57 + stubs(:user).returns(nil)
57 58 expects(:locale).returns('en')
58 59 with_box_decorator self do
59 60 display_box_content(box, '')
... ... @@ -73,6 +74,7 @@ class BoxesHelperTest &lt; ActiveSupport::TestCase
73 74 box.save!
74 75 expects(:display_block).with(b, '').never
75 76 stubs(:request).returns(request)
  77 + stubs(:user).returns(nil)
76 78 stubs(:block_target).returns('')
77 79 expects(:locale).returns('en')
78 80 display_box_content(box, '')
... ... @@ -105,16 +107,47 @@ class BoxesHelperTest &lt; ActiveSupport::TestCase
105 107 assert block_css_classes(Block.new(:display => 'never')).split.any? { |item| item == 'invisible-block'}
106 108 end
107 109  
108   - should 'fill context with the article, request_path and locale' do
  110 + should 'fill context with the article, request_path, user and locale' do
109 111 request = mock()
110 112 box = Box.create!(:owner => fast_create(Profile))
111 113 request.expects(:path).returns('/')
112 114 request.expects(:params).returns({})
113 115 stubs(:request).returns(request)
  116 + stubs(:user).returns(nil)
114 117 expects(:locale).returns('en')
115   - box_decorator.expects(:select_blocks).with([], {:article => nil, :request_path => '/', :locale => 'en', :params => {}}).returns([])
  118 + box_decorator.expects(:select_blocks).with([], {:article => nil, :request_path => '/', :locale => 'en', :params => {}, :user => nil}).returns([])
116 119  
117 120 display_box_content(box, '')
118 121 end
119 122  
  123 + should 'filter blocks by user' do
  124 + b1 = Block.new
  125 + b2 = Block.new
  126 + arr = user_filter([b1, b2], {:display_user => 'all'})
  127 + assert_equivalent [b1, b2], arr
  128 + end
  129 +
  130 + should 'filter blocks by logged user' do
  131 + b1 = Block.new(:display_user => 'logged')
  132 + b2 = Block.new(:display_user => 'not_logged')
  133 + stubs(:user).returns(User.new)
  134 + arr = user_filter([b1, b2], {:display_user => 'logged'})
  135 + assert_equivalent [b1], arr
  136 + end
  137 +
  138 + should 'filter blocks by not logged user' do
  139 + b1 = Block.new(:display_user => 'logged')
  140 + b2 = Block.new(:display_user => 'not_logged')
  141 + stubs(:user).returns(User.new)
  142 + arr = user_filter([b1, b2], {:display_user => 'not_logged'})
  143 + assert_equivalent [b2], arr
  144 + end
  145 +
  146 + should 'select blocks by user filter' do
  147 + b = Block.new
  148 + expects(:user_filter).once.returns([b])
  149 + arr = select_blocks([], {:psarams => {}})
  150 + assert_equal [b], arr
  151 + end
  152 +
120 153 end
... ...