Commit 4548480c0a7fec9141c9a6e90674e9cecac28084

Authored by Daniela Feitosa
Committed by Antonio Terceiro
1 parent 0f6c78ef

ActionItem1218: allowing admin decide number of news boxes

app/controllers/admin/admin_panel_controller.rb
... ... @@ -32,16 +32,17 @@ class AdminPanelController < AdminController
32 32 end
33 33  
34 34 def set_portal_folders
35   - @portal_folders = environment.portal_community.folders
  35 + @selected = (environment.portal_folders || [])
  36 + @available_portal_folders = environment.portal_community.folders - @selected
  37 +
36 38 if request.post?
37 39 env = environment
38   - folders = params[:folders].map{|fid| Folder.find(:first, :conditions => {:profile_id => env.portal_community, :id => fid})}
  40 + folders = params[:folders].map{|fid| Folder.find(:first, :conditions => {:profile_id => env.portal_community, :id => fid})} if params[:folders]
39 41 env.portal_folders = folders
40 42 if env.save
41 43 flash[:notice] = _('Saved the portal folders')
42 44 redirect_to :action => 'index'
43 45 end
44 46 end
45   - @selected = (environment.portal_folders || []).map(&:id)
46 47 end
47 48 end
... ...
app/models/environment.rb
... ... @@ -622,7 +622,7 @@ class Environment < ActiveRecord::Base
622 622 end
623 623  
624 624 def portal_folders=(folders)
625   - settings[:portal_folders] = folders.map(&:id)
  625 + settings[:portal_folders] = folders ? folders.map(&:id) : nil
626 626 end
627 627  
628 628 def help_message_to_add_enterprise
... ...
app/views/admin_panel/set_portal_folders.rhtml
1 1 <h1><%= _('Select folders') %></h1>
2 2  
3   -<p><%= _('Select what folder will hold the news for witch area of the initial page of the environment') %></p>
  3 +<p><%= _('Select the folders that will hold the news of the initial page of the environment') %></p>
  4 +
  5 +<script type="text/javascript">
  6 + jQuery(document).ready(function() {
  7 + jQuery('#add').click(function() {
  8 + return !jQuery('#portal-folders-list option:selected').remove().appendTo('#selected-folders');
  9 + });
  10 + jQuery('#remove').click(function() {
  11 + return !jQuery('#selected-folders option:selected').remove().appendTo('#portal-folders-list');
  12 + });
  13 +
  14 + jQuery('form').submit(function() {
  15 + jQuery('#selected-folders option').each(function(i) {
  16 + jQuery(this).attr('selected','selected');
  17 + });
  18 + });
  19 + });
  20 + </script>
  21 +
  22 +<div id='available-folders'>
  23 + <%= labelled_form_field(_('Available folders'), select_tag( 'folders[]', options_from_collection_for_select(@available_portal_folders, :id, :name, nil), {:id => 'portal-folders-list', :multiple => true, :size => 6 }) ) %>
  24 +</div>
  25 +
  26 +<div id='selection-buttons'>
  27 + <%= button :down, _('Add'), '#', { :id => 'add' } %>
  28 + <%= button :up, _('Remove'), '#', { :id => 'remove' } %>
  29 +</div>
4 30  
5 31 <% form_tag do %>
6   - <% (1..4).each do |i| %>
7   - <%= labelled_select _('Area %d: ') % i, 'folders[]', :id, :name, @selected[i-1], @portal_folders %> <br>
8   - <% end %>
  32 + <div id='portal-folders'>
  33 + <%= labelled_form_field(_('Portal folders'), select_tag( 'folders[]', options_from_collection_for_select(@selected, :id, :name, nil), {:id => 'selected-folders', :multiple => true, :size => 6 })) %>
  34 + </div>
9 35  
10 36 <% button_bar do %>
11 37 <%= submit_button 'save', _('Save'), :cancel => {:action => 'index'} %>
... ...
public/stylesheets/controller_admin_panel.css 0 → 100644
... ... @@ -0,0 +1,11 @@
  1 +#available-folders select {
  2 + width: 50%;
  3 +}
  4 +
  5 +#selection-buttons {
  6 + display: block;
  7 +}
  8 +
  9 +#portal-folders select {
  10 + width: 50%;
  11 +}
... ...
test/functional/admin_panel_controller_test.rb
... ... @@ -160,9 +160,43 @@ class AdminPanelControllerTest &lt; Test::Unit::TestCase
160 160 env.save!
161 161  
162 162 get :set_portal_folders
163   - assert_tag :tag => 'option', :attributes => {:value => local.id}, :content => local.name
164   - assert_tag :tag => 'option', :attributes => {:value => tech.id}, :content => tech.name
165   - assert_tag :tag => 'option', :attributes => {:value => politics.id}, :content => politics.name
  163 + [local, tech, politics].each do |folder|
  164 + assert_tag :tag => 'div', :attributes => {:id => 'available-folders'}, :descendant => {:tag => 'option', :attributes => {:value => folder.id}, :content => folder.name}
  165 + end
  166 + end
  167 +
  168 + should 'not display not portal folders on portal-folders' do
  169 + env = Environment.default
  170 + c = Community.create!(:name => 'portal')
  171 + env.portal_community = c
  172 + local = Folder.create!(:profile => c, :name => 'local news')
  173 + tech = Folder.create!(:profile => c, :name => 'tech news')
  174 + politics = Folder.create!(:profile => c, :name => 'politics news')
  175 + env.save!
  176 +
  177 + get :set_portal_folders
  178 + [local, tech, politics].each do |folder|
  179 + assert_no_tag :tag => 'div', :attributes => {:id => 'portal-folders'}, :descendant => {:tag => 'option', :attributes => {:value => folder.id}, :content => folder.name}
  180 + end
  181 + end
  182 +
  183 + should 'list portal folders for removal' do
  184 + env = Environment.default
  185 + c = Community.create!(:name => 'portal')
  186 + env.portal_community = c
  187 + local = Folder.create!(:profile => c, :name => 'local news')
  188 + tech = Folder.create!(:profile => c, :name => 'tech news')
  189 + politics = Folder.create!(:profile => c, :name => 'politics news')
  190 + env.save!
  191 + env.portal_folders = [local, tech]
  192 + env.save!
  193 +
  194 + get :set_portal_folders
  195 + [local, tech].each do |folder|
  196 + assert_tag :tag => 'div', :attributes => {:id => 'portal-folders'}, :descendant => {:tag => 'option', :attributes => {:value => folder.id}, :content => folder.name}
  197 + end
  198 +
  199 + assert_no_tag :tag => 'div', :attributes => {:id => 'portal-folders'}, :descendant => {:tag => 'option', :attributes => {:value => politics.id}, :content => politics.name}
166 200 end
167 201  
168 202 should 'save a list of folders as portal folders for the environment' do
... ... @@ -180,4 +214,16 @@ class AdminPanelControllerTest &lt; Test::Unit::TestCase
180 214  
181 215 assert_equal [local, politics, tech].map(&:id), env.portal_folders.map(&:id)
182 216 end
  217 +
  218 + should 'remove all folders as portal folders for the environment' do
  219 + env = Environment.default
  220 + @controller.stubs(:environment).returns(env)
  221 + c = Community.create!(:name => 'portal')
  222 + env.portal_community = c
  223 + env.save!
  224 +
  225 + post :set_portal_folders
  226 +
  227 + assert_equal [], env.portal_folders
  228 + end
183 229 end
... ...
test/unit/environment_test.rb
... ... @@ -741,6 +741,15 @@ class EnvironmentTest &lt; Test::Unit::TestCase
741 741 assert_equal [], e.portal_folders
742 742 end
743 743  
  744 + should 'remove all portal folders' do
  745 + e = Environment.default
  746 +
  747 + e.portal_folders = nil
  748 + e.save!; e.reload
  749 +
  750 + assert_equal [], e.portal_folders
  751 + end
  752 +
744 753 should 'have roles with names independent of other environments' do
745 754 e1 = Environment.create!(:name => 'a test environment')
746 755 role1 = Role.create!(:name => 'test_role', :environment => e1)
... ...