Redmine Refactor #94: Extract PreviewsController from IssuesController

Today I used extract class on IssuesController once again, this time to create PreviewsController. For the same reasons as yesterday, putting all of the preview actions together into a single controller will make things easier to maintain and might even give me some code reuse.

Before

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class IssuesController  [:new, :create, :update_form, :preview]
 
  def preview
    @issue = @project.issues.find_by_id(params[:id]) unless params[:id].blank?
    if @issue
      @attachements = @issue.attachments
      @description = params[:issue] && params[:issue][:description]
      if @description && @description.gsub(/(\r?\n|\n\r?)/, "\n") == @issue.description.to_s.gsub(/(\r?\n|\n\r?)/, "\n")
        @description = nil
      end
      @notes = params[:notes]
    else
      @description = (params[:issue] ? params[:issue][:description] : nil)
    end
    render :layout => false
  end
end

After

1
2
3
4
class IssuesController  [:new, :create, :update_form]
 
  # ...
end
1
2
3
4
5
6
7
8
9
10
11
12
13
class PreviewsController  false
  end
 
  private
 
  def find_project
    project_id = (params[:issue] && params[:issue][:project_id]) || params[:project_id]
    @project = Project.find(project_id)
  rescue ActiveRecord::RecordNotFound
    render_404
  end
 
end

Nothing too complex in this refactoring; just creating the new controller class, moving the method over, and updating all of the callers to use the new controller. The reference commit shows some other updates like the routing and view changes.

Little by little the IssuesController is getting a long overdue cleanup. Now there are only a few non-REST actions left to move:

  • index
  • changes – non-REST
  • show
  • new
  • create
  • edit
  • update
  • bulk_edit – non-REST
  • destroy
  • context_menu – non-REST
  • update_form – non-REST

Reference commit