Rails cantrips

Weston Triemstra
Weston TriemstraPrincipal at Netsign
Rails Cantrips
Rails cantrips
We make modern web
applications for desktop, mobile,
and augmented reality.
Rails cantrips
Environment
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
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 see
this .
# config/application.rb
config.paths.add 'extras', eager_load: true
Arkency post
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,
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?
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
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
Gems
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" %>
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, 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
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
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
Thank you
Weston Triemstra
@thermistor
weston@netsign.com
1 of 24

Recommended

Cyansible by
CyansibleCyansible
CyansibleAlan Norton
10.8K views58 slides
Automating aws infrastructure and code deployments using Ansible @WebEngage by
Automating aws infrastructure and code deployments using Ansible @WebEngageAutomating aws infrastructure and code deployments using Ansible @WebEngage
Automating aws infrastructure and code deployments using Ansible @WebEngageVishal Uderani
1.1K views18 slides
CI/CD with Kubernetes, Helm & Wercker (#madScalability) by
CI/CD with Kubernetes, Helm & Wercker (#madScalability)CI/CD with Kubernetes, Helm & Wercker (#madScalability)
CI/CD with Kubernetes, Helm & Wercker (#madScalability)Diacode
12.8K views32 slides
Ansible 2 and Ansible Galaxy 2 by
Ansible 2 and Ansible Galaxy 2Ansible 2 and Ansible Galaxy 2
Ansible 2 and Ansible Galaxy 2Jeff Geerling
5.3K views21 slides
VSTS Release Pipelines with Kubernetes by
VSTS Release Pipelines with KubernetesVSTS Release Pipelines with Kubernetes
VSTS Release Pipelines with KubernetesMarc Müller
800 views78 slides
Testing with Ansible by
Testing with AnsibleTesting with Ansible
Testing with AnsibleBas Meijer
554 views16 slides

More Related Content

What's hot

Using Ansible at Scale to Manage a Public Cloud by
Using Ansible at Scale to Manage a Public CloudUsing Ansible at Scale to Manage a Public Cloud
Using Ansible at Scale to Manage a Public CloudJesse Keating
13.6K views36 slides
Architecting for the cloud by
Architecting for the cloudArchitecting for the cloud
Architecting for the cloudLeonidas Tsementzis
384 views41 slides
Ansible presentation by
Ansible presentationAnsible presentation
Ansible presentationSuresh Kumar
7.1K views21 slides
Getting Started with Ansible by
Getting Started with AnsibleGetting Started with Ansible
Getting Started with AnsibleAhmed AbouZaid
646 views19 slides
DotNet Conf Valencia 2019 - Building cloud native apps with .NRT core 3.0 and... by
DotNet Conf Valencia 2019 - Building cloud native apps with .NRT core 3.0 and...DotNet Conf Valencia 2019 - Building cloud native apps with .NRT core 3.0 and...
DotNet Conf Valencia 2019 - Building cloud native apps with .NRT core 3.0 and...Alberto Diaz Martin
430 views34 slides
Ansible introduction - XX Betabeers Galicia by
Ansible introduction - XX Betabeers GaliciaAnsible introduction - XX Betabeers Galicia
Ansible introduction - XX Betabeers GaliciaJuan Diego Pereiro Arean
380 views32 slides

What's hot(20)

Using Ansible at Scale to Manage a Public Cloud by Jesse Keating
Using Ansible at Scale to Manage a Public CloudUsing Ansible at Scale to Manage a Public Cloud
Using Ansible at Scale to Manage a Public Cloud
Jesse Keating13.6K views
Ansible presentation by Suresh Kumar
Ansible presentationAnsible presentation
Ansible presentation
Suresh Kumar7.1K views
Getting Started with Ansible by Ahmed AbouZaid
Getting Started with AnsibleGetting Started with Ansible
Getting Started with Ansible
Ahmed AbouZaid646 views
DotNet Conf Valencia 2019 - Building cloud native apps with .NRT core 3.0 and... by Alberto Diaz Martin
DotNet Conf Valencia 2019 - Building cloud native apps with .NRT core 3.0 and...DotNet Conf Valencia 2019 - Building cloud native apps with .NRT core 3.0 and...
DotNet Conf Valencia 2019 - Building cloud native apps with .NRT core 3.0 and...
Ansible, best practices by Bas Meijer
Ansible, best practicesAnsible, best practices
Ansible, best practices
Bas Meijer13.1K views
DevOps - Infrastructure as Code by Andre Marcelo-Tanner by DEVCON
DevOps - Infrastructure as Code by Andre Marcelo-TannerDevOps - Infrastructure as Code by Andre Marcelo-Tanner
DevOps - Infrastructure as Code by Andre Marcelo-Tanner
DEVCON 377 views
Ansible Intro - June 2015 / Ansible Barcelona User Group by Orestes Carracedo
Ansible Intro - June 2015 / Ansible Barcelona User GroupAnsible Intro - June 2015 / Ansible Barcelona User Group
Ansible Intro - June 2015 / Ansible Barcelona User Group
Orestes Carracedo2.1K views
Ansible with AWS by Allan Denot
Ansible with AWSAnsible with AWS
Ansible with AWS
Allan Denot2.3K views
Infrastructure = Code by Georg Sorst
Infrastructure = CodeInfrastructure = Code
Infrastructure = Code
Georg Sorst1.1K views
A quick intro to Ansible by Dan Vaida
A quick intro to AnsibleA quick intro to Ansible
A quick intro to Ansible
Dan Vaida503 views
9 ways to consume kubernetes on open stack in 15 mins (k8s meetup) by Stacy Véronneau
9 ways to consume kubernetes on open stack in 15 mins (k8s meetup)9 ways to consume kubernetes on open stack in 15 mins (k8s meetup)
9 ways to consume kubernetes on open stack in 15 mins (k8s meetup)
Stacy Véronneau2.3K views
DevOps in a Regulated World - aka 'Ansible, AWS, and Jenkins' by rmcleay
DevOps in a Regulated World - aka 'Ansible, AWS, and Jenkins'DevOps in a Regulated World - aka 'Ansible, AWS, and Jenkins'
DevOps in a Regulated World - aka 'Ansible, AWS, and Jenkins'
rmcleay11.1K views
What's New in v2 - AnsibleFest London 2015 by jimi-c
What's New in v2 - AnsibleFest London 2015What's New in v2 - AnsibleFest London 2015
What's New in v2 - AnsibleFest London 2015
jimi-c38.4K views
Introduction to ansible galaxy by Ivan Serdyuk
Introduction to ansible galaxyIntroduction to ansible galaxy
Introduction to ansible galaxy
Ivan Serdyuk316 views

Similar to Rails cantrips

Rails Engine | Modular application by
Rails Engine | Modular applicationRails Engine | Modular application
Rails Engine | Modular applicationmirrec
6.3K views29 slides
Ruby on Rails and Docker - Why should I care? by
Ruby on Rails and Docker - Why should I care?Ruby on Rails and Docker - Why should I care?
Ruby on Rails and Docker - Why should I care?Adam Hodowany
1.2K views68 slides
Deploying your web application with AWS ElasticBeanstalk by
Deploying your web application with AWS ElasticBeanstalkDeploying your web application with AWS ElasticBeanstalk
Deploying your web application with AWS ElasticBeanstalkJulien SIMON
2.2K views33 slides
Deploying a simple Rails application with AWS Elastic Beanstalk by
Deploying a simple Rails application with AWS Elastic BeanstalkDeploying a simple Rails application with AWS Elastic Beanstalk
Deploying a simple Rails application with AWS Elastic BeanstalkJulien SIMON
6.9K views17 slides
Rails web api 开发 by
Rails web api 开发Rails web api 开发
Rails web api 开发shaokun
1.3K views45 slides
DCEU 18: Developing with Docker Containers by
DCEU 18: Developing with Docker ContainersDCEU 18: Developing with Docker Containers
DCEU 18: Developing with Docker ContainersDocker, Inc.
595 views36 slides

Similar to Rails cantrips(20)

Rails Engine | Modular application by mirrec
Rails Engine | Modular applicationRails Engine | Modular application
Rails Engine | Modular application
mirrec6.3K views
Ruby on Rails and Docker - Why should I care? by Adam Hodowany
Ruby on Rails and Docker - Why should I care?Ruby on Rails and Docker - Why should I care?
Ruby on Rails and Docker - Why should I care?
Adam Hodowany1.2K views
Deploying your web application with AWS ElasticBeanstalk by Julien SIMON
Deploying your web application with AWS ElasticBeanstalkDeploying your web application with AWS ElasticBeanstalk
Deploying your web application with AWS ElasticBeanstalk
Julien SIMON2.2K views
Deploying a simple Rails application with AWS Elastic Beanstalk by Julien SIMON
Deploying a simple Rails application with AWS Elastic BeanstalkDeploying a simple Rails application with AWS Elastic Beanstalk
Deploying a simple Rails application with AWS Elastic Beanstalk
Julien SIMON6.9K views
Rails web api 开发 by shaokun
Rails web api 开发Rails web api 开发
Rails web api 开发
shaokun1.3K views
DCEU 18: Developing with Docker Containers by Docker, Inc.
DCEU 18: Developing with Docker ContainersDCEU 18: Developing with Docker Containers
DCEU 18: Developing with Docker Containers
Docker, Inc.595 views
Hands on Docker - Launch your own LEMP or LAMP stack - SunshinePHP by Dana Luther
Hands on Docker - Launch your own LEMP or LAMP stack - SunshinePHPHands on Docker - Launch your own LEMP or LAMP stack - SunshinePHP
Hands on Docker - Launch your own LEMP or LAMP stack - SunshinePHP
Dana Luther408 views
Practical Chef and Capistrano for Your Rails App by SmartLogic
Practical Chef and Capistrano for Your Rails AppPractical Chef and Capistrano for Your Rails App
Practical Chef and Capistrano for Your Rails App
SmartLogic10.7K views
[EXTENDED] Ceph, Docker, Heroku Slugs, CoreOS and Deis Overview by Leo Lorieri
[EXTENDED] Ceph, Docker, Heroku Slugs, CoreOS and Deis Overview[EXTENDED] Ceph, Docker, Heroku Slugs, CoreOS and Deis Overview
[EXTENDED] Ceph, Docker, Heroku Slugs, CoreOS and Deis Overview
Leo Lorieri2K views
Kubered -Recipes for C2 Operations on Kubernetes by Jeffrey Holden
Kubered -Recipes for C2 Operations on KubernetesKubered -Recipes for C2 Operations on Kubernetes
Kubered -Recipes for C2 Operations on Kubernetes
Jeffrey Holden704 views
Docker for Web Developers: A Sneak Peek by msyukor
Docker for Web Developers: A Sneak PeekDocker for Web Developers: A Sneak Peek
Docker for Web Developers: A Sneak Peek
msyukor873 views
Deploying configurable frontend web application containers by José Moreira
Deploying configurable frontend web application containersDeploying configurable frontend web application containers
Deploying configurable frontend web application containers
José Moreira88 views
Chef - industrialize and automate your infrastructure by Michaël Lopez
Chef - industrialize and automate your infrastructureChef - industrialize and automate your infrastructure
Chef - industrialize and automate your infrastructure
Michaël Lopez600 views
Infrastructureascode slideshare-160331143725 by miguel dominguez
Infrastructureascode slideshare-160331143725Infrastructureascode slideshare-160331143725
Infrastructureascode slideshare-160331143725
miguel dominguez53 views
Infrastructureascode slideshare-160331143725 by MortazaJohari
Infrastructureascode slideshare-160331143725Infrastructureascode slideshare-160331143725
Infrastructureascode slideshare-160331143725
MortazaJohari25 views
Infrastructure as code: running microservices on AWS using Docker, Terraform,... by Yevgeniy Brikman
Infrastructure as code: running microservices on AWS using Docker, Terraform,...Infrastructure as code: running microservices on AWS using Docker, Terraform,...
Infrastructure as code: running microservices on AWS using Docker, Terraform,...
Yevgeniy Brikman177.5K views
Docker for developers on mac and windows by Docker, Inc.
Docker for developers on mac and windowsDocker for developers on mac and windows
Docker for developers on mac and windows
Docker, Inc.1.7K views
DB proxy server test: run tests on tens of virtual machines with Jenkins, Vag... by Timofey Turenko
DB proxy server test: run tests on tens of virtual machines with Jenkins, Vag...DB proxy server test: run tests on tens of virtual machines with Jenkins, Vag...
DB proxy server test: run tests on tens of virtual machines with Jenkins, Vag...
Timofey Turenko85 views
Kubernetes - training micro-dragons without getting burnt by Amir Moghimi
Kubernetes -  training micro-dragons without getting burntKubernetes -  training micro-dragons without getting burnt
Kubernetes - training micro-dragons without getting burnt
Amir Moghimi952 views

Recently uploaded

The Path to DevOps by
The Path to DevOpsThe Path to DevOps
The Path to DevOpsJohn Valentino
5 views6 slides
JioEngage_Presentation.pptx by
JioEngage_Presentation.pptxJioEngage_Presentation.pptx
JioEngage_Presentation.pptxadmin125455
8 views4 slides
Using Qt under LGPL-3.0 by
Using Qt under LGPL-3.0Using Qt under LGPL-3.0
Using Qt under LGPL-3.0Burkhard Stubert
13 views11 slides
Playwright Retries by
Playwright RetriesPlaywright Retries
Playwright Retriesartembondar5
6 views1 slide
Advanced API Mocking Techniques Using Wiremock by
Advanced API Mocking Techniques Using WiremockAdvanced API Mocking Techniques Using Wiremock
Advanced API Mocking Techniques Using WiremockDimpy Adhikary
5 views11 slides
Ports-and-Adapters Architecture for Embedded HMI by
Ports-and-Adapters Architecture for Embedded HMIPorts-and-Adapters Architecture for Embedded HMI
Ports-and-Adapters Architecture for Embedded HMIBurkhard Stubert
33 views19 slides

Recently uploaded(20)

JioEngage_Presentation.pptx by admin125455
JioEngage_Presentation.pptxJioEngage_Presentation.pptx
JioEngage_Presentation.pptx
admin1254558 views
Advanced API Mocking Techniques Using Wiremock by Dimpy Adhikary
Advanced API Mocking Techniques Using WiremockAdvanced API Mocking Techniques Using Wiremock
Advanced API Mocking Techniques Using Wiremock
Dimpy Adhikary5 views
Ports-and-Adapters Architecture for Embedded HMI by Burkhard Stubert
Ports-and-Adapters Architecture for Embedded HMIPorts-and-Adapters Architecture for Embedded HMI
Ports-and-Adapters Architecture for Embedded HMI
Burkhard Stubert33 views
Supercharging your Python Development Environment with VS Code and Dev Contai... by Dawn Wages
Supercharging your Python Development Environment with VS Code and Dev Contai...Supercharging your Python Development Environment with VS Code and Dev Contai...
Supercharging your Python Development Environment with VS Code and Dev Contai...
Dawn Wages5 views
Electronic AWB - Electronic Air Waybill by Freightoscope
Electronic AWB - Electronic Air Waybill Electronic AWB - Electronic Air Waybill
Electronic AWB - Electronic Air Waybill
Freightoscope 5 views
Top-5-production-devconMunich-2023-v2.pptx by Tier1 app
Top-5-production-devconMunich-2023-v2.pptxTop-5-production-devconMunich-2023-v2.pptx
Top-5-production-devconMunich-2023-v2.pptx
Tier1 app8 views
Introduction to Git Source Control by John Valentino
Introduction to Git Source ControlIntroduction to Git Source Control
Introduction to Git Source Control
John Valentino7 views
How to build dyanmic dashboards and ensure they always work by Wiiisdom
How to build dyanmic dashboards and ensure they always workHow to build dyanmic dashboards and ensure they always work
How to build dyanmic dashboards and ensure they always work
Wiiisdom14 views
aATP - New Correlation Confirmation Feature.pptx by EsatEsenek1
aATP - New Correlation Confirmation Feature.pptxaATP - New Correlation Confirmation Feature.pptx
aATP - New Correlation Confirmation Feature.pptx
EsatEsenek1205 views
ADDO_2022_CICID_Tom_Halpin.pdf by TomHalpin9
ADDO_2022_CICID_Tom_Halpin.pdfADDO_2022_CICID_Tom_Halpin.pdf
ADDO_2022_CICID_Tom_Halpin.pdf
TomHalpin95 views
Dapr Unleashed: Accelerating Microservice Development by Miroslav Janeski
Dapr Unleashed: Accelerating Microservice DevelopmentDapr Unleashed: Accelerating Microservice Development
Dapr Unleashed: Accelerating Microservice Development
Miroslav Janeski15 views
How To Make Your Plans Suck Less — Maarten Dalmijn at the 57th Hands-on Agile... by Stefan Wolpers
How To Make Your Plans Suck Less — Maarten Dalmijn at the 57th Hands-on Agile...How To Make Your Plans Suck Less — Maarten Dalmijn at the 57th Hands-on Agile...
How To Make Your Plans Suck Less — Maarten Dalmijn at the 57th Hands-on Agile...
Stefan Wolpers42 views

Rails cantrips

  • 3. We make modern web applications for desktop, mobile, and augmented reality.
  • 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
  • 17. Gems
  • 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