]> source.dussan.org Git - redmine.git/commitdiff
Make the only enabled activity in a project the default one for time entry (#10314).
authorGo MAEDA <maeda@farend.jp>
Sun, 17 Jul 2022 00:16:31 +0000 (00:16 +0000)
committerGo MAEDA <maeda@farend.jp>
Sun, 17 Jul 2022 00:16:31 +0000 (00:16 +0000)
Patch by Mizuki ISHIKAWA.

git-svn-id: https://svn.redmine.org/redmine/trunk@21709 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/helpers/timelog_helper.rb
app/models/time_entry.rb
app/models/time_entry_activity.rb
test/unit/time_entry_test.rb

index 838943a8cdbd1f9d647a7e155ea8892453432541..e82da6e312029c6a03affebfb75f12978a095cdd 100644 (file)
@@ -26,11 +26,7 @@ module TimelogHelper
   def activity_collection_for_select_options(time_entry=nil, project=nil)
     project ||= time_entry.try(:project)
     project ||= @project
-    if project.nil?
-      activities = TimeEntryActivity.shared.active
-    else
-      activities = project.activities
-    end
+    activities = TimeEntryActivity.available_activities(project)
 
     collection = []
     if time_entry && time_entry.activity && !time_entry.activity.active?
index 7776db0d881e224abd34b7b24ded93a392d05e6e..18ae8eeb8a8aae1633ffed1a35a9688bd39b1eca 100644 (file)
@@ -109,6 +109,8 @@ class TimeEntry < ActiveRecord::Base
     if new_record? && self.activity.nil?
       if default_activity = TimeEntryActivity.default(self.project)
         self.activity_id = default_activity.id
+      elsif (activities = TimeEntryActivity.available_activities(self.project)) && activities.count == 1
+        self.activity_id = activities.first.id
       end
       self.hours = nil if hours == 0
     end
index 40505019ce44142fb553a3eb42ca5953a56242c8..1f0c98b79007cd19705b724b21f2c5b01b4da5cd 100644 (file)
@@ -32,6 +32,14 @@ class TimeEntryActivity < Enumeration
     project.activities.detect { |activity| activity.parent_id == default_activity.id }
   end
 
+  def self.available_activities(project=nil)
+    if project.nil?
+      TimeEntryActivity.shared.active
+    else
+      project.activities
+    end
+  end
+
   def option_name
     OptionName
   end
index 7f9e181735b93158b7087da47b814f2a377abd5a..088af350451cd3dfcc348472ae1f136810cd5b92 100644 (file)
@@ -139,6 +139,21 @@ class TimeEntryTest < ActiveSupport::TestCase
     assert_equal entry.activity_id, project_specific_default_activity.id
   end
 
+  def test_activity_id_should_be_set_automatically_if_there_is_only_one_activity_available
+    project = Project.find(1)
+    TimeEntry.all.destroy_all
+    TimeEntryActivity.destroy_all
+    only_one_activity = TimeEntryActivity.create!(
+      name: 'Development',
+      parent_id: nil,
+      project_id: nil,
+      is_default: false
+    )
+
+    entry = TimeEntry.new(project: project)
+    assert_equal entry.activity_id, only_one_activity.id
+  end
+
   def test_should_accept_future_dates
     entry = TimeEntry.generate
     entry.spent_on = User.current.today + 1