How to implement a RESTful resource for a state machine or finite automata

前端 未结 4 528
误落风尘
误落风尘 2021-01-01 10:33

I\'m a Rails and REST newbie and I\'m trying to figure how best to expose a resource that is backed by a domain object that has a state machine (in other words is a finite a

4条回答
  •  春和景丽
    2021-01-01 10:43

    Bit late to the party here and far from an expert as I have a similar query but...

    How about making the event a resource?

    So instead of...

    PUT /order/53?state_event="pay" #Order.update_attributes({state_event: "pay})
    

    You would...

    POST /order/53/pay     #OrderEvent.create(event_name: :pay)
    POST /order/53/cancel  #OrderEvent.create(event_name: :cancel)
    

    With a pub/sub listener between Order and OrderEvent or callback that attempts to fire that event on Order and records the transition messages. It also gives you a handy audit of all state change events.

    Idea stolen from Willem Bergen at Shopify

    Am I missing something? Sorry, struggling to understand this myself.

提交回复
热议问题