Now that #move
and #perform_move
are separated, I can start to refactor the duplication they share.
Before
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
class IssuesController false if request.xhr? end # TODO: more descriptive name? move to separate controller like IssueMovesController? def perform_move @issues.sort! @copy = params[:copy_options] && params[:copy_options][:copy] @allowed_projects = Issue.allowed_target_projects_on_move @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:new_project_id]} if params[:new_project_id] @target_project ||= @project @trackers = @target_project.trackers @available_statuses = Workflow.available_statuses(@project) if request.post? new_tracker = params[:new_tracker_id].blank? ? nil : @target_project.trackers.find_by_id(params[:new_tracker_id]) unsaved_issue_ids = [] moved_issues = [] @issues.each do |issue| issue.reload issue.init_journal(User.current) call_hook(:controller_issues_move_before_save, { :params => params, :issue => issue, :target_project => @target_project, :copy => !!@copy }) if r = issue.move_to_project(@target_project, new_tracker, {:copy => @copy, :attributes => extract_changed_attributes_for_move(params)}) moved_issues << r else unsaved_issue_ids < 'issues', :action => 'show', :id => moved_issues.first else redirect_to :controller => 'issues', :action => 'index', :project_id => (@target_project || @project) end else redirect_to :controller => 'issues', :action => 'index', :project_id => @project end return end end end |
After
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
class IssuesController false if request.xhr? end # TODO: more descriptive name? move to separate controller like IssueMovesController? def perform_move prepare_for_issue_move if request.post? new_tracker = params[:new_tracker_id].blank? ? nil : @target_project.trackers.find_by_id(params[:new_tracker_id]) unsaved_issue_ids = [] moved_issues = [] @issues.each do |issue| issue.reload issue.init_journal(User.current) call_hook(:controller_issues_move_before_save, { :params => params, :issue => issue, :target_project => @target_project, :copy => !!@copy }) if r = issue.move_to_project(@target_project, new_tracker, {:copy => @copy, :attributes => extract_changed_attributes_for_move(params)}) moved_issues << r else unsaved_issue_ids < 'issues', :action => 'show', :id => moved_issues.first else redirect_to :controller => 'issues', :action => 'index', :project_id => (@target_project || @project) end else redirect_to :controller => 'issues', :action => 'index', :project_id => @project end return end end def prepare_for_issue_move @issues.sort! @copy = params[:copy_options] && params[:copy_options][:copy] @allowed_projects = Issue.allowed_target_projects_on_move @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:new_project_id]} if params[:new_project_id] @target_project ||= @project @trackers = @target_project.trackers @available_statuses = Workflow.available_statuses(@project) end end |
Using extract method, I was able to pull out all of the duplication between #move
and #perform_move
. #perform_move
still has some messy code in it’s method body but I think that can wait until after my next set of refactoring, extracting these methods to a new controller.