320 State machines [3d]

Posted Over 8 years ago. Visible to the public.

Requirements

Movies in MovieDB should have one of the following workflow states:

  • draft
  • pending
  • published
  • declined

A movie always begins as a draft and then transitions through the states as it's getting reviewed. This could be a typical state flow for a movie:

stateDiagram-v2
    [*] --> draft
    draft --> pending
    pending --> declined: Reason
    declined --> pending
    pending --> published

Change the visibility rules (Consul powers) so:

  • All users can see published movies of other users.
  • All users can see the movies they created themselves, regardless of their state.
  • An admin can see movies from other users that are published, pending or declined.

We will now add some additional controls to transition a movie between states:

  • When a user views one of her draft or declined movies, she sees a button Submit on the movie's show view. This changes the movie's state to pending.
  • When an admin opens a pending movie by any user, she sees two buttons Publish and Decline.
  • Before the admin may decline a movie, she is required to enter a declining reason. The declining reason should be entered in a form like the other forms in your app, with the same form round trip and controller handling. Consider whether you want to implement this inside an existing :movies resource or in a new one just for the declining form.
  • In addition, when an admin edits a movie by any user, she can choose any state from a select box, regardless of the movie's current state. Note that any transition constraints and callbacks will not be enforced when the #state is set directly. We only do this to allow admins to correct user errors.

Implementation

Make two implementations of the requirements above:

Make sure that you have tests for all the changes you make.

Tip

factory_bot offers traits Show archive.org snapshot to define variants of a factory. Your default factory should create records in a state that is useful for the vast majority of tests.

Henning Koch
Last edit
10 months ago
Michael Leimstädtner
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra Curriculum (2015-09-08 17:25)