Skip to content

Instantly share code, notes, and snippets.

@joegaudet
Forked from jilucev/bad_state.rb
Last active August 24, 2018 21:51
Show Gist options
  • Save joegaudet/dc1ad7ca8c5985206989d87919a6b479 to your computer and use it in GitHub Desktop.
Save joegaudet/dc1ad7ca8c5985206989d87919a6b479 to your computer and use it in GitHub Desktop.
Enema of the state
# frozen_string_literal: true
module Hodor
class MainService
def initialize(
foo_klass: Foo
bar_service: Hodor::BarService.new,
baz_service: Hodor::BazService.new,
qux_service: Hodor::QuxService.new
)
@foo_klass = foo_klass
#initialize dependencies
@bar_service = bar_service
@baz_service = baz_service
@qux_service = qux_service
end
def call(foo_id, status_name)
@foo = foo_klass.find(foo_id)
@foo_status = foo.statuses.where(name: status_name)
#call each dependent service
# This service updates all bar records with foo_status to have a different status
@bar_service.(foo_id, foo_status)
# This service erases foo_status from bar's history (join records)
@baz_service.(foo_id, foo_status)
# This service erases foo_status from foo (destroys statuses that were associated with foo)
@qux_service.(foo_id, foo_status)
# publish changes to foo
foo
end
end
end
module Hodor
class MainServiceTest
test 'it should call' do
sut = MainService.new(foo_klass: fake, bar_service: FakeService)
result = sut.call(1)
assert_result
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment