]> source.dussan.org Git - redmine.git/commitdiff
Moves blocks definition to Redmine::MyPage.
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Sat, 29 Oct 2016 10:10:31 +0000 (10:10 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Sat, 29 Oct 2016 10:10:31 +0000 (10:10 +0000)
git-svn-id: http://svn.redmine.org/redmine/trunk@15930 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/controllers/my_controller.rb
app/helpers/my_helper.rb
app/models/user_preference.rb
app/views/my/page_layout.html.erb
lib/redmine/my_page.rb [new file with mode: 0644]
lib/redmine/views/my_page/block.rb [deleted file]
test/functional/my_controller_test.rb

index 73ac2222cc30184a14a01c0f99043d8b658d8c51..6e328c270292ff49b0c3755b186b0537a1966de3 100644 (file)
@@ -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
index 23e738977c1b7147b06fedf14ee53de712b8db97..b3e4e02b7f2462639549a3d37a53e47b40d349cb 100644 (file)
@@ -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)).
index 4a07e990c64c8ef21714eace9e0e8023773fd191..bf54e5a5b055b80d5e0e4fc69b8f3b35edfa02fd 100644 (file)
@@ -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
index 70bf835fb28f70b5fec1b49e3e91ac8f0e1fb4c0..690aa25a895dbf697dd81fb07a4fa36052d419fe 100644 (file)
@@ -1,12 +1,9 @@
 <div class="contextual">
-<% 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' %>
 </div>
diff --git a/lib/redmine/my_page.rb b/lib/redmine/my_page.rb
new file mode 100644 (file)
index 0000000..0c11459
--- /dev/null
@@ -0,0 +1,62 @@
+# Redmine - project management software
+# Copyright (C) 2006-2016  Jean-Philippe Lang
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+
+module Redmine
+  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/lib/redmine/views/my_page/block.rb b/lib/redmine/views/my_page/block.rb
deleted file mode 100644 (file)
index 07398d7..0000000
+++ /dev/null
@@ -1,32 +0,0 @@
-# Redmine - project management software
-# Copyright (C) 2006-2016  Jean-Philippe Lang
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License
-# as published by the Free Software Foundation; either version 2
-# of the License, or (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# 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
-      end
-    end
-  end
-end
index 41d9171b78ef1f562a76c3980596650e463489a2..b64b311a2f28f24c6b3a4a973501133051d6c0a4 100644 (file)
@@ -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!