Skip to content

Instantly share code, notes, and snippets.

@darrencauthon
Created September 29, 2014 18:24
Show Gist options
  • Save darrencauthon/adac5bb79396c27ec18b to your computer and use it in GitHub Desktop.
Save darrencauthon/adac5bb79396c27ec18b to your computer and use it in GitHub Desktop.
Using a base class to simplify workers that subscribe to an event
class OldWorker
include Sidekiq::Worker
def perform user_event_id
user_event = UserEvent.find user_event_id
user = User.find user_event.user_id
user_application = user.find_application user_event.application_id
# do something
end
end
class UserEventSubscriber
include Sidekiq::Worker
def perform user_event_id
@user_event_id = user_event_id
execute
end
def user_event
@user_event ||= UserEvent.find @user_event_id
end
def user
@user ||= User.find user_event.user_id
end
def user_application
@user_application ||= user.find_application user_event.application_id
end
end
class NewWorker < UserEventSubscriber
def execute
#do something, the values are set up for me
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment