copasetickid/draftsman

Improve documentation for use with associations

SirRawlins opened this issue ยท 5 comments

Great gem folks ๐Ÿ‘

However, I'm struggling a little to see how to work this with draft associations, and I think it's something which could perhaps be covered in the documentation?

Let's say I have a draftable blog post, and a post can have various tags associated with it.

class Post < ApplicationRecord
    has_drafts
    has_many :tags
end

class Tag < ApplicationRecord
    belongs_to :post
end

Drafting changes to the blog post in isolation is simple enough, I can change it's title, or correct some typos in it's content, that's working just lovely.

However, what if I want to change the tags associated with my blog post?. How can I make those association changes 'draft' rather than immediate?

How would model/controller code look in a scenario like that?

If someone can help me understand, I'm happy to wrap up a PR to improve the README around this.

You mean edit a tag and make that tag a draft? If that that is the case, you should make Tag draftable:

class Post < ApplicationRecord
    has_drafts
    has_many :tags
end

class Tag < ApplicationRecord
    has_drafts
    belongs_to :post
end

You can then draft and publish Tags.

If that is not the case, please clarify.

@jmfederico thanks for the suggestion, that makes sense.

Do the draft? and publish! methods cascade? i.e. if I publish a post, will it publish all it's draft tags? Or vice-versa, will publishing a draft tag also publish its parent post?

When you publish a child, and the parent is new (never published), the parent also gets published.

Publishing the parent does not publish the child.

@jmfederico ๐Ÿ‘ thanks, that makes a great deal of sense.

Let's imagine I have a many-to-many association (has_many :through), such as, tags -> taggings -> posts. Where tags already existing, but I want to 'draft' the assignment of them to any given blog post.

I presume I would add draftsman to the join model? And then I can draft and publish the links, and perhaps cleanup before publishing to remove previously published associations?