My team is debating how to transition a portion of our Rails application to allow "drafts". For simplicities sake, we can imagine a Post object where we want to allow a kind of draft mode.
Our real model contains, say, roughly 20 fields and 5 has_many relationships. There are some reasonably complex business rules (i.e., content can't be changed after a Post is delivered), and a state machine (Posts are pending, sending, then delivered).
class Post
string content
enum content_type
date deliver_on
enum delivery_state
has_many customer_group_segments
end
Two options being considered, but we are open to more. One is to duplicate all the existing tables (i.e., post_drafts
, customer_group_segment_drafts
, etc). The second is to include a kind of is_draft
field on the post.
The first option would separate all the data, but we'd need to consider sharing business logic between all models. And in the case of the draft, all of the validation rules would be optional until you tried to convert a Draft to a Post. And we'd also need to create a kind of transfer mechanism, etc...
The second option would remove all constraints from the underlying tables. We'd also need to make some kind of mode system, to ensure validation only kicks in where appropriate. And we'd also need to implement various checks to ensure, for example, that we don't try and deliver a draft.
There is a similar, but older question. I'm curious of there are any new opinions. Any ideas, observations, or working examples would be great!
is_draft
variable as providing that state functionality. Although that's food and perhaps I can future proof by using another state machine to implement draft mode (if I go with a single underlying table). – rooney Apr 20 '19 at 13:56content
andcontent_type
can be blank in thedraft
, but are required for apost
. Thedeliver_on
date can be blank in a draft, is required for apost
, but in both cases it must have a proper date format. Thedraft
can exist indefinitely. The two states are not reversible. – rooney Apr 23 '19 at 12:55