Class: TimeEntry

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Redmine::SafeAttributes
Defined in:
app/models/time_entry.rb

Overview

Redmine - project management software Copyright (C) 2006-2016 Jean-Philippe Lang

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

Since:

  • 0.5.0

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Redmine::SafeAttributes

#delete_unsafe_attributes, #safe_attribute?, #safe_attribute_names

Constructor Details

#initialize(attributes = nil, *args) ⇒ TimeEntry

Returns a new instance of TimeEntry

Since:

  • 1.4.0



88
89
90
91
92
93
94
95
96
# File 'app/models/time_entry.rb', line 88

def initialize(attributes=nil, *args)
  super
  if new_record? && self.activity.nil?
    if default_activity = TimeEntryActivity.default
      self.activity_id = default_activity.id
    end
    self.hours = nil if hours == 0
  end
end

Class Method Details

.visible_condition(user, options = {}) ⇒ Object

Returns a SQL conditions string used to find all time entries visible by the specified user

Since:

  • 3.1.0



63
64
65
66
67
68
69
70
71
72
73
# File 'app/models/time_entry.rb', line 63

def self.visible_condition(user, options={})
  Project.allowed_to_condition(user, :view_time_entries, options) do |role, user|
    if role.time_entries_visibility == 'all'
      nil
    elsif role.time_entries_visibility == 'own' && user.id && user.logged?
      "#{table_name}.user_id = #{user.id}"
    else
      '1=0'
    end
  end
end

Instance Method Details

#editable_by?(usr) ⇒ Boolean

Returns true if the time entry can be edited by usr, otherwise false

Returns:

  • (Boolean)

Since:

  • 0.7.0



149
150
151
152
153
# File 'app/models/time_entry.rb', line 149

def editable_by?(usr)
  visible?(usr) && (
    (usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project)
  )
end

#editable_custom_field_values(user = nil) ⇒ Object

Returns the custom_field_values that can be edited by the given user

Since:

  • 2.6.0



156
157
158
# File 'app/models/time_entry.rb', line 156

def editable_custom_field_values(user=nil)
  visible_custom_field_values
end

#editable_custom_fields(user = nil) ⇒ Object

Returns the custom fields that can be edited by the given user

Since:

  • 2.6.0



161
162
163
# File 'app/models/time_entry.rb', line 161

def editable_custom_fields(user=nil)
  editable_custom_field_values(user).map(&:custom_field).uniq
end

#hoursObject

Since:

  • 1.4.0



130
131
132
133
134
135
136
137
# File 'app/models/time_entry.rb', line 130

def hours
  h = read_attribute(:hours)
  if h.is_a?(Float)
    h.round(2)
  else
    h
  end
end

#hours=(h) ⇒ Object

Since:

  • 0.7.0



126
127
128
# File 'app/models/time_entry.rb', line 126

def hours=(h)
  write_attribute :hours, (h.is_a?(String) ? (h.to_hours || h) : h)
end

#safe_attributes=(attrs, user = User.current) ⇒ Object

Since:

  • 2.5.1



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/models/time_entry.rb', line 98

def safe_attributes=(attrs, user=User.current)
  if attrs
    attrs = super(attrs)
    if issue_id_changed? && issue
      if issue.visible?(user) && user.allowed_to?(:log_time, issue.project)
        if attrs[:project_id].blank? && issue.project_id != project_id
          self.project_id = issue.project_id
        end
        @invalid_issue_id = nil
      else
        @invalid_issue_id = issue_id
      end
    end
  end
  attrs
end

#set_project_if_nilObject

Since:

  • 1.3.0



115
116
117
# File 'app/models/time_entry.rb', line 115

def set_project_if_nil
  self.project = issue.project if issue && project.nil?
end

#spent_on=(date) ⇒ Object

tyear, tmonth, tweek assigned where setting spent_on attributes these attributes make time aggregations easier



141
142
143
144
145
146
# File 'app/models/time_entry.rb', line 141

def spent_on=(date)
  super
  self.tyear = spent_on ? spent_on.year : nil
  self.tmonth = spent_on ? spent_on.month : nil
  self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil
end

#validate_time_entryObject

Since:

  • 1.3.0



119
120
121
122
123
124
# File 'app/models/time_entry.rb', line 119

def validate_time_entry
  errors.add :hours, :invalid if hours && (hours < 0 || hours >= 1000)
  errors.add :project_id, :invalid if project.nil?
  errors.add :issue_id, :invalid if (issue_id && !issue) || (issue && project!=issue.project) || @invalid_issue_id
  errors.add :activity_id, :inclusion if activity_id_changed? && project && !project.activities.include?(activity)
end

#visible?(user = nil) ⇒ Boolean

Returns true if user or current user is allowed to view the time entry

Returns:

  • (Boolean)

Since:

  • 3.1.0



76
77
78
79
80
81
82
83
84
85
86
# File 'app/models/time_entry.rb', line 76

def visible?(user=nil)
  (user || User.current).allowed_to?(:view_time_entries, self.project) do |role, user|
    if role.time_entries_visibility == 'all'
      true
    elsif role.time_entries_visibility == 'own'
      self.user == user
    else
      false
    end
  end
end