Daily Refactor #66: Pull Up Method to ApplicationController

The Refactoring

Today I cleaned up one of the duplications from yesterday’s refactoring, using pull up method.

Before

1
2
3
4
5
6
7
8
9
# app/controllers/issues_controller.rb
class IssuesController < ApplicationController
  def query_statement_invalid(exception)
    logger.error "Query::StatementInvalid: #{exception.message}" if logger
    session.delete(:query)
    sort_clear
    render_error "An error occurred while executing the query and has been logged. Please report this error to your Redmine administrator."
  end
end
1
2
3
4
5
6
7
8
9
# app/controllers/gantts_controller.rb
class GanttsController < ApplicationController
  def query_statement_invalid(exception)
    logger.error "Query::StatementInvalid: #{exception.message}" if logger
    session.delete(:query)
    sort_clear
    render_error "An error occurred while executing the query and has been logged. Please report this error to your Redmine administrator."
  end
end
1
2
3
4
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  # ...
end

After

1
2
3
4
# app/controllers/issues_controller.rb
class IssuesController < ApplicationController
  # ...
end
1
2
3
4
# app/controllers/gantts_controller.rb
class GanttsController < ApplicationController
  # ...
end
1
2
3
4
5
6
7
8
9
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  def query_statement_invalid(exception)
    logger.error "Query::StatementInvalid: #{exception.message}" if logger
    session.delete(:query)
    sort_clear if respond_to?(:sort_clear)
    render_error "An error occurred while executing the query and has been logged. Please report this error to your Redmine administrator."
  end
end

Review

This leaves one duplicated method left, GanttsController#find_optional_project. It turns out, it’s duplicated seven times in Redmine but with slightly different content each time. Fixing this should be a good refactoring to wrap up the week.

Reference commit