Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would you package CSS/JS files in a Gem for Rails 3.1 without generators?

I want to write a gem that, once bundled to a Rails 3.1 Gemfile, includes some boilerplate CSS and JS files.

I know about generators, but how could I do this without them so that the gem's effects can be added and removed without anything more than editing one line in the Rails Gemfile?

Ideally, I'd like the gem to include its default CSS/JS upon installation, then let the user use the generator to generate the CSS/JS files if they want to make any modifications.


Here's a sample gem that I copied from jquery-rails, which includes javascript files without generators.

css_gem/
  lib/
    css_gem.rb       {1}
    css_gem/
      engine.rb      {2}
  app/
    assets/
      stylesheets/
        css_gem/
          index.css  {3}
          base.css   {4}

{1} lib/css_gem.rb

module CssGem
  require "css_gem/engine"
end

{2} lib/css_gem/engine.rb

module CSSGem
  class Engine < Rails::Engine
  end
end

{3} app/assets/stylesheets/css_gem/index.css

/*
 *= require base
 */

{4} app/assets/stylesheets/css_gem/base.css

.custom { color: red; }

Rails Gemfile

gem 'css_gem', :path => 'path_to_my_local_gem'

This isn't working for me and Rails doesn't see the CSS file. What am I doing wrong?


Solution: Thankfully, I found a video to hold my hand: http://house9.blogspot.com/2011/06/rails-31-asset-gem.html

I had to still manually add *= require css_gem to my Rails stylesheet manifest (app/assets/stylesheets/application.css). Duh.

like image 876
danneu Avatar asked Jul 17 '11 19:07

danneu


2 Answers

There is a great article by Zurb on how they packaged foundation assets as gem:

"Yetify Your Rails: New Foundation Gem and How To Gemify Your Own Assets"

like image 124
Henry Jacob Avatar answered Oct 02 '22 03:10

Henry Jacob


I collect several articles on how to gemify your assets:

  • Gemify Assets for Rails
  • Jammit: Industrial Strength Asset Packaging for Rails
  • Gemify your assets · Carlos Alexandro Becker
  • Yetify Your Rails: New Foundation Gem and How To Gemify Your Own Assets by ZURB
like image 44
Juanito Fatas Avatar answered Oct 02 '22 04:10

Juanito Fatas