Project

General

Profile

Feature #24277 » 02_add_remaining_time_to_log_time_15945.patch

Marius BĂLTEANU, 2016-11-07 01:22

View differences:

app/models/time_entry.rb
belongs_to :activity, :class_name => 'TimeEntryActivity'
attr_protected :user_id, :tyear, :tmonth, :tweek
attr_accessor :remaining_time_action, :remaining_time_hours
acts_as_customizable
acts_as_event :title => Proc.new {|o| "#{l_hours(o.hours)} (#{(o.issue || o.project).event_title})"},
......
validates_presence_of :user_id, :activity_id, :project_id, :hours, :spent_on
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
validate :validate_time_entry
after_save :update_issue_remaining_hours
scope :visible, lambda {|*args|
joins(:project).
where(TimeEntry.visible_condition(args.shift || User.current, *args))
......
where("#{Issue.table_name}.root_id = #{issue.root_id} AND #{Issue.table_name}.lft >= #{issue.lft} AND #{Issue.table_name}.rgt <= #{issue.rgt}")
}
safe_attributes 'hours', 'comments', 'project_id', 'issue_id', 'activity_id', 'spent_on', 'custom_field_values', 'custom_fields'
safe_attributes 'hours', 'comments', 'project_id', 'issue_id', 'activity_id', 'spent_on', '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={})
......
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)
errors.add :remaining_time_hours, :invalid if (remaining_time_action == 'set' && remaining_time_hours.blank?)
end
def hours=(h)
......
def editable_custom_fields(user=nil)
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
end
app/views/issues/_edit.html.erb
<% @time_entry.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
<% @time_entry.custom_field_values.each do |value| %>
<p><%= custom_field_tag_with_label :time_entry, value %></p>
<% 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), @time_entry.activity_id) %>');
$('#new_time_entry p#issue_remaining_time').html('<%= escape_javascript render :partial => "remaining_time", :locals => { :issue => @time_entry.issue } %>');
config/locales/en.yml
label_font_default: Default font
label_font_monospace: Monospaced font
label_font_proportional: Proportional font
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; }
#preview fieldset {margin-top: 1em; background: url(../images/draft.png)}
test/functional/timelog_controller_test.rb
assert_select 'input[name=?][type=hidden]', 'project_id', 0
assert_select 'input[name=?][type=hidden]', 'issue_id'
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 'option', :text => 'Inactive Activity', :count => 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 'form[action=?]', '/time_entries/2'
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")
......
assert_select 'form#bulk_edit_form[action=?]', '/time_entries/bulk_update' do
# System wide custom field
assert_select 'select[name=?]', 'time_entry[custom_field_values][10]'
# Activities
assert_select 'select[name=?]', 'time_entry[activity_id]' do
assert_select 'option[value=""]', :text => '(No change)'
......
@request.session[:user_id] = 2
# makes user a manager on the other project
Member.create!(:user_id => 2, :project_id => 3, :role_ids => [1])
# update time entry activity
post :bulk_update, :params => {:ids => [1, 2, 4], :time_entry => { :activity_id => 9 }}
test/integration/api_test/issues_test.rb
parent = Issue.find(3)
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.content_type
......
parent = Issue.find(3)
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.content_type
test/unit/time_entry_test.rb
:activity => activity)
assert_equal project.id, te.project.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
(3-3/14)