Rails:在Sweeper中访问控制器变量

时间:2012-01-18 03:25:19

标签: ruby-on-rails caching cookies parameters sweeper

所以我在这里有一些代码我需要修改Rails Sweeper:

class UserTrackingSweeper < ActionController::Caching::Sweeper
  observe User

  def after_update(user)
    return if user.nil? || user.created_at.nil? #fix weird bug complaining about to_date on nil class
    return if user.created_at.to_date < Date.today || user.email.blank?

    user.send_welcome_email if user.email_was.blank?
  end

  #use sweeper as a way to ingest metadata from the user access to the site automatically
  def after_create(user)
    begin
      if !cookies[:user_tracking_meta].nil?
        full_traffic_source = cookies[:user_tracking_meta]
      else
        if !session.empty? && !session[:session_id].blank?
          user_tracking_meta = Rails.cache.read("user_meta_data#{session[:session_id]}")
          full_traffic_source = CGI::unescape(user_tracking_meta[:traffic_source])
        end
      end
      traffic_source = URI::parse(full_traffic_source).host || "direct"
    rescue Exception => e
      Rails.logger.info "ERROR tracking ref link. #{e.message}"
      traffic_source = "unknown"
      full_traffic_source = "unknown"
    end

    # if I am registered from already, than use that for now (false or null use site)
    registered_from = user.registered_from || "site" 
    if params && params[:controller]
      registered_from = "quiz" if params[:controller].match(/quiz/i)
      # registered_from = "api" if params[:controller].match(/api/i)
    end


    meta = {
      :traffic_source => user.traffic_source || traffic_source,
      :full_traffic_source => full_traffic_source,
      :registered_from => registered_from,
      :id_hash => user.get_id_hash
    }
    user.update_attributes(meta)
  end

end

问题是我注意到它似乎无法在清扫器中访问cookie和参数哈希,但它在我们公司的某些集成环境中看起来很好。但它在我的本地机器上不起作用。所以我的问题是:

  1. 如何在Sweeper中访问params / cookies?
  2. 如果不可能,你会做什么呢?
  3. 由于

1 个答案:

答案 0 :(得分:0)

我确信你可以在Cache Sweeper中使用会话变量,所以如果有什么东西放在你需要的东西而你已经设置