design - State machine and params in a rails app -


i in process of implementing state machine model having subscription behaviour usin aasm. want state machine able trigger actions when changing states.

currently use update action fail if date not correct callbacks.

class contrat   validate :active_start_is_valid end 

i want use state machine implementations imagine seems messy me :

-i create class method or service object kind of method :

def start_subscription(date)      date = validate_input_date(date)      @contrat.start_date=date      @contrat.activate!  end 
  • i in before_save method triggered update. 1 more complicated need check if start_date has changed, , if yes check date , update it, before changing state.

-the controller action activation limited update action (filtered custom params require) :

if @contrat.update_attributes   @contrat.activate! end 

i not comfortable of theses. state machine articles, rails or not, never mention params event, there other pattern should aware of?

am missing obvious?

this not how should implement state machine, should create models , state field indicates state of record decide state's object , use transition:

if want indicate initial state use:

state_machine :state, initial: :started ... end

if wish create transition:

event :start transition [:created] => :started end

do jobs on transition:

after_transition [:created] => :started, do: :send_mail

read more here:

https://github.com/pluginaweek/state_machine


Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -