Changed the Timelogs to use both the Systemwide and Project specific TimeEntryActivities

* Added Project#activities to return all the Systemwide and Project specific
  activities, excluding Systemwide ones that are overridden.
* Added some tests for TimelogHelper.

  #4077

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@2948 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Eric Davis 2009-10-21 22:34:39 +00:00
parent 29301c8a38
commit e615266e9a
5 changed files with 133 additions and 4 deletions

View File

@ -29,10 +29,16 @@ module TimelogHelper
# Returns a collection of activities for a select field. time_entry
# is optional and will be used to check if the selected TimeEntryActivity
# is active.
def activity_collection_for_select_options(time_entry=nil)
def activity_collection_for_select_options(time_entry=nil, project=nil)
project ||= @project
if project.nil?
activities = TimeEntryActivity.active
else
activities = project.activities
end
collection = []
if time_entry && !time_entry.activity.active?
if time_entry && time_entry.activity && !time_entry.activity.active?
collection << [ "--- #{l(:actionview_instancetag_blank_option)} ---", '' ]
else
collection << [ "--- #{l(:actionview_instancetag_blank_option)} ---", '' ] unless activities.detect(&:is_default)

View File

@ -62,7 +62,7 @@ class Enumeration < ActiveRecord::Base
named_scope :active, lambda {
{
:conditions => {:active => true},
:conditions => {:active => true, :project_id => nil},
:order => 'position'
}
}

View File

@ -20,6 +20,7 @@ class Project < ActiveRecord::Base
STATUS_ACTIVE = 1
STATUS_ARCHIVED = 9
has_many :time_entry_activities, :conditions => {:active => true } # Specific overidden Activities
has_many :members, :include => :user, :conditions => "#{User.table_name}.type='User' AND #{User.table_name}.status=#{User::STATUS_ACTIVE}"
has_many :member_principals, :class_name => 'Member',
:include => :principal,
@ -155,6 +156,17 @@ class Project < ActiveRecord::Base
statements.empty? ? base_statement : "((#{base_statement}) AND (#{statements.join(' OR ')}))"
end
# Returns all the Systemwide and project specific activities
def activities
overridden_activity_ids = self.time_entry_activities.collect(&:parent_id)
if overridden_activity_ids.empty?
return TimeEntryActivity.active
else
return system_activities_and_project_overrides
end
end
# Returns a :conditions SQL string that can be used to find the issues associated with this project.
#
# Examples:
@ -446,4 +458,12 @@ private
def allowed_actions
@actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten
end
# Returns the systemwide activities merged with the project specific overrides
def system_activities_and_project_overrides
return TimeEntryActivity.active.
find(:all,
:conditions => ["id NOT IN (?)", self.time_entry_activities.collect(&:parent_id)]) +
self.time_entry_activities
end
end

View File

@ -0,0 +1,56 @@
# Redmine - project management software
# Copyright (C) 2006-2009 Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
require File.dirname(__FILE__) + '/../../test_helper'
class TimelogHelperTest < HelperTestCase
include TimelogHelper
include ActionView::Helpers::TextHelper
include ActionView::Helpers::DateHelper
fixtures :projects, :roles, :enabled_modules, :users,
:repositories, :changesets,
:trackers, :issue_statuses, :issues, :versions, :documents,
:wikis, :wiki_pages, :wiki_contents,
:boards, :messages,
:attachments,
:enumerations
def setup
super
end
def test_activities_collection_for_select_options_should_return_array_of_activity_names_and_ids
activities = activity_collection_for_select_options
assert activities.include?(["Design", 9])
assert activities.include?(["Development", 10])
end
def test_activities_collection_for_select_options_should_not_include_inactive_activities
activities = activity_collection_for_select_options
assert !activities.include?(["Inactive Activity", 14])
end
def test_activities_collection_for_select_options_should_use_the_projects_override
project = Project.find(1)
override_activity = TimeEntryActivity.create!({:name => "Design override", :parent => TimeEntryActivity.find_by_name("Design"), :project => project})
activities = activity_collection_for_select_options(nil, project)
assert !activities.include?(["Design", 9]), "System activity found in: " + activities.inspect
assert activities.include?(["Design override", override_activity.id]), "Override activity not found in: " + activities.inspect
end
end

View File

@ -313,6 +313,53 @@ class ProjectTest < ActiveSupport::TestCase
assert_equal 1, copied_project.status
end
def test_activities_should_use_the_system_activities
project = Project.find(1)
assert_equal project.activities, TimeEntryActivity.find(:all, :conditions => {:active => true} )
end
def test_activities_should_use_the_project_specific_activities
project = Project.find(1)
overridden_activity = TimeEntryActivity.new({:name => "Project", :project => project})
assert overridden_activity.save!
assert project.activities.include?(overridden_activity), "Project specific Activity not found"
end
def test_activities_should_not_include_the_inactive_project_specific_activities
project = Project.find(1)
overridden_activity = TimeEntryActivity.new({:name => "Project", :project => project, :parent => TimeEntryActivity.find(:first), :active => false})
assert overridden_activity.save!
assert !project.activities.include?(overridden_activity), "Inactive Project specific Activity found"
end
def test_activities_should_not_include_project_specific_activities_from_other_projects
project = Project.find(1)
overridden_activity = TimeEntryActivity.new({:name => "Project", :project => Project.find(2)})
assert overridden_activity.save!
assert !project.activities.include?(overridden_activity), "Project specific Activity found on a different project"
end
def test_activities_should_handle_nils
TimeEntryActivity.delete_all
project = Project.find(1)
assert project.activities.empty?
end
def test_activities_should_override_system_activities_with_project_activities
project = Project.find(1)
parent_activity = TimeEntryActivity.find(:first)
overridden_activity = TimeEntryActivity.new({:name => "Project", :project => project, :parent => parent_activity})
assert overridden_activity.save!
assert project.activities.include?(overridden_activity), "Project specific Activity not found"
assert !project.activities.include?(parent_activity), "System Activity found when it should have been overridden"
end
context "Project#copy" do
setup do
ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests