Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DRY in ActiveJob::Base

I need to use sanitization in Jobs, so i wrote method

  def sanitized_sql_statement(query)
    ActiveRecord::Base.send(
        :sanitize_sql_array,
        query
    )
  end

I have 2 different job files. Where I should place this method and how to use it from Jobs?

like image 481
Max Paprikas Avatar asked Feb 10 '16 02:02

Max Paprikas


People also ask

What is Activejob?

Active Job is a framework for declaring jobs and making them run on a variety of queueing backends. These jobs can be everything from regularly scheduled clean-ups, to billing charges, to mailings. Anything that can be chopped up into small units of work and run in parallel, really.

How do I enqueue a job in rails?

Enqueue jobs The different enqueuing options are the following: wait : With this, the job is enqueued after the mentioned time period. wait_until : With this, the job is enqueued after that the specified time has elapsed. queue : With this, the job is added to the specified queue.

What are workers in rails?

Often in the 'Rails world' a worker is referring to a priority queue type system that has separate processes popping off tasks to execute outside the application cycle. One of the most popular of these systems is DelayedJobs.

What is background job rails?

In the background the server can process all background jobs one by one. Rails provides Active Job to process background jobs and making them run on a variety of queueing applications.


1 Answers

Use concern.
create concerns directory under jobs directory app/jobs/concerns like models and controllers do.

write concern file.

module SqlSanitizer
  extend ActiveSupport::Concern

  def sanitized_sql_statement(query)
    ActiveRecord::Base.send(
      :sanitize_sql_array,
      query
    )
  end
end

And include in your job

class YourJob < ActiveJob::Base
  include SqlSanitizer

  # ... do something
end

If you fail to auto load SqlSanitizer, add autoload path in config/application.rb file => config.autoload_paths << "#{Rails.root}/app/jobs/concerns"

like image 67
Jaehyun Shin Avatar answered Sep 28 '22 11:09

Jaehyun Shin