gocardless/statesman — pull request #8 by dluxemburg (2013-10-31 13:54:17+00:00)
Title: Mongoid adapter

Awesome gem. I've been mulling over the idea of extracting state machine logic into a non-persisted class that acts on other records for a while. Glad someone did the hard work for me!

The Mongoid adapter included passes the adapter shared examples and versions of the existing specs for ActiveRecord. There are only two changes to the code outside of the Mongoid-related classes:

1. Change the `#create` shared adapter to convert the `to_value` retrieved from the model to a symbol before comparing it to the initial value. This is consistent with how the `#last` test works at the bottom of the file. The change is necessary to support Mongoid because Mongoid models either won't convert a symbol value if the field type is unspecified (which breaks more tests) or coerces it immediately if the field is defined as a string.

2. Don't require active record until (almost) absolutely necessary. Including it when the adapter is loaded and having the adapter always required means projects that don't use Active Record can't use Statesman. There might be a better way of doing this conditionally, but this seems to work. An alternative could be to throw an error in any adapter with an ORM dependency if that ORM isn't present telling users to require it themselves, but that would change the existing behavior.

Please provide any feedback about the implementation, I'm happy to keep at it if it needs more work.