In the
Django blog, I occasionally see
posts with
reviews of interesting extensions for this framework. I liked the idea, and I decided that it would be nice to make a similar cycle of notes about Ruby on Rails.
Over the last 9 months of working with RoR, I have accumulated a small list of gems that greatly simplify the life of the developer, and which I could advise to solve various tasks. In this article I want to talk about five of them.
foreigner
A small but very useful gem for creating foreign keys on tables. It adds two new methods to migrations -
add_foreign_key and
remove_foreign_key .
So, for example, if you need to add a key from the
comments table to the
users table, you need to do the following:
')
class CreateComments < ActiveRecord::Migration def change create_table :comments do |t|
Github page
pacecar
Quite an interesting gem from
thoughtbot guys. Its essence is that it analyzes the model to which you connect it, and on the basis of this analysis adds various scope-methods.
Suppose you have a
Post model that has fields in a table:
title ,
content ,
created_at ,
updated_at . And there is a link
has_many: comments . Add Pacecar to our model:
class Post << ActiveRecord::Base include Pacecar has_many :comments, :dependent => :destroy end
After that, for example, such scopes will become available to us:
Post.title_matches('Some title')
Gem adds quite a few useful methods, more details can be found on Github.
Github page
settingslogic
There is a fairly large number of gems that solve the problem of settings in the application. The most successful implementation, in my opinion, is
Settingslogic from the well-known
Ben Johnson . Simple, lightweight, uses a YML file.
The author of the gem suggests using the
Settings class and storing it next to the models (
app / models / settings.rb ).
class Settings < Settingslogic source "
The settings file in this case is stored in
config / application.yml :
defaults: &defaults default_email: admin@example.com development: <<: *defaults test: <<: *defaults production: <<: *defaults
Settings will be called relative to the current Rails environment. We can access them through the
Settings class.
Settings.default_email
Github page
Documentation
letter_opener
Hem-stub authorship
Raina Bates , without which I can not do. The meaning of the work is extremely simple - we indicate in the application config (for example, in config / enviroments / development.rb) that we want to use the
letter_opener as a way of delivering mail.
config.action_mailer.delivery_method = :letter_opener
After that, all outgoing mail begins to form in the
/ tmp / letter_opener folder , and each letter immediately after sending opens in a new browser tab. Simple and convenient.
Github page
immortal
Teambox hem from developers, which allows you to mark a
file as 'deleted' instead of deleting a record. It can be very useful if we want to protect a careless user from data loss.
To use the heme you need to add the
deleted field to the table whose records we want to make "immortal":
class AddDeletedToTasks << ActiveRecord::Migration def change add_column :tasks, :deleted, :boolean end end
Then connect it in the desired model:
class Task << ActiveRecord::Base include Immortal end
Now each entry in the
Task model instead of a complete deletion from the database will be marked as deleted.
Github page
That's all!
At this point I finish this note, I hope you have found something useful in it for yourself.