RSSAmplifier

Blog

HYBRD Posts

hybrd.coRSS feed ↗35 posts

Latest posts

Electronic Signatures with Rails

I was recently exploring electronic signature solutions for a client project and found a nice approach using Stimulus, Active Storage, and Signature Pad . I investigated several Javascript signature libraries and found Signature Pad to have the smoothest looking signatures, with a solid API. My goal was to wrap a good signature library in a Stimulus controller, and use a Rails form and Active…

Upgrading a Rails App to Tailwind v4

Last week, I upgraded a client’s Rails app to Tailwind v4. I rarely delay upgrading dependencies, but after briefly looking through the release notes on Tailwind v4 when it first came out, I decided I would hold off for a bit on most of my projects. (I also tend to wait a bit on upgrading to a major release to until a few patch releases have come out.) For this modest-sized Rails app, the upgrade…

Stimulus Autocomplete with Combobox Navigation

I’ve been slowly migrating a client’s Rails app from jQuery and UJS to Hotwire in between feature work. One of the last remaining pieces has been replacing jQuery UI Autocomplete , a pretty common library to see in web applications of a certain age. A datalist Digression For this particular use-case, the list of autocomplete values was available in the rendered view and didn’t require a server…

Adding Meta Tags to a Rails CMS with Polymorphism

If you’ve been a web developer for a while and you’re like me, you might have built CMS functionality into a Rails app…or two. “At least” https://t.co/v4ixlT3EIj — Jeremy Smith (@jeremysmithco) March 7, 2022 It’s popular these days to separate your marketing site from your web application, but there are still times when it’s useful to build CMS features directly into your app. For example: If your…

Time Tracking for Freelance Developers

Time tracking is terribly mundane. But if you charge by the hour, or need to account for how you spend your work day, it’s absolutely essential. One of the minor blessings of full-time, on-site employment is that you are considered “working” as soon as you walk into the building. Your presence makes it count, even if you read a blog post with your first cup of coffee, take a personal call, or chat…

Audit Logging in Rails

In simple, CRUD -only Rails applications, the records in your database represent the current state of your system, but don’t tell you much about it’s history. You can see when a record was created, and when it was last updated, but you have no way of knowing how many times it was updated, or by whom, or what changes were made. You also don’t know what records were deleted, unless you check for…

Full Text Search in Postgres with View-Backed Models in Rails

I often work on projects where, in the near-term, experimentation and rapid iteration are more important than stability and performance. This is often the case for small organizations, startups, prototypes, and even side projects. When time is of the essence, it can be difficult to know what corners to cut and what choices will come back to haunt you. Over the years, I’ve come to believe that…

GitHub Issue-style File Uploader Using Stimulus and Active Storage

I love GitHub’s Markdown editor for Issues and PRs. I guess that’s a good thing, since I spend so much time writing in it. One of the things I love most is the ability to drag-and-drop or copy-and-paste files and have appropriate Markdown embedded automatically. I decided I would try to reproduce this functionality using Stimulus and Active Storage for the latest iteration of my blog. I’ll share…

Migrating from Webpacker to the New Bundling Approach

You may have heard that Rails 7 will have three great answers to Javascript in 2021 . Well, you don’t have to wait until Rails 7 is released to switch from webpacker to the new, simpler bundling approach, using jsbundling-rails and cssbundling-rails . If you’re on Webpacker today, it’s a very modest jump to switch to one of the bundlers made available through the jsbundling-rails gem. You don’t…

Client Collaboration

Professional web design is always collaborative. Even if you are the sole designer or developer on a project, you are in collaboration with your client. What you create together will always be constrained in some ways by your client, as it will also be constrained by you. I fought this early in my career. I wanted to receive a set of objectives, and then disappear during the creative process, only…

Managing Sidekiq Processes with Upstart

I normally use resque for background jobs in Rails (with capistrano-resque for managing resque processes). But I decided to switch to Sidekiq on a new application. I spent some time familiarizing myself with the best practices and came across this post by Mike Perham, Sidekiq’s author. The best and most reliable way to manage multiple Sidekiq processes is with Upstart. Many developers know little…

Better SQL in Ruby with the sql_query gem

I’ve been writing a lot of SQL lately, as evidenced by my last couple posts . I was working on a particularly long query the other day when I thought, “it would be nice I could write SQL using partials to manage duplicated logic.” One quick Google search confirmed I was not the only one with that idea. The sql_query Ruby gem lets you write and load SQL queries using ERB templates. I had a large…

Returning The Greater of Two Date Fields, Where Either Could Be NULL

In keeping with SQL tricks I wrote about a few days ago , here’s another technique that I’ve found useful recently. Let’s say you want to return the greater of two date fields in your result set. If you know that both fields will contain a date, then you can simply use the GREATEST function . GREATEST will return the largest of your argument. However, if either field could be NULL , then GREATEST…

Handy SQL Functions For Self-Referencing Tables

I have a client that uses NetSuite , a cloud-based CRM/ERP system. NetSuite records customer information in a datbase table called (surprise!) customers . The table is self-referencing, so you can have customers with many subcustomers by setting the parent_id on the subcustomer records to the customer_id on the customer record. When we originally set up my client’s usage of the system, we decided…

Five Types of Internal Documentation Needed for Software Teams

As I see it, there are five types of internal documentation needed for software teams. (Often, a software team will produce external documentation for their end users, in the form of a help section, user guides, or a searchable knowledge base. But the following are categories of documentation I see as being necessary internally, for the team itself.) 1. Internal Application Documentation This type…

Using the Dentaku Gem to Manage Business Policies Outside of Your Codebase

I was reading The Pragmatic Programmer recently, and was struck by something I hadn’t considered before. Be sure not to confuse requirements that are fixed, inviolate laws with those that are merely policies that might change with a new management regime. That’s why we use the term semantic invariants—it must be central to the very meaning of a thing, and not subject to the whims of policy (which…

Making Static Sites Dynamic...Somewhat

I’m kind of in love with static site generators ( Middleman in particular). For marketing and informational sites that don’t change very often, I get many of the benefits of my normal Rails framework development and deployment toolset, with a simpler, faster, and more secure final product. (It’s just hard to beat static HTML.) But there some circumstances where a static Middleman site can be…

A Reusable SJR Approach in Rails

I was recently working on a Rails client project where certain pages needed to display various record types and each collection of records needed to be listed on the page, and needed to be editable in-place. So for example, let’s say a company had associated contacts, products, and locations. On the company page, each set of associated records needed to be listed on the page and needed to be…

Managing Redirects in Middleman

If you need to maintain redirects for old pages in a Middleman site, you can do so by adding something like this to your config file: redirect "/my/old-page.html" , to: "/my/new-page/" If you’ve only got a few redirects, this isn’t too bad. But if you have dozens or hundreds, all those redirect calls makes reading your config much more difficult. Instead, try this. Create a data file for your…

Table of Contents Helper for Markdown in Middleman

For pages on your site with a lot of content, it can be nice to have sidebar navigation with a Table of Contents for the page. If you’re using Middleman with Markdown content, here’s a way you can do that. 1. Use the redcarpet gem to render Markdown for your project. Add the following to your Gemfile and then bundle install : gem 'redcarpet' Redcarpet provides a special renderer ,…

DRYing up Nginx configurations

I have a web server that hosts quite a few Middleman sites via Nginx. Since the sites are very similar, their Nginx configuration is largely the same. I recently moved common configurations into a separate include file, which has made it easier to understand the differences between site configuration files, and has simplified making changes that should apply across all sites. Below is an example…

Poor man’s inbound email processing in Rails with the Mail gem and Gmail

Most Rails developers seem to be using transactional email services to handle their inbound email processing needs these days. See services like Postmark , SendGrid , and Mandrill , which can all send webhook requests to your application whenever an email is received. But what if you’re on a limited budget, or can’t use one of these services for some other reason? Here’s a simple approach using…

Custom currency input for Simple Form

Today, I was moving a standard Rails form to Simple Form and realized that one of the form inputs is actually a Bootstrap input group , with an add-on at the beginning of the input field with the proper currency symbol. There’s no built-in way to replicate this with Simple Form, but the documentation suggests building a currency Custom Input to show the currency symbol. In my case, I wanted to be…

How to mark optional form fields with Simple Form

Simple Form is a powerful gem for creating forms in Rails. For instance, Simple Form can look at your form object and display fields as “required” if they have :presence validations on them, rather than you hard-coding that in the markup. By default, Simple Form will prepend required field labels with an asterisk. You can change the asterisk to something else (such as writing out the word…

Auto-generate navigation from page headers in Middleman

I’m working on a Middleman site right now that has one long page of content with a sidebar of navigation that links to the headers and sub-headers found on the page. ( Underscore.js is a good example of this.) This is a common style for technical documentation sites these days. I didn’t want to have to maintain the navigation content by hand. And since the navigation is meant to exactly reflect…

A simple interface for the controller context in your Rails app

Oftentimes in a Rails app, you namespace your controllers based on the context. Admin:: will namespace all your admin controllers, Members:: may namespace all the controllers in your members-only area. Have you ever needed to know the namespace context in your layout? Here’s a simple interface you can use: First, drop the following Namespace class into your lib folder. It’s going to determine the…

How to add High Voltage static pages to your sitemap file

High Voltage is a fantastic gem for adding static pages to your Rails app. I’m using it extensively on a project that has a large number of static, style-heavy pages that don’t make sense to serve from the database. The other day my client asked me to add a sitemap XML file to the app that Google could consume (this is something that you can set up under Google Webmaster Tools that is supposed to…

Active Record enum form select

I’ve been enjoying the new Active Record enums in Rails 4.1. But I haven’t found a nice way to represent the enum options in a form select. So, I wrote a couple small helper methods that I put in application_helper.rb: def options_from_enum_for_select ( instance , enum ) options_for_select ( enum_collection ( instance , enum ), instance . send ( enum )) end def enum_collection ( instance , enum )…

Get a word count from your Rails app views

I needed a way to calculate the number of words in a Rails application’s views, to estimate the cost of translating the content to another language. Pass this script the path to your views directory (or a subdirectory) to get a word count. #!/usr/bin/env ruby require "action_view" require "fileutils" class WordCounter include ActionView :: Helpers :: SanitizeHelper def initialize (…

A Tumblr Theme Development Workflow

This site has been running on Tumblr for a while now and, while I love using Tumblr for posting content, I’ve found theme development to be frustrating and tedious, particularly with trying to manage asset files. I’m used to single-command site deployment, so going back to copy-and-paste and manual file uploads is painful. I thought there must be a better way, so I Googled around and came across…

Simple Site-wide Announcements in Rails

I recently needed the ability to post site-wide announcements on a multi-tenant Rails application I work on, to announce scheduled downtime. Most solutions I’ve seen call for the creation of an announcements model and database table. It seemed a bit much to be querying an announcements table with every page request, when announcements would only be used occassionally. Thankfully, in my case, I…

Devise & CAS, using devise_cas_authenticatable and CASino

I work on a multi-tenant Rails application that serves four different sites (a.com, b.com, c.com, d.com) from a single instance. The user accounts are not constrained to a single site, so a user can log in to any one of the sites with the same credentials. The business owners asked me to find a way to allow a user who has logged into any one domain (e.g. a.com) to be able to visit any of the…

Rails AJAX Forms & Geocoding with Google

If you’ve done geocoding using Google’s APIs lately, you know that the best practice nowadays is to geocode on the client side and pass your latitude/longitude coordinates to the server, rather than having the server make the geocode request. Google caps the number of geocode requests based on IP to (I think) 1500 per day. This is fine on the client side for many use cases, but would quickly…

Setting up multiple instances of Redis on a Mac

I’m using Redis on a project and couldn’t find a good guide for setting up multiple instances on a Mac. So, I decided to write up how I did it. (You might want this if you need one instance for development and a separate instance for running your test suite.) 1. Install Homebrew (great package manager for OS X) 2. Install Redis package with Homebrew brew install redis 3. Create separate config…

Pow Awesomeness for PHP & Static Sites

I recently started using Pow for my Rails apps in development. Pow is a “zero-config Rack server for Mac OS X” that makes it a cinch to access your Rails apps by individual host name and without having to futz with /etc/hosts or funky ports. Just create a quick symlink to your apps’ directory and then go to appname.dev in your browser! Problem is, if you’re using Pow, you’re going to have trouble…