views:

11

answers:

0

I'm writing a Rails ActiveRecord model that needs a weaker version of validations. Essentially, the flow goes like so:

  1. Email is received and parsed by application. (already handled)
  2. Input from email is used to create/populate new model instance. (already handled)
  3. Model is checked for a series of conditions. (conditions already written)
  4. If all conditions return true:
    1. Set the model to be displayed to other users
    2. Save
    Otherwise:
    1. Set the model to be hidden from other users
    2. Save it for review by moderators
    3. Send a reply email to the submitting user enumerating the conditions which failed, and why

Validations could handle everything other than the "save the invalid record" portion of the spec.

I'm looking at Qoobaa's Transitions state machine gem as one option. It looks promising -- I could define states as :unchecked, :approved, and :denied, transitions from :unchecked -> :approved, :unchecked -> :denied, and :approved <-> :denied (for moderator use). Guard methods could be used to check the validation conditions on :unchecked -(validate)-> :approved. However, the guard method API can only return true/false, and I'd like to write logic that extracts the failure reasons to compose the rejection email, similar in form to the validation errors collection.

Any thoughts on best practices to implement this? Should I stick with Transitions, or would another method/paradigm work better?