Skip to content

Instantly share code, notes, and snippets.

@gogogarrett
Last active December 17, 2015 03:38
Show Gist options
  • Save gogogarrett/5544359 to your computer and use it in GitHub Desktop.
Save gogogarrett/5544359 to your computer and use it in GitHub Desktop.
Using Reform with a Workflow object to allow the controller to be simpler.
class ArtistsController < ApplicationController
def create
@form = create_new_form
workflow = Workflows::ArtistWorkflow.new(@form, params[:artist])
workflow.process do |obj|
return respond_with obj
end
render :new
end
def update
@form = create_edit_form
workflow = Workflows::ArtistWorkflow.new(@form, params[:artist])
workflow.process do |obj|
return respond_with obj
end
render :edit
end
private
def create_new_form
ArtistForm.new(artist: Artist.new, song: StudentProfile.new)
end
def create_edit_form
artist = Artist.find(params[:id])
ArtistForm.new(artist: artist, song: artist.song)
end
end
module Workflows
class ArtistWorkflow
attr_reader :form, :params
def initialize(form, params)
@form = form
@params = params
end
def process
if form.validate(params)
form.save do |data, map|
if form.student.new_record?
new_artist = Services::CreateArtistWithSong(map[:student], map[:song]).call
NotifyArtistCreation(new_artist).call
else
new_artist = Service::ManageArtistWithSong.new(map[:student], map[:profile]).update(form.artist.id, form.song.id)
end
yield new_artist if block_given?
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment