Asset pipeline and gems - ruby-on-rails

Imagine that I have a engine gem A that has your application.css and application.js
Those files are loaded via stylesheet_link_tag and javascript_include_tag. This works and everybody is happy.
Then comes gem B. That gem also has it's own css/js that it wants gem A to use. So it injects it into the header of gem A like so stylesheet_link_tag('gem_b'). Works fine in development but blows up when trying to deploy with AssetNotPrecompiled exception.
So what kind of solution do I have?
I can use config.assets.precompile on the main app. That's what I do now, but this really sucks.
Is there a way to inject into gem A application.css manifest somehow? That would be optimal.
Thanks.

Basically the idea is to always to only link application.js/css manifests. So you can't safely use stylesheet_link_tag('engine_a') but you can totally do stylesheet_link_tag('engine_a/application').

Related

Why use gems for serving assets instead of the vendor file?

I am relatively new to Rails and I have a question about serving assets from a gem vs just loading the files into the asset pipeline.
As far as I can tell, they do virtually the same thing in that they both make the files available in the asset pipeline to be called in the manifest.
What are the advantages to serving something like gem 'jquery-rails' as a gem instead of just putting /vendor/assets/javascripts/jQuery.js in the vendor assets and loading it that way?
The advantage is you don't have to add the file(s) to your repo and manage updates, you update the gem and you've updated the dependency. They can also add helpers to use the assets more easily.
Not all JS/CSS projects are out-of-the-box compatible with the asset pipeline too, so sometimes the gems will do that work for you as well.
Just because the files get served to clients doesn't make it much different than any other dependency in your application.
The gem includes the unobtrusive javascript for Rails as well as jQuery itself. It also allows you to user assert_select_jquery in tests.
jquery-rails is gem contains js file for both jquery.js, jquery_ujs.js. If you does not include jquery-rails, then you have include both jquery.js and jquery_ujs.js.If you are not using gem for jquery-rails, you have manually keep track what version jquery.js is used for jquery_ujs.js. Currently these dependency management is taken care by gem 'jquery-rails'.
Benefits:
You don't need to manually copy them when you get a new version of
jquery released, gem will make sure to add the latest codes only.
Check this link:
https://github.com/rails/jquery-rails/blob/master/lib/jquery/assert_select.rb#LC48
It provides couple of methods which helps while testing your code.

How to tell Rails to not clean some assets in the public folder

The issue here is that I have Bootstrap on production looking for the fonts at:
assets/spree/fonts/glyphicons-the-file-name.something
When in development mode, it looks for these assets in:
fonts/glyphicons-the-file-name.something
So what I did was I added the fonts folder into public and it all worked. I did the same for production. You can guess that I'm now dealing with a rails assets:clean issue that must be running and removing the files, hence not allowing them to appear.
Is there a way to tell Rails to not clean the files in assets/spree/fonts?
I'm assuming you installed the bootstrap files manually?
If you instead use a gem such as the following, then you won't have to worry about these issues:
gem "bootstrap-sass"
Alternatively, you should be installing everything into your vendor directory. As you've found you'll then have issues with any linked assets within these files. The correct fix for this would be to edit the bootstrap source to use the correct asset_path helpers.
Obviously that's quite a bit of maintenance overhead when you get round to doing the next bootstrap update.
I'd take a look at the bootstrap-sass gem, even if you decide not to use it.

Ruby assets not installing properly

So first off, this question spawns from me being fairly new to rails and just figuring this out as I go.
I am working on a project and have added a few gems to my gemfile since I started and I have encountered something that seems trivial to solve, but I can't figure it out. When I add a gem to my Gemfile and run bundle the assets that are associated with that gem are not being dropped into my assets directory.
For example, I just added Leaflet Marker Cluster to my project, and this gem has 3 files that are needed in the assets folder, but after running bundle, they are no where to be found.
What am I doing wrong here?
They won't be physically in your assets folder. You'll need to require them in your application.css/js manifest like this..
//=require leaflet.markercluster.js
If you want the assets to be compiled inside the assets folder you have two options:
1- Add the asset you want on your manifest:
//= require some_file_from_your_plugin
2- Add the asset on your precompile assets array config:
config/environments/production.rb, go to config.assets.precompile and add the assets you want to compile there, like:
config.assets.precompile += %w( my_asset1.css another_asset.js)
You need one each depending on how you implement each asset on your site:
- if you only load application.js you need to add the asset like in option 1
- if you use something like "= javascript_include_tag 'another_asset.js'" then you need option 2
I really recomend you take your time and read rails guide about assets http://guides.rubyonrails.org/asset_pipeline.html it's REALLY helpful, it covers almost anything you need to know about assets, cache, compression, minification.
It has problems with rails-assets. Just look at this https://github.com/Leaflet/Leaflet.markercluster/issues/394.
If you want to use leaflet.markercluster without wait a next release, here is solution for you.
#= require leaflet.markercluster/dist/leaflet.markercluster.js

Where can i find the chosen.jquery.js file in my rails app?

I'm guessing this is a silly question, but i'm a bit of a newb...
I've added the chosen gem to my rails app by adding it to my Gemfile and requiring chosen-jquery in my application.js file.
My question is: where can i find the actual javascript file for chosen? Is it downloading it automatically?
Simply include the JS and CSS files like described in the documentation on the main page of the Github project and it will be included when precompiling your assets. The files are not within your project directory but rather within the gem and will be resolved by the pipeline when the gem is included.
If you need to get to the file directly (for modifications), you need to put the JS in there manually. You can then still include it in the main application.js for the pipeline and have better control of the version in use. To me, this is the preferred method.
However, may I suggest switching to Select2 which is originally based on Chosen but under much more active development and better documentation:
http://ivaynberg.github.io/select2/
https://github.com/ivaynberg/select2
There's also a gem for it if you like:
https://github.com/argerim/select2-rails
It's in a gem itself. If you look at vendor/assets/javascripts in gem root, you'll see all javascripts that come with chosen gem. They are added by assets pipeline, with
//= require chosen-jquery
line in your application.js.
Look up your assets load path. Like this:
in terminal cd to your app root
then open rails console
then run Rails.application.config.assets.paths
Your chosen-jquery is in one of those folders.

Sharing CSS through Ruby Gem

We have common CSS styling in our organisation that most of the projects use. These assets (css, images etc) are included in every project's source code.
I would like to have a gem that could host these assets and the projects that use this gem would be able to directly use them. At the moment, I can only find ways to use generators and 'install' the assets into a project, not to use them from the gem itself.
The main requirement is that if there's a bug fix/ improvement made to the assets, just updating the gem should get me the latest in all projects that use the upgraded gem.
how do I go about doing this?
You can do this quite easily in rails 3.1+ if you make your gem a rails engine. Among other things if you add assets to an engine then you can require those CSS files from your application's manifest files etc.
There's a walk through on how to do this here and quite a few gems out there that wrap js/CSS packages with that exact aim of being able to upgrade the assets used without having to run generators or anything. For example the jquery-rails gem does this for jquery. A more complicated example is jquery-ui-rails, which bundles all the jquery ui js,CSS, images etc and lets you load only the jquery ui components you actually need.

Resources