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, 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

Porting Apple’s Core Data Books Sample Project to RubyMotion

In this post we’re going to reconstruct the Apple sample project Core Data Books, building it step by step from scratch using RubyMotion. This will take us through: Use of NSFetchedResultsController to manage a collection of objects to be displayed in a table view, Providing undo and redo functionality via the device shake gesture, Use of a nested context to isolate changes made in an add operation. The app presents a table view of books following the master-detail pattern; tapping … Continued

Simpler Core Data with RubyMotion and CDQ

Core Data can be one of the more intimidating Apple frameworks to work with, in part because it doesn’t quite have the polish that many of the other frameworks have but also because when you’re dealing with users’ data it is worth treading carefully: imagining correcting a poorly-executed migration on thousands of remote devices should be enough to persuade anyone of that! Luckily, RubyMotion and Core Data Query can make Core Data a lot more familiar and manageable, particularly if … Continued

Exposing Private Methods Cleanly When Testing iOS Apps

When unit testing iOS apps you will want to be able to test the internal methods of your classes in your test suite but from the outside the non-public methods won’t be readily accessible. One way around this could be to make the method public; this would allow you to test it and wouldn’t break your app but now you’ve added clutter to your interface which over time could add up to a significant problem, particularly if you (or a … Continued