Skip to content

Instantly share code, notes, and snippets.

@renatocarvalho
Forked from afeld/gist:5704079
Created July 3, 2013 22:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renatocarvalho/5923508 to your computer and use it in GitHub Desktop.
Save renatocarvalho/5923508 to your computer and use it in GitHub Desktop.

This was tested using Ruby 2.0.0 and Rails 4.0.0.rc1.

Bower

  1. Set the install directory for Bower components:

    // .bowerrc
    {
      "directory": "app/assets/components"
    }
  2. Follow the Bower instructions and create a bower.json file in your directory with your dependencies, e.g.

    // bower.json
    {
      // ...
      "dependencies": {
        "d3": "~3.1.0",
        "underscore": "~1.4.4",
      }
    }
  3. Include them in your JavaScript:

    # config/application.rb
    
    # include Bower components in compiled assets
    config.assets.paths << Rails.root.join('app', 'assets', 'components')
    // /Users/aidan/dev/code_cruise/app/assets/javascripts/application.js
    //
    // Bower packages
    //= require d3/d3
    //= require underscore/underscore
    //
    //= require jquery
    //= require jquery_ujs
    // ...

At this point, make sure your app runs and the JS libs installed by Bower are loading on the page.

Heroku

To have Heroku deploys (Cedar stack only) install and compile Bower components on push:

  1. Use a custom Heroku buildpack that includes Node.js and Bower:

    heroku config:set BUILDPACK_URL='git://github.com/qnyp/heroku-buildpack-ruby-bower.git#run-bower'
  2. Add the following to Rakefile before the require:

    # Rakefile
    
    namespace :assets do
      desc "A little hack to make sprockets handle Bower files"
      # Add extensions to all Bower-installed files that don't have them.
      # See https://github.com/sstephenson/sprockets/issues/347
      task :precompile do
        Dir['app/assets/components/**/*'].each do |filename|
          if File.file?(filename) && File.extname(filename) == ''
            File.rename(filename, "#{filename}.txt")
          end
        end
      end
    end
    
    require File.expand_path('../config/application', __FILE__)
    # ...
  3. Change the following setting to allow assets to be compiled in production (I don't believe this is Bower-specific):

    # config/environments/production.rb
    config.assets.compile = true

Try a deploy: it should successfully compile assets.


It's just that easy!!! ::facepalm::

Additional resources:

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