}.freeze
verify :xhr => true,
- :session => :page_layout,
:only => [:add_block, :remove_block, :order_blocks]
def index
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
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
# 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
# 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
}, :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>
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
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