Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding callbacks for draft creation, update, and destroy #14

Closed
wants to merge 3 commits into from

Conversation

npafundi
Copy link
Contributor

@npafundi npafundi commented Apr 4, 2015

Hey @chrisdpeters -- I had a few situations where I needed ActiveRecord-style callbacks to run on draft content, e.g. before_draft_creation rather than ActiveRecord's before_create.

This pull request includes support for nine callbacks:
(before | around | after)_draft_creation
(before | around | after)_draft_update
(before | around | after)_draft_destroy

I've included multiple tests for each of these callbacks -- 25 in total.

For the most part, I haven't changed how draftsman works (and the current specs all pass). The draft_creation, draft_update, and draft_destroy methods are basically just wrapped by the callbacks. There is one change to model.rb#L225 -- I've moved the 'return' outside of the callbacks to make sure they always have a chance to run (unless there is an exception).

Thought you (or others) might find this useful. Let me know if you have questions, or if you'd like me to modify how I'm doing anything.

Thanks!

npafundi added 2 commits April 4, 2015 11:42
Nine callbacks have been added via ActiveModel::Callbacks:
(before | around | after)_draft_creation
(before | around | after)_draft_update
(before | around | after)_draft_destroy
These work very similarly to traditional ActiveRecord callbacks, (e.g. `before_create`), except they are triggered when working with drafts.

This commit simply wraps the `draft_creation`, `draft_update`, and `draft_destroy` methods with a `run_callbacks` call.  Any callbacks defined on models which have drafts will be run as defined.

We only extend models which have drafts, to avoid further cluttering models.
Added 25 specs to test each of the nine draft callbacks.
  before_draft_creation, around_draft_creation, after_draft_creation
  before_draft_update, around_draft_update, after_draft_update
  before_draft_destroy, around_draft_destroy, after_draft_destroy

The `Talkative` model contains a few attributes which are modified via these callbacks.

The tests check that the attributes are modified correctly, and also check that they're called at the appropriate times.
@chrisdpeters chrisdpeters added this to the 0.4.0 milestone Apr 5, 2015
@chrisdpeters
Copy link
Collaborator

This looks awesome. I'm trying to get all organized-like, so I created a 0.4.0 milestone and placed this in there. I hope to not let issues and pull requests lie around quite as long from this point forth, so this pull request should be included in the gem soon.

Thanks!

@npafundi
Copy link
Contributor Author

npafundi commented Apr 6, 2015

Sounds good. Let me know if you'd like any changes.

@npafundi
Copy link
Contributor Author

npafundi commented Apr 6, 2015

Added docs for these callbacks to complete the circle

@npafundi
Copy link
Contributor Author

npafundi commented Apr 8, 2015

I've had to make a couple other changes (unrelated to this PR), and unfortunately I sent this pull request off my master branch. I'm going to close this PR and reopen it off a new branch from my repo so I can make a few other changes (and pull in the upstream). Other than pulling upstream, it won't be any different.

@npafundi npafundi closed this Apr 8, 2015
chrisdpeters added a commit that referenced this pull request Nov 11, 2015
Resubmitting #14 - Adding callbacks for draft creation, update, and destroy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants