Time to discontinue rails-assets

by Sahil Gadimbayli, Founder / Senior Engineer

It was Friday evening when I pushed my changes to Github, waiting for CI pipeline to show green, so that I can merge my pull request.

But, no! Something has to go wrong, and it did, as it happens almost once a month with rails-assets, which was down.

It has been a while since the new kid on the block is not new anymore, whom everyone started to use.

Also, the occasionally maintained bower, and nearly abandoned rails-assets repositories.

When was the last time you have added a new dependency using rails-assets? Or perhaps updated any?

For me it was in January 2018, and there was no need to have this sitting in the Gemfile, putting burden on bundler as well as having occasional downtime.

Solution

So, today we will be removing the ancient bootstrap-whatever dependencies from our gemfile, extracting their contents (assets) into /vendor, and precompiling them during application boot.

Let's get to it:

Copy gems' assets into /vendor, running a shell script

We will run a script in which we will go through each rails assets gem and move its assets to /vendor folder of the rails application.

To do that, we need to find where are the gems stored. This, may differ for instance, if you are packaging them yourself, using RVM or letting bundler to handle them.

To do that, just use bundle show see where it's stored.

bundle show rails-assets-YOUR-PACKAGE-NAME-AS-IN-GEMFILE

Once you identify where they are stored, modify the PATH_WHERE_YOUR_GEMS_STORED variable value and run the script. Please, be cautious when running random scripts from web.

# Needs to be run from root folder of your Rails Application.

# set the path where your gems are installed
PATH_WHERE_YOUR_GEMS_STORED="/home/yourusername/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems"
# create new folder in vendor
mkdir vendor/assets/rails-assets

# iterate through all gems which has
# rails-assets- prefix, and move its
# assets into ./vendor/assets/rails-assets

for gem_dir in $PATH_WHERE_YOUR_GEMS_STORED/rails-assets-*; do
  PACKAGE_FOLDER_NAME=$(echo ${gem_dir##*/})

  mkdir ./vendor/assets/rails-assets/$PACKAGE_FOLDER_NAME
  cp -r $gem_dir/app/assets ./vendor/assets/rails-assets/$PACKAGE_FOLDER_NAME/assets
done

Add /vendor/assets/rails-assets to application assets path

Now we have the assets placed in the vendor/assets/rails-assets path, we have to tell our application to load these files during precompilation.

In your application.rb file or assets.rb initializer add:

Rails.application.configure do
  config.assets.paths += Dir["#{Rails.root}/vendor/assets/rails-assets/*/*"]
end

Voilà! Now, go ahead and remove the rails asset gems from the Gemfile, run bundle exec rake assets:precompile to test if everything works as it should, and checking your changes.

No more failures because of rails-assets.org downtimes, and your bundle install gets a slight bump in speed.

Thanks for everything Rails Assets, it was good to know you...

More articles

Dumping and restoring data between different Schemas in Postgres

Going from multiple database setup into a single database with multiple schemas, we will go through the process of dumping and restoring data between different schemas in PostgreSQL.

Read more

Adding index to text columns in Postgres

Speed up your queries by adding index to text columns in Postgres using gin strategy.

Read more

Tell us about your project

lightfulweb OÜ

  • Tallin
    Sepapaja tn 6
    15551 Tallin, Estonia