SlideShare a Scribd company logo
USING	AASM	IN	SERVICE
OBJECT
Kamil	BaćkowskiKamil	Baćkowski
WHAT	IS	AASM	?
Library	for	adding	finite	state	machines	to	RubyLibrary	for	adding	finite	state	machines	to	Ruby
classes.classes.
EXAMPLE	USAGE
class	Job
		include	AASM
		aasm	do
				state	:sleeping,	:initial	=>	true
				state	:running,	:cleaning
				event	:run	do
						transitions	:from	=>	:sleeping,	:to	=>	:running
				end
				event	:clean	do
						transitions	:from	=>	:running,	:to	=>	:cleaning
				end
		end
end
OUR	SCENARIO
class	Account	<	ApplicationRecord
		enum	state:	{	step1:	'step1',	step2:	'step2',
																step3:	'step3',	finished:	'finished'	}
end
Service	objectService	object
class	AccountManager
		include	AASM,	ActiveModel::Model
		attr_accessor	:account,	:name,	:country,	:language
		validates	:name,	presence:	true,	if:	:step1?
		validates	:country,	presence:	true,	if:	:step2?
		validates	:language,	presence:	true,	if:	:step3?
		aasm	do
				state	:step1,	initial:	true
				state	:step2,	:step3,	:finished
				after_all_transitions	:update_account_state
				event	:next_step	do
						transitions	from:	:step1,	to:	:step2
						transitions	from:	:step2,	to:	:step3
						transitions	from:	:step3,	to:	:finished
				end
		end
		def	initialize(attributes	=	{})
				super
				aasm_write_state_without_persistence(account.state.to_sym)
		end
		def	save
				if	valid?
						account.update!	send(STEP_AVAILABLE_VALUES.fetch(aasm.current_state))
				else
						false
				end
		end
		private
		def	update_account_state
				account.update_column(:state,	aasm.to_state)
		end
end
CONTROLLER
class	AccountsController	<	ApplicationController
		def	new
				@account	=	Account.new
		end
		def	edit
				@account	=	Account.find(params[:id])
		end
		def	create
				@account	=	Account.new
				account_manager	=	AccountManager.new(account_params.merge(account:	@account))
				redirect_to	edit_account_path(@account)	if	account_manager.save	&&	account_manager.next_step
		end
		def	update
				@account	=	Account.find(params[:id])
				account_manager	=	AccountManager.new(account_params.merge(account:	@account))
				redirect_to	edit_account_path(@account)	if	account_manager.save	&&	account_manager.next_step
		end
		private
		def	account_params
				params.require(:account).permit(:name,	:country,	:language)
		end
end
ADVANTAGES
Proper	logic	separation
Easy	to	maintain
Simple	to	unit	test
Can	bypass	aasm	transitions	in	tests
SUMMARY
Always	use	aasm	oustide	of	ActiveRecord	models.Always	use	aasm	oustide	of	ActiveRecord	models.
Questions	?Questions	?

More Related Content

PDF
Using Async in your Mobile Apps - Marek Safar
PDF
Async Await for Mobile Apps
PDF
Ninad cucumber rails
PDF
Converting LotusScript Agents to Java Agents
PDF
Anatomy of a Reactive Application
PDF
Gearman bundle, Warszawa 2013 edition
ODP
Funambol Automated Tests for SyncML Clients
DOC
Qtp quiz for you
Using Async in your Mobile Apps - Marek Safar
Async Await for Mobile Apps
Ninad cucumber rails
Converting LotusScript Agents to Java Agents
Anatomy of a Reactive Application
Gearman bundle, Warszawa 2013 edition
Funambol Automated Tests for SyncML Clients
Qtp quiz for you

What's hot (12)

PDF
Asynchronous javascript
TXT
How to run
PDF
Multi tenant/lang application with Ruby on Rails
PDF
Orbiter and how to extend Docker Swarm
PPT
Deploying Rails Apps with Capistrano
RTF
Constructor in detail
PDF
Pretenders talk at PyconUK 2012
PPTX
From Web Developer to Hardware Developer
PPTX
Cmdb
PDF
Angular for rubyists
Asynchronous javascript
How to run
Multi tenant/lang application with Ruby on Rails
Orbiter and how to extend Docker Swarm
Deploying Rails Apps with Capistrano
Constructor in detail
Pretenders talk at PyconUK 2012
From Web Developer to Hardware Developer
Cmdb
Angular for rubyists
Ad

More from Railwaymen (11)

PDF
How to start application development?
PDF
We digitize your business vision
PDF
Speed up rspec tests - part 1
PDF
Railwaymen Booklet 2017
PDF
Railwaymen Presentation 2017
PDF
Will it pass or not? - A few words about automation
PDF
Mobile App Development
PDF
The evil scientist - Railwaymen DevDay vol.1
PDF
Smartwatch - something more than an additional screen for notifications?
PDF
Smartwatch - jednak coś więcej niż dodatkowy ekran na notyfikacje?
PDF
RoR Workshop - Web applications hacking - Ruby on Rails example
How to start application development?
We digitize your business vision
Speed up rspec tests - part 1
Railwaymen Booklet 2017
Railwaymen Presentation 2017
Will it pass or not? - A few words about automation
Mobile App Development
The evil scientist - Railwaymen DevDay vol.1
Smartwatch - something more than an additional screen for notifications?
Smartwatch - jednak coś więcej niż dodatkowy ekran na notyfikacje?
RoR Workshop - Web applications hacking - Ruby on Rails example
Ad

Recently uploaded (20)

PPTX
Essential Infomation Tech presentation.pptx
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PPTX
L1 - Introduction to python Backend.pptx
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PPTX
history of c programming in notes for students .pptx
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
ai tools demonstartion for schools and inter college
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PPTX
Transform Your Business with a Software ERP System
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
Essential Infomation Tech presentation.pptx
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
wealthsignaloriginal-com-DS-text-... (1).pdf
Design an Analysis of Algorithms II-SECS-1021-03
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
L1 - Introduction to python Backend.pptx
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Odoo Companies in India – Driving Business Transformation.pdf
Wondershare Filmora 15 Crack With Activation Key [2025
history of c programming in notes for students .pptx
Understanding Forklifts - TECH EHS Solution
ai tools demonstartion for schools and inter college
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Transform Your Business with a Software ERP System
VVF-Customer-Presentation2025-Ver1.9.pptx

Using assm in service object