From efb58740a3e74f2f29c1dade0fbd171cc251db59 Mon Sep 17 00:00:00 2001 From: Eric Davis Date: Tue, 21 Apr 2009 16:51:20 -0700 Subject: [PATCH] [#2281] Added TimeEntryActivity#billable? query method which will lookup the settings and the current custom values of the TimeEntryActivity. --- init.rb | 7 ++ lib/overhead_time_entry_activity_patch.rb | 42 ++++++++++ ...overhead_time_entry_activity_patch_spec.rb | 77 +++++++++++++++++++ 3 files changed, 126 insertions(+) create mode 100644 lib/overhead_time_entry_activity_patch.rb create mode 100644 spec/lib/overhead_time_entry_activity_patch_spec.rb diff --git a/init.rb b/init.rb index 6120958..9d63c63 100644 --- a/init.rb +++ b/init.rb @@ -1,5 +1,12 @@ require 'redmine' +# Patches to the Redmine core. +require 'dispatcher' +require 'overhead_time_entry_activity_patch' +Dispatcher.to_prepare do + TimeEntryActivity.send(:include, OverheadTimeEntryActivityPatch) +end + Redmine::Plugin.register :redmine_overhead do name 'Overhead plugin' author 'Eric Davis' diff --git a/lib/overhead_time_entry_activity_patch.rb b/lib/overhead_time_entry_activity_patch.rb new file mode 100644 index 0000000..4bc4cba --- /dev/null +++ b/lib/overhead_time_entry_activity_patch.rb @@ -0,0 +1,42 @@ +require_dependency 'time_entry_activity' + +module OverheadTimeEntryActivityPatch + def self.included(base) + base.extend(ClassMethods) + + base.send(:include, InstanceMethods) + + base.class_eval do + unloadable + end + + end + + module ClassMethods + + end + + module InstanceMethods + # Is the Activity Billable, based on it's custom data? + def billable? + if Setting['plugin_redmine_overhead'] && Setting['plugin_redmine_overhead']['custom_field'] + billable_field = TimeEntryActivityCustomField.find_by_id(Setting['plugin_redmine_overhead']['custom_field']) + + if billable_field && field = self.custom_value_for(billable_field) + if billable_field.field_format == 'bool' + setting = (Setting['plugin_redmine_overhead']['billable_value'] == "true") ? true : false + activity_field = (field.value == "1") ? true : false + return activity_field == setting + else + return field.value == Setting['plugin_redmine_overhead']['billable_value'] + end + else + return false + end + else + return false + end + 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 new file mode 100644 index 0000000..9fa64ea --- /dev/null +++ b/spec/lib/overhead_time_entry_activity_patch_spec.rb @@ -0,0 +1,77 @@ +require File.dirname(__FILE__) + '/../spec_helper' + +describe TimeEntryActivity, 'billable?' do + before(:each) do + @activity = TimeEntryActivity.new + + end + + describe 'with a boolean Custom Field' do + before(:each) do + @custom_field = mock_model(TimeEntryActivityCustomField) + @custom_field.should_receive(:field_format).and_return("bool") + TimeEntryActivityCustomField.should_receive(:find_by_id).and_return(@custom_field) + + Setting.should_receive(:[]).with('plugin_redmine_overhead').at_least(:once).and_return do + { + "custom_field"=> @custom_field.id.to_s, + "billable_value"=>"true", + "overhead_value"=>"false" + } + end + end + + it 'should be true if the current Billing Status value is the Billable value' do + @activity.should_receive(:custom_value_for).with(@custom_field).and_return do + mock_model(CustomValue, :value => "1") + end + + @activity.billable?.should be_true + end + + it 'should be false if the current Billing Status value is a different Billable value' do + @activity.should_receive(:custom_value_for).with(@custom_field).and_return do + mock_model(CustomValue, :value => "0") + end + + @activity.billable?.should be_false + end + end + + describe 'with a list Custom Field' do + before(:each) do + @custom_field = mock_model(TimeEntryActivityCustomField) + @custom_field.should_receive(:field_format).and_return("list") + TimeEntryActivityCustomField.should_receive(:find_by_id).and_return(@custom_field) + + Setting.should_receive(:[]).with('plugin_redmine_overhead').at_least(:once).and_return do + { + "custom_field"=> @custom_field.id.to_s, + "billable_value"=>"is Billable", + "overhead_value"=>"is not Billable" + } + end + end + + it 'should be true if the current Billing Status value is the Billable value' do + @activity.should_receive(:custom_value_for).with(@custom_field).and_return do + mock_model(CustomValue, :value => "is Billable") + end + + @activity.billable?.should be_true + end + + it 'should be false if the current Billing Status value is a different Billable value' do + @activity.should_receive(:custom_value_for).with(@custom_field).and_return do + mock_model(CustomValue, :value => "is not Billable") + end + + @activity.billable?.should be_false + end + end + + it 'should be false if the Billing Status is not configured' do + @activity.billable?.should be_false + end + +end