📜 ⬆️ ⬇️

5 more gems for all occasions

In the continuation of the topic of different and useful gems , I want to talk about a few more that make my (and then, I hope, your) work with RoR even more enjoyable and convenient.


high_voltage


Heme from the guys from thoughtbot , which greatly simplifies working with static pages of the site.
The heme works very simply - we add the necessary pages to the folder app / views / pages . After that, the page becomes available at / pages / (filename) . For example, the page that lies in app / views / pages / about.haml will be available at example.com/pages/about .
In addition, heme adds url-helper page_path , which as an argument you need to pass the desired page. For the example above, this will be the page_path (: about) .

Github page
')

gon


It is often necessary to transfer data from Rails to JS. There are several solutions for this.
For example, you can download them separately with an additional ajax request. In the case when there is not very much data and there is no point in making unnecessary calls, gon will do a great job with this task from a domestic manufacturer that allows you to transfer data straight from the controller.
For heme to work, first add to your layout:

<html> <head> <title>Application Title</title> <%= include_gon %> <!--     JS --> 


Next, transfer data to the controller using gon :

 def show @user = User.find(params[:id]) gon.user = @user end 


Now the data from @user is available in your JS, and you can reach it through the gon object. For example:

 $(function() { alert(gon.user); }); 


Heme can be customized for yourself - use camelCase instead of snake_case in the name of exported variables, specify your namespace, make friends with the popular JBuilder and RABL .

Gon @ Railscasts
Github page

http_accept_language


If you are faced with the task of supporting several languages ​​in a rails application, I advise you to pay attention to the http_accept_language gem. It adds several methods to the request object, with which you can easily determine which user’s preferred language is. For example, like this:

 class ApplicationController < ActionController::Base before_filter :set_locale protected def set_locale allowed_locales = %w(ru-RU en-US) I18n.locale = request.preferred_language_from(allowed_locales) || I18n.default_locale end end 


Github page

email_spec


It is extremely useful to always be aware of how your mailers behave, what they send and where they are sent, whether the sent messages contain important information (links to activation, logins / passwords, etc.) For these purposes email_spec is extremely convenient - a set of goodies for RSpec and Cucumber from database_cleaner , which makes it easy to test your mailers.
For example, you want to be sure that the letter with instructions on how to activate your account will be sent to the correct address, will have the correct title and contain a link to the activation. With email_spec, it will look something like this:

 describe UserMailer do let(:user) { create(:user) } describe '#confirmation_instructions' subject { UserMailer.confirmation_instructions(user.id) } it 'should be set to be delivered to the user email address' do subject.should deliver_to(user.email) #      e-mail . end it 'should have the correct subject' do subject.should have_subject(I18n.t('user_mailer.confirmation_instructions.subject')) #      . end it 'should contain the confirmation url' do subject.should have_body_text(/#{user_confirmation_url}/) #        . end end end 


Github page

heroku_san


If you are actively using Heroku , then you definitely should pay attention to this gem. heroku_san makes application deployment even easier and faster.
Suppose you want your project to have several insights on Heroku - production and staging . To do this, after installing the gem, call in the console:

 rake heroku:create_config 


This command will create a configuration file ( config / heroku.yml ) that describes all of your Heroku applications associated with this project. For example, for production and staging environments it looks like this:

 production: &defaults app: myfacebookkiller stack: cedar config: BUNDLE_WITHOUT: 'development:test' staging: <<: *defaults app: myfacebookkiller-staging 


Now, if you have not yet created these applications on Heroku - do it now:

 rake all heroku:create 


Now with the help of heroku_san you can deploy your project to several applications at once:

 rake all deploy 


Or only on a specific application:

 rake production deploy 


heroku_san offers quite a few more useful things to work with Heroku, more details can be found on the gem page on Github.

Github page

Source: https://habr.com/ru/post/138994/


All Articles