]> source.dussan.org Git - redmine.git/commitdiff
Add time entries custom fields to the context menu for quick bulk edit (#17484).
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Tue, 29 Jul 2014 17:52:27 +0000 (17:52 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Tue, 29 Jul 2014 17:52:27 +0000 (17:52 +0000)
git-svn-id: http://svn.redmine.org/redmine/trunk@13335 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/controllers/context_menus_controller.rb
app/helpers/context_menus_helper.rb
app/models/time_entry.rb
app/views/context_menus/time_entries.html.erb
test/functional/context_menus_controller_test.rb

index 2b174c016783873a82813db344a894f038dffbae..36cb13f261e3d0815d6db8822aa7142e449caf59 100644 (file)
@@ -71,6 +71,9 @@ class ContextMenusController < ApplicationController
   def time_entries
     @time_entries = TimeEntry.where(:id => params[:ids]).preload(:project).to_a
     (render_404; return) unless @time_entries.present?
+    if (@time_entries.size == 1)
+      @time_entry = @time_entries.first
+    end
 
     @projects = @time_entries.collect(&:project).compact.uniq
     @project = @projects.first if @projects.size == 1
@@ -79,6 +82,18 @@ class ContextMenusController < ApplicationController
             :delete => User.current.allowed_to?(:edit_time_entries, @projects)
             }
     @back = back_url
+
+    @options_by_custom_field = {}
+    if @can[:edit]
+      custom_fields = @time_entries.map(&:editable_custom_fields).reduce(:&).reject(&:multiple?)
+      custom_fields.each do |field|
+        values = field.possible_values_options(@projects)
+        if values.present?
+          @options_by_custom_field[field] = values
+        end
+      end
+    end
+
     render :layout => false
   end
 end
index 9dcfbc1445a2f84456d131b5948a94e05c2d00de..b97d9844afc0b94b2fe86bf5f29980220455c3e3 100644 (file)
@@ -40,4 +40,11 @@ module ContextMenusHelper
       :method => :post,
       :selected => (@issue && @issue.custom_field_value(field) == value)
   end
+
+  def bulk_update_time_entry_custom_field_context_menu_link(field, text, value)
+    context_menu_link h(text),
+      bulk_update_time_entries_path(:ids => @time_entries.map(&:id), :time_entry => {'custom_field_values' => {field.id => value}}, :back_url => @back),
+      :method => :post,
+      :selected => (@time_entry && @time_entry.custom_field_value(field) == value)
+  end
 end
index cfca47639205b576ce88122a6a054a5287806289..103d5ae628e8c40aa5998cd345bb6daba9530e7b 100644 (file)
@@ -126,4 +126,14 @@ class TimeEntry < ActiveRecord::Base
   def editable_by?(usr)
     (usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project)
   end
+
+  # Returns the custom_field_values that can be edited by the given user
+  def editable_custom_field_values(user=nil)
+    visible_custom_field_values
+  end
+
+  # Returns the custom fields that can be edited by the given user
+  def editable_custom_fields(user=nil)
+    editable_custom_field_values(user).map(&:custom_field).uniq
+  end
 end
index 10dbcafd25aee15138793059e0e9a60c62c1ffbe..3ab8d07aa536db7e66732799ea719e1d7ab51a1b 100644 (file)
   </li>
   <% end %>
 
+  <% @options_by_custom_field.each do |field, options| %>
+    <li class="folder cf_<%= field.id %>">
+      <a href="#" class="submenu"><%= h(field.name) %></a>
+      <ul>
+      <% options.each do |text, value| %>
+        <li><%= bulk_update_time_entry_custom_field_context_menu_link(field, text, value || text) %></li>
+      <% end %>
+      <% unless field.is_required? %>
+        <li><%= bulk_update_time_entry_custom_field_context_menu_link(field, l(:label_none), '__none__') %></li>
+      <% end %>
+      </ul>
+    </li>
+  <% end %>
+
   <%= call_hook(:view_time_entries_context_menu_end, {:time_entries => @time_entries, :can => @can, :back => @back }) %>
 
   <li>
index b83c2d696914840855472433d3f82fc276bca734..6670927cddbc5dc3695a8394bd666f803540af7f 100644 (file)
@@ -250,6 +250,23 @@ class ContextMenusControllerTest < ActionController::TestCase
     assert_select 'a:not(.disabled)', :text => 'Edit'
   end
 
+  def test_time_entries_context_menu_should_include_custom_fields
+    field = TimeEntryCustomField.generate!(:name => "Field", :field_format => "list", :possible_values => ["foo", "bar"])
+
+    @request.session[:user_id] = 2
+    get :time_entries, :ids => [1, 2]
+    assert_response :success
+    assert_select "li.cf_#{field.id}" do
+      assert_select 'a[href=#]', :text => "Field"
+      assert_select 'ul' do
+        assert_select 'a', 3
+        assert_select 'a[href=?]', "/time_entries/bulk_update?ids%5B%5D=1&amp;ids%5B%5D=2&amp;time_entry%5Bcustom_field_values%5D%5B#{field.id}%5D=foo", :text => 'foo'
+        assert_select 'a[href=?]', "/time_entries/bulk_update?ids%5B%5D=1&amp;ids%5B%5D=2&amp;time_entry%5Bcustom_field_values%5D%5B#{field.id}%5D=bar", :text => 'bar'
+        assert_select 'a[href=?]', "/time_entries/bulk_update?ids%5B%5D=1&amp;ids%5B%5D=2&amp;time_entry%5Bcustom_field_values%5D%5B#{field.id}%5D=__none__", :text => 'none'
+      end
+    end
+  end
+
   def test_time_entries_context_menu_without_edit_permission
     @request.session[:user_id] = 2
     Role.find_by_name('Manager').remove_permission! :edit_time_entries