How I can do something like that at sidekiq?
Gateway::AddUser.delay.new(6).call
For now, Gateway::AddUser.delay.new(6) return a string, and call method trying to run on it. But I want to call just Gateway::AddUser.new(6).call delayed
Solved just like this:
Gateway::AddUser.delay.perform(6)
Where perform method is:
def self.perform(params)
new(params).call
end
Just rewrite 2 methods to 1 =)
why don't you just wrap that in other method?
class User
def self.add_user_via_gateway(attributes)
Gateway::AddUser.new(attributes).call
end
end
User.delay.add_user_via_gatway(attributes)
EDIT:
If you prefer, you can also create a worker class.
class AddUserViaGatewayWorker
include Sidekiq::Worker
def perform(attributes)
Gateway::AddUser.new(attributes).call
end
end
AddUserViaGatewayWorker.perform_async(attributes)
Related
Lets say I have a method in my model
class Mod < ...
after_create :update_some_stuff
private
def update_some_stuff
....
end
end
And I want to ensure that "update some stuff" is only called by after create, and to raise an error if it is called in any other context. Is there a way to do this in Ruby on Rails?
maybe you can use a block and avoid doing weird tricks to prevent the method getting called outside the context you want:
after_create do
....
end
It's hacky, and you probably don't need to be testing this because Rails tests this for you, but this should work:
class Mod < ...
after_create :update_some_stuff
private
def update_some_stuff
return unless id_previously_changed?
# do something
end
end
This hooks into previous_changes which is implemented by ActiveModel::Dirty.
So, I'm using 'state_machine' gem in my app for making Lead model a state machine. And I wrote a lot of custom methods on transitions in the LeadObserver like below.
class LeadObserver < ActiveRecord::Observer
def after_review
end
def after_convert
end
def after_mark_invalid
end
end
And there're around 15 methods like these.
Now, I have a class method, say Seller.do_something which has to be called at the end of all these 15 methods. Is there an elegant Rails way to call this Seller.do_something method at the end of every method like after_filter for controller. Thanks. :)
i think you can but its not great to use.
ActiveModell::Callbacks is what you are looking for.
http://api.rubyonrails.org/classes/ActiveModel/Callbacks.html
state_machine itself does provide an after_transition - maybe thats helping you?
Maybe it's not wise question... but I have a one.
I was playing with Ruby and tried to create methods with a dynamic names in a loop, like this:
class Test
.....
class methods
.....
for i in 1..100
def method_#{i}
my_hash[:test].first[i]
end
end
end
I noticed that's impossible, so... Is there any solution using a :define_method, or :send to solve my problem and gets methods like:
method_0, method_1, method_2 etc. which return my_hash[:test].first[1], my_hash[:test].first[2] etc. ?
You can do it using define_method. Here is the code:
class Test
.....
class methods
.....
1.upto(100) do |num|
define_method("method_#{num}") do
my_hash[:test].first[num]
end
end
end
After make a big class with many method, I want all of these method be called in a delayed jobs.
But the practice of the Delayed::job, is that you have to create a class with a perform method, like that :
class Me < Struct.new(:something)
def perform
puts "GO"
end
end
and call it like :
Delayed::Job.enqueue Me.new(1)
But the problem is that my class as already many method like this type
class NameHandler
def self.init
ap "TODO : Delayed jobs "
end
def self.action_one
...
end
def self.action_two
...
end
etc.
end
and I want to call it like :
Delayed::Job.enqueue NameHandler.action_one params...
Theres is an best practice for that ? Or I have to follow the classic Delayed::job way and lose many times ?
In the README it has a number of ways:
Me.new.delay.action_one
or
class NameHandler
handle_asynchronously :action_one
def action_one
end
def self.action_one
new.action_one
end
end
NameHandler.action_one
In order to start delayed_job's on a schedule you need to have helper classes with a perform method that delayed_job can call. These need to be defined before any of the classes that use them to create scheduled delayed_jobs are called. All very short, and many of them in my case. For example:
class AccountUpdateJob < Struct.new(:account_id)
def perform
acct = Account.find(account_id)
acct.api_update
end
end
I'm doing this in a file called "dj_helper_classes" in the initializers folder. Is that the right thing to do?
I keep mine in lib/jobs, one file per class. So, your example would be in lib/jobs/account_update_job.rb
module Jobs
class AccountUpdateJob < Struct.new(:account_id)
def perform
acct = Account.find(account_id)
acct.api_update
end
end
end