Skip to content

Instantly share code, notes, and snippets.

@fcsonline
Created January 24, 2019 08:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fcsonline/61cd1bd3aaacfe946dc27707ee4e0600 to your computer and use it in GitHub Desktop.
Save fcsonline/61cd1bd3aaacfe946dc27707ee4e0600 to your computer and use it in GitHub Desktop.
Events / Commands / Callbacks / Observers / Webhooks
module ApiCore
module TeamInteractor
class Create
attr_accessor(
:team,
:team_params,
:team_metadata_params,
:author
)
def initialize(author:, team_params:, team_metadata_params:)
@author = author
@team_params = team_params.to_h
@team_metadata_params = team_metadata_params.to_h
@team = Team.new(team_params)
end
def call
Result.wrap do
# Quiz 4
team.after_commit_once do
track_metrics!
notify_admins!
notify_coworkers
end
team.save!
# Quiz 5
ApiCore::TeamInteractor::Metadata::Create.new(team_metadata_params).call
ActiveSupport::Notifications.instrument 'team.creation' do
team
end
team
end
end
private
def track_metrics!
# Quiz 1
Metrics.track(
author: author,
event: 'Created team',
properties: {
target: team.name,
target_id: team.id
}
)
end
def notify_admins!
# Quiz 2
admins.each do |access|
Notifications::EmployeeNotifier.employee_update(access, team).deliver_later!
end
end
def notify_coworkers!
# Quiz 3
RescuedIterator.iterate(coworkers) do |access|
Notifications::EmployeeNotifier.employee_update(access, team).deliver_later!
end
end
end
end
end
# Another class
class AnotherModule
ActiveSupport::Notifications.subscribe "team.creation" do |args|
# Quiz 6
puts args
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment