V2 - Ruby on Rails Tips and Tricks

V2Master

Introduction

This pages includes tips and tricks for Ruby on Rails Development.

Decreasing Startup Times

It is very annoying that rake, rails c, etc, ar starting very slowly. Use rails-sh to solve this problem. Install it as root:

And use it as a shell:

Asset Pipeline

General documentation o the Asset Pipeline is available at:

The following applies to applications running in the production environment

Icon Display

After a production deployment, you need to check to ensure that icons are displayed correctly.

TODO: This should be part of a basic post-install smoke test. A smoke test should probably also look for fatal errors and missing file errors in the log.

Rails Applications Deployed Sub-DIrectories

If a Rails application is served from a sub-directory (e.g. v2.softxs.ch/v2p0) then you need a line like the following in config/environments/production.rb

If this line is missing, then image asset urls will be like :

Instead of:

The following may be an additional way of setting a relative path (not tested):

Getting a Production Rails System to Recognize Code Changes

Due to caching, nothing happens when you change .erb (and probably other) files. In order to get the rails server to recognize changes run the following command:

You have to run the following command to get Rails to rebuild it's cached assets (stored in public/assets):

To delete all precompiled assets: (this basically does a 'rm -rf public/assets')

In addition, the cache, located in tmp/cache, can be cleared with the folloring command (this basically does a 'rm -rf /tmp/cache/*'):

It is unlikely that you will need to run this command.

After running commands that pre-compile or clear assets, or clear the cache, don't forget to run the touch tmp/restart.txt command.

Production Asset Processing

You have two choices for asset compilation, which is defined in the file config/environments/{development|test|production}.rb

Note that if you set production to compile assets automatically (the non-default true setting above), then you also have to change config/application.rb:

If you select fase, then you need to pre-compile assets. Also if you empty the public/assets directory you need to pre-compile assets again. This is explained in the following article:

You pre-compile assets with the following command:

Note that in 'real production' we do not want config.assets.compile = true, as it causes a 'Sprockets call' for each requested assets file to check if the cache is up to date. See:

You can display the asset paths, the list of all directories (including images, javascripts and stylesheets directories) from which assets will be compiled in the Rails console, using the following command:

Note that assets are taken from all the gems that are used by the application.

Bootstrap CSS Files

When using the bootstrap-sass gem, the bootstrap.css and bootstrap-responsive.css files are automatically compiled as assets, provided that app/assets/stylesheets/custom.css.scss contains:

You don't need derectives in app/assets/stylesheets/application.css. E.g. the following is unnecessary:

Note that you don't need a copy of bootstrap-responsive.css in your codebase in app/assets (or lib/assets or vendor/assets) it is automatically included from the gem.

Testing

Testing with RSpec, Capybara and Selenium

Transaction Problems

Tips

Testing JQuery File Upload with Capybara

To achieve, that capybara allows to set file in the file input field the copy the #{GEMDIR}/jquery-fileupload-rails-0.4.1/vendor/assets/stylesheets/jquery.fileupload-ui.scss file into app/assets/stylesheets/jquery.fileupload-ui.scss.erb and change two lines, i.e. the translate will not be effective in test mode:

Otherwise the following line will report "NonVisibleError":

Testing and Folder Select

The folder select input in test mode can be set to contain generally all folders to allow to test authorizations, even with simulating hacking. E.g. app/views/file_assets/_fields.html.erb

Internationalization

The localization is set per session in app/controllers/application_controller.rb in method set_user_localization. The setting is taken currently from the configuration, but could be taken from the user's setting later on.

Date, Date Format, JQuery Date-Picker

The gem 'bootstrap-datepicker-rails' is used for all date fields in the system. For simple_form fields the DatePickerInput class is defined in app/inputs/date_picker_input.rb. It can be utilized with the "as: :date_picker" option, e.g.:

For ActiveForm::FormBuilder fields the method datepicker_field is defined, e.g.:

These methods add "datepicker" the HTML class of the input field and set the HTML value to the localized value of the current attribute if any.

The global javascript function v2_set_datepicker (defined in app/views/shared/_datepicker.js.erb and ) sets the HTML spaceholder value to the 'date.datepicker.format' and sets all datepicker options. This is the place where to change datepicker options. This function can be used also in unobtrusive javascripts also to set datepicker behaviour for dinamically added fields.

If a localized date string must be used in SQL, e.g in AREL where clause, it must be converted to ISO date format. Use Util.i18n_date_to_iso for is.

The format of the date is defined in the locale file, e.g. config/locales/en.yml (currentl the ISO format is defined, but it can be changed):

Delayed Jobs

Using ''render_to_string'' in Delayed Jobs

Delayed jobs can schedule model methods, while render_to_string is available in controllers and views. The trick is: create a controller instance in the model and call a controller method to render the view. E.g.

app/model/transmittal.rb:

app/controller/transmittals_controller.rb

Per Request Variables

Using of class variables (@@variable) in Ruby on Rails can cause surprises because they will live forever, until the server is restarted or the garbage collector removes the class. If we need per request storage, e.g. to save same data created many time in a single request, we can use RequestStore from the gem request_store. We can store everything in the hash RequestStore.store and it's life time will be guaranteed only the current request. See examples in app/lib/dyn_classifications_associations.rb, app/model/folder.rb.

Mixin Class Methods

A module can be used the define instance and even class methods:

See module DynClassificationsAssociations defined in app/lib/dyn_classifications_associations.rb and used in more models.

V2RailsTips (last edited 2014-08-29 14:44:59 by gw)

Copyright 2008-2014, SoftXS GmbH, Switzerland