README.md in can_has_state-0.2.2 vs README.md in can_has_state-0.3.0
- old
+ new
@@ -102,11 +102,12 @@
# happen post-save, instead of pre-validation. Default pre-validation
# triggers are recommended for changing other attributes. Post-save
# triggers are useful for logging or cascading changes to association
# models. Deferred trigger actions are run within the same database
# transaction (for ActiveRecord and other ActiveModel children that
- # implement this).
+ # implement this). Deferred triggers require support for after_save
+ # callbacks, compatible with that supported by ActiveRecord.
#
# Last, wildcards are supported. Note that the ruby parser requires a
# space after the asterisk for wildcards on the left side:
# works: :* =>:whatever
# doesn't: :*=>:whatever
@@ -129,15 +130,17 @@
### Just ActiveModel ###
class Account
- include ActiveModel::Dirty
+ include CanHasState::DirtyHelper
include ActiveModel::Validations
include ActiveModel::Validations::Callbacks
include CanHasState::Machine
+ track_dirty :account_state
+
state_machine :account_state do
state :active, :initial,
:from => :inactive
state :inactive,
:from => :active
@@ -146,12 +149,22 @@
:timestamp => :deleted_at
end
end
+ActiveModel::Dirty tracking must be enabled for the attribute(s) that hold the
+state(s) in your model (see docs for ActiveModel::Dirty). If you're building on
+top of a library that supports this (ActiveRecord, Mongoid, etc.), you're fine.
+If not, CanHasState provides a helper module, `CanHasState::DirtyHelper`, that
+provides the supporting implementation required by ActiveModel::Dirty. Just call
+`track_dirty :attr_one, :attr_two` as shown above.
+Hint: deferred triggers aren't supported with bare ActiveModel. However, if a
+non-ActiveRecord persistence engine provides #after_save, then deferred triggers
+will be enabled.
+
## Managing states ##
States are set directly via the relevant state column--no added methods.
@account = Account.new
@@ -228,11 +241,13 @@
## Notes on triggers and initial state ##
`can_has_state` relies on the `ActiveModel::Dirty` module to detect when a state
-attribute has changed. In general, this shouldn't matter much to you.
+attribute has changed. In general, this shouldn't matter much to you as long as
+you're using ActiveRecord, Mongoid, or something that implements full Dirty
+support.
However, triggers involving initial values can be tricky. If your database
schema sets the default value to the initial value, `:on_enter` and custom
triggers will *not* be called because nothing has changed. On the other hand,
if the state column defaults to a null value, then the triggers will be called
@@ -240,6 +255,6 @@
## Compatibility ##
-Tested with Ruby 1.9 and ActiveSupport and ActiveModel 3.2.8.
+Tested with Ruby 1.9 and ActiveSupport and ActiveModel 4.0.