如何将块传递给Rails.application.config.assets?

时间:2015-11-12 12:19:39

标签: ruby ruby-on-rails-4 asset-pipeline assets

我正在编写rails 4应用程序,我正在尝试重构assets.rb文件。

Currect状态:

Rails.application.config.assets.enabled = true
Rails.application.config.assets.version = '1.0'
Rails.application.config.assets.prefix  = '/sprockets'
Rails.application.config.assets.precompile += %w(404.html 500.html)
Rails.application.config.assets.precompile << %w( big array of css files)
Rails.application.config.assets.precompile << %w( big array of js files)

我想看到的内容:

Rails.application.config.assets do |assets|
  assets.enabled = true
  assets.version = '1.0'
  assets.prefix  = '/sprockets'

  assets.paths << "#{Rails.root}/app/assets/plugins"
  assets.paths << "#{Rails.root}/app/assets/html"

  assets.precompile += %w(404.html 500.html)
  assets.precompile << /\.(?:svg|eot|woff|ttf|swf)\z/

  # css
  assets.precompile << %w( big array of css files)

  # js
  assets.precompile << %w( big array of js files)
end

是否可以将一段代码传递到Rails.application.config.assets

3 个答案:

答案 0 :(得分:1)

不可能,因为如果你查看它的签名,资产不会期望阻止。您可以传递它,但它不会对资产产生任何影响。

答案 1 :(得分:0)

我对rails不是很熟悉,但是像这样的东西可以解决这个问题:

def configure_assets obj
  yield obj
end

configure_assets(Rails.application.config.assets) do |asset|
  asset.enabled = true
  asset.version = '1.0'
  # ...etc....
end

答案 2 :(得分:0)

使用tap

Rails.application.config.assets.tap do |assets|
  assets.enabled = true
  # ...
end