Last active
December 17, 2015 03:38
Using Reform with a Workflow object to allow the controller to be simpler.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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