OSDN Git Service

Refactor: Moved the contents of #issue_update into Issue.
authorEric Davis <edavis@littlestreamsoftware.com>
Fri, 5 Mar 2010 17:11:50 +0000 (17:11 +0000)
committerEric Davis <edavis@littlestreamsoftware.com>
Fri, 5 Mar 2010 17:11:50 +0000 (17:11 +0000)
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@3545 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/controllers/issues_controller.rb
app/models/issue.rb

index 4279846..b77f76c 100644 (file)
@@ -199,13 +199,25 @@ class IssuesController < ApplicationController
   def update
     update_issue_from_params
 
-    if issue_update
+    if @issue.save_issue_with_child_records(params, @time_entry)
+      render_attachment_warning_if_needed(@issue)
+      if !@issue.current_journal.new_record?
+        # Only send notification if something was actually changed
+        flash[:notice] = l(:notice_successful_update)
+      end
+
       respond_to do |format|
         format.html { redirect_back_or_default({:action => 'show', :id => @issue}) }
         format.xml  { head :ok }
       end
     else
+      render_attachment_warning_if_needed(@issue)
+      if !@issue.current_journal.new_record?
+        # Only send notification if something was actually changed
+        flash[:notice] = l(:notice_successful_update)
+      end
       @journal = @issue.current_journal
+
       respond_to do |format|
         format.html { render :action => 'edit' }
         format.xml  { render :xml => @issue.errors, :status => :unprocessable_entity }
@@ -562,32 +574,4 @@ private
     end
 
   end
-
-  # TODO: Temporary utility method for #update.  Should be split off
-  # and moved to the Issue model (accepts_nested_attributes_for maybe?)
-  def issue_update
-    if params[:time_entry] && params[:time_entry][:hours].present? && User.current.allowed_to?(:log_time, @project)
-      @time_entry = TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => Date.today)
-      @time_entry.attributes = params[:time_entry]
-      @issue.time_entries << @time_entry
-    end
-
-    if @issue.valid?
-      attachments = Attachment.attach_files(@issue, params[:attachments])
-      render_attachment_warning_if_needed(@issue)
-
-      attachments[:files].each {|a| @issue.current_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 => @issue.current_journal})
-      if @issue.save
-        if !@issue.current_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 => @issue.current_journal})
-        return true
-      end
-    end
-    # failure, returns false
-
-  end
 end
index c4434d3..9aaebce 100644 (file)
@@ -394,6 +394,34 @@ class Issue < ActiveRecord::Base
     s
   end
 
+  # Saves an issue, time_entry, attachments, and a journal from the parameters
+  def save_issue_with_child_records(params, existing_time_entry=nil)
+    if params[:time_entry] && params[:time_entry][:hours].present? && User.current.allowed_to?(:log_time, project)
+      @time_entry = existing_time_entry || TimeEntry.new
+      @time_entry.project = project
+      @time_entry.issue = self
+      @time_entry.user = User.current
+      @time_entry.spent_on = Date.today
+      @time_entry.attributes = params[:time_entry]
+      self.time_entries << @time_entry
+    end
+
+    if valid?
+      attachments = Attachment.attach_files(self, params[:attachments])
+
+      attachments[:files].each {|a| @current_journal.details << JournalDetail.new(:property => 'attachment', :prop_key => a.id, :value => a.filename)}
+      # TODO: Rename hook
+      Redmine::Hook.call_hook(:controller_issues_edit_before_save, { :params => params, :issue => self, :time_entry => @time_entry, :journal => @current_journal})
+      if save
+        # TODO: Rename hook
+        Redmine::Hook.call_hook(:controller_issues_edit_after_save, { :params => params, :issue => self, :time_entry => @time_entry, :journal => @current_journal})
+        return true
+      end
+    end
+    # failure, returns false
+
+  end
+
   # Unassigns issues from +version+ if it's no longer shared with issue's project
   def self.update_versions_from_sharing_change(version)
     # Update issues assigned to the version