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

Share

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

Related posts:

  1. Daily Refactor #81: Move last_invoice_number to Model
  2. Daily Refactor #54: Move Method to KanbanPane
  3. Daily Refactor #31: Extract Method in IssuesHelper#show_detail
  4. Daily Refactor #30: Move Method into Issue
  5. Daily Refactor #8: Move method in the ReportsController

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