do_not_allow_spent_time_with_0_hours.patch

Marius BALTEANU, 2017-05-27 16:30

Download (1.95 KB)

View differences:

app/models/time_entry.rb
44 44
  validates_presence_of :user_id, :activity_id, :project_id, :hours, :spent_on
45 45
  validates_presence_of :issue_id, :if => lambda { Setting.timelog_required_fields.include?('issue_id') }
46 46
  validates_presence_of :comments, :if => lambda { Setting.timelog_required_fields.include?('comments') }
47
  validates_numericality_of :hours, :allow_nil => true, :message => :invalid
47
  validates_numericality_of :hours, :allow_nil => true, :greater_than => 0, :less_than_or_equal_to => 1000
48 48
  validates_length_of :comments, :maximum => 1024, :allow_nil => true
49 49
  validates :spent_on, :date => true
50 50
  before_validation :set_project_if_nil
......
122 122
  end
123 123

  
124 124
  def validate_time_entry
125
    errors.add :hours, :invalid if hours && (hours < 0 || hours >= 1000)
126 125
    errors.add :project_id, :invalid if project.nil?
127 126
    errors.add :issue_id, :invalid if (issue_id && !issue) || (issue && project!=issue.project) || @invalid_issue_id
128 127
    errors.add :activity_id, :inclusion if activity_id_changed? && project && !project.activities.include?(activity)
test/unit/time_entry_test.rb
184 184
      assert_equal ["Comment cannot be blank", "Issue cannot be blank"], entry.errors.full_messages.sort
185 185
    end
186 186
  end
187

  
188
  def test_create_should_not_allow_spent_time_with_0_hours
189
    entry = TimeEntry.new(:project_id => 1, :spent_on => '2010-01-01', :hours => 0, :user => User.find(1), :activity => TimeEntryActivity.first)
190

  
191
    assert !entry.save
192
    assert_equal 1, entry.errors.count
193
  end
187 194
end