Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rails Cantrips

thermistor
February 23, 2017

Rails Cantrips

A collection of useful little bits of knowledge about how to work with Rails. This talk was given at the Vancouver Ruby Meetup. #vanruby

The video of the presentation is here: https://www.youtube.com/watch?v=CSCE7Tc42kA

thermistor

February 23, 2017
Tweet

More Decks by thermistor

Other Decks in Programming

Transcript

  1. 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
  2. Disable generation of gem docs for fast gem installs: #

    ~/.gemrc or /etc/gemrc gem: --no-rdoc --no-ri
  3. 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
  4. 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,
  5. 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?
  6. 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
  7. 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
  8. Handy way to obfuscate mailto links from spambots: # Gemfile

    gem 'actionview-encoded_mail_to' # in a template <%= mail_to "[email protected]", "Email me", encode: "javascript" %>
  9. 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
  10. 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
  11. 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