@@ -0,0 +1,9 | |||
|
1 | class ChangeTimeEntriesCommentsLimitTo1024 < ActiveRecord::Migration | |
|
2 | def self.up | |
|
3 | change_column :time_entries, :comments, :string, :limit => 1024 | |
|
4 | end | |
|
5 | ||
|
6 | def self.down | |
|
7 | change_column :time_entries, :comments, :string, :limit => 255 | |
|
8 | end | |
|
9 | end |
@@ -1,160 +1,160 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2015 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | class TimeEntry < ActiveRecord::Base |
|
19 | 19 | include Redmine::SafeAttributes |
|
20 | 20 | # could have used polymorphic association |
|
21 | 21 | # project association here allows easy loading of time entries at project level with one database trip |
|
22 | 22 | belongs_to :project |
|
23 | 23 | belongs_to :issue |
|
24 | 24 | belongs_to :user |
|
25 | 25 | belongs_to :activity, :class_name => 'TimeEntryActivity' |
|
26 | 26 | |
|
27 | 27 | attr_protected :user_id, :tyear, :tmonth, :tweek |
|
28 | 28 | |
|
29 | 29 | acts_as_customizable |
|
30 | 30 | acts_as_event :title => Proc.new {|o| "#{l_hours(o.hours)} (#{(o.issue || o.project).event_title})"}, |
|
31 | 31 | :url => Proc.new {|o| {:controller => 'timelog', :action => 'index', :project_id => o.project, :issue_id => o.issue}}, |
|
32 | 32 | :author => :user, |
|
33 | 33 | :group => :issue, |
|
34 | 34 | :description => :comments |
|
35 | 35 | |
|
36 | 36 | acts_as_activity_provider :timestamp => "#{table_name}.created_on", |
|
37 | 37 | :author_key => :user_id, |
|
38 | 38 | :scope => joins(:project).preload(:project) |
|
39 | 39 | |
|
40 | 40 | validates_presence_of :user_id, :activity_id, :project_id, :hours, :spent_on |
|
41 | 41 | validates_numericality_of :hours, :allow_nil => true, :message => :invalid |
|
42 |
validates_length_of :comments, :maximum => |
|
|
42 | validates_length_of :comments, :maximum => 1024, :allow_nil => true | |
|
43 | 43 | validates :spent_on, :date => true |
|
44 | 44 | before_validation :set_project_if_nil |
|
45 | 45 | validate :validate_time_entry |
|
46 | 46 | |
|
47 | 47 | scope :visible, lambda {|*args| |
|
48 | 48 | joins(:project). |
|
49 | 49 | where(TimeEntry.visible_condition(args.shift || User.current, *args)) |
|
50 | 50 | } |
|
51 | 51 | scope :on_issue, lambda {|issue| |
|
52 | 52 | joins(:issue). |
|
53 | 53 | where("#{Issue.table_name}.root_id = #{issue.root_id} AND #{Issue.table_name}.lft >= #{issue.lft} AND #{Issue.table_name}.rgt <= #{issue.rgt}") |
|
54 | 54 | } |
|
55 | 55 | |
|
56 | 56 | safe_attributes 'hours', 'comments', 'project_id', 'issue_id', 'activity_id', 'spent_on', 'custom_field_values', 'custom_fields' |
|
57 | 57 | |
|
58 | 58 | # Returns a SQL conditions string used to find all time entries visible by the specified user |
|
59 | 59 | def self.visible_condition(user, options={}) |
|
60 | 60 | Project.allowed_to_condition(user, :view_time_entries, options) do |role, user| |
|
61 | 61 | if role.time_entries_visibility == 'all' |
|
62 | 62 | nil |
|
63 | 63 | elsif role.time_entries_visibility == 'own' && user.id && user.logged? |
|
64 | 64 | "#{table_name}.user_id = #{user.id}" |
|
65 | 65 | else |
|
66 | 66 | '1=0' |
|
67 | 67 | end |
|
68 | 68 | end |
|
69 | 69 | end |
|
70 | 70 | |
|
71 | 71 | # Returns true if user or current user is allowed to view the time entry |
|
72 | 72 | def visible?(user=nil) |
|
73 | 73 | (user || User.current).allowed_to?(:view_time_entries, self.project) do |role, user| |
|
74 | 74 | if role.time_entries_visibility == 'all' |
|
75 | 75 | true |
|
76 | 76 | elsif role.time_entries_visibility == 'own' |
|
77 | 77 | self.user == user |
|
78 | 78 | else |
|
79 | 79 | false |
|
80 | 80 | end |
|
81 | 81 | end |
|
82 | 82 | end |
|
83 | 83 | |
|
84 | 84 | def initialize(attributes=nil, *args) |
|
85 | 85 | super |
|
86 | 86 | if new_record? && self.activity.nil? |
|
87 | 87 | if default_activity = TimeEntryActivity.default |
|
88 | 88 | self.activity_id = default_activity.id |
|
89 | 89 | end |
|
90 | 90 | self.hours = nil if hours == 0 |
|
91 | 91 | end |
|
92 | 92 | end |
|
93 | 93 | |
|
94 | 94 | def safe_attributes=(attrs, user=User.current) |
|
95 | 95 | if attrs |
|
96 | 96 | attrs = super(attrs) |
|
97 | 97 | if issue_id_changed? && issue |
|
98 | 98 | if user.allowed_to?(:log_time, issue.project) |
|
99 | 99 | if attrs[:project_id].blank? && issue.project_id != project_id |
|
100 | 100 | self.project_id = issue.project_id |
|
101 | 101 | end |
|
102 | 102 | @invalid_issue_id = nil |
|
103 | 103 | else |
|
104 | 104 | @invalid_issue_id = issue_id |
|
105 | 105 | end |
|
106 | 106 | end |
|
107 | 107 | end |
|
108 | 108 | attrs |
|
109 | 109 | end |
|
110 | 110 | |
|
111 | 111 | def set_project_if_nil |
|
112 | 112 | self.project = issue.project if issue && project.nil? |
|
113 | 113 | end |
|
114 | 114 | |
|
115 | 115 | def validate_time_entry |
|
116 | 116 | errors.add :hours, :invalid if hours && (hours < 0 || hours >= 1000) |
|
117 | 117 | errors.add :project_id, :invalid if project.nil? |
|
118 | 118 | errors.add :issue_id, :invalid if (issue_id && !issue) || (issue && project!=issue.project) || @invalid_issue_id |
|
119 | 119 | errors.add :activity_id, :inclusion if activity_id_changed? && project && !project.activities.include?(activity) |
|
120 | 120 | end |
|
121 | 121 | |
|
122 | 122 | def hours=(h) |
|
123 | 123 | write_attribute :hours, (h.is_a?(String) ? (h.to_hours || h) : h) |
|
124 | 124 | end |
|
125 | 125 | |
|
126 | 126 | def hours |
|
127 | 127 | h = read_attribute(:hours) |
|
128 | 128 | if h.is_a?(Float) |
|
129 | 129 | h.round(2) |
|
130 | 130 | else |
|
131 | 131 | h |
|
132 | 132 | end |
|
133 | 133 | end |
|
134 | 134 | |
|
135 | 135 | # tyear, tmonth, tweek assigned where setting spent_on attributes |
|
136 | 136 | # these attributes make time aggregations easier |
|
137 | 137 | def spent_on=(date) |
|
138 | 138 | super |
|
139 | 139 | self.tyear = spent_on ? spent_on.year : nil |
|
140 | 140 | self.tmonth = spent_on ? spent_on.month : nil |
|
141 | 141 | self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil |
|
142 | 142 | end |
|
143 | 143 | |
|
144 | 144 | # Returns true if the time entry can be edited by usr, otherwise false |
|
145 | 145 | def editable_by?(usr) |
|
146 | 146 | visible?(usr) && ( |
|
147 | 147 | (usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project) |
|
148 | 148 | ) |
|
149 | 149 | end |
|
150 | 150 | |
|
151 | 151 | # Returns the custom_field_values that can be edited by the given user |
|
152 | 152 | def editable_custom_field_values(user=nil) |
|
153 | 153 | visible_custom_field_values |
|
154 | 154 | end |
|
155 | 155 | |
|
156 | 156 | # Returns the custom fields that can be edited by the given user |
|
157 | 157 | def editable_custom_fields(user=nil) |
|
158 | 158 | editable_custom_field_values(user).map(&:custom_field).uniq |
|
159 | 159 | end |
|
160 | 160 | end |
@@ -1,47 +1,47 | |||
|
1 | 1 | <%= error_messages_for 'time_entry' %> |
|
2 | 2 | <%= back_url_hidden_field_tag %> |
|
3 | 3 | |
|
4 | 4 | <div class="box tabular"> |
|
5 | 5 | <% if @time_entry.new_record? %> |
|
6 | 6 | <% if params[:project_id] %> |
|
7 | 7 | <%= hidden_field_tag 'project_id', params[:project_id] %> |
|
8 | 8 | <% elsif params[:issue_id] %> |
|
9 | 9 | <%= hidden_field_tag 'issue_id', params[:issue_id] %> |
|
10 | 10 | <% else %> |
|
11 | 11 | <p><%= f.select :project_id, project_tree_options_for_select(Project.allowed_to(:log_time).to_a, :selected => @time_entry.project, :include_blank => true) %></p> |
|
12 | 12 | <% end %> |
|
13 | 13 | <% end %> |
|
14 | 14 | <p> |
|
15 | 15 | <%= f.text_field :issue_id, :size => 6 %> |
|
16 | 16 | <span id="time_entry_issue"><%= "#{@time_entry.issue.tracker.name} ##{@time_entry.issue.id}: #{@time_entry.issue.subject}" if @time_entry.issue %></span> |
|
17 | 17 | </p> |
|
18 | 18 | <p><%= f.text_field :spent_on, :size => 10, :required => true %><%= calendar_for('time_entry_spent_on') %></p> |
|
19 | 19 | <p><%= f.text_field :hours, :size => 6, :required => true %></p> |
|
20 |
<p><%= f.text_field :comments, :size => 100, :maxlength => |
|
|
20 | <p><%= f.text_field :comments, :size => 100, :maxlength => 1024 %></p> | |
|
21 | 21 | <p><%= f.select :activity_id, activity_collection_for_select_options(@time_entry), :required => true %></p> |
|
22 | 22 | <% @time_entry.custom_field_values.each do |value| %> |
|
23 | 23 | <p><%= custom_field_tag_with_label :time_entry, value %></p> |
|
24 | 24 | <% end %> |
|
25 | 25 | <%= call_hook(:view_timelog_edit_form_bottom, { :time_entry => @time_entry, :form => f }) %> |
|
26 | 26 | </div> |
|
27 | 27 | |
|
28 | 28 | <%= javascript_tag do %> |
|
29 | 29 | <% if @time_entry.new_record? %> |
|
30 | 30 | $(document).ready(function(){ |
|
31 | 31 | $('#time_entry_project_id, #time_entry_issue_id').change(function(){ |
|
32 | 32 | $.ajax({ |
|
33 | 33 | url: '<%= escape_javascript new_time_entry_path(:format => 'js') %>', |
|
34 | 34 | type: 'post', |
|
35 | 35 | data: $('#new_time_entry').serialize() |
|
36 | 36 | }); |
|
37 | 37 | }); |
|
38 | 38 | }); |
|
39 | 39 | <% end %> |
|
40 | 40 | |
|
41 | 41 | observeAutocompleteField('time_entry_issue_id', '<%= escape_javascript auto_complete_issues_path(:project_id => @project, :scope => (@project ? nil : 'all'))%>', { |
|
42 | 42 | select: function(event, ui) { |
|
43 | 43 | $('#time_entry_issue').text(ui.item.label); |
|
44 | 44 | $('#time_entry_issue_id').blur(); |
|
45 | 45 | } |
|
46 | 46 | }); |
|
47 | 47 | <% end %> |
General Comments 0
You need to be logged in to leave comments.
Login now