Custom Data Migrations in Core Data with RubyMotion

Overview Data vs. Schema Migrations If you are only making relatively simple changes to your schema such as adding/removing a model attribute or relationship then it is likely that you can simply create a new version of your schema and Core Data’s automatic lightweight migration process will take care of it for you, generating the necessary SQL and migrating the data store in place in a fraction of a second. If instead however you need to modify the data that … Continued

Automatic Off-site Backups for your Heroku Postgres Databases

One of the great things about platforms like Heroku is that they generally follow the pattern of immutable infrastructure: on every call to git push heroku master your app contains everything necessary for it to be built and deployed, and if deploying from scratch then everything required will indeed be fetched and all relevant server processes set up, such that your app is deployed and ready in production. Compare that to the case where you have a production server that … Continued

Testing with Rails and Capybara: Methods that Wait, Methods that Won’t

The Problem You’ve started doing automated testing of your key front-end features using a real browser, e.g. Capybara + Selenium & Firefox. Testing is going well, but you are often slowed down by intermittent failures that are hard or impossible to reproduce consistently. You’re testing a remote (AJAX) form and looking to check that the page updates as it should, and you sometimes get errors like: Selenium::WebDriver::Error::StaleElementReferenceError: Element not found in the cache – perhaps the page has changed since … Continued

Simpler Polymorphic Selects in Rails 4 with Global ID

Polymorphic Relations in Rails Rails allows you to define polymorphic relations, where one attribute can point to more than one type of record. For example, if we have Tasks that should be assignable to Clients, but we have two types of Client, Company and Individual and we want to be able to reference either, then we could set up a polymorphic relation like this: class Company < ActiveRecord::Base has_many :tasks, as: :client end class Individual < ActiveRecord::Base has_many :tasks, as: … Continued

Robust Integration Testing in Rails 4 with RSpec, Capybara and Selenium

The Problem Using RSpec and Capybara to test your Rails apps can make writing tests easier which is good both from a perspective of getting people to actually write tests but also for our general productivity as developers. While it has its detractors, I like the terse syntax and ease with which we can define our own helpers which help set up certain contexts such as in this case setting up the default account and switching into its subdomain: feature … Continued

Simpler Handling of AJAX Form Errors in Rails 4

The Problem With unobtrusive Javascript, converting a form in Rails to use AJAX is as easy as setting remote: true: form_for Client.new, remote: true do |form| = form.text_field :name This will cause our form to submit asynchronously, and expect a Javascript response, which will be executed directly to e.g. update the page appropriately. DHH recommends that we design our apps in this manner, where in the Javascript response we re-render the model using its template and use jQuery to update … Continued

Safe and Robust Scheduled Tasks in Rails

I recently added a feature to a SaaS app that I am building that sends out a daily summary email to each user. It is encapsulated as a rake task which is invoked once a day at 7:30am using the Heroku Scheduler add-on, which provides a simplistic cron-like interface for free. In its first incarnation this feature had two critical flaws: If the task failed (processes will occasionally segfault on Heroku) then it would not be retried until the following … Continued

Extending Engine Models in Rails 4

For a SaaS platform I’m working I decided to build it in Rails instead of Django. To get up up to speed with the latest developments in Rails 4 I bought Ryan Bigg’s book, Multitenancy with Rails. In the book he takes you through how to set up a multi-tenant system with Rails and Postgres, with the multi-tenant part of the system encapsulated as a Rails engine. Having the multi-tenant part split out into an engine is nice from a … Continued

Sending Emails Using Rails and Bootstrap

If you’ve built your site’s look and feel using Bootstrap and you’re now looking to start sending emails with similar styling then as you are no doubt aware, email clients are generally extremely limited in their understanding of CSS in particular and so we’re going to have to do a bit of work. The topic of email HTML and CSS rendering is a complex one with an increasing number of caveats as you try to support more email clients. Some … Continued

Safer (Soft) Deletion in Django

The Problem The default behaviour in Django is to cascade deletes, such that when a record is deleted any records that are associated with this record (via a foreign key) are also deleted. This preserves referential integrity within the database which is what we want. Where this can be problematic though is that one careless or mistaken deletion in the admin can cause any number of related records to also be deleted, with no neat way to undo the operation … Continued