diff --git a/app/views/timesheet/_overhead_form.html.erb b/app/views/timesheet/_overhead_form.html.erb index 5e0ba82..3835ed3 100644 --- a/app/views/timesheet/_overhead_form.html.erb +++ b/app/views/timesheet/_overhead_form.html.erb @@ -4,7 +4,7 @@
<%= select_tag('timesheet[billable][]', - options_for_select(billable_values, selected_values), + options_for_select([['Billable','billable'],['Overhead','overhead']], selected_values), {:multiple => true, :size => list_size}) %>

diff --git a/lib/overhead_time_entry_activity_patch.rb b/lib/overhead_time_entry_activity_patch.rb index be48862..d2c06ae 100644 --- a/lib/overhead_time_entry_activity_patch.rb +++ b/lib/overhead_time_entry_activity_patch.rb @@ -24,20 +24,49 @@ module OverheadTimeEntryActivityPatch Setting['plugin_redmine_overhead'] && Setting['plugin_redmine_overhead']['custom_field'] end - def find_with_billable_values(values = []) + def billable_value_configured? + Setting['plugin_redmine_overhead']['billable_value'] + end + + def overhead_value_configured? + Setting['plugin_redmine_overhead']['overhead_value'] + end + + def find_billable_activities return [] unless overhead_configured? + return [] unless billable_value_configured? billable_field = TimeEntryActivity.billable_custom_field if billable_field + value = Setting['plugin_redmine_overhead']['billable_value'] == 'true' # convert "true" to true... finder = ARCondition.new finder.add "#{CustomValue.table_name}.customized_type = '#{TimeEntryActivity.class_name}'" finder.add ["#{CustomValue.table_name}.custom_field_id = ?", billable_field.id] - finder.add ["#{CustomValue.table_name}.value IN (?)", values] + finder.add ["#{CustomValue.table_name}.value IN (?)", value] + return TimeEntryActivity.find(:all, :include => :custom_values, :conditions => finder.conditions) + else + return [] + end + + end + + def find_overhead_activities + return [] unless overhead_configured? + return [] unless overhead_value_configured? + + billable_field = TimeEntryActivity.billable_custom_field + if billable_field + value = Setting['plugin_redmine_overhead']['overhead_value'] == 'true' # convert "true" to true... + finder = ARCondition.new + finder.add "#{CustomValue.table_name}.customized_type = '#{TimeEntryActivity.class_name}'" + finder.add ["#{CustomValue.table_name}.custom_field_id = ?", billable_field.id] + finder.add ["#{CustomValue.table_name}.value IN (?)", value] return TimeEntryActivity.find(:all, :include => :custom_values, :conditions => finder.conditions) else return [] end + end end diff --git a/lib/overhead_timesheet_hook.rb b/lib/overhead_timesheet_hook.rb index 5c76476..5924d5b 100644 --- a/lib/overhead_timesheet_hook.rb +++ b/lib/overhead_timesheet_hook.rb @@ -23,7 +23,6 @@ class OverheadTimesheetHook < Redmine::Hook::ViewListener # Added a new field for filtering based on "billable?" def plugin_timesheet_views_timesheet_form(context={}) - billable_values = select_values_for_field(TimeEntryActivity.billable_custom_field) if context[:params] && context[:params][:timesheet] && context[:params][:timesheet][:billable] @@ -37,15 +36,24 @@ class OverheadTimesheetHook < Redmine::Hook::ViewListener :layout => false, :locals => { :list_size => context[:list_size] || 5, - :billable_values => billable_values, :selected_values => selected_values }) end def plugin_timesheet_controller_report_pre_fetch_time_entries(context = {}) if context[:params] && context[:params][:timesheet] && context[:params][:timesheet][:billable] - activities = TimeEntryActivity.find_with_billable_values(context[:params][:timesheet][:billable]) - context[:timesheet].activities = activities.collect(&:id) unless activities.empty? + billable_options = context[:params][:timesheet][:billable] + activities = [] + + if billable_options.include?("billable") + activities << TimeEntryActivity.find_billable_activities + end + + if billable_options.include?("overhead") + activities << TimeEntryActivity.find_overhead_activities + end + + context[:timesheet].activities = activities.flatten.uniq.compact.collect(&:id).sort unless activities.empty? end end end diff --git a/spec/lib/overhead_time_entry_activity_patch_spec.rb b/spec/lib/overhead_time_entry_activity_patch_spec.rb index ca048c0..49b77f9 100644 --- a/spec/lib/overhead_time_entry_activity_patch_spec.rb +++ b/spec/lib/overhead_time_entry_activity_patch_spec.rb @@ -96,16 +96,17 @@ describe TimeEntryActivity, '#billable_custom_field' do end end -describe TimeEntryActivity, '#find_with_billable_values' do +describe TimeEntryActivity, '#find_billable_activities' do before(:each) do TimeEntryActivity.stub!(:overhead_configured?).and_return(true) + TimeEntryActivity.stub!(:billable_value_configured?).and_return(true) end it 'should return nothing if none are found' do - TimeEntryActivity.find_with_billable_values.should be_empty + TimeEntryActivity.find_billable_activities.should be_empty end - it 'should return the time entry activities with the specified billable value' do + it 'should return the billable time entry activities' do custom_field = mock_model(TimeEntryActivityCustomField, :possible_values => ['A','B','Nil'], :field_format => 'list') @@ -114,7 +115,31 @@ describe TimeEntryActivity, '#find_with_billable_values' do activities = [mock_model(TimeEntryActivity), mock_model(TimeEntryActivity)] TimeEntryActivity.should_receive(:find).and_return(activities) - response = TimeEntryActivity.find_with_billable_values(['A','B']) + response = TimeEntryActivity.find_billable_activities + response.should eql(activities) + end +end + +describe TimeEntryActivity, '#find_overhead_activities' do + before(:each) do + TimeEntryActivity.stub!(:overhead_configured?).and_return(true) + TimeEntryActivity.stub!(:overhead_value_configured?).and_return(true) + end + + it 'should return nothing if none are found' do + TimeEntryActivity.find_overhead_activities.should be_empty + end + + it 'should return the overhead time entry activities' do + custom_field = mock_model(TimeEntryActivityCustomField, + :possible_values => ['A','B','Nil'], + :field_format => 'list') + TimeEntryActivity.stub!(:billable_custom_field).and_return(custom_field) + + activities = [mock_model(TimeEntryActivity), mock_model(TimeEntryActivity)] + TimeEntryActivity.should_receive(:find).and_return(activities) + + response = TimeEntryActivity.find_overhead_activities response.should eql(activities) end end diff --git a/spec/lib/overhead_timesheet_hook_spec.rb b/spec/lib/overhead_timesheet_hook_spec.rb index 27406b8..a7aea91 100644 --- a/spec/lib/overhead_timesheet_hook_spec.rb +++ b/spec/lib/overhead_timesheet_hook_spec.rb @@ -63,37 +63,25 @@ describe OverheadTimesheetHook, "#plugin_timesheet_views_timesheet_form", :type response.should have_tag('select[name=?][multiple=multiple]', 'timesheet[billable][]') end - it 'should populate the select field with the possible options of the custom data field' do - @custom_field = mock_model(TimeEntryActivityCustomField, - :possible_values => ['A','B','Nil'], - :field_format => 'list') - TimeEntryActivity.should_receive(:billable_custom_field).and_return(@custom_field) - + it 'should populate the select field with "Billable" and "Overhead" options' do response = call_hook(:plugin_timesheet_views_timesheet_form, {}) response.should have_tag('select') do - with_tag('option[value=?]','A','A') - with_tag('option[value=?]','B','B') - with_tag('option[value=?]','Nil','none') + with_tag('option[value=?]','billable','Billable') + with_tag('option[value=?]','overhead','Overhead') end end it 'should pre-select the values from the submission' do - @custom_field = mock_model(TimeEntryActivityCustomField, - :possible_values => ['A','B','Nil'], - :field_format => 'list') - TimeEntryActivity.stub!(:billable_custom_field).and_return(@custom_field) - context = { - :params => {:timesheet => {:billable => ['A','Nil']}} + :params => {:timesheet => {:billable => ['overhead']}} } response = call_hook(:plugin_timesheet_views_timesheet_form, context) + response.should have_tag('select') do - with_tag('option[value=?][selected=selected]','A','A') - with_tag('option[value=?]','B','B') - with_tag('option[value=?][selected=selected]','Nil','none') + with_tag('option[value=?]','billable','Billable') + with_tag('option[value=?][selected=selected]','overhead','Overhead') end - end end @@ -111,32 +99,52 @@ describe OverheadTimesheetHook, "#plugin_timesheet_controller_report_pre_fetch_t call_hook(:plugin_timesheet_controller_report_pre_fetch_time_entries, context) end - - it 'should change the activity list based on the values submitted' do - custom_field = mock_model(TimeEntryActivityCustomField, - :possible_values => ['A','B','Nil'], - :field_format => 'list') - TimeEntryActivity.stub!(:billable_custom_field).and_return(custom_field) - activities = [ - mock_model(TimeEntryActivity, :id => 100), - mock_model(TimeEntryActivity, :id => 201), - mock_model(TimeEntryActivity, :id => 342) - ] - - TimeEntryActivity.should_receive(:find_with_billable_values).with(['A','Nil']).and_return(activities) - - @timesheet.should_receive(:activities=).with([100,201,342]) - context = { - :timesheet => @timesheet, - :params => - { - :timesheet => { - :billable => ['A','Nil'] + describe 'should change the activities' do + before(:each) do + @billable_activities = [ + mock_model(TimeEntryActivity, :id => 100), + mock_model(TimeEntryActivity, :id => 201), + mock_model(TimeEntryActivity, :id => 342) + ] + @overhead_activities = [ + mock_model(TimeEntryActivity, :id => 102), + mock_model(TimeEntryActivity, :id => 202), + mock_model(TimeEntryActivity, :id => 344) + ] + end + + def call_hook_with_billable_options(options = []) + context = { + :timesheet => @timesheet, + :params => + { + :timesheet => { + :billable => options + } } } - } - call_hook(:plugin_timesheet_controller_report_pre_fetch_time_entries, context) + call_hook(:plugin_timesheet_controller_report_pre_fetch_time_entries, context) + end + + it 'to only the billable activities when "billable" is selected' do + TimeEntryActivity.should_receive(:find_billable_activities).and_return(@billable_activities) + @timesheet.should_receive(:activities=).with([100,201,342]) + call_hook_with_billable_options(['billable']) + end + + it 'to only the overhead activities when "overhead" is selected' do + TimeEntryActivity.should_receive(:find_overhead_activities).and_return(@overhead_activities) + @timesheet.should_receive(:activities=).with([102,202,344]) + call_hook_with_billable_options(['overhead']) + end + + it 'to all activities when "billable" and "overhead" is selected' do + TimeEntryActivity.should_receive(:find_billable_activities).and_return(@billable_activities) + TimeEntryActivity.should_receive(:find_overhead_activities).and_return(@overhead_activities) + @timesheet.should_receive(:activities=).with([100,102,201,202,342,344]) + call_hook_with_billable_options(['billable','overhead']) + end end end