Tag Archives: ruby

Prefer `if object.present?` over `if object`

So you’ve got a controller method class DressesController < ApplicationController def show @dress = Dress.find_by(params.permit(:id)) end end And a view <div class=”<%= dress ? ‘dress&.status’ : ” %>”>Status Indicator</div> Looks good! Works great! Now your app grows, and you have a lot of other views that use the dress. Long after your view has been […]

How to parameterize an RSpec shared context

I had a RSpec shared_context which was creating a FactoryGirl user mock and then logging in with it, and then running some shared examples for testing permissions on a generic user. I needed to modify it to accept a parameter of using a different user. It originally looked like this: RSpec.shared_context “auth” do let(:current_user) { […]