diff --git a/lib/overhead_time_entry_activity_patch.rb b/lib/overhead_time_entry_activity_patch.rb index 13e3732..be48862 100644 --- a/lib/overhead_time_entry_activity_patch.rb +++ b/lib/overhead_time_entry_activity_patch.rb @@ -23,6 +23,22 @@ module OverheadTimeEntryActivityPatch def overhead_configured? Setting['plugin_redmine_overhead'] && Setting['plugin_redmine_overhead']['custom_field'] end + + def find_with_billable_values(values = []) + return [] unless overhead_configured? + + billable_field = TimeEntryActivity.billable_custom_field + if billable_field + 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] + + return TimeEntryActivity.find(:all, :include => :custom_values, :conditions => finder.conditions) + else + return [] + end + end end module InstanceMethods diff --git a/lib/overhead_timesheet_hook.rb b/lib/overhead_timesheet_hook.rb index ecac969..5c76476 100644 --- a/lib/overhead_timesheet_hook.rb +++ b/lib/overhead_timesheet_hook.rb @@ -41,4 +41,11 @@ class OverheadTimesheetHook < Redmine::Hook::ViewListener :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? + 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 2e9bdef..ca048c0 100644 --- a/spec/lib/overhead_time_entry_activity_patch_spec.rb +++ b/spec/lib/overhead_time_entry_activity_patch_spec.rb @@ -95,3 +95,26 @@ describe TimeEntryActivity, '#billable_custom_field' do end end end + +describe TimeEntryActivity, '#find_with_billable_values' do + before(:each) do + TimeEntryActivity.stub!(:overhead_configured?).and_return(true) + end + + it 'should return nothing if none are found' do + TimeEntryActivity.find_with_billable_values.should be_empty + end + + it 'should return the time entry activities with the specified billable value' 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_with_billable_values(['A','B']) + 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 5456b38..27406b8 100644 --- a/spec/lib/overhead_timesheet_hook_spec.rb +++ b/spec/lib/overhead_timesheet_hook_spec.rb @@ -96,3 +96,47 @@ describe OverheadTimesheetHook, "#plugin_timesheet_views_timesheet_form", :type end end + +describe OverheadTimesheetHook, "#plugin_timesheet_controller_report_pre_fetch_time_entries", :type => :view do + include Redmine::Hook::Helper + + before(:each) do + stub_view_to_use_controller_instance + @timesheet = Timesheet.new + end + + it 'should do nothing to the activity list if no billable values are submitted' do + @timesheet.should_not_receive(:activities=) + context = {:timesheet => @timesheet} + 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'] + } + } + } + call_hook(:plugin_timesheet_controller_report_pre_fetch_time_entries, context) + + end +end