仅在何处放置控制器的帮助程序方法



我希望编写某些方法来处理字符串,以及在我的许多控制器中发生的其他任务。我知道在您的控制器中包含帮助程序是一种不好的做法,所以我只是想知道,将应用程序范围的方法放在控制器中使用的最佳位置在哪里?

我知道你们中的一些人会说把它们放在模型中,但你必须意识到并非所有控制器都有关联的模型。任何和所有的意见将不胜感激。

我倾向于将它们放入助手中。它们包含在视图中的事实自动对我来说不是问题。您也可以将它们放入类似应用程序/关注/或库/

我不喜欢用私有方法弄乱应用程序控制器因为这经常变得一团糟。

例:

module AuthenticationHelper
  def current_user
    @current_user # ||= ...
  end
  def authenticate!
    redirect_to new_session_url unless current_user.signed_in?
  end
end
module MobileSubdomain
  def self.included(controller)
    controller.before_filter :set_mobile_format
  end
  def set_mobile_format
    request.format = :mobile if request.subdomain == "m"
  end
end
class ApplicationController
  include AuthenticationHelper
  include MobileSubdomain
end
如果您需要在

应用程序范围内使用方法,那么我建议您将这些方法保留在应用程序控制器中,以便在视图中使用它们。 将它们声明为辅助方法。

例如

class ApplicationController < ActionController::Base
 helper_method :current_user, :some_method
 def current_user
   @user ||= User.find_by_id(session[:user_id])
 end
 def some_method
 end
end

我建议将它们放在lib文件夹中。例如,我有:

lib/utils/string_utils
module StringUtils
  def foo
     ...
  end
end
class BarController < ActionController::Base
  include StringUtils
end

这展示了称为胖模型、瘦控制器的好方法,在这种情况下,我们使用 Mixins 而不是模型来分离逻辑,但想法是相同的。您希望控制器尽可能简单。

这完全取决于您的需求。我将在这里提供 2 个示例。它们都只是一个自定义库,位于lib目录下。

第一个示例 - "自定义字符串处理"

# lib/filters.rb
module Filters
  # Converts value to canonical view
  def self.phone(value)
    # remove all non-digits
    clean_value = value.gsub(/D/, '')
    country_codes = configus.phone.country_codes
    area_code = configus.phone.defaults.area_code
    case clean_value.length
      when 7
        "#{area_code}#{clean_value}"
      when 11
        # remove country code only if phone starts with the allowed country code
        if country_codes.include?(clean_value[0].to_i)
          clean_value[1..-1]
        else
          clean_value
        end
      else clean_value
    end
  end
# usage
# app/api/phones_controller.rb
class Api::PhonesController < Api::ApplicationController
  def exists
    if params[:q]
      clean_value = Filters.phone(params[:q])
      ...
    end
  end
end

第二个示例 - 闪存消息的帮助程序

# lib/flash_helper.rb
module FlashHelper
  def flash_translate(key, options = {})
    scope = [:flash, :controllers]
    scope += params[:controller].split('/')
    scope << params[:action]
    t(key, {:scope => scope}.merge(options))
  end
end
# app/application_controller.rb
class ApplicationController < ActionController::Base
  include FlashHelper
end
# usage
# app/your_controller.rb
class YourController < ApplicationController
  def create
    @object = Object.new(params[:object])
    if @object.save
      flash[:success] = flash_translate(:success)
      ...
    end
  end
end

注意:不要忘记将lib目录添加到自动加载路径中。在config/application.rb添加/修改此行config.autoload_paths += %W(#{config.root}/lib) 。所以对我来说,答案是lib目录。

从 Rails 4 开始,有一个专用文件夹app/controllers/concerns 。因此,您可以在那里创建一个模块,然后将其包含在特定的控制器或应用程序控制器中(如果需要它在所有控制器中可用)。

如果这些方法在许多控制器中使用,我会在 application_controller.rb 中定义它们。每个控制器都将继承自它,并且能够使用其中定义的任何方法

最新更新