Daily Refactor #72: Move Method in KanbansController

The Refactoring

Today I used move method to clean up a hack job I made a couple of months ago.

Before

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class KanbansController < ApplicationController
  def sync
    # Brute force update :)
    Issue.all.each do |issue|
      KanbanIssue.update_from_issue(issue)
    end
 
    respond_to do |format|
      format.html {
        flash[:notice] = l(:kanban_text_notice_sync)
        redirect_to kanban_path
      }
    end
  end
end

After

1
2
3
4
5
6
7
8
9
10
11
class KanbansController < ApplicationController
  def sync
    Issue.sync_with_kanban
 
    respond_to do |format|
      format.html {
        flash[:notice] = l(:kanban_text_notice_sync)
        redirect_to kanban_path
      }
    end
end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
module RedmineKanban
  module IssuePatch
    module ClassMethods
 
      # Brute force update :)
      def sync_with_kanban
        Issue.all.each do |issue|
          KanbanIssue.update_from_issue(issue)
        end
      end
 
    end
  end
end

Review

This refactoring accomplishes two things:

  1. It helps someone understand what the #sync action in the controller does.
  2. It gives a little handhold for anyone wanting to run the sync from someone else in the code. (e.g. like scripting the sync from a Rake task)

Reference commit