object service
# real use in controller
def destroy
@result = YourService.call(first, second)
if @result.success?
#...
else
#...
end
end
object service
# real use in controller
def destroy
@result = YourService.call(first, second)
if @result.success?
#...
else
#...
end
end
object service
class BaseService
def self.call(*args)
new(*args).call
end
def call
raise NotImplementedError, "Missing instance method `call` of #{self.class} class"
end
end
class RespondSevice
attr_reader :payload, :errors
def initialize(payload: nil, errors: [])
@payload, @errors = payload, errors
end
def fail?
errors.any?
end
def success?
!fail?
end
end
object service
class YourService < BaseService
def initialize(first, second)
@first, @second = first, second
end
def call
#... process something
return RespondService.new(errors: [{
title: 'Cannot ...',
detail: '...'
}])
#... process something
RespondService.new(payload: payload)
rescue ActiveRecord::RecordInvalid => e
RespondService.new(errors: e.record.errors)
end
private
# ... methods for call
end
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us