Service Objects for Good
Service objects are a handy tool to use in any ruby application, that has complex logic that needs to be extracted out of a controller or model. There are some nice benefits to extracting complex things into a more testable interface. To avoid providing a contrived example, I will use similar code to how uploads on rubyfm are handled. class UploadService attr_reader :user, :logger, :upload def initialize(user, options = {}) @user = user @logger = options[:logger] || NullObject.new @errors = {} end def start(params) @upload = build_from_params(params) unless @upload.save logger.error { 'failed to save the upload' } # more logging info on why return false end unless transcode logger.error { 'failed to transcode upload' } return false end true end def transcode AmazonTranscodeService.new(@upload).start end end And the UploadsController that utilizes the UploadService with the following. ...