Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Rails Cantrips
We make modern web
applications for desktop, mobile,
and augmented reality.
Environment
Have open files in a different editor,
and use to make it nice.
better_errors Eg.
# Gemfile
gem 'better_errors', group: :de...
Disable generation of gem docs for fast gem installs:
# ~/.gemrc or /etc/gemrc
gem: --no-rdoc --no-ri
Multiplex ssh for faster connections
# ~/.ssh/config
Host *
ControlMaster auto
ControlPath ~/.ssh/master-%C
A .railsrc file for default database
# ~/.railsrc
-d postgresql
for autocomplete and syntax highlightingpgcli
# ln -s /usr/local/bin/pgcli ./bin/psql
rails db
Configuration
Put your reusable code in extras, and make it eager
loaded on production:
Don't put it somewhere under lib. For more info ...
Organize your locale files:
So you can have folders for mailers, models, views, etc.
# From this default:
config.i18n.load...
Ensure the session cookie is only served over https,
with secure flag.
Rails.application.config.session_store :cookie_stor...
Move database.yml to database.yml.sample, remove
the production stanza. Add database.yml to
.gitignore, then update bin/se...
Getting errors in sidekiq from other projects?
Configure redis to use a different database for each
project via a REDIS_URL...
Gems
Handy way to obfuscate mailto links from spambots:
# Gemfile
gem 'actionview-encoded_mail_to'
# in a template
<%= mail_to ...
Use bundler's open command:
Can customize which editor with BUNDLER_EDITOR
bundle open devise
Boilerplate
Migrations getting unwieldly? Have legacy data
transforms? Collapse your migrations.
Delete all your other migrations, rak...
WrappingForm for contextual validations ...
# hat tip to Max Savchenko @robotector
class WrappingForm
include ActiveModel:...
Use capistrano for remote tasks:
namespace :remote do
desc 'Run a remote rake task, example: "cap staging remote:rake[db:v...
Thank you
Weston Triemstra
@thermistor
weston@netsign.com
Rails cantrips
Rails cantrips
Upcoming SlideShare
Loading in …5
×

Rails cantrips

97 views

Published on

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

Published in: Software
  • Be the first to comment

  • Be the first to like this

Rails cantrips

  1. 1. Rails Cantrips
  2. 2. We make modern web applications for desktop, mobile, and augmented reality.
  3. 3. Environment
  4. 4. 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
  5. 5. Disable generation of gem docs for fast gem installs: # ~/.gemrc or /etc/gemrc gem: --no-rdoc --no-ri
  6. 6. Multiplex ssh for faster connections # ~/.ssh/config Host * ControlMaster auto ControlPath ~/.ssh/master-%C
  7. 7. A .railsrc file for default database # ~/.railsrc -d postgresql
  8. 8. for autocomplete and syntax highlightingpgcli # ln -s /usr/local/bin/pgcli ./bin/psql rails db
  9. 9. Configuration
  10. 10. 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
  11. 11. 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,
  12. 12. 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?
  13. 13. 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
  14. 14. 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
  15. 15. Gems
  16. 16. 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" %>
  17. 17. Use bundler's open command: Can customize which editor with BUNDLER_EDITOR bundle open devise
  18. 18. Boilerplate
  19. 19. 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
  20. 20. 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
  21. 21. 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
  22. 22. Thank you Weston Triemstra @thermistor weston@netsign.com

×