Skip to content

Instantly share code, notes, and snippets.

@zzzhc
Created December 12, 2011 08:24
Show Gist options
  • Save zzzhc/1465892 to your computer and use it in GitHub Desktop.
Save zzzhc/1465892 to your computer and use it in GitHub Desktop.

The asset pipeline provides a framework to concatenate and minify or compress JavaScript and CSS assets. It also adds the ability to write these assets in other languages such as CoffeeScript, Sass and ERB.

javascripts/stylesheets/images moved to app/assets

configuration

in config/application.rb, add:

config.assets.enabled = true
config.assets.version = '1.0'

in config/environments/development.rb

# Do not compress assets
config.assets.compress = false

# Expands the lines which load the assets
config.assets.debug = true

in config/environments/production.rb

# Compress JavaScripts and CSS
config.assets.compress = false

# fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = true

# Generate digests for assets URLs
config.assets.digest = true

# Defaults to Rails.root.join("public/assets")
# config.assets.manifest = YOUR_PATH

# Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
config.assets.precompile += %w(ui-component.js all.js)
config.assets.precompile << /\.js$/
config.assets.precompile << /\.css$/
config.assets.precompile << /\.ico$/
 

precompile asset

# a little slow
bundle exec rake assets:precompile

speed up asset pipeline in development environment

why it is slow? Rails & application middleware will run in every request. skip middlewares will reduce response time.

modify config.ru

map '/assets' do
  use Rack::Lock

  assets = Rails.application.assets

  if Rails.env.development?
    run lambda {|env|
      if (path = env["PATH_INFO"]) =~ /i18n_translations|js_named_routes/
        path = path.sub(/^\//, "")
        asset = assets.find_asset(path)
        body = ERB.new(asset.pathname.read).result
        return [200, {}, body]
      end
      assets.call(env)
    }
  else
    run assets
  end
end

map '/' do
  run Rails.application
end

boootable modules

module is a mountable plugin

#!/bin/bash
MODULE_NAME=$1
if [ "$MODULE_NAME" = "" ]; then
  echo "usage: $0 module_name" >&2
  exit 1
fi
rails plugin new "modules/$MODULE_NAME" --dummy-path=dummy --mountable -J  -T -d mysql --skip-bundle -G
rm -rf "$MODULE_NAME/dummy" "$MODULE_NAME/MIT-LICENSE" "$MODULE_NAME/README.rdoc"
touch "$MODULE_NAME/README.rdoc"
mkdir -p "$MODULE_NAME/app/assets/javascripts"

cp core/config.ru "$MODULE_NAME"
cp core/Rakefile "$MODULE_NAME"
cp core/script/rails "$MODULE_NAME/script"

GEMFILE="$MODULE_NAME/Gemfile"
echo 'gem "ui-common", :path => "../ui-common"' >>$GEMFILE
echo 'gem "core", :path => "../core"' >>$GEMFILE

share dummy app

  • move dummy to modules/boot
  • modify module's script/rails
#!/usr/bin/env ruby

ENGINE_PATH = File.expand_path('../..',  __FILE__)

script_rails = File.expand_path('../../../boot/script/rails',  __FILE__)
Dir.chdir File.expand_path('../../../boot', __FILE__)
load script_rails
  • modify Rakefile
APP_RAKEFILE = File.expand_path("../../boot/Rakefile", __FILE__)
ENGINE_PATH = ::File.expand_path('..',  __FILE__) unless defined? ENGINE_PATH
load 'rails/tasks/engine.rake'

Bundler::GemHelper.install_tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment