diff --git a/app/controllers/issues_controller.rb b/app/controllers/issues_controller.rb index ff75659d7e7..d30924a80bc 100644 --- a/app/controllers/issues_controller.rb +++ b/app/controllers/issues_controller.rb @@ -183,20 +183,7 @@ def new UPDATABLE_ATTRS_ON_TRANSITION = %w(status_id assigned_to_id fixed_version_id done_ratio) unless const_defined?(:UPDATABLE_ATTRS_ON_TRANSITION) def edit - @allowed_statuses = @issue.new_statuses_allowed_to(User.current) - @priorities = IssuePriority.all - @edit_allowed = User.current.allowed_to?(:edit_issues, @project) - @time_entry = TimeEntry.new - - @notes = params[:notes] - journal = @issue.init_journal(User.current, @notes) - # User can change issue attributes only if he has :edit permission or if a workflow transition is allowed - if (@edit_allowed || !@allowed_statuses.empty?) && params[:issue] - attrs = params[:issue].dup - attrs.delete_if {|k,v| !UPDATABLE_ATTRS_ON_TRANSITION.include?(k) } unless @edit_allowed - attrs.delete(:status_id) unless @allowed_statuses.detect {|s| s.id.to_s == attrs[:status_id].to_s} - @issue.safe_attributes = attrs - end + update_issue_from_params respond_to do |format| format.html { } @@ -208,20 +195,7 @@ def edit # Start converting to the Rails REST controllers #++ def update - @allowed_statuses = @issue.new_statuses_allowed_to(User.current) - @priorities = IssuePriority.all - @edit_allowed = User.current.allowed_to?(:edit_issues, @project) - @time_entry = TimeEntry.new - - @notes = params[:notes] - journal = @issue.init_journal(User.current, @notes) - # User can change issue attributes only if he has :edit permission or if a workflow transition is allowed - if (@edit_allowed || !@allowed_statuses.empty?) && params[:issue] - attrs = params[:issue].dup - attrs.delete_if {|k,v| !UPDATABLE_ATTRS_ON_TRANSITION.include?(k) } unless @edit_allowed - attrs.delete(:status_id) unless @allowed_statuses.detect {|s| s.id.to_s == attrs[:status_id].to_s} - @issue.safe_attributes = attrs - end + update_issue_from_params if request.get? # nop @@ -230,18 +204,18 @@ def update @time_entry.attributes = params[:time_entry] if (@time_entry.hours.nil? || @time_entry.valid?) && @issue.valid? attachments = attach_files(@issue, params[:attachments]) - attachments.each {|a| journal.details << JournalDetail.new(:property => 'attachment', :prop_key => a.id, :value => a.filename)} - call_hook(:controller_issues_edit_before_save, { :params => params, :issue => @issue, :time_entry => @time_entry, :journal => journal}) + attachments.each {|a| @journal.details << JournalDetail.new(:property => 'attachment', :prop_key => a.id, :value => a.filename)} + call_hook(:controller_issues_edit_before_save, { :params => params, :issue => @issue, :time_entry => @time_entry, :journal => @journal}) if @issue.save # Log spend time if User.current.allowed_to?(:log_time, @project) @time_entry.save end - if !journal.new_record? + if !@journal.new_record? # Only send notification if something was actually changed flash[:notice] = l(:notice_successful_update) end - call_hook(:controller_issues_edit_after_save, { :params => params, :issue => @issue, :time_entry => @time_entry, :journal => journal}) + call_hook(:controller_issues_edit_after_save, { :params => params, :issue => @issue, :time_entry => @time_entry, :journal => @journal}) respond_to do |format| format.html { redirect_back_or_default({:action => 'show', :id => @issue}) } format.xml { head :ok } @@ -584,4 +558,25 @@ def query_statement_invalid(exception) sort_clear render_error "An error occurred while executing the query and has been logged. Please report this error to your Redmine administrator." end + + # Used by #edit and #update to set some common instance variables + # from the params + # TODO: Refactor, not everything in here is needed by #edit + def update_issue_from_params + @allowed_statuses = @issue.new_statuses_allowed_to(User.current) + @priorities = IssuePriority.all + @edit_allowed = User.current.allowed_to?(:edit_issues, @project) + @time_entry = TimeEntry.new + + @notes = params[:notes] + @journal = @issue.init_journal(User.current, @notes) + # User can change issue attributes only if he has :edit permission or if a workflow transition is allowed + if (@edit_allowed || !@allowed_statuses.empty?) && params[:issue] + attrs = params[:issue].dup + attrs.delete_if {|k,v| !UPDATABLE_ATTRS_ON_TRANSITION.include?(k) } unless @edit_allowed + attrs.delete(:status_id) unless @allowed_statuses.detect {|s| s.id.to_s == attrs[:status_id].to_s} + @issue.safe_attributes = attrs + end + + end end