module Comable
  class ProductsController < Comable::ApplicationController
    def index
      @category = Comable::Category.where(id: params[:category_id]).first
      if @category
        subtree_of_category = Comable::Category.subtree_of(@category)
        @products = Comable::Product.eager_load(:categories).merge(subtree_of_category)
      else
        @products = Comable::Product.search(params[:q])
      end
      @products = @products.page(params[:page]).per(Comable::Config.products_per_page)
    end

    def show
      @product = Comable::Product.find(params[:id])
    end
  end
end

module Comable
  class ProductsController < Comable::ApplicationController
    before_filter :load_category_and_products, only: :index

    def index
      @products = @products.page(params[:page]).per(Comable::Config.products_per_page)
    end

    def show
      @product = Comable::Product.find(params[:id])
    end

    private

    def load_category_and_products
      @category = Comable::Category.where(id: params[:category_id]).first
      if @category
        subtree_of_category = Comable::Category.subtree_of(@category)
        @products = Comable::Product.eager_load(:categories).merge(subtree_of_category)
      else
        @products = Comable::Product.search(params[:q])
      end
    end
  end
end

Refactor to split the method
rubocop: Refactor to split the method
