原创作者: hideto   阅读:853次   评论:0条   更新时间:2011-05-26    
《Rails Recipes》已经告一段落,今天开始一起学习Rails插件
首先安装我们今天要看的account_location插件:
ruby script/plugin install account_location

account_location很简单,就是几个helper方法,我们先看看源码:
# Copyright (c) 2005 David Heinemeier Hansson
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
module AccountLocation
  def self.included(controller)
    controller.helper_method(:account_domain, :account_subdomain, :account_host, :account_url)
  end

  protected
    def default_account_subdomain
      @account.username if @account && @account.respond_to?(:username)
    end

    def account_url(account_subdomain = default_account_subdomain, use_ssl = request.ssl?)
      (use_ssl ? "https://" : "http://") + account_host(account_subdomain)
    end

    def account_host(account_subdomain = default_account_subdomain)
      account_host = ""
      account_host << account_subdomain + "."
      account_host << account_domain
    end

    def account_domain
      account_domain = ""
      account_domain << request.subdomains[1..-1].join(".") + "." if request.subdomains.size > 1
      account_domain << request.domain + request.port_string
    end

    def account_subdomain
      request.subdomains.first
    end
end

大家是否联想到蛙眼的用户子域名是怎么实现的?对,就是account_location插件
我们这样使用该插件:
class ApplicationController < ActionController::Base
  include AccountLocation
  before_filter :find_account
    
  protected
    def find_account
      @account = Account.find_by_username(account_subdomain)
    end
end

class BlogController < ApplicationController
  def index
    @topics = Topic.find_by_user_id(@account.id)
  end

  # ...
end

在view中:
Your domain: <%= account_url %>

不过这对于@account为nil时就会报异常,DHH写的代码也垃圾啊
所以我改写了一下account_location:
module AccountLocation
  def self.included(controller)
    controller.helper_method(:account_domain, :account_subdomain, :account_host, :account_url)
  end

  protected
  
    def account_url(use_ssl = request.ssl?)
      (use_ssl ? "https://" : "http://") + account_host
    end

    def account_host()
      account_host = ""
      if account_subdomain == ""
      	account_host << ""
      else
        account_host << account_subdomain + "."
      end
      account_host << account_domain
    end

    def account_domain
      account_domain = ""
      account_domain << request.subdomains[1..-1].join(".") + "." if request.subdomains.size > 1
      account_domain << request.domain + request.port_string
    end
    
    def account_subdomain
      request.subdomains.first || ""
    end
end

然后我们可以修改C:\WINDOWS\system32\drivers\etc\hosts或/etc/hosts,添加一行
127.0.0.1       blog.hideto.com

或者类似的本机域名来进行测试
评论 共 0 条 请登录后发表评论

发表评论

您还没有登录,请您登录后再发表评论

文章信息

Global site tag (gtag.js) - Google Analytics