A practical guide for new Rails 5.2 projects
I love learning how others work. Hoping to glean some wisdom or insight that could help me sharpen my skills, I’m constantly reevaluating what works best. Regardless of the nature of the rails application, I’ve found the following workflow to be quite helpful. Having built my personal site on Rail 5.2, I can confirm that each gem still works and this workflow continues to be relevant.
Preliminary Notes and Assumptions
Order matters. Installing each gem in the following order optimizes for their generator workflow. A few of the gems build nicely on top of one another. Reorder at your own discomfort.
RailsCasts are old, but helpful. It’s been years since Ryan Bates published a new RailsCast or updated an old one, but the content is still helpful. As long as you can fill in the cracks between rails versions, it’s Rails gold.
This is not an environment setup guide. This workflow assumes you already have appropriate Ruby and Rails versions installed on your machine. If you’re looking for a setup manual, this is not it. Google ‘setup ruby on rails’ for help.
Run ‘rails new app_name -T -d postgresql’
Beginning with the end in mind, setting a postgresql database over the Rails default will make Heroku deployments a lot simpler.
Make sure to swap ‘app_name’ for your actual app name. I understand this may seem obvious, but if you’re a copy-paste-madman this may seem less obvious.
rails new app_name -T -d postgresql
I waited too long to throw my weight behind writing tests. If you’re not there yet either, I’m not going to use this time to convince you: skip to ‘install haml’.
If you are interested in exploring or are a TDD junky, rspec is a great place to start.
If rspec, then use factory_bot too. Formerly known as factory_girl, factory_bot allows for the simple setup of Ruby objects for testing. Building baseline information for each object, writing tests becomes a matter of manipulating those objects, not rewriting from the ground up.
You can learn more about HAML, but suffice it to say your code starts to look more like poetry. HAML is shorthand, indentation based markup that consolidates and expedites the code you write.
HTML to Haml Converter: http://htmltohaml.com/
Simple Form extends Rails ‘form_for’ helper and adds a level of sophistication. Combined with HAML, Simple Forms keeps form lean and limits the useless repetition.
Disclaimer: Installing Simple Form after haml-rails will ensure appropriate scaffolds are also built. Failing to do so will cause the generator to continue to build forms without the ‘simple_form_for’ helper.
Friendly Id allows for clean, readable urls. With the added benefits of obscuring an object’s ID and aligning more closely with Google keywords, friendly_id is a simple, powerful tool.
Devise is a flexible authentication solution that is consistently updated and battle tested. With a vibrant community and extensions like devise_inviteable and oauth, I have never been presented with a compelling enough reason to use anything else.
You could build your own airplane to get to California, or you could just book a flight on Delta.
Disclaimer: Follow their documentation and the command line feedback after install. If you skip some steps on setup, you could experience from frustration upon deployment.
Update generator preferences
Rails generators are great, but sometimes they need to be scaled back a bit. A quick update to the config/application.rb can cut down on the clutter and reduce the amount of code cleanup required.
# Configure Generators
config.generators do | g |
Analytics for Rails. Admittedly, I discovered this tool while building out a recent Rails 5.2 project. Unlike the other gems in the list, Ahoy is a new tool that isn’t fully vetted. Having used a number of other tools over the years, Ahoy immediately overcame my largest objections to those tools.
I’m currently working through some pains around querying with Ahoy, but other than that, it has been pretty great.
After you’ve run through the workflow above, it’s time to start building your custom software. From here, with each new migration or generate command, the initial heavy lifting should be done.
If you have thoughts, hit me up on Twitter: @morganjlopes
If you have code related questions, tap me on stack overflow: @morganjlopes