Rubocop for Sorting Models (Upgrading a gem!)

Kenneth Teh
3 min readAug 31, 2020

--

Was starting a new project today, and wanted to make sure I had my models sorted properly right from the start. I’m already a big fan of Rubocop, and one of the “plugins” I discovered was https://github.com/pocke/rubocop-rails-order_model_declarative_methods. Direct quote: “Sort declarative methods of Rails model, as an extension to Rubocop.”

The problem is that the gem hasn’t been updated for 5 years, and the dependency is on rubocop 0.37.2. Ouch! A world of change has happened since then and the current release is 0.89.1. Decided I would upgrade the gem myself!

Step 1: Fork the repo

Simply click “Fork” on the Github repository page.

Step 2: Clone the fork into your local system

git clone git@github...

Step 3: Update the gemspec

Gems don’t use Gemfile s like your regular projects — they use *.gemspec files. Just like your Gemfile, there are development and runtime (think production) dependencies. In this case, I had to update the runtime dependency for Rubocop, i.e. point to 0.70 instead of 0.37.2.

bundle install

Step 4: Run the gem locally (in the project where you wanted to use the gem)

Update your Gemfile to use the local gem — point to the gem project directory.

gem 'rubocop-rails-order_model_declarative_methods', path: '~/workspace/rubocop-rails-order_model_declarative_methods'

Step 5: Hope it works

rubocop --auto-correct -d app/models/user.rb

This obviously didn’t work.

Step 6: Update the code based on the rubocop upgrade guide

Dug around in the codebase and found this!

Step 7: Update the specs

Just as the original gem creator did, I copied the (updated) spec support files from the rubocop-rspec repo. Then, I followed the new spec patterns to rewrite the spec for this cop.

Step 8: Make a pull request

Step 9: Wait for the gem owner to approve the request

https://github.com/pocke/rubocop-rails-order_model_declarative_methods/pull/11

In the meantime, use my version of the gem in my project Gemfile :

gem 'rubocop-rails-order_model_declarative_methods', git: 'https://github.com/kennethteh90/rubocop-rails-order_model_declarative_methods.git'

--

--

Kenneth Teh
Kenneth Teh

Written by Kenneth Teh

Software Engineer primarily working with Rails and Vue.JS... sometimes DevOps and shell stuff too

Responses (1)