set_project_activity_if_needed.diff

Alex MacAulay, 2014-10-14 17:29

Download (1.26 KB)

View differences:

app/models/time_entry.rb (working copy)
42 42
  validates_length_of :comments, :maximum => 255, :allow_nil => true
43 43
  validates :spent_on, :date => true
44 44
  before_validation :set_project_if_nil
45
  before_validation :set_project_activity_if_needed
45 46
  validate :validate_time_entry
46 47

  
47 48
  scope :visible, lambda {|*args|
......
93 94
    self.project = issue.project if issue && project.nil?
94 95
  end
95 96

  
97
  # If the activity_id corresponds to the system activity, and an equivalent project activity exists, then change the activity_id
98
  # to be that of the project instead (needed because only the system activity_id is exposed by the REST API).
99
  def set_project_activity_if_needed
100
    if self.project
101
      project_activity = TimeEntryActivity.find {|a| a['project_id'] == self.project.id && a['parent_id'] == self.activity_id }
102
      if project_activity
103
        self.activity_id = project_activity.id
104
      end
105
    end
106
  end
107

  
96 108
  def validate_time_entry
97 109
    errors.add :hours, :invalid if hours && (hours < 0 || hours >= 1000)
98 110
    errors.add :project_id, :invalid if project.nil?