這些rails資產標記幫助程序是ActionView的子模塊,ActionView::Helpers::AssetTagHelper
提供了用於生成將視圖鏈接到資源(如圖像,JavaScript,樣式表和Feed)的HTML的方法。
由於modules
有class
作爲超這意味着AssetTagHelpers也會有它
irb(main):016:0> ActionView::Helpers::AssetTagHelper
=> ActionView::Helpers::AssetTagHelper
irb(main):017:0> ActionView::Helpers::AssetTagHelper.class
=> Module
irb(main):018:0> ActionView::Helpers::AssetTagHelper.class.superclass
=> Object
irb(main):019:0> ActionView::Helpers::AssetTagHelper.class.superclass.superclass
=> BasicObject
irb(main):020:0> ActionView::Helpers::AssetTagHelper.class.superclass.superclass.superclass
=> nil
注:爲簡單起見,我將只專注於JavascriptIncludeTag的緣故,但他們都非常相似。
在這裏你會發現一個名爲ActionView::Helpers::AssetTagHelper::JavascriptIncludeTag
類,你可以實例化
JavascriptIncludeTag.new(config, asset_paths)
的JavascriptIncludeTag類有一個叫做asset_tag
方法,然後調用一個方法content_tag
並返回正確的標籤。
路徑:/actionpack/lib/action_view/helpers/asset_tag_helpers/javascript_tag_helpers.rb
require 'action_view/helpers/asset_tag_helpers/asset_include_tag'
# NOTE: on the 'action_view/helpers/asset_tag_helpers/asset_include_tag' it requires '/actionpack/lib/action_view/helpers/tag_helper.rb' so now all this files are connected :)
.
.
.
def asset_tag(source, options)
content_tag("script", "", { "src" => path_to_asset(source) }.merge(options))
end
路徑:/actionpack/lib/action_view/helpers/tag_helper.rb
def content_tag(name, content_or_options_with_block = nil, options = nil, escape = true, &block)
if block_given?
options = content_or_options_with_block if content_or_options_with_block.is_a?(Hash)
content_tag_string(name, capture(&block), options, escape)
else
content_tag_string(name, content_or_options_with_block, options, escape)
end
end
使更不用說他們的工作方式。
注意:如果你發現我的解釋有點棘手,只是讓我知道,我會編輯,以提供更好的解釋
當你看到方法調用沒有明確的接收器,然後接收器'self'。你究竟如何得出結論:「一切都是對象」是不正確的? –
Ohk,我不知道「自我」。我想我錯了,謝謝! – CuriousMind
我會將其升級爲答案:) –