A collection of useful little bits of knowledge about how to work with Rails.
This talk was given at the Vancouver Ruby Meetup of Feb 23, 2017. #vanruby
The video of the presentation is here: https://www.youtube.com/watch?v=CSCE7Tc42kA
6. Have open files in a different editor,
and use to make it nice.
better_errors Eg.
# Gemfile
gem 'better_errors', group: :development
# config/environments/development.rb
BetterErrors.editor = :sublime
subl_handler
7. Disable generation of gem docs for fast gem installs:
# ~/.gemrc or /etc/gemrc
gem: --no-rdoc --no-ri
8. Multiplex ssh for faster connections
# ~/.ssh/config
Host *
ControlMaster auto
ControlPath ~/.ssh/master-%C
9. A .railsrc file for default database
# ~/.railsrc
-d postgresql
10. for autocomplete and syntax highlightingpgcli
# ln -s /usr/local/bin/pgcli ./bin/psql
rails db
12. Put your reusable code in extras, and make it eager
loaded on production:
Don't put it somewhere under lib. For more info see
this .
# config/application.rb
config.paths.add 'extras', eager_load: true
Arkency post
13. Organize your locale files:
So you can have folders for mailers, models, views, etc.
# From this default:
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '*.{rb,yml}'
# to using a recursive directory glob:
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,
14. Ensure the session cookie is only served over https,
with secure flag.
Rails.application.config.session_store :cookie_store,
key: '_app_session', secure: !Rails.env.development?
15. Move database.yml to database.yml.sample, remove
the production stanza. Add database.yml to
.gitignore, then update bin/setup:
# uncomment these lines in bin/setup
puts "n== Copying sample files =="
unless File.exist?('config/database.yml')
cp 'config/database.yml.sample', 'config/database.yml'
end
16. Getting errors in sidekiq from other projects?
Configure redis to use a different database for each
project via a REDIS_URL env variable
Managed easily using .
# .env
REDIS_URL=redis://127.0.0.1:6379/2
dotenv
18. Handy way to obfuscate mailto links from spambots:
# Gemfile
gem 'actionview-encoded_mail_to'
# in a template
<%= mail_to "weston@netsign.com", "Email me", encode: "javascript" %>
19. Use bundler's open command:
Can customize which editor with BUNDLER_EDITOR
bundle open devise
21. Migrations getting unwieldly? Have legacy data
transforms? Collapse your migrations.
Delete all your other migrations, rake
db:migrate:reset
class CollapseMigrations < ActiveRecord::Migration
def up
# schema.rb's ActiveRecord::Schema.define() block
end
def down
raise ActiveRecord::IrreversibleMigration
end
end
22. WrappingForm for contextual validations ...
# hat tip to Max Savchenko @robotector
class WrappingForm
include ActiveModel::Model
def initialize(model)
@model = model
end
attr_reader :model
end
class UpdateProfileForm < WrappingForm
delegate :first_name, :last_name, to: :model
validates :first_name, :last_name, presence: true
end
23. Use capistrano for remote tasks:
namespace :remote do
desc 'Run a remote rake task, example: "cap staging remote:rake[db:version]"'
task :rake, [:task] do |t, args|
on primary fetch(:migration_role) do
within release_path do
with rails_env: fetch(:rails_env) do
execute :rake, args.task
end
end
end
end
desc 'Run a remote command, example: "cap production remote:cmd[ps,aux]"'
task :cmd, [:cmd, :options] do |t,args|
on primary fetch(:migration_role) do