state-machines / state_machines

Adds support for creating state machines for attributes on any Ruby class
https://github.com/state-machines/state_machines
MIT License
814 stars 91 forks source link

Better support for activerecord 4.2 initialization #3

Closed huoxito closed 9 years ago

huoxito commented 9 years ago

This is an attempt to get both this build and state_machines-ar builds green.

Currently state_machines-ar has at least 3 issues on latest rails assuming I got this right (features that used to work before):

  1. State machines initial states used to override database column defaults,
  2. You could set up a field that didn't exist on the database and the state machine could still keep track of it. Helpers method would be defined on the fly and mapped to the state column.
  3. You could override the state machine initial state by simply passing it around on initialization. e.g. Post.new state: 'finished'

I believe the changes here fix those tree issues on the activerecord side. However it changes the way the api for initialize_states work. Now if one don't want to force the initial state it has to explicitly pass static: false instead, previously it would only force if one passed static: :force. Still the fix for # 2 mentioned above I got is so awful terrible I'd rather drop that feature. What you think? wouldn't really want to merge this as is myself

ps. once this is resolved I'd submit https://github.com/huoxito/state_machines-activerecord/compare/4.2-states-initialization to sm-ar and double check if spree builds are ok with the latest version of all these projects.

// @seuros

seuros commented 9 years ago

Can you add a Changelog.md entry ?

huoxito commented 9 years ago

@seuros ammended PR and submitted another one to sm-ar. Could you provide an example changelog entry? I'd add something like this there:

### Pass `static: false` in case you don't want initial states to be forced. e.g.

# will set the initial machine state
@machines.initialize_states(@object)

# optionally you can pass the attributes to have that as the initial state
@machines.initialize_states(@object, {}, { state: 'finished' })

# or pass set `static` to false if you want to keep the `object.state` current value
@machines.initialize_states(@object, { static: false })
huoxito commented 9 years ago

ops missed your reply while writing mine sorry

seuros commented 9 years ago

You can choose the format for the Changelog

huoxito commented 9 years ago

just added changelog entry