我正在尝试扩展一个 gem 文件并从中创建一个库。在 gem 文件中,我想访问请求参数并从中找到特定的子域。
我在我的应用程序上使用 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 自动检测子域并采取相应措施。