Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating gems with app assets

I followed http://railscasts.com/episodes/245-new-gem-with-bundler to make a gem with bundler and this is great for gems where i only need a lib, is there a standard practice for gems where i need to create mini apps with assets/controllers/models/views ?

like image 781
user1283644 Avatar asked Mar 21 '12 14:03

user1283644


1 Answers

You would be looking to create an engine at that point. Reading the Engines Guides guide should give you a great start on that.

The bare-bones components you need inside your gem are a file at lib/your_gem.rb that serves the purpose of simply requiring whatever it is your gem needs. If your gem has no other dependencies, then it looks like this:

require 'your_gem/engine'

One line, so much power. The lib/your_gem/engine.rb file it requires has this code in it:

module YourGem
  class Engine < Rails::Engine
  end
end

By simply inheriting from Rails::Engine, this triggers an inheritance hook on Rails::Engine that notifies the framework that there is an engine at the location of your gem.

If you then create a file at app/assets/stylesheets/your_gem/beauty.css, you can then include that in your application (assuming you have the asset pipeline enabled, of course) using this line:

<%= stylesheet_link_tag "your_gem/beauty" %>

Now that I've given you the short version of it, I really, really, really recommend reading the Engines Guide top to bottom to better understand it.

like image 151
Ryan Bigg Avatar answered Sep 22 '22 08:09

Ryan Bigg