I am a Sr. Software Developer at Oracle Cloud. The opinions expressed here are my own and not necessarily those of my employer.
Rake tasks vs. Ruby classes
When several years ago I started working with Ruby on Rails I liked Rake tasks. To me they were a great step up from ad hoc bash and SQL scripts. It was a way to build powerful CLIs to do basis sysadmin tasks, generate ad hoc reports, upload/download files, etc.
Rake tasks
With Rake we can create multiple .rake
files and group tasks into different namespaces. We also can use multiple namespaces w/in same .rake
file but it feels cleaner to separate them. We can even pass parameters and do exception handling. Tasks can be executed via cron to run periodic processes and can call other tasks.
Ruby classes
But with more complex business logic it is harder to fit it into .rake
files. That’s when it is usually better to use POROs. They can be tested via rspec or minitest. They can be executed from UI via controllers or via ActiveJob. Or even called from Rake tasks. And we can run Ruby classes them via rails r MyClass.new(params).perform
when needed.
Both approaches have their place. I still use Rake for various server configuration and system maintenance tasks (for example, uploading config files to production servers). But when it comes to complex business logic I put that in Ruby service objects.