Installing (and using) ActiveAndroid in your Android App

Posted on: December 17, 2014

If you’re persisting the data in your Android app with SQLite, then ActiveAndroid can keep things much cleaner and simpler for you. You won’t have to define your own SQLiteOpenHelper class; ActiveAndroid will take care of it, as long as you provide it a model and feed it migrations when you make changes to your data structure. Plus, you can interact with your objects pretty much like normal objects, making your code more readable. It’s very similar to the concept of ActiveRecord for Rails. Now, let’s get going setting up!

...

Modify the Data in Your Rails App via Data Migrations

Posted on: December 5, 2014

Although it's generally not a great idea to modify the data in your production database, you may have a few reasons for needing to do so. For example, you might be changing the status system that is used to describe a state of something in your app. Or, you need to delete some orphaned records to clean things up. These are 1-time tasks that we need to make sure are carried out on every environment. How can we make these changes as safely as possible?

...

How to Build a Google Sitemap for your Ruby on Rails App

Posted on: December 2, 2014

How do you get search engines like Google to know how to index the pages on your awesome site? You provide those search engines with a sort of table of contents called a Sitemap. And I'll show you how to add one to your Ruby on Rails application.

...

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

Posted on: November 25, 2014

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

Posted on: November 24, 2014

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 myservice.com is up:

...

Scripting a Ruby on Rails Setup Using Vagrant and Puppet Librarian

Posted on: November 23, 2014

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

Posted on: October 19, 2014

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

Posted on: October 6, 2014

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://website.com/api", http_basic_authentication: ["username","password"], &:read)
  end
end

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

Posted on: October 6, 2014

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

Posted on: October 4, 2014

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)

Posted on: September 30, 2014

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 (http://guides.rubyonrails.org/asset_pipeline.html) 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

Posted on: September 29, 2014

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

Posted on: September 29, 2014

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 = "http://path.to.my.solr.instance.com/solr?q=(!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?

...