Kyle Decot
Kyle Decot

Reputation: 20815

Requiring a gem's assets from another gem

I am creating a gem that will contain the foundation-rails gem along w/ some common variables that are used across my applications. I have created a stylesheet at vendor/assets/stylesheets/foundation.scss. I load this from within my application as such

Gemfile

gem 'foobar-foundation-rails', path: '...'

app/assets/stylesheets/application.css

//= require foundation

This is a good starting point but how do I include the foundation-rails gem's stylesheet from within this file? I am unsure how to reference another gem's assets

Upvotes: 3

Views: 802

Answers (1)

engineerDave
engineerDave

Reputation: 3935

I think the best approach is to put the responsibility for the require statements in your rails app's javascripts file. This is most likely not functionality you want to bury in a gem, as it hides what is happening.

Then make sure you require your gem's css file before the foundation-rails require. However you should put a dependency requirement in your gem's gemspec file to ensure that the foundation-rails gem will be installed by bundler when your gem is installed.

Also you may have to "namespace" your gems style sheet in order to avoid namespace collisions.

vendor/assets/stylesheets/foobar_foundation_rails/foundation.css

Which would change the require in your stylesheet file to

require 'foobar_foundation_rails/foundation.scss'

Lastly, the naming of a gem establishes how the gem gets required. When you use dashes Rails expects things to be required, and hence your gem's directory structure to follow

lib/foobar/foundation/rails

As opposed to an underscore naming foobar_foundation_rails

lib/foobar_foundation_rails

Unless you're going to build an "extension" to the foundation-rails gem, which would need to be called foundation-rails-foobar, you may want to go with the underscore syntax to save yourself some require headaches. The devise gem is a good example of extension gems.

Upvotes: 2

Related Questions