Вопрос

I'm pretty new to Rails and I'm not sure what is best practice when it comes to adding assets.

Can anybody tell me the advantages and disadvantages of having Javascripts within the assets vs using corresponding gem?

I find gems for pretty much all javascript libraries I want to use. For example, IntroJS. Should I go with the gem or download the javascript and have the library in my assets?

Это было полезно?

Решение

If the gem does not provide any helpers or generators, then it's typically just a convenience that allows you to easily update the version of the assets via the Gemfile. This is nice, as long as the gem itself is relatively close in version to the actual source.

I usually start with the gem, make sure it's being updated regularly, and then move to the source if I really need to.

I've also recently started using bower for some JS sources, and there is a complementary bower-rails gem that provides some bundler like capabilities. Pretty sweet actually!

Другие советы

I have preferred using direct javascript sources vs gem because:

  • Makes frontend logic independent of rails. Used similarly as 99% of other projects using the js library.
  • Version updates. Its likely that your gem would be updated to point to latest js version quite later. Makes little less flexibility over choosing versions.
  • And the main reason being Manual modifications : Even in some of good js/jquery libraries at many times I have come across to an issue/bug which is unresolved and its easier that you can correct it by quickly editing some lines in source, or some fix exists as posted by some users which hasn't made to a stable release yet etc. Using gem would require you to override it(not that easy as overriding a ruby module :) ) or fork gem which provides no future value.

I use gem for only one reason:

Prevent the repository from unnecessary junks

You need to update Javascript libs, that'll happen frequently. If you pull JS libs directly into your application, your code base will be polluted with unnecessary information when commit.

Use gems. If it does not exist, create one, it's very simple. If current gem is not updated, send a pull request or use your fork.

Here's an attempt at a pros vs. cons list.

Using Gems for your Javascript libraries:

Pros:

  • It keeps your JS dependencies in your Gemfile with your other project dependencies.
  • Using the Gemfile allows grouping of Javascript libs into certain environments.
  • Slightly quicker and cleaner to add a Gem to your project. Compare:

    Add JS library using Gem:

    • Specify Gem in Gemfile
    • $ bundle install
    • require in application.js

    Adding Javascript library manually:

    • Download Javascript library from source.
    • Copy to assets directory.
    • (optional) remove old asset.
    • require in application.js.

Cons:

  • Your project now has an additional place for dependencies: Gemfile and assets directory.
  • Not every Javascript library has a Gem.
  • Creating a Gem for every JS library that doesn't have a Gem is time consuming.
    • If you push your Gem to rubygems.org you're now in charge of keeping the Gem up to date with version changes in the library.
    • If you keep your compiled Gem local, where do you put it? Also updating to the latest version of the JS library, requires you to repackage the Gem which is tedious.

My opinion is use both.

For Javascript libraries that have a well-maintained up-to-date Gem associated with it, use the Gem. For Javascript libraries that don't have a Gem, copy the lib to assets, but keep it whole. Customize the library by putting your overrides in a separate file.

This should make upgrading the library as painless as possible.

Also, in your application.js, put a comment that says where you are including the library from (Gem or asset) for clarity's sake.

  1. When you use asset pipeline, your user's browser makes only one request to fetch javascript

  2. When you add jquery manually, by giving a link to a CDN, its a good chance that user already has jquery downloaded and dont need to download it again

I'm also new to rails and would like to know more =)

UPDATE In my projects there are lots of js, so i go whith the gem and one request for all js. Its also easier(for me) to update a bunch of Gemfile's instead of diving into sources of each project

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top