]> source.dussan.org Git - redmine.git/commitdiff
Don't hardcode the groups on My page.
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Tue, 4 Apr 2017 17:17:47 +0000 (17:17 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Tue, 4 Apr 2017 17:17:47 +0000 (17:17 +0000)
git-svn-id: http://svn.redmine.org/redmine/trunk@16475 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/controllers/my_controller.rb
app/models/user_preference.rb
app/views/my/page.html.erb
lib/redmine/my_page.rb
public/stylesheets/application.css
public/stylesheets/responsive.css
test/functional/my_controller_test.rb

index f0b56e44f5c75d49da29a98af8a9df58a78c0ded..15fc9e3882500b7a0650ca3bd376e1a9dff0d1c3 100644 (file)
@@ -37,6 +37,7 @@ class MyController < ApplicationController
   # Show user's page
   def page
     @user = User.current
+    @groups = @user.pref.my_page_groups
     @blocks = @user.pref.my_page_layout
   end
 
@@ -178,14 +179,8 @@ class MyController < ApplicationController
   # params[:blocks] : array of block ids of the group
   def order_blocks
     @user = User.current
-    group = params[:group].to_s
-    if %w(top left right).include? group
-      group_items = (params[:blocks] || []).collect(&:underscore)
-      # remove group blocks if they are presents in other groups
-      group_items.each {|s| @user.pref.remove_block(s)}
-      @user.pref.my_page_layout[group] = group_items
-      @user.pref.save
-    end
+    @user.pref.order_blocks params[:group], params[:blocks]
+    @user.pref.save
     head 200
   end
 end
index 836ad39786d9aa086e6436f3ccefb5282484e57f..861cf9323cb401ac72b9350b6cc0e3213ab7e753 100644 (file)
@@ -88,6 +88,14 @@ class UserPreference < ActiveRecord::Base
   def textarea_font; self[:textarea_font] end
   def textarea_font=(value); self[:textarea_font]=value; end
 
+  # Returns the names of groups that are displayed on user's page
+  # Example:
+  #   preferences.my_page_groups
+  #   # => ['top', 'left, 'right']
+  def my_page_groups
+    Redmine::MyPage.groups
+  end
+
   def my_page_layout
     self[:my_page_layout] ||= Redmine::MyPage.default_layout.deep_dup
   end
@@ -110,10 +118,12 @@ class UserPreference < ActiveRecord::Base
   end
 
   # Removes block from the user page layout
+  # Example:
+  #   preferences.remove_block('news')
   def remove_block(block)
     block = block.to_s.underscore
-    %w(top left right).each do |f|
-      (my_page_layout[f] ||= []).delete(block)
+    my_page_layout.keys.each do |group|
+      my_page_layout[group].delete(block)
     end
     my_page_layout
   end
@@ -126,9 +136,22 @@ class UserPreference < ActiveRecord::Base
     return unless Redmine::MyPage.valid_block?(block, my_page_layout.values.flatten)
 
     remove_block(block)
-    # add it on top
-    my_page_layout['top'] ||= []
-    my_page_layout['top'].unshift(block)
+    # add it to the first group
+    group = my_page_groups.first
+    my_page_layout[group] ||= []
+    my_page_layout[group].unshift(block)
+  end
+
+  # Sets the block order for the given group.
+  # Example:
+  #   preferences.order_blocks('left', ['issueswatched', 'news'])
+  def order_blocks(group, blocks)
+    group = group.to_s
+    if Redmine::MyPage.groups.include?(group) && blocks.present?
+      blocks = blocks.map(&:underscore) & my_page_layout.values.flatten
+      blocks.each {|block| remove_block(block)}
+      my_page_layout[group] = blocks
+    end
   end
 
   def update_block_settings(block, settings)
index 7f67d0a90e6a9c0ae3099977b6eb941a5d431a9e..ba150fd790d00dced9aebee37a6f778d75c180d6 100644 (file)
@@ -8,17 +8,11 @@
 <h2><%=l(:label_my_page)%></h2>
 
 <div id="my-page">
-<div id="list-top" class="block-receiver">
-  <%= render_blocks(@blocks['top'], @user) %>
-</div>
-
-<div id="list-left" class="splitcontentleft block-receiver">
-  <%= render_blocks(@blocks['left'], @user) %>
-</div>
-
-<div id="list-right" class="splitcontentright block-receiver">
-  <%= render_blocks(@blocks['right'], @user) %>
-</div>
+<% @groups.each do |group| %>
+  <div id="list-<%= group %>" class="block-receiver">
+    <%= render_blocks(@blocks[group], @user) %>
+  </div>
+<% end %>
 </div>
 
 <%= context_menu %>
@@ -26,7 +20,7 @@
 <%= javascript_tag do %>
 $(document).ready(function(){
   $('#block-select').val('');
-  $('#list-top, #list-left, #list-right').sortable({
+  $('.block-receiver').sortable({
     connectWith: '.block-receiver',
     tolerance: 'pointer',
     handle: '.sort-handle',
index 02feb54bb9487575100ee0924d18d1e4a766fcec..e2f078a14156229213c625742dc31b8acb2f358f 100644 (file)
@@ -19,6 +19,8 @@ module Redmine
   module MyPage
     include Redmine::I18n
 
+    CORE_GROUPS = ['top', 'left', 'right']
+
     CORE_BLOCKS = {
         'issuesassignedtome' => {:label => :label_assigned_to_me_issues},
         'issuesreportedbyme' => {:label => :label_reported_issues},
@@ -30,6 +32,10 @@ module Redmine
         'timelog' => {:label => :label_spent_time}
       }
 
+    def self.groups
+      CORE_GROUPS.dup.freeze
+    end
+
     # Returns the available blocks
     def self.blocks
       CORE_BLOCKS.merge(additional_blocks).freeze
index a250ba36beec56cc0392462eadfedfb360e4ab35..5c45cf5a66b8e4406ab3d0aeac1e0e7bee501fe8 100644 (file)
@@ -428,8 +428,8 @@ div.square {
 .message .contextual { margin-top: 0; }
 
 .splitcontent {overflow:auto;}
-.splitcontentleft{float:left; width:49%;}
-.splitcontentright{float:right; width:49%;}
+.splitcontentleft, #list-left {float:left; width:49%;}
+.splitcontentright, #list-right {float:right; width:49%;}
 form {display: inline;}
 input, select {vertical-align: middle; margin-top: 1px; margin-bottom: 1px;}
 input[type="submit"] { -webkit-appearance: button; }
index 8e7f7465ada457cc05ca1f826c6a70caaab8d123..e4b8be20eaeeca7772bab8d2bf68bc1bb883d3d9 100644 (file)
     display: none;
   }
 
-  .splitcontentleft {
+  .splitcontentleft, #list-left {
     width: 100%;
   }
 
-  .splitcontentright {
+  .splitcontentright, #list-right {
     width: 100%;
   }
 
index 92f5f8fa2c6c70cd60b0c9e3dcc5d926d0bd3bd0..c35ff90436b30d8e937dd9f8d069c7daa5f0d6b0 100644 (file)
@@ -411,9 +411,23 @@ class MyControllerTest < Redmine::ControllerTest
   end
 
   def test_order_blocks
-    xhr :post, :order_blocks, :group => 'left', 'blocks' => ['documents', 'calendar', 'latestnews']
+    pref = User.find(2).pref
+    pref.my_page_layout = {'left' => ['news', 'calendar','documents']}
+    pref.save!
+    
+    xhr :post, :order_blocks, :group => 'left', :blocks => ['documents', 'calendar', 'news']
     assert_response :success
-    assert_equal ['documents', 'calendar', 'latestnews'], User.find(2).pref[:my_page_layout]['left']
+    assert_equal ['documents', 'calendar', 'news'], User.find(2).pref.my_page_layout['left']
+  end
+
+  def test_move_block
+    pref = User.find(2).pref
+    pref.my_page_layout = {'left' => ['news','documents'], 'right' => ['calendar']}
+    pref.save!
+    
+    xhr :post, :order_blocks, :group => 'left', :blocks => ['news', 'calendar', 'documents']
+    assert_response :success
+    assert_equal({'left' => ['news', 'calendar', 'documents'], 'right' => []}, User.find(2).pref.my_page_layout)
   end
 
   def test_reset_rss_key_with_existing_key