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.
Share
Related posts:
- Redmine Refactor #105: Move method from ProjectsController#save_activities to ProjectEnumerationsController#save
- Redmine Refactor #95: Extract ContextMenusController from IssuesController
- Daily Refactor #54: Move Method to KanbanPane
- Daily Refactor #31: Extract Method in IssuesHelper#show_detail
- Daily Refactor #8: Move method in the ReportsController
