Easily switch or experiment with different databases in Rails

I just learned about rake db:system:change task that Rails provides, to speed up switching between different database engines. If you’re experimenting with something, or benchmarking things, this is super useful.

I had a simple PostgreSQL app and running rake db:system:change --to=sqlite3 and afterwards rake db:setup got me going with my task in seconds.

Small detail I noticed, rake -T which should list all available tasks, does not show anything about db:system for some reason.

Starting new Rails applications

It’s been a while since I reviewed the documentation for rails new console command. There are a few useful features that I didn’t know about:

Most of the times, for database engine, PostgreSQL is my choice. This is easily accomplished with the -d switch, but even more convenient is adding this as a preset to your environment, using the .railsrc dotfile. Configuring such on your system will allow you to just stick to rails new appname without having to add switches.

The second useful thing I didn’t know exists is the Rails Application Templates which makes it faster to start new Rails applications with the same set of gems and configuration that you might be reusing across your apps.

Speeding up office work with Ruby and IRB

I was just paying taxes 😅. One of the steps involved was copying and pasting a few long reference numbers from a PDF into my banking app. The thing is, when copied, the reference number would contain extra spaces, after each character, making it invalid where I needed to paste it.

Now there are myriad of different solutions for this I’m sure, but as a programmer, let’s take a look at how to speedily create a makeshift tool in Ruby to do this.

irb
space_eater = ->(text) { text.gsub(' ', '') }

We launched into the interactive shell, and defined a lambda we can call on strings we want to sanitize. Like so:

space_eater.call('2 3 9 2 9 9 A 8 2 8 0 0 0 1')
=> "239299A8280001"

And we’re done. ❤️ Ruby.

Who is this feature for?

In software development, when thinking about and designing new features, or trying to solve existing issues, it’s important to, early, ask the question: Who is this feature for?

That applies to writing tools intended for your developer colleagues, web application features for customers or anything in between.

I found, that the result only tends to be good, if the feature or solution was actually designed for a specific person, doing specific task and this solution allows them to improve the way they do this task considerably.

For example when deciding for a Content Management System for a website, developers often jump to first evaluating various offerings out there, based on features, APIs and supported languages. Instead of first identifying the person that is going to be working with such a system and establishing what their workflow is going to be. What’s important for them, what’s not important and only based on that then look for a solution, or build a custom one.

Tools that are build for a very specific need and for a real person that can tell you, this helps, or not; tend to end up being simpler, more pleasurable to use, last longer and require less maintenance and generally tend to achieve the goal set out for them at the beginning.

Don’t forget about X-Forwarded-Host header

Recently I was working on a Rails application on Heroku living behind a reverse proxy. This application serves requests coming to a specific folder on the target domain. For it to correctly generate full URLs, you have to somehow tell this app the hostname you want it to use. In Rails, you can configure a hostname in the environment config file, but that’s a static value, which has to be maintained and changed per environment. Also it does not work well if you want to access the application from multiple domains.

It’s much better to be able to set something up on the proxy itself. For this reason the X-Forwarded-Host HTTP header exists. Rails–being a good web citizen–supports it out of the box.

Before I learned about this header, I even implemented my own middleware to deal with this issue and a custom header. I was able to dump that extra code once I stumbled at this header.

Web technology apps taking over?

It’s interesting to see that my 3 most used desktop apps, during my day to day computer use, are all web technology based:

  • Atom – My code/text editor.
  • Slack – Communication with my team.
  • Chrome – of course

The last time I was working on a desktop app, I used NW.js, which is a platform for building desktop apps using web technologies.

From my perspective, it’s no coincidence. HTML, CSS and JS are great tools for building lots of types of apps. I like this trend.

Principal – Integrated Business Management

Integrated business management web app for a sales company with CRM features. The functionality this Rails app provides is extensive:

  • Management of distributor accounts across multiple countries.
  • Capture, edit and build lead portfolio.
  • Logging of phone calls, appointments and notes.
  • Management of orders.
  • Invoicing.
  • Integrated credit card payments (Stripe, MyGate).
  • Management of stock and its reordering.
  • Payouts and commissions.
  • Client support pages.
  • Product download, activation and update facilities.

For me, the most important part of this project is the extensive test suite that was developed from day one, which allows for all the integrated functionality to coexist in one, easy to manage and extend codebase; and rapid development and deployment of new features.

UI Tour

Leads overview.
Leads overview. Leads can be added, edited, commented on, moved between different statutes, filtered and searched. (Note: these screenshots do not contain any real client information. This data is randomly generated for the purpose of this presentation.)

Logging and appointment.
Logging an appointment.

Logging a phone call with a lead. (Note: these screenshots do not contain any real client information. This data is randomly generated for the purpose of this presentation.)
Logging a call for a lead.

Captured order.
Captured order.

Excerpt from a stock management screen.
Excerpt from a stock management screen.

On-site credit card payment.
On-site credit card payment.

Rails stats

Lines of code, test coverage & other project stats.
Lines of code, test coverage & other project stats. Almost 14 000 lines of code – this is one of the biggest projects I’ve created, developed and worked with in production. Despite the relatively large amount of code, I am extremely happy with how Rails handles a decent sized project like this, all in one integrated, business specific and complete codebase.

Educational Software

VB is a desktop educational software for Microsoft Windows and Mac OS X. It’s build using web technologies—powered by the awesome nw.js platform. I was responsible for the whole project from start to finish, from the initial brainstorming and design of the functionality with the client, to the choice of technologies, design of the user interface and programming implementation of the functionality.

VB runs on OS X and Windows, utilizing the same codebase, which is a Backbone.js backed client side application running within the browser (nw.js). I’ve build a custom ORM layer for the build in sqlite database (also in CoffeeScript) which plugs into Backbone.sync. Custom database migration toolset modeled after Rails migrations is also included.

The design of the whole application is strongly influenced by Rails code structure: everything is neatly compartmentalized into models, collections and templates directories. The whole thing is compiled and packaged for production utilizing Grunt as well as Ruby for preparing ISO images and such.

The app includes an extensive test suite covering integration tests (full user flows through the application) as well as isolated model and Backbone View tests. The test suite is powered by the fantastic QUnit.

Tour

VB Login Screen
VB supports multiple user accounts, user and administrator roles and an out-of-the-box guest account.

Home screen users see after login. It provides access to resume where the user left off and other quick links.
Home screen users see after login. It provides access to resume where the user left off and other quick links.

VB includes a build in English dictionary
VB includes a build in English dictionary.

Users can add new content utilizing a word list editor.
Users can add new content utilizing a word list editor.

Visual diffs on incorrect recognition of a word.
Visual diffs on incorrect recognition of a word.

Easy update to the latest version from the app itself.
Easy update to the latest version from the app itself.

Detailed reports of user progress are available to both the current user as well as administrators.
Detailed reports of user progress are available to both the current user as well as administrators.

Test suite that covers the whole app. Development was done using a mixture of test driven development as well as regression testing, depending on the particular area of code.
Test suite that covers the whole app. Development was done using a mixture of test driven development as well as regression testing, depending on the particular area of code.

Code structure heavily influenced by Rails. Don't be fooled though, this is 100% client side, CoffeeScript, HTML & SASS.
Code structure heavily influenced by Rails. Don’t be fooled though, this is 100% client side, CoffeeScript, HTML & SASS.

curl request and return headers only

The UNIX command line tools is something that just keeps giving. Within web development I often find myself wanting to quickly debug a URL, see whether it’s alive or what the response is. Often I do not want to download the whole content (a large file for example). Before I learned the following, I would use Chromes Developer Tools. That is until I learned how to do it more efficiently and quicker with good old curl:

curl -I https://klevo.sk

Which returns something like:

HTTP/1.1 200 OK
Server: cloudflare-nginx
Date: Sat, 27 Jun 2015 17:27:17 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive

It’s especially handy when setting up and testing temporary or permanent HTTP redirects. Doing that in a browser can be cumbersome due to caching.

Faster SSH workflow with multiplexing

I was reading The Art of Command Line (great stuff) and tried the SSH configuration tips. With the below config I noticed considerable speedup in various SSH and Git related workflows. My ~/.ssh/config now includes:

Host *
  TCPKeepAlive=yes
  ServerAliveInterval=15
  ServerAliveCountMax=6
  Compression=yes
  ControlMaster auto
  ControlPath /tmp/%[email protected]%h:%p
  ControlPersist yes

Speed improvements I noticed:

  • I push my code to the remote often. Thanks to the keep alive options, the connection is kept open and subsequent pushes do not incur the penalty of establishing a new connections.
  • The same applies to server provisioning and maintenance. Once the initial connection is established, it is kept alive and sessions opened in new terminal tab or window begin instantly.

More on this topic with in depth explanations: