[#2279] Hooked up the Billable filter to adjust the Timesheet's Activities.
* Hooked into the TimesheetController to modify the Timesheet activities * Added TimeEntryActivity#find_with_billable_values to find all the TimeEntryActivities which have the value in the billable custom field.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user