SlideShare ist ein Scribd-Unternehmen logo
1 von 8
Downloaden Sie, um offline zu lesen
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	?

Weitere ähnliche Inhalte

Was ist angesagt?

Cmdb
CmdbCmdb
Cmdb
ice j
 

Was ist angesagt? (12)

Rack
RackRack
Rack
 
Asynchronous javascript
 Asynchronous javascript Asynchronous javascript
Asynchronous javascript
 
How to run
How to runHow to run
How to run
 
Multi tenant/lang application with Ruby on Rails
Multi tenant/lang application with Ruby on RailsMulti tenant/lang application with Ruby on Rails
Multi tenant/lang application with Ruby on Rails
 
Orbiter and how to extend Docker Swarm
Orbiter and how to extend Docker SwarmOrbiter and how to extend Docker Swarm
Orbiter and how to extend Docker Swarm
 
Deploying Rails Apps with Capistrano
Deploying Rails Apps with CapistranoDeploying Rails Apps with Capistrano
Deploying Rails Apps with Capistrano
 
Constructor in detail
Constructor in detailConstructor in detail
Constructor in detail
 
Pretenders talk at PyconUK 2012
Pretenders talk at PyconUK 2012Pretenders talk at PyconUK 2012
Pretenders talk at PyconUK 2012
 
From Web Developer to Hardware Developer
From Web Developer to Hardware DeveloperFrom Web Developer to Hardware Developer
From Web Developer to Hardware Developer
 
Cmdb
CmdbCmdb
Cmdb
 
Angular for rubyists
Angular for rubyistsAngular for rubyists
Angular for rubyists
 
Rack
RackRack
Rack
 

Ähnlich wie Using assm in service object

Carlosbrando Rubyonrails21 En
Carlosbrando Rubyonrails21 EnCarlosbrando Rubyonrails21 En
Carlosbrando Rubyonrails21 En
Casey Bradford
 
Découplez votre appli en micro-APIs
Découplez votre appli en micro-APIsDécouplez votre appli en micro-APIs
Découplez votre appli en micro-APIs
Nicolas Blanco
 

Ähnlich wie Using assm in service object (20)

Building Distributed Applications with AWS Step Functions
Building Distributed Applications with AWS Step FunctionsBuilding Distributed Applications with AWS Step Functions
Building Distributed Applications with AWS Step Functions
 
AWS Summit Singapore - Lambda, Step Functions and Datadog: A Symphony
AWS Summit Singapore - Lambda, Step Functions and Datadog: A SymphonyAWS Summit Singapore - Lambda, Step Functions and Datadog: A Symphony
AWS Summit Singapore - Lambda, Step Functions and Datadog: A Symphony
 
Decompose the monolith into AWS Step Functions
Decompose the monolith into AWS Step FunctionsDecompose the monolith into AWS Step Functions
Decompose the monolith into AWS Step Functions
 
Serverless Apps with AWS Step Functions
Serverless Apps with AWS Step FunctionsServerless Apps with AWS Step Functions
Serverless Apps with AWS Step Functions
 
Designing and Implementing a Serverless Media Processing Workflow Using AWS S...
Designing and Implementing a Serverless Media Processing Workflow Using AWS S...Designing and Implementing a Serverless Media Processing Workflow Using AWS S...
Designing and Implementing a Serverless Media Processing Workflow Using AWS S...
 
Ruby on Rails 2.1 What's New
Ruby on Rails 2.1 What's NewRuby on Rails 2.1 What's New
Ruby on Rails 2.1 What's New
 
Design and Implement a Serverless Media-Processing Workflow - SRV328 - Atlant...
Design and Implement a Serverless Media-Processing Workflow - SRV328 - Atlant...Design and Implement a Serverless Media-Processing Workflow - SRV328 - Atlant...
Design and Implement a Serverless Media-Processing Workflow - SRV328 - Atlant...
 
AWS Step Functions 実践
AWS Step Functions 実践AWS Step Functions 実践
AWS Step Functions 実践
 
Act as state machine
Act as state machineAct as state machine
Act as state machine
 
Design and Implement a Serverless Media-Processing Workflow
Design and Implement a Serverless Media-Processing Workflow Design and Implement a Serverless Media-Processing Workflow
Design and Implement a Serverless Media-Processing Workflow
 
Step into serverless into the box 2018
Step into serverless into the box 2018Step into serverless into the box 2018
Step into serverless into the box 2018
 
Carlosbrando Rubyonrails21 En
Carlosbrando Rubyonrails21 EnCarlosbrando Rubyonrails21 En
Carlosbrando Rubyonrails21 En
 
Rails 3 Beautiful Code
Rails 3 Beautiful CodeRails 3 Beautiful Code
Rails 3 Beautiful Code
 
How to ship customer value faster with step functions
How to ship customer value faster with step functionsHow to ship customer value faster with step functions
How to ship customer value faster with step functions
 
Découplez votre appli en micro-APIs
Découplez votre appli en micro-APIsDécouplez votre appli en micro-APIs
Découplez votre appli en micro-APIs
 
Say Goodbye to Procedural Programming - Nick Sutterer
Say Goodbye to Procedural Programming - Nick SuttererSay Goodbye to Procedural Programming - Nick Sutterer
Say Goodbye to Procedural Programming - Nick Sutterer
 
Serverless archtiectures
Serverless archtiecturesServerless archtiectures
Serverless archtiectures
 
What’s new in serverless - re:Invent 2020
What’s new in serverless - re:Invent 2020What’s new in serverless - re:Invent 2020
What’s new in serverless - re:Invent 2020
 
AWS Step Functions를 이용한 마이크로서비스 개발하기 - 김현민 (4CSoft)
AWS Step Functions를 이용한 마이크로서비스 개발하기 - 김현민 (4CSoft)AWS Step Functions를 이용한 마이크로서비스 개발하기 - 김현민 (4CSoft)
AWS Step Functions를 이용한 마이크로서비스 개발하기 - 김현민 (4CSoft)
 
Going Serverless
Going ServerlessGoing Serverless
Going Serverless
 

Mehr von Railwaymen

Mehr von Railwaymen (11)

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

Kürzlich hochgeladen

AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 

Kürzlich hochgeladen (20)

Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
 

Using assm in service object