强制rails创建资产缓存文件

时间:2009-06-11 21:33:19

标签: ruby-on-rails capistrano deployment asset-management

我在s3上托管我的资产。在生产中,rails正在寻找/javascripts/cache/all.js和/stylesheets/cache/all.css。当我使用cap部署时,我正在使用插件将公共目录翻到s3。问题是rails在第一次请求它们之前不会创建这些缓存文件,所以当我转移公共目录时它们在部署期间不存在。是否有一种简单的方法可以在部署期间强制创建这些文件?

5 个答案:

答案 0 :(得分:4)

创建缓存文件的实际rails模块是ActionView::Helpers::AssetTagHelper,您可以在部署期间重新使用它来创建缓存文件。以下对我有用:

require 'action_view'
class AssetCacheWriter

  include ActionView::Helpers::AssetTagHelper

  def write
    write_asset_file_contents(File.join(JAVASCRIPTS_DIR, "all.js"), compute_javascript_paths([:all], true))
    write_asset_file_contents(File.join(STYLESHEETS_DIR, "all.css"), compute_stylesheet_paths([:all], true))
'standard_all')
  end

end

我从rails代码中解除了write_asset_file_contents次调用。 _DIR常量在包含的模块中定义,true调用中的compute_xxx_paths参数表示应递归包含所有文件。

我创建了一个简单的上限任务,只写出了文件:

namespace :sample
  task :assets do
    AssetCacheWriter.new.write
  end
end

答案 1 :(得分:3)

我在rake任务文件中使用它。由于您使用的是stylesheet_link_tag / javascript_include_tag

中的值,因此可以避免创建其他类并保持干燥状态
desc "Clears javascripts/cache and stylesheets/cache"   task :clear => :environment do
  puts "Clearing javascripts/cache and stylesheets/cache"
  FileUtils.rm(Dir['public/javascripts/cache_[^.]*']) # use :cache => 'cache_all.js' in stylesheet_link_tag
  FileUtils.rm(Dir['public/stylesheets/cache_[^.]*']) # use :cache => 'cache_all.css' in javascript_include_tag
end

desc "Recreate the javascripts/stylesheets cache."
  task :generate => [:environment, :clear] do
  puts "Recreate the javascripts/stylesheets cache"
  ActionController::Base.perform_caching = true
  app = ActionController::Integration::Session.new
  app.get '/'
end

答案 2 :(得分:1)

如果你正在使用Engine Yard Cloud,那就是我所做的:

添加了一个文件/lib/tasks/assetcache.rake

    require 'assetwriter'

    namespace :assetcache do

      desc "Clears javascripts/cache and stylesheets/cache"   
      task :clear => :environment do
        puts "Clearing javascripts/cache and stylesheets/cache"
        FileUtils.rm(Dir['public/javascripts/cache_[^.]*'])
# use :cache => 'cache_all.js' in stylesheet_link_tag
        FileUtils.rm(Dir['public/stylesheets/cache_[^.]*'])
# use :cache => 'cache_all.css' in javascript_include_tag
      end

      desc "Recreate the javascripts/stylesheets cache."
      task :generate => [:environment, :clear] do
        puts "Recreate the javascripts/stylesheets cache"
        AssetCacheWriter.new.write
      end

    end

然后我添加了/lib/assetwriter.rb

要求'action_view' class AssetCacheWriter

包括ActionView :: Helpers :: AssetTagHelper

def写道     write_asset_file_contents(File.join(JAVASCRIPTS_DIR,“cache / all.js”),compute_javascript_paths([:all],true))     write_asset_file_contents(File.join(STYLESHEETS_DIR,“cache / all.css”),compute_stylesheet_paths([:all],true))   端

然后我添加到/deploy/after_restart.rb

运行“cd#{release_path}&& bundle exec rake assetcache:generate”

答案 3 :(得分:0)

我发现包含顺序很重要(例如,jQuery包含在其他脚本之前),因此使用了以下方法。还允许使用:defaults。

首先是lib / asset_pacakger:

require 'action_view'

class AssetCacheWriter
  include ActionView::Helpers::AssetTagHelper
  include ActionView::Helpers::TagHelper 

  def write
    ActionController::Base.perform_caching = true    
    stylesheet_link_tag *ApplicationController::STYLESHEET_INCLUDES
    javascript_include_tag *ApplicationController::JAVASCRIPT_INCLUDES
  end
end

然后是scripts / package_assets:

#!/usr/bin/env ruby

RAILS_ROOT = File.expand_path(File.dirname(__FILE__) + '/../')
Dir.chdir(RAILS_ROOT)

RAILS_ENV = ENV['RAILS_ENV'] || 'development'

puts "Bundling assets for environment #{RAILS_ENV}"

require File.join('config', 'environment') 

AssetCacheWriter.new.write

然后是一个上限任务:

desc "Package assets"
task :package_assets do
  %x[#{Dir.getwd}/script/package_assets] 
  %x[svn commit #{Dir.getwd}/public/javascripts/defaults.js #{Dir.getwd}/public/stylesheets/defaults.css -m "(capistrano) packaging assets to trunk"]
  logger.info "packaged assets to trunk"
end

答案 4 :(得分:0)

在Rails 3.0.7中,上述方法似乎都不起作用。关于配置未定义我遇到了很多错误。

在看完Rails代码之后,我想出了一个适合我的解决方案。

LIB /任务/ cache_assets.rake

require 'asset_packager'

desc "cache assets"
task :cache_assets do

  puts "-----> Caching Assets"
  AssetCacheWriter.new.write
  puts "-----> Done!"

end

然后是lib / asset_packager.rb

require 'action_view'

class AssetCacheWriter
  include ActionView::Helpers::AssetTagHelper
  include ActionView::Helpers::TagHelper 

  def config
    ApplicationController
  end

  def controller    
    ApplicationController
  end


  def write
    ActionController::Base.perform_caching = true    

    # You can replace these with your app's cached assets. They should look the same
    # as in your view templates.
    stylesheet_link_tag :vendor, :defaults, :cache => 'cached/all'
    javascript_include_tag :bd_defaults, :'vendor-head', :cache => 'cached/defaults_vendor'

  end

end

然后执行它,只需运行rake cache_assets

相关问题