]> source.dussan.org Git - redmine.git/commitdiff
Merged r21668 and r21669 to 5.0-stable (#33914).
authorMarius Balteanu <marius.balteanu@zitec.com>
Mon, 20 Jun 2022 17:40:39 +0000 (17:40 +0000)
committerMarius Balteanu <marius.balteanu@zitec.com>
Mon, 20 Jun 2022 17:40:39 +0000 (17:40 +0000)
git-svn-id: https://svn.redmine.org/redmine/branches/5.0-stable@21670 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/models/time_entry.rb
app/models/time_entry_activity.rb
test/unit/time_entry_activity_test.rb
test/unit/time_entry_test.rb

index 34e4aab7b4cabe48cd61f8100e8802e81150a5c9..7776db0d881e224abd34b7b24ded93a392d05e6e 100644 (file)
@@ -107,7 +107,7 @@ class TimeEntry < ActiveRecord::Base
   def initialize(attributes=nil, *args)
     super
     if new_record? && self.activity.nil?
-      if default_activity = TimeEntryActivity.default
+      if default_activity = TimeEntryActivity.default(self.project)
         self.activity_id = default_activity.id
       end
       self.hours = nil if hours == 0
index 973ec923f73e3ad2d3f0d8bec4400106bd2d2b04..40505019ce44142fb553a3eb42ca5953a56242c8 100644 (file)
@@ -22,6 +22,16 @@ class TimeEntryActivity < Enumeration
 
   OptionName = :enumeration_activities
 
+  def self.default(project=nil)
+    default_activity = super()
+
+    if default_activity.nil? || project.nil? || project.activities.blank? || project.activities.include?(default_activity)
+      return default_activity
+    end
+
+    project.activities.detect { |activity| activity.parent_id == default_activity.id }
+  end
+
   def option_name
     OptionName
   end
index f4437c327147eb23fa34205e4abcb21a2d2315b6..dc2e44c13a06b820fe92f04dc324c056d99f5d38 100644 (file)
@@ -219,4 +219,16 @@ class TimeEntryActivityTest < ActiveSupport::TestCase
       )
     end
   end
+
+  def test_default_should_return_default_activity_if_default_activity_is_included_in_the_project_activities
+    project = Project.find(1)
+    assert_equal TimeEntryActivity.default(project).id, 10
+  end
+
+  def test_default_should_return_project_specific_default_activity_if_default_activity_is_not_included_in_the_project_activities
+    project = Project.find(1)
+    project_specific_default_activity = TimeEntryActivity.create!(name: 'Development', parent_id: 10, project_id: project.id, is_default: false)
+    assert_not_equal TimeEntryActivity.default(project).id, 10
+    assert_equal TimeEntryActivity.default(project).id, project_specific_default_activity.id
+  end
 end
index 0998d98b91ba677668c85b562236e126ba89da27..7f9e181735b93158b7087da47b814f2a377abd5a 100644 (file)
@@ -126,6 +126,19 @@ class TimeEntryTest < ActiveSupport::TestCase
     end
   end
 
+  def test_activity_id_should_default_activity_id
+    project = Project.find(1)
+    default_activity = TimeEntryActivity.find(10)
+    entry = TimeEntry.new(project: project)
+    assert_equal entry.activity_id, default_activity.id
+
+    # If there are project specific activities
+    project_specific_default_activity = TimeEntryActivity.create!(name: 'Development', parent_id: 10, project_id: project.id, is_default: false)
+    entry = TimeEntry.new(project: project)
+    assert_not_equal entry.activity_id, default_activity.id
+    assert_equal entry.activity_id, project_specific_default_activity.id
+  end
+
   def test_should_accept_future_dates
     entry = TimeEntry.generate
     entry.spent_on = User.current.today + 1