How to Set Up a Staging Environment for Your Existing Heroku App

It's a great idea to have an intermediate environment between Development and Production that is as similar to Production as possible. The purpose is for testing. Your local environment can be too different from the way Production is set up, so when you push straight from Development to Production, you run the risk of your changes breaking things—on a live environment (Yikes!). The good news is that Heroku makes it pretty easy to set up another environment that is pretty much the same as Production.

Assuming you already have a heroku app up and running, the following steps will get you your staging environment:


How to Pass a Variable to Your Specs

There may come a time when you need to pass an environment variable to your specs. For example, on one of my projects we had a services monitor running for every environment. We needed a way to run the same specs, but for different environments. So if the service monitor spec failed, we would know which environment it failed for.

We wanted a way to specify the environment variable when we run the specs like this TEST_ENV=staging rspec services_monitor/service_spec.rb so that it would run the specs, only hitting the urls that exist in staging.

How do we do this? It’s quite simple, really. Let’s say we have a spec that tests whether is up:


Scripting a Ruby on Rails Setup Using Vagrant and Puppet Librarian

Why would you want to put in extra work to make your development box replicable? There are lots of benefits! And I’ll give you the main motivators here:


Pluralize your own way in Ruby on Rails

I have faced a situation on a project where we needed the distinction between a single 'media' and multiple 'media.' By default, Rails will pluralize 'media' to the same thing:

'media'.pluralize #=> 'media'

In our Rails app, that was unacceptable; we had a Media model, and multiple media could belong to a single other model. We needed a way to distinguish between a single media and multiple media. After some discussion about what we should go with, we decided with 1 'media' and 2 'medias.' How to get Rails to recognize this?


Testing &block Arguments with Rspec

In my example, I have a service that calls OpenURI's open, which accepts a block as one of its arguments:

class MyService

  def call_response
    open("", http_basic_authentication: ["username","password"], &:read)

In this case, that last argument calls .read on the response automatically to get to its body. Now I'd like to test the following:


Track the Activity History of Your Embedded Mongoid 3 Documents with the mongoid-history Gem

Recently on my Rails 3 project, we had to keep track of all the updates made to our mongoid documents. There are multiple gems out there to help with this, but the best ones were only compatible with Mongoid 4 (and Rails 4), not functioning with embedded documents, or only recording that something was updated without keeping track of the previous values. We had to settle for the most basic gem for the job: the mongoid-history gem. The biggest struggle was figuring out how to get the gem to keep track of history of embedded documents. This is why I would like to share the solution.


How to Set Up and Run a Separate Set of Tests with Their Own Spec Helper File

There may come a time when you don’t want to run all of your tests together. A common example is wanting to split out running slow integration tests and unit test. On one of my projects we needed a separate set of tests to run at intervals just to make sure some of the numbers were within safe bounds, so we had to set up completely separate tests. This involved setting up a different spec_helper configuration for the different set of tests. Here, I will do a quick walkthrough of how to set up and run those tests.


The Basics of How to Take Advantage of the Rails Asset Pipeline (for v3.2)

You may have heard of the term "asset pipeline" used in the Rails context and wondered what it was and how to set it up. I've recently read up on the details about it ( and had to set up my project to use it, and I'm happy to explain in brief the why and how if you want to just jump right into it.

The most basic point of the asset pipeline is to speed up page load time for your users. Most applications have many javascript libraries and css stylesheets that may take a while to load fully when somebody goes onto your site—especially for the first time.


How to get to the source code of a Ruby gem

Often enough I’ve followed the instructions to installing a gem in my rails application, just to see it not work. Eventually, I would have to uninstall the gem since it wasn’t working for me, and would have to either settle for a not-as-good other gem, or reinvent the wheel and write a solution to the problem again.

What I didn’t know was that you could reveal the gem’s source code in your text editor of choice. You’re actually able to do some debugging to--hopefully--get that gem working with your app.


How to write more readable url strings that have parameters in Ruby

On my current project, we are using many queries to Solr in the form of a url. In some instances, this could mean unreadable and confusing and I’m-not-even-going-to-try-to-change-this url strings. For example, we could have something like (and this is an easier version of urls we use!):

number_of_rows = 10

url = "!field_1:* AND !field_2:* AND !field_3:\"VALUE1\" AND !field_4:\"VALUE2\")&rows=#{number_of_rows}&fl=field_5&wt=json&indent=true"

uri = URI.encode url

I personally find it hard to read, and change. Not to mention that this line runs really long—you’d have to scroll to the right to get all of it. And notice how we also have to call URI.encode on the url to uri encode special characters. What if I told you that it can be better?