Daily Refactor #46: Extract Method for Kanban panes – Part 2

This is the second in a series of refactorings to clean up how the Kanban class gets it’s pane data.

The Refactoring

Using extract method again, I was able to merge #get_active and #get_testing so they both use a new method #issues_from_kanban_issue.

Before

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# app/models/kanban.rb
class Kanban
  def get_active
    active = {}
    @users.each do |user|
      active[user] = KanbanIssue.find_active(user.id)
    end unless @users.blank?
    active
  end
 
  def get_testing
    testing = {}
    @users.each do |user|
      testing[user] = KanbanIssue.find_testing(user.id)
    end unless @users.blank?
    testing
  end
end

After

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# app/models/kanban.rb
class Kanban
  def get_active
    issues_from_kanban_issue(:active)
  end
 
  def get_testing
    issues_from_kanban_issue(:testing)
  end
 
  private
 
  def issues_from_kanban_issue(pane)
    return {} unless [:active, :testing].include?(pane)
 
    issues = {}
    @users.each do |user|
      issues[user] = KanbanIssue.send('find_' + pane.to_s, user.id)
    end unless @users.blank?
    issues
 
  end
end

Review

This gets me one step closer to uniting all of the Kanban#get_* methods.

Reference commit