如何在gem文件中检测请求中的子域

时间:2013-09-12 17:22:07

标签: ruby-on-rails ruby-on-rails-4

我正在尝试扩展gem文件并从中创建一个库。在gem文件中,我想访问 request 参数并从中查找特定的子域。

我在我的应用程序上使用了request.subdomain来访问子域,但是当我尝试相同时,它会导致错误。

这是我在项目 Gemfile

中访问gem的本地副本的方法
gem 'i18n-active_record', :path => '/home/myname/Downloads/i18n-active_record-master' ,:require => 'i18n/active_record'

这是我尝试访问子域的方法

require 'rails'
require 'active_record'

module I18n
  module Backend

    class ActiveRecord
      class Translation < ::ActiveRecord::Base
        TRUTHY_CHAR = "\001"
        FALSY_CHAR = "\002"

        set_table_name 'translations'
        attr_protected :is_proc, :interpolations

        serialize :value
        serialize :interpolations, Array

        class << self
          def locale(locale)
            school_id = find_school
            scoped(:conditions => { :school_id => school_id.nil? ? nil : school_id, :locale => locale.to_s })
          end

          #for finding subdomain from request
          def find_subdomain
              subdomain = request.subdomain
              subdomain_id = Rails.cache.fetch([subdomain.hash,TRUTHY_CHAR.hash]){ School.find_by_subdomain(subdomain).id }
              return subdomain_id
          end


          def lookup(keys, *separator)
            column_name = connection.quote_column_name('key')
            keys = Array(keys).map! { |key| key.to_s }

            unless separator.empty?
              warn "[DEPRECATION] Giving a separator to Translation.lookup is deprecated. " <<
                "You can change the internal separator by overwriting FLATTEN_SEPARATOR."
            end

            namespace = "#{keys.last}#{I18n::Backend::Flatten::FLATTEN_SEPARATOR}%"
            scoped(:conditions => ["#{column_name} IN (?) OR #{column_name} LIKE ?", keys, namespace])
          end

          def available_locales
            Translation.find(:all, :select => 'DISTINCT locale').map { |t| t.locale.to_sym }
          end
        end

        def interpolates?(key)
          self.interpolations.include?(key) if self.interpolations
        end

        def value
          value = read_attribute(:value)
          if is_proc
            Kernel.eval(value)
          elsif value == FALSY_CHAR
            false
          elsif value == TRUTHY_CHAR
            true
          else
            value
          end
        end

        def value=(value)
          if value === false
            value = FALSY_CHAR
          elsif value === true
            value = TRUTHY_CHAR
          end

          write_attribute(:value, value)
        end
      end
    end
  end
end

我希望gem能够自动检测子域并采取相应的行动。

2 个答案:

答案 0 :(得分:1)

听起来你需要将当前请求存储在线程变量中。这不是最佳实践,因为它就像使用全局变量一样,但在这种情况下可能没有其他方法。

示例:在您的控制器中设置一个前置过滤器(如果您需要所有控制器和操作,则在ApplicationController中),这样做:

Thread.current[:current_request] = request

然后在gem中,只需访问变量,确保在值为nil时处理该情况。问题是宝石现在依赖于过滤器。可能有办法让它更清洁,但这个问题超出了这个问题。

修改

使这个更清洁的一种可能方法是在gem中提供一个类方法来设置当前请求。该方法将处理当前线程。例如:

class I18n::Backend::ActiveRecord::Translation
  THREAD_KEY = :i18n_backend_ar_trans_request

  class << self
    def current_request=(request)
      Thread.current[THREAD_KEY] = request
    end

    def with_current_request(request)
      Thread.current[THREAD_KEY] = request
      yield
    ensure
      Thread.current[THREAD_KEY] = nil
    end

    # This is for your gem to access the value.
    # It's your choice whether to make it private, but I recommend doing so.
    private
    def current_request
      Thread.current[THREAD_KEY]
    end
  end
end

在你的around_filter中:

def your_filter
  I18n::Backend::ActiveRecord::Translation.with_current_request(request) do
    yield
  end
end

答案 1 :(得分:0)

最有可能的原因是您的request对象在gem内部无法访问。您可以将其作为参数传递给find_subdomain方法,如此

def find_subdomain(request)
   subdomain = request.subdomain
   subdomain_id = Rails.cache.fetch([subdomain.hash,TRUTHY_CHAR.hash]){ School.find_by_subdomain(subdomain).id }
   return subdomain_id
end

您可能需要剖析并探索此宝石subdomain-fu以获取更多信息