Redmine Refactor #103: Move method from ProjectsController#list_files to FilesController#index

Looking through Redmine’s ProjectsController, I found a few actions that were being used to manage project files. These files are separate resources on a project so using move method and extract class I created a new controller, FilesController.

Before

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class ProjectsController  [:list_files, :add_file]
 
  def list_files
    sort_init 'filename', 'asc'
    sort_update 'filename' => "#{Attachment.table_name}.filename",
                'created_on' => "#{Attachment.table_name}.created_on",
                'size' => "#{Attachment.table_name}.filesize",
                'downloads' => "#{Attachment.table_name}.downloads"
 
    @containers = [ Project.find(@project.id, :include => :attachments, :order => sort_clause)]
    @containers += @project.versions.find(:all, :include => :attachments, :order => sort_clause).sort.reverse
    render :layout => !request.xhr?
  end
end

After

1
2
class ProjectsController  [:add_file]
end
1
2
3
4
5
6
7
8
9
10
11
class FilesController  "#{Attachment.table_name}.filename",
                'created_on' => "#{Attachment.table_name}.created_on",
                'size' => "#{Attachment.table_name}.filesize",
                'downloads' => "#{Attachment.table_name}.downloads"
 
    @containers = [ Project.find(@project.id, :include => :attachments, :order => sort_clause)]
    @containers += @project.versions.find(:all, :include => :attachments, :order => sort_clause).sort.reverse
    render :layout => !request.xhr?
  end
 
end

Not only did this refactoring create the new FilesController but it also renamed the #list_files method to the standard #index. This will make it easier to convert the controller into a REST controller with an API in the future.

Reference commit

Share

  • Facebook
  • Twitter
  • HackerNews
  • Reddit
  • Tumblr
  • Delicious
  • Email
  • RSS

Related posts:

  1. Redmine Refactor #105: Move method from ProjectsController#save_activities to ProjectEnumerationsController#save
  2. Redmine Refactor #95: Extract ContextMenusController from IssuesController
  3. Daily Refactor #54: Move Method to KanbanPane
  4. Daily Refactor #31: Extract Method in IssuesHelper#show_detail
  5. Daily Refactor #8: Move method in the ReportsController

About Eric Davis

I founded Little Stream Software where I provide Redmine and ChiliProject services to help projects teams. I also created an ebook, Redmine Tips, were I show you how to become more productive using Redmine. I am also the author of Refactoring Redmine, where I go about refactoring Rails using Redmine as an example.

, , , ,

Chirk HR     Reuse your existing job applicants »
WP Socializer Aakash Web