Automatic text wrapping in vim

This is something I use all the time. I don’t have it in my ~/.vimrc yet - partially because I haven’t got around to it, partially because I sometimes want to use different line lenghts, and partially because I don’t want to bikeshed myself trying to decide on what line length I want for different file formats. Vim supports soft...

An access token state store in Elixir

I’ve recently been working on a small project that uses OAuth for authentication, which is proving to be a great approach for what I need it for. After I got the OAuth authentication layer working through, I realised that I needed a place to store the hash of user data I receive back from the OAuth provider, along with some...

Generating error messages using ActiveModel::Errors

ActiveModel::Errors are widely used in ActiveRecord and therefore Rails, and while custom messages can be provided to each validation using the message key, it’s much more flexible to instead pass a symbol as the message, which will cause the message to be looked up using the internationalisation framework provided by the i18n gem that is a dependency of Rails. Here...

Configuring Cloudflare for a Heroku application

Cloudflare is a brilliant service, and I’ve been using it for personal use for a couple of years now. In particular, its universal SSL, CDN, and always online capabilities have really come in handy. I use Cloudflare for my personal domain, and frequently build little toy applications that I usually host on Heroku free tier dynos. Because of how Heroku...

Configuring S3 redirect rules

S3 supports redirecting content in a number of ways. In most cases, providing an XML-formatted collection of redirect rules is a good way of centralising where content is going. Using the XML format of redirect rules is also a neat way of extending an S3 bucket with additional behaviour by redirecting to a Lambda function or similar dynamic generation endpoint....

Test CORS headers with Rails in development

When testing CORS headers in Rails, an additional step is needed to check that the headers are working correctly. This is because of a behaviour in Chrome specifically, where requests that are to the same origin do not perform CORS validation - so it’s very difficult to tell whether CORS is working correctly or not. Chrome does not even appear...

Pattern for testing ActiveRecord ideas in a standalone script

This code snippet is a reusable pattern for testing out a model/association structure in ActiveRecord to see whether it’s worth proceeding with. The snippet covers installing necessary dependencies, setting up an in-memory SQLite3 database, and loading a database schema into this database. The example included in the pattern was prepared to demonstrate a has many through association between users and...

Logging to multiple destinations using ActiveSupport 4+

Logging to multiple destinations is something I seem to have to do all the time, and until now, I’ve never really been able to find an approach I’m happy with. Almost always, I’m writing some kind of developer-script, and I want to write messages to both the terminal (to let the dev know what is going on), and to a...

Calculating a SHA1 fingerprint for a file in Ruby

Fingerprinting a file isn’t something I need to do super often, but it is useful to detect duplicates of files, or verify that the file has not been changed (this can be useful for more than auditing and security - for example, it’s particulalry useful for caching, which is exactly why Rails’ asset pipeline calculates hash digests for asset files...

ActiveRecord: Mark all records of type as read only

Note: This post is created based on this Stackoverflow Answer. Occasionally, you may have a type of record that already exists in a database which you wish to read from, but never modify. In these situations, it can be worth adding a little code to mark these types of records as read-only. This will prevent most ActiveRecord data modification methods...

I'm Josh McArthur, a web developer specializing in Ruby on Rails and Phoenix. I contribute code on GitHub and write blog posts about technology and tramping here.