我正在尝试覆盖Sprockets在Rails中生成的路径,特别是对于sass帮助程序。
我已经在某处使用各种解决方案:
a)覆盖视图中的image_tag助手
b)覆盖ActionView :: AssetPaths计算功能
c)在Sass :: Script :: Functions
中包含新模块但感觉应该有比这更简单的方法。有一个很好的方式 装饰链轮路径?我这样做是因为我们有一些自定义网址和包装内容正在发生。
答案 0 :(得分:2)
所以,最后我发现这条消息隐藏在sprockets / context.rb
中Custom asset_path helper is not implemented
Extend your environment context with a custom method.
environment.context_class.class_eval do
def asset_path(path, options = {})
end
end
这似乎表明Rails.application.assets.context_class.class_eval会 允许我更改asset_path处理程序,但它似乎不起作用。我不确定这是不是我的错,因为我找不到任何这方面的例子。
目前我的工作原理是:
ActionView::AssetPaths.class_eval do
def compute_public_path(source, dir, options = {})
my_transform(source)
end
end
Sprockets::Helpers::RailsHelper::AssetPaths.class_eval do
def compute_public_path(source, dir, options = {})
my_transform(source)
end
end
ActionView::Helpers::AssetTagHelper::AssetPaths.class_eval do
def compute_public_path(source, dir, options = {})
my_transform(source)
end
end
我怀疑我不需要所有这三个,但我不确定。任何知道他们的链轮的人都想对此发表评论吗?