Daily Refactor #14: Extract method to before_filter in ReportsController

The Refactoring

I started on a larger refactoring of the issue_report_details method today but found a potential security bug so I threw it away. It’s better to be secure than refactored :)

So instead, I performed a common Rails refactoring for removing duplicate code: extracting some code to a before filter.

Before

1
2
3
4
5
6
7
8
9
10
11
12
13
# app/controllers/reports_controller.rb
class ReportsController  'position')
 
    # ...
  end  
 
  def issue_report_details
    @statuses = IssueStatus.find(:all, :order => 'position')
 
    # ...
  end
 
end

After

1
2
3
4
# app/controllers/reports_controller.rb
class ReportsController  'position')
  end
end

Review

This is a good refactoring when your controller actions need some data but that data isn’t their core responsibility. For example the find_project and authorize before filters in Redmine will find the current project, user, and check that the permissions allow access. These don’t have anything to do with the actions, but they are still required.

Reference commit

Share

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

Related posts:

  1. Redmine Refactor #140: Extract Method WikiController#edit to #update
  2. Redmine Refactor #118: Split Edit Method in NewsController
  3. Redmine Refactor #88: Extract Method from move and perform_move
  4. Daily Refactor #63: Extract Method to before_filter in IssuesController
  5. Daily Refactor #47: Extract Method for Kanban panes – Part 3

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