From efc05bc73cf53e2c9d57147f7226f9b17a6350ce Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Tue, 29 Jul 2014 17:52:27 +0000 Subject: [PATCH] Add time entries custom fields to the context menu for quick bulk edit (#17484). git-svn-id: http://svn.redmine.org/redmine/trunk@13335 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/controllers/context_menus_controller.rb | 15 +++++++++++++++ app/helpers/context_menus_helper.rb | 7 +++++++ app/models/time_entry.rb | 10 ++++++++++ app/views/context_menus/time_entries.html.erb | 14 ++++++++++++++ .../functional/context_menus_controller_test.rb | 17 +++++++++++++++++ 5 files changed, 63 insertions(+) diff --git a/app/controllers/context_menus_controller.rb b/app/controllers/context_menus_controller.rb index 2b174c016..36cb13f26 100644 --- a/app/controllers/context_menus_controller.rb +++ b/app/controllers/context_menus_controller.rb @@ -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 diff --git a/app/helpers/context_menus_helper.rb b/app/helpers/context_menus_helper.rb index 9dcfbc144..b97d9844a 100644 --- a/app/helpers/context_menus_helper.rb +++ b/app/helpers/context_menus_helper.rb @@ -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 diff --git a/app/models/time_entry.rb b/app/models/time_entry.rb index cfca47639..103d5ae62 100644 --- a/app/models/time_entry.rb +++ b/app/models/time_entry.rb @@ -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 diff --git a/app/views/context_menus/time_entries.html.erb b/app/views/context_menus/time_entries.html.erb index 10dbcafd2..3ab8d07aa 100644 --- a/app/views/context_menus/time_entries.html.erb +++ b/app/views/context_menus/time_entries.html.erb @@ -23,6 +23,20 @@ <% end %> + <% @options_by_custom_field.each do |field, options| %> +
  • + <%= h(field.name) %> + +
  • + <% end %> + <%= call_hook(:view_time_entries_context_menu_end, {:time_entries => @time_entries, :can => @can, :back => @back }) %>
  • diff --git a/test/functional/context_menus_controller_test.rb b/test/functional/context_menus_controller_test.rb index b83c2d696..6670927cd 100644 --- a/test/functional/context_menus_controller_test.rb +++ b/test/functional/context_menus_controller_test.rb @@ -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&ids%5B%5D=2&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&ids%5B%5D=2&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&ids%5B%5D=2&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 -- 2.39.5