samedi 30 avril 2016

Rake tasks sequentially calling multiple class methods - Rails 4, Design

I have a rake task called pull_orders which calls methods of a RemoteDbConnector class to do things like establish a connection to an external db, generate raw SQL queries, execute queries and store records in local db.

While trying to test it, I stumbled upon this answer which got me thinking whether my design is flawed.

Should rake tasks really be one liners? If so, where should put all these method calls, given that they need to be called in a particular sequence?

My task looks like this:

namespace :db do
  desc 'finds and populates data from remote db'
  task pull_orders: :environment do
    ...
    columns = ...
    table = ...
    options = ...
    column_mappings = ...
    RemoteDbConnector.generate_query(...)
    RemoteDbConnector.execute_query(...)
    RemoteDbConnector.map_column_names(...)
    Order.create(...) #creates records based on hash generated by RemoteDbConnector
    ...

  end
end

Aucun commentaire:

Enregistrer un commentaire