Refactoring methods via concerns

Viewed 21

In the process of refactoring, at some point it becomes apparent that the number of methods grows to increasingly annoying sizes. Moving methods to the ApplicationController tends to compound the problem.

The following is a question of process composed by a number of subquestions which are, alas, not rigourously independent of each other, as they combine in the refactoring effort. Consulting lots of documentation, the issues still require clarification

Assuming a starting situation as such :

class ApplicationController < ActionController::Base
  def index_shop
    if !user_operator?
      redirect_to root_path and return
    end
  end
end

class ArticlesController < ApplicationController
  def results_section
    @articles = Article.order(aisle_id: :asc, section_id: :asc).where('section_id = ?', params[:section_id]).all
    generic_output_process(@articles)
  end

  private
  def generic_output_process(articles)
    set_shop_basics(@shop)
    article_ids = articles.pluck(:id)
    @components = Component.where('article_id in (?)', article_ids).all
    index_shop
  end
end

which would evolve via the creation of a file in the concerns directory purchasable.rb, leaving the ApplicationController invaried.

class ArticlesController < ApplicationController
  include Purchasable

  def results_section
    @articles = Article.order(aisle_id: :asc, section_id: :asc).where('section_id = ?', params[:section_id]).all
    generic_output_process(@articles)
  end
end

module Purchasable
  extend ActiveSupport::Concern
  
  included do
  
  end
  
  module ArticleMethods

    def generic_output_process(articles)
      set_shop_basics(@shop)
      article_ids = articles.pluck(:id)
      @components = Component.where('article_id in (?)', article_ids).all
      index_shop
    end
  
  end

end
  • Do instance variables percolate exactly as they did previously (@shop is set in ApplicationController before_action, @components will go to the view)?
  • will the concern inherit the method from the application controller?
  • do renders/redirects behave in the same accounting way (i.e. only one redirect per action including its processed methods, wherever they may be placed?
0 Answers
Related