Fixes block reordering on my page (#2971).

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@3194 e93f8b46-1217-0410-a6f0-8f06a7374b81
pull/351/head
Jean-Philippe Lang 15 years ago
parent 008ad85d10
commit 9d120c872c
  1. 33
      app/controllers/my_controller.rb
  2. 3
      app/views/my/page_layout.rhtml
  3. 17
      test/fixtures/user_preferences.yml
  4. 26
      test/functional/my_controller_test.rb

@ -35,7 +35,6 @@ class MyController < ApplicationController
}.freeze
verify :xhr => true,
:session => :page_layout,
:only => [:add_block, :remove_block, :order_blocks]
def index
@ -109,8 +108,6 @@ class MyController < ApplicationController
def page_layout
@user = User.current
@blocks = @user.pref[:my_page_layout] || DEFAULT_LAYOUT.dup
session[:page_layout] = @blocks
%w(top left right).each {|f| session[:page_layout][f] ||= [] }
@block_options = []
BLOCKS.each {|k, v| @block_options << [l("my.blocks.#{v}", :default => [v, v.to_s.humanize]), k.dasherize]}
end
@ -122,10 +119,13 @@ class MyController < ApplicationController
block = params[:block].to_s.underscore
(render :nothing => true; return) unless block && (BLOCKS.keys.include? block)
@user = User.current
layout = @user.pref[:my_page_layout] || {}
# remove if already present in a group
%w(top left right).each {|f| (session[:page_layout][f] ||= []).delete block }
%w(top left right).each {|f| (layout[f] ||= []).delete block }
# add it on top
session[:page_layout]['top'].unshift block
layout['top'].unshift block
@user.pref[:my_page_layout] = layout
@user.pref.save
render :partial => "block", :locals => {:user => @user, :block_name => block}
end
@ -133,8 +133,12 @@ class MyController < ApplicationController
# params[:block] : id of the block to remove
def remove_block
block = params[:block].to_s.underscore
@user = User.current
# remove block in all groups
%w(top left right).each {|f| (session[:page_layout][f] ||= []).delete block }
layout = @user.pref[:my_page_layout] || {}
%w(top left right).each {|f| (layout[f] ||= []).delete block }
@user.pref[:my_page_layout] = layout
@user.pref.save
render :nothing => true
end
@ -143,25 +147,20 @@ class MyController < ApplicationController
# params[:list-(top|left|right)] : array of block ids of the group
def order_blocks
group = params[:group]
@user = User.current
if group.is_a?(String)
group_items = (params["list-#{group}"] || []).collect(&:underscore)
if group_items and group_items.is_a? Array
layout = @user.pref[:my_page_layout] || {}
# remove group blocks if they are presents in other groups
%w(top left right).each {|f|
session[:page_layout][f] = (session[:page_layout][f] || []) - group_items
layout[f] = (layout[f] || []) - group_items
}
session[:page_layout][group] = group_items
layout[group] = group_items
@user.pref[:my_page_layout] = layout
@user.pref.save
end
end
render :nothing => true
end
# Save user's page layout
def page_layout_save
@user = User.current
@user.pref[:my_page_layout] = session[:page_layout] if session[:page_layout]
@user.pref.save
session[:page_layout] = nil
redirect_to :action => 'page'
end
end

@ -46,8 +46,7 @@ function removeBlock(block) {
}, :class => 'icon icon-add'
%>
<% end %>
<%= link_to l(:button_save), {:action => 'page_layout_save'}, :class => 'icon icon-save' %>
<%= link_to l(:button_cancel), {:action => 'page'}, :class => 'icon icon-cancel' %>
<%= link_to l(:button_back), {:action => 'page'}, :class => 'icon icon-cancel' %>
</div>
<h2><%=l(:label_my_page)%></h2>

@ -4,11 +4,10 @@ user_preferences_001:
---
:my_page_layout:
left:
- latest_news
- latestnews
- documents
right:
- issues_assigned_to_me
- issues_reported_by_me
- issuesassignedtome
top:
- calendar
@ -16,8 +15,16 @@ user_preferences_001:
user_id: 1
hide_mail: true
user_preferences_002:
others: |+
--- {}
others: |
---
:my_page_layout:
left:
- latestnews
- documents
right:
- issuesassignedtome
top:
- calendar
id: 2
user_id: 3

@ -22,7 +22,7 @@ require 'my_controller'
class MyController; def rescue_action(e) raise e end; end
class MyControllerTest < ActionController::TestCase
fixtures :users, :issues, :issue_statuses, :trackers, :enumerations, :custom_fields
fixtures :users, :user_preferences, :roles, :projects, :issues, :issue_statuses, :trackers, :enumerations, :custom_fields
def setup
@controller = MyController.new
@ -105,4 +105,28 @@ class MyControllerTest < ActionController::TestCase
assert_redirected_to 'my/account'
assert User.try_to_login('jsmith', 'hello')
end
def test_page_layout
get :page_layout
assert_response :success
assert_template 'page_layout'
end
def test_add_block
xhr :post, :add_block, :block => 'issuesreportedbyme'
assert_response :success
assert User.find(2).pref[:my_page_layout]['top'].include?('issuesreportedbyme')
end
def test_remove_block
xhr :post, :remove_block, :block => 'issuesassignedtome'
assert_response :success
assert !User.find(2).pref[:my_page_layout].values.flatten.include?('issuesassignedtome')
end
def test_order_blocks
xhr :post, :order_blocks, :group => 'left', 'list-left' => ['documents', 'calendar', 'latestnews']
assert_response :success
assert_equal ['documents', 'calendar', 'latestnews'], User.find(2).pref[:my_page_layout]['left']
end
end

Loading…
Cancel
Save