Feature #24277 » 02_add_remaining_time_to_log_time_5.1.2.patch
| app/models/time_entry.rb | ||
|---|---|---|
|
belongs_to :author, :class_name => 'User'
|
||
|
belongs_to :activity, :class_name => 'TimeEntryActivity'
|
||
|
attr_accessor :remaining_time_action, :remaining_time_hours
|
||
|
acts_as_customizable
|
||
|
acts_as_event(
|
||
|
:title =>
|
||
| ... | ... | |
|
validates_presence_of :issue_id, :if => lambda {Setting.timelog_required_fields.include?('issue_id')}
|
||
|
validates_presence_of :comments, :if => lambda {Setting.timelog_required_fields.include?('comments')}
|
||
|
validates_numericality_of :hours, :allow_nil => true, :message => :invalid
|
||
|
validates :remaining_time_action, :inclusion => { :in => ['auto', 'set', 'nothing'] }, :allow_nil => true
|
||
|
validates :remaining_time_hours, :numericality => {:greater_than_or_equal_to => 0, :allow_blank => true, :message => :invalid}
|
||
|
validates_length_of :comments, :maximum => 1024, :allow_nil => true
|
||
|
validates :spent_on, :date => true
|
||
|
before_validation :set_project_if_nil
|
||
| ... | ... | |
|
before_validation :set_author_if_nil
|
||
|
validate :validate_time_entry
|
||
|
after_save :update_issue_remaining_hours
|
||
|
scope :visible, (lambda do |*args|
|
||
|
joins(:project).
|
||
|
where(TimeEntry.visible_condition(args.shift || User.current, *args))
|
||
| ... | ... | |
|
safe_attributes 'user_id', 'hours', 'comments', 'project_id',
|
||
|
'issue_id', 'activity_id', 'spent_on',
|
||
|
'custom_field_values', 'custom_fields'
|
||
|
'custom_field_values', 'custom_fields',
|
||
|
'remaining_time_action', 'remaining_time_hours'
|
||
|
# Returns a SQL conditions string used to find all time entries visible by the specified user
|
||
|
def self.visible_condition(user, options={})
|
||
| ... | ... | |
|
end
|
||
|
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)
|
||
|
errors.add :remaining_time_hours, :invalid if (remaining_time_action == 'set' && remaining_time_hours.blank?)
|
||
|
if spent_on && spent_on_changed? && user
|
||
|
errors.add :base, I18n.t(:error_spent_on_future_date) if !Setting.timelog_accept_future_dates? && (spent_on > user.today)
|
||
|
end
|
||
| ... | ... | |
|
editable_custom_field_values(user).map(&:custom_field).uniq
|
||
|
end
|
||
|
def remaining_time_action
|
||
|
@remaining_time_action
|
||
|
end
|
||
|
def remaining_time_hours
|
||
|
@remaining_time_hours
|
||
|
end
|
||
|
def update_issue_remaining_hours
|
||
|
issue = self.issue
|
||
|
return unless issue
|
||
|
h = (remaining_time_hours.is_a?(String)) ? remaining_time_hours.to_hours : remaining_time_hours
|
||
|
case remaining_time_action
|
||
|
when "auto"
|
||
|
new_remaining_hours = issue.remaining_hours - self.hours unless issue.remaining_hours.nil?
|
||
|
when "set"
|
||
|
new_remaining_hours = h
|
||
|
when "nothing"
|
||
|
return
|
||
|
end
|
||
|
issue.init_journal(User.current)
|
||
|
issue.remaining_hours = new_remaining_hours
|
||
|
issue.save
|
||
|
end
|
||
|
def visible_custom_field_values(user = nil)
|
||
|
user ||= User.current
|
||
|
custom_field_values.select do |value|
|
||
| app/views/issues/_edit.html.erb | ||
|---|---|---|
|
<% @time_entry.editable_custom_field_values.each do |value| %>
|
||
|
<p><%= custom_field_tag_with_label :time_entry, value %></p>
|
||
|
<% end %>
|
||
|
<p id="issue_remaining_time">
|
||
|
<%= render :partial => 'timelog/remaining_time', :locals => { :issue => @issue } %>
|
||
|
</p>
|
||
|
<% end %>
|
||
|
</fieldset>
|
||
|
<% end %>
|
||
| app/views/timelog/_form.html.erb | ||
|---|---|---|
|
<%= wikitoolbar_for "time_entry_custom_field_values_#{value.custom_field_id}", preview_issue_path(:project_id => @project) %>
|
||
|
<% end %>
|
||
|
<% end %>
|
||
|
<p id="issue_remaining_time">
|
||
|
<%= render :partial => 'remaining_time', :locals => { :issue => @time_entry.issue } %>
|
||
|
</p>
|
||
|
<%= call_hook(:view_timelog_edit_form_bottom, { :time_entry => @time_entry, :form => f }) %>
|
||
|
</div>
|
||
| app/views/timelog/_remaining_time.html.erb | ||
|---|---|---|
|
<% return if (!issue) || (!issue.safe_attribute? 'remaining_hours') %>
|
||
|
<label for=""><%= l(:label_issue_remaining_hours) %></label>
|
||
|
<span class="check_box_group">
|
||
|
<% if @time_entry.new_record? %>
|
||
|
<label>
|
||
|
<%= radio_button 'time_entry', 'remaining_time_action', 'auto' %> <%= l(:label_remaining_time_action_auto) %>
|
||
|
</label>
|
||
|
<% end %>
|
||
|
<label class="hours">
|
||
|
<%= radio_button 'time_entry', 'remaining_time_action', "set" %> <%= l(:label_remaining_time_action_set) %>
|
||
|
</label>
|
||
|
<%= text_field 'time_entry', "remaining_time_hours", :size => "3", :disabled => true %> <%= l(:field_hours) %>
|
||
|
<label>
|
||
|
<%= radio_button 'time_entry', 'remaining_time_action', 'nothing' %> <%= l(:label_remaining_time_action_nothing) %>
|
||
|
</label>
|
||
|
</span>
|
||
|
<%= javascript_tag do %>
|
||
|
$(document).ready(function(){
|
||
|
var block = $("p#issue_remaining_time");
|
||
|
if (block.find('#time_entry_remaining_time_action_set').is(':checked')) {
|
||
|
block.find('input#time_entry_remaining_time_hours').prop("disabled", false)
|
||
|
}
|
||
|
block.find("input[type=radio]").change(function(e){
|
||
|
block.find('input#time_entry_remaining_time_hours').prop("disabled", true)
|
||
|
if ($(e.target).val() === 'set') {
|
||
|
block.find('input#time_entry_remaining_time_hours').prop("disabled", false).focus()
|
||
|
}
|
||
|
})
|
||
|
});
|
||
|
<% unless params[:time_entry].present? %>
|
||
|
<% if @time_entry.new_record? %>
|
||
|
$('#time_entry_remaining_time_action_auto').prop('checked', true);
|
||
|
<% else %>
|
||
|
$('#time_entry_remaining_time_action_nothing').prop('checked', true);
|
||
|
<% end %>
|
||
|
<% end %>
|
||
|
<% end %>
|
||
| app/views/timelog/new.js.erb | ||
|---|---|---|
|
$('#time_entry_activity_id').html('<%= escape_javascript options_for_select(activity_collection_for_select_options(@time_entry), default_activity(@time_entry)) %>');
|
||
|
$('#new_time_entry p#issue_remaining_time').html('<%= escape_javascript render :partial => "remaining_time", :locals => { :issue => @time_entry.issue } %>');
|
||
|
$('#time_entry_issue').html('<%= escape_javascript link_to_issue(@time_entry.issue) if @time_entry.issue.try(:visible?) %>');
|
||
| config/locales/en.yml | ||
|---|---|---|
|
label_default_query: Default query
|
||
|
label_edited: Edited
|
||
|
label_time_by_author: "%{time} by %{author}"
|
||
|
label_issue_remaining_hours: Issue remaining time
|
||
|
label_remaining_time_action_auto: Adjust automatically
|
||
|
label_remaining_time_action_set: Set to
|
||
|
label_remaining_time_action_nothing: Do not update remaining time
|
||
|
button_login: Login
|
||
|
button_submit: Submit
|
||
| public/stylesheets/application.css | ||
|---|---|---|
|
width:auto;
|
||
|
}
|
||
|
input#time_entry_comments { width: 90%;}
|
||
|
p#issue_remaining_time label.hours { display: inline-block; }
|
||
|
input#months { width: 46px; }
|
||
|
.jstBlock .jstTabs, .jstBlock .wiki-preview { width: 99%; }
|
||
| test/functional/timelog_controller_test.rb | ||
|---|---|---|
|
assert_select 'input[name=?][type=hidden]', 'issue_id'
|
||
|
assert_select 'a[href=?]', '/issues/2', :text => /Feature request #2/
|
||
|
assert_select 'select[name=?]', 'time_entry[project_id]', 0
|
||
|
assert_select 'input[type=radio][name=?]', 'time_entry[remaining_time_action]', 3
|
||
|
assert_select 'input[type=text][name=?]', 'time_entry[remaining_time_hours]', 1
|
||
|
end
|
||
|
def test_new_without_project_should_prefill_the_form
|
||
| ... | ... | |
|
assert_select 'select[name=?]', 'time_entry[user_id]', 0
|
||
|
end
|
||
|
def test_new_on_issue_with_remaining_time_disabled_should_not_show_the_update_issue_remaining_time_section
|
||
|
tracker = Tracker.find(2)
|
||
|
tracker.core_fields = tracker.core_fields - %w(remaining_hours)
|
||
|
tracker.save!
|
||
|
@request.session[:user_id] = 3
|
||
|
get :new, :issue_id => 2
|
||
|
assert_response :success
|
||
|
assert_template 'new'
|
||
|
assert_select 'input[type=radio][name=?]', 'time_entry[remaining_time_action]', 0
|
||
|
assert_select 'input[type=text][name=?]', 'time_entry[remaining_time_hours]', 0
|
||
|
end
|
||
|
def test_post_new_as_js_should_update_activity_options
|
||
|
@request.session[:user_id] = 3
|
||
|
post :new, :params => {:time_entry => {:project_id => 1}, :format => 'js'}
|
||
| ... | ... | |
|
assert_select 'a.user.active', :text => 'Redmine Admin'
|
||
|
end
|
||
|
def test_get_should_not_show_the_adjust_automatically_option_in_issue_remaining_time_section
|
||
|
@request.session[:user_id] = 2
|
||
|
get :edit, :id => 2, :project_id => nil
|
||
|
assert_response :success
|
||
|
assert_template 'edit'
|
||
|
assert_select 'input[type=radio][name=?]', 'time_entry[remaining_time_action]', 2
|
||
|
assert_select 'input[type=text][name=?]', 'time_entry[remaining_time_hours]', 1
|
||
|
assert_select 'input[type=radio][id=?]', 'time_entry_remaining_time_action_auto]', 0
|
||
|
end
|
||
|
def test_get_edit_with_an_existing_time_entry_with_inactive_activity
|
||
|
te = TimeEntry.find(1)
|
||
|
te.activity = TimeEntryActivity.find_by_name("Inactive Activity")
|
||
| test/integration/api_test/issues_test.rb | ||
|---|---|---|
|
parent.update_columns :estimated_hours => 2.0
|
||
|
child = Issue.generate!(:parent_issue_id => parent.id, :estimated_hours => 3.0, :remaining_hours => 1.0)
|
||
|
TimeEntry.create!(:project => child.project, :issue => child, :user => child.author, :spent_on => child.author.today,
|
||
|
:hours => '2.5', :comments => '', :activity_id => TimeEntryActivity.first.id)
|
||
|
:hours => '2.5', :comments => '', :activity_id => TimeEntryActivity.first.id, :remaining_time_action => 'nothing')
|
||
|
get '/issues/3.xml'
|
||
|
assert_equal 'application/xml', response.media_type
|
||
| ... | ... | |
|
parent.update_columns :estimated_hours => 2.0
|
||
|
child = Issue.generate!(:parent_issue_id => parent.id, :estimated_hours => 3.0, :remaining_hours => 1.0)
|
||
|
TimeEntry.create!(:project => child.project, :issue => child, :user => child.author, :spent_on => child.author.today,
|
||
|
:hours => '2.5', :comments => '', :activity_id => TimeEntryActivity.first.id)
|
||
|
:hours => '2.5', :comments => '', :activity_id => TimeEntryActivity.first.id, :remaining_time_action => 'nothing')
|
||
|
get '/issues/3.json'
|
||
|
assert_equal 'application/json', response.media_type
|
||
| test/unit/time_entry_test.rb | ||
|---|---|---|
|
assert_equal [2], time_entry.assignable_users.map(&:id)
|
||
|
end
|
||
|
def test_time_entry_decrease_issue_remaining_time_with_logged_time
|
||
|
issue = Issue.find(1)
|
||
|
issue.update_attribute(:remaining_hours, 3)
|
||
|
te = TimeEntry.new(:spent_on => '2010-01-01', :hours => 1, :issue => issue,
|
||
|
:user => User.find(1), :activity => TimeEntryActivity.first,
|
||
|
:remaining_time_action => "auto")
|
||
|
assert te.save
|
||
|
assert_equal 2, issue.remaining_hours
|
||
|
end
|
||
|
def test_time_entry_set_issue_remaining_time
|
||
|
issue = Issue.find(1)
|
||
|
issue.update_attribute(:remaining_hours, 3)
|
||
|
te = TimeEntry.new(:spent_on => '2010-01-01', :hours => 1, :issue => issue,
|
||
|
:user => User.find(1), :activity => TimeEntryActivity.first,
|
||
|
:remaining_time_action => "set", :remaining_time_hours => 4)
|
||
|
assert te.save
|
||
|
assert_equal 4, issue.remaining_hours
|
||
|
end
|
||
|
def test_time_entry_do_not_update_issue_remaining_time
|
||
|
issue = Issue.find(1)
|
||
|
issue.update_attribute(:remaining_hours, 3)
|
||
|
te = TimeEntry.new(:spent_on => '2010-01-01', :hours => 1, :issue => issue,
|
||
|
:user => User.find(1), :activity => TimeEntryActivity.first,
|
||
|
:remaining_time_action => "nothing", :remaining_time_hours => 4)
|
||
|
assert te.save
|
||
|
assert_equal 3, issue.remaining_hours
|
||
|
end
|
||
|
def test_time_entry_do_not_decrease_issue_remaining_time_when_issue_remaining_time_is_nil
|
||
|
issue = Issue.find(1)
|
||
|
issue.update_attribute(:remaining_hours, '')
|
||
|
te = TimeEntry.new(:spent_on => '2010-01-01', :hours => 1, :issue => issue,
|
||
|
:user => User.find(1), :activity => TimeEntryActivity.first,
|
||
|
:remaining_time_action => "auto", :remaining_time_hours => 4)
|
||
|
assert te.save
|
||
|
assert_nil issue.remaining_hours
|
||
|
end
|
||
|
def test_validate_time_entry_remaining_time_action
|
||
|
te = TimeEntry.new(:spent_on => '2010-01-01', :hours => 1, :issue => Issue.find(1),
|
||
|
:user => User.find(1), :activity => TimeEntryActivity.first,
|
||
|
:remaining_time_action => "test", :remaining_time_hours => 'one')
|
||
|
assert !te.valid?
|
||
|
assert_equal 2, te.errors.count
|
||
|
end
|
||
|
end
|
||