Daily Refactor #38: Extract Method in StuffToDo

Since I’m planning to release an update to my StuffToDo plugin next, I decided it could use a week of refactoring in order to clean it up.

The Refactoring

Since this plugin doesn’t have a lot of code (349 lines), there aren’t as many code smells as in the Redmine core. But according to Caliper there are still a few sections that are worse than the community averages. Starting with my trusted friend flay, I found this refactoring.

Before

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# app/models/stuff_to_do.rb
class StuffToDo  '0',
    'Only Issues' => '1',
    'Only Projects' => '2'
  }
 
  def self.using_projects_as_items?
    using = USE.index(Setting.plugin_stuff_to_do_plugin['use_as_stuff_to_do'])
    using == 'All' || using  == 'Only Projects'
  end
 
  def self.using_issues_as_items?
    using = USE.index(Setting.plugin_stuff_to_do_plugin['use_as_stuff_to_do'])
    using == 'All' || using  == 'Only Issues'
  end
end

After

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class StuffToDo  '0',
    'Only Issues' => '1',
    'Only Projects' => '2'
  }
  def self.using_projects_as_items?
    use_setting == 'All' || use_setting  == 'Only Projects'
  end
 
  def self.using_issues_as_items?
    use_setting == 'All' || use_setting  == 'Only Issues'
  end
 
  private
 
  def self.use_setting
    USE.index(Setting.plugin_stuff_to_do_plugin['use_as_stuff_to_do'])
  end
end

Review

This alone wasn’t a big enough refactoring for flay, it still sees #using_projects_as_items? and #using_issues_as_items? as similar enough code to flag. I already have the next refactoring planned which should remove that duplication. Do you have an idea of what it could be? Post your idea to the comments below.

Reference commit