rick
rick

Reputation: 463

share code between a rails helper and a background worker

I have code in a rails helper that is being used in a view and i also have the same code in a background worker class.

How do i extract this code out into its own class or module to use for both the helper and background class?

Please can any one help.

Upvotes: 1

Views: 1284

Answers (1)

lyricat
lyricat

Reputation: 1996

To me, code that needs to be shared between background tasks and rails goes logically in lib/my_library.rb. Just require 'my_library' in your controller and job files.

lib/my_library.rb:

class MyLibrary
    def self.do_something(foo)
    end
end

In app/jobs/my_job.rb:

require 'my_library'

# ...
MyLibrary.do_something( "x" )

Upvotes: 2

Related Questions