diff --git a/app/controllers/my_controller.rb b/app/controllers/my_controller.rb index 73ac222..6e328c2 100644 --- a/app/controllers/my_controller.rb +++ b/app/controllers/my_controller.rb @@ -27,19 +27,6 @@ class MyController < ApplicationController helper :users helper :custom_fields - BLOCKS = { 'issuesassignedtome' => :label_assigned_to_me_issues, - 'issuesreportedbyme' => :label_reported_issues, - 'issueswatched' => :label_watched_issues, - 'news' => :label_news_latest, - 'calendar' => :label_calendar, - 'documents' => :label_document_plural, - 'timelog' => :label_spent_time - }.merge(Redmine::Views::MyPage::Block.additional_blocks).freeze - - DEFAULT_LAYOUT = { 'left' => ['issuesassignedtome'], - 'right' => ['issuesreportedbyme'] - }.freeze - def index page render :action => 'page' @@ -48,7 +35,7 @@ class MyController < ApplicationController # Show user's page def page @user = User.current - @blocks = @user.pref[:my_page_layout] || DEFAULT_LAYOUT + @blocks = @user.pref.my_page_layout end # Edit user's account @@ -146,13 +133,7 @@ class MyController < ApplicationController # User's page layout configuration def page_layout @user = User.current - @blocks = @user.pref[:my_page_layout] || DEFAULT_LAYOUT.dup - @block_options = [] - BLOCKS.each do |k, v| - unless @blocks.values.flatten.include?(k) - @block_options << [l("my.blocks.#{v}", :default => [v, v.to_s.humanize]), k.dasherize] - end - end + @blocks = @user.pref.my_page_layout end # Add a block to user's page @@ -160,14 +141,14 @@ class MyController < ApplicationController # params[:block] : id of the block to add def add_block block = params[:block].to_s.underscore - if block.present? && BLOCKS.key?(block) + if block.present? && Redmine::MyPage.blocks.key?(block) @user = User.current - layout = @user.pref[:my_page_layout] || {} + layout = @user.pref.my_page_layout # remove if already present in a group %w(top left right).each {|f| (layout[f] ||= []).delete block } # add it on top layout['top'].unshift block - @user.pref[:my_page_layout] = layout + @user.pref.my_page_layout = layout @user.pref.save end redirect_to my_page_layout_path @@ -179,9 +160,9 @@ class MyController < ApplicationController block = params[:block].to_s.underscore @user = User.current # remove block in all groups - layout = @user.pref[:my_page_layout] || {} + layout = @user.pref.my_page_layout %w(top left right).each {|f| (layout[f] ||= []).delete block } - @user.pref[:my_page_layout] = layout + @user.pref.my_page_layout = layout @user.pref.save redirect_to my_page_layout_path end @@ -196,13 +177,13 @@ class MyController < ApplicationController group_items = (params["blocks"] || []).collect(&:underscore) group_items.each {|s| s.sub!(/^block_/, '')} if group_items and group_items.is_a? Array - layout = @user.pref[:my_page_layout] || {} + layout = @user.pref.my_page_layout # remove group blocks if they are presents in other groups %w(top left right).each {|f| layout[f] = (layout[f] || []) - group_items } layout[group] = group_items - @user.pref[:my_page_layout] = layout + @user.pref.my_page_layout = layout @user.pref.save end end diff --git a/app/helpers/my_helper.rb b/app/helpers/my_helper.rb index 23e7389..b3e4e02 100644 --- a/app/helpers/my_helper.rb +++ b/app/helpers/my_helper.rb @@ -40,7 +40,7 @@ module MyHelper # Renders a single block content def render_block_content(block, user) - unless MyController::BLOCKS.keys.include?(block) + unless Redmine::MyPage.blocks.key?(block) Rails.logger.warn("Unknown block \"#{block}\" found in #{user.login} (id=#{user.id}) preferences") return end @@ -53,6 +53,15 @@ module MyHelper end end + def block_select_tag(user) + disabled = user.pref.my_page_layout.values.flatten + options = content_tag('option') + Redmine::MyPage.block_options.each do |label, block| + options << content_tag('option', label, :value => block, :disabled => disabled.include?(block)) + end + content_tag('select', options, :id => "block-select") + end + def calendar_items(startdt, enddt) Issue.visible. where(:project_id => User.current.projects.map(&:id)). diff --git a/app/models/user_preference.rb b/app/models/user_preference.rb index 4a07e99..bf54e5a 100644 --- a/app/models/user_preference.rb +++ b/app/models/user_preference.rb @@ -82,4 +82,12 @@ class UserPreference < ActiveRecord::Base def textarea_font; self[:textarea_font] end def textarea_font=(value); self[:textarea_font]=value; end + + def my_page_layout + self[:my_page_layout] ||= Redmine::MyPage.default_layout.deep_dup + end + + def my_page_layout=(arg) + self[:my_page_layout] = arg + end end diff --git a/app/views/my/page_layout.html.erb b/app/views/my/page_layout.html.erb index 70bf835..690aa25 100644 --- a/app/views/my/page_layout.html.erb +++ b/app/views/my/page_layout.html.erb @@ -1,12 +1,9 @@
-<% if @block_options.present? %> - <%= form_tag({:action => "add_block"}, :id => "block-form") do %> + +<%= form_tag({:action => "add_block"}, :id => "block-form") do %> <%= label_tag('block-select', l(:label_my_page_block)) %>: - <%= select_tag 'block', - content_tag('option') + options_for_select(@block_options), - :id => "block-select" %> + <%= block_select_tag(@user) %> <%= link_to l(:button_add), '#', :onclick => '$("#block-form").submit()', :class => 'icon icon-add' %> - <% end %> <% end %> <%= link_to l(:button_back), {:action => 'page'}, :class => 'icon icon-cancel' %>
diff --git a/lib/redmine/views/my_page/block.rb b/lib/redmine/my_page.rb similarity index 68% rename from lib/redmine/views/my_page/block.rb rename to lib/redmine/my_page.rb index 07398d7..0c11459 100644 --- a/lib/redmine/views/my_page/block.rb +++ b/lib/redmine/my_page.rb @@ -16,17 +16,47 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. module Redmine - module Views - module MyPage - module Block - def self.additional_blocks - @@additional_blocks ||= Dir.glob("#{Redmine::Plugin.directory}/*/app/views/my/blocks/_*.{rhtml,erb}").inject({}) do |h,file| - name = File.basename(file).split('.').first.gsub(/^_/, '') - h[name] = name.to_sym - h - end - end + module MyPage + include Redmine::I18n + + CORE_BLOCKS = { + 'issuesassignedtome' => :label_assigned_to_me_issues, + 'issuesreportedbyme' => :label_reported_issues, + 'issueswatched' => :label_watched_issues, + 'news' => :label_news_latest, + 'calendar' => :label_calendar, + 'documents' => :label_document_plural, + 'timelog' => :label_spent_time + } + + # Returns the available blocks + def self.blocks + CORE_BLOCKS.merge(additional_blocks).freeze + end + + def self.block_options + options = [] + blocks.each do |k, v| + options << [l("my.blocks.#{v}", :default => [v, v.to_s.humanize]), k.dasherize] end + options + end + + # Returns the additional blocks that are defined by plugin partials + def self.additional_blocks + @@additional_blocks ||= Dir.glob("#{Redmine::Plugin.directory}/*/app/views/my/blocks/_*.{rhtml,erb}").inject({}) do |h,file| + name = File.basename(file).split('.').first.gsub(/^_/, '') + h[name] = name.to_sym + h + end + end + + # Returns the default layout for My Page + def self.default_layout + { + 'left' => ['issuesassignedtome'], + 'right' => ['issuesreportedbyme'] + } end end end diff --git a/test/functional/my_controller_test.rb b/test/functional/my_controller_test.rb index 41d9171..b64b311 100644 --- a/test/functional/my_controller_test.rb +++ b/test/functional/my_controller_test.rb @@ -52,7 +52,7 @@ class MyControllerTest < Redmine::ControllerTest end def test_page_with_all_blocks - blocks = MyController::BLOCKS.keys + blocks = Redmine::MyPage.blocks.keys preferences = User.find(2).pref preferences[:my_page_layout] = {'top' => blocks} preferences.save!