Rails 5. Draft version and publish version of model

Which is the best way to have a draft version of a model? I have a Course model with his description and lesson models.

class Course < ApplicationRecord
 has_many :lessons
 has_one :description
...
end

class Description < ApplicationRecord
 belongs_to :course
...
end

class Lesson < ApplicationRecord
 belongs_to :course
...
end

The description and lesson model have a state machine with a "published" status, that when they have it, their information will be published. I need the draft version to modify their information without change the published information. How can I do this?


PD: I have tried to use the draftsman gem, but doesn't work with Rails 5.

Upvotes: 4

Views: 1115

Answers (1)

Simon Franzen
Simon Franzen

Reputation: 2727

You could try draftpunk or use their approach to implement your custom solution.

https://github.com/stevehodges/draftpunk

Idea is to create store a draft version in the same database table. Just with an id referer. In your application you have to take care:

  1. Display to end users -> make a scope where you just return non-drafts (so the foreign id is nil)
  2. When call edit -> return the draft version of model, if nil -> create one. Let the user ony edit draft versions.
  3. Setup a publish action and a button in your frontend: Call a 'publish' function where you take care of handling the data publish process

I am interested how you end up

Upvotes: 2

Related Questions