fix-33914.patch

Mizuki ISHIKAWA, 2020-08-27 10:19

Download (3.17 KB)

View differences:

app/models/time_entry.rb
97 97
  def initialize(attributes=nil, *args)
98 98
    super
99 99
    if new_record? && self.activity.nil?
100
      if default_activity = TimeEntryActivity.default
100
      if default_activity = TimeEntryActivity.default(self.project)
101 101
        self.activity_id = default_activity.id
102 102
      end
103 103
      self.hours = nil if hours == 0
app/models/time_entry_activity.rb
22 22

  
23 23
  OptionName = :enumeration_activities
24 24

  
25
  def self.default(project=nil)
26
    default_activity = super()
27
    if project.nil? || project.activities.blank? || project.activities.include?(default_activity)
28
      return default_activity
29
    end
30

  
31
    project.activities.find_by(parent_id: default_activity.id)
32
  end
33

  
25 34
  def option_name
26 35
    OptionName
27 36
  end
test/unit/time_entry_activity_test.rb
131 131
    assert_include activity, project.activities
132 132
    assert_include TimeEntryActivity.find(9), project.activities
133 133
  end
134

  
135
  def test_default_should_return_default_activity_if_default_activity_is_included_in_the_project_activities
136
    project = Project.find(1)
137
    assert_equal TimeEntryActivity.default(project).id, 10
138
  end
139

  
140
  def test_default_should_return_project_specific_default_activity_if_default_activity_is_not_included_in_the_project_activities
141
    project = Project.find(1)
142
    project_specific_default_activity = TimeEntryActivity.create!(name: 'Development', parent_id: 10, project_id: project.id, is_default: false)
143
    assert_not_equal TimeEntryActivity.default(project).id, 10
144
    assert_equal TimeEntryActivity.default(project).id, project_specific_default_activity.id
145
  end
134 146
end
test/unit/time_entry_test.rb
125 125
    end
126 126
  end
127 127

  
128
  def test_activity_id_should_default_activity_id
129
    project = Project.find(1)
130
    default_activity = TimeEntryActivity.find(10)
131
    entry = TimeEntry.new(project: project)
132
    assert_equal entry.activity_id, default_activity.id
133

  
134
    # If there are project specific activities
135
    project_specific_default_activity = TimeEntryActivity.create!(name: 'Development', parent_id: 10, project_id: project.id, is_default: false)
136
    entry = TimeEntry.new(project: project)
137
    assert_not_equal entry.activity_id, default_activity.id
138
    assert_equal entry.activity_id, project_specific_default_activity.id
139
  end
140

  
128 141
  def test_should_accept_future_dates
129 142
    entry = TimeEntry.generate
130 143
    entry.spent_on = User.current.today + 1