OK, while GitHub is down, a code design question:
I always get analysis paralysis with non-standard RESTful actions in rails apps (and generally).
I've got Jobs, and I want to be able to cancel (and re-activate) them. It's not as simple as setting a checkbox; there's a few other things that need to happen., so I can't just use the existing JobsController#update
action.
Here are my options as I see it:
1. Just add cancel
and reactivate
to existing jobs controller.
The routes would be something like:
POST /admin/jobs/cancel/:job_id
POST /admin/jobs/reactivate/:job_id
(Not RESTful; assuming this is a bad idea)
2. Create a JobCancellationsController
with create
and destroy
actions.
Re-activating a job is destroy
-ing a JobCancellation
resource.
I'll use nested routes as per below:
resources :jobs, except: :show do
resource :job_cancellation, only: [:create, :destroy]
end
which by default will give me something like
a)
POST /admin/jobs/:job_id/job_cancellation
DELETE /admin/jobs/:job_id/job_cancellation
I could tidy the routes themselves up, without changing the controller, to be like:
b)
POST /admin/jobs/:job_id/cancellation
DELETE /admin/jobs/:job_id/cancellation
Although that doesn't seem very intuitive - 'cancellation' would be better as cancel
. So I can change the routes while keeping the controller the same:
c)
POST /admin/jobs/:job_id/cancel
DELETE /admin/jobs/:job_id/cancel
That first route now makes sense (although it's not RESTful
strictly speaking?), but the second doesn't... "deleting a job cancel"? So you'd change it to something like:
d)
POST /admin/jobs/:job_id/cancel
POST /admin/jobs/:job_id/reactivate
Now the routes makes sense, but look suspiciously close to option 1) above, even though the routes do map to RESTful actions in the JobCancellationsController
rather than non-RESTful actions in the JobsController
. And it seems very bizzare to have the POST /admin/jobs/:job_id/reactivate
route mapping to the JobCancellationsController#destroy
action.
To avoid that last odity with JobCancellationsController#destroy
, I could instead do:
3. Similar to option 2, but create two controllers: JobCancellationsController
with a create
action only, and JobReactivationsController
with a create
action only.
What's the 'correct' way to do this? Or at least, which are the 'incorrect' ways that I can quickly eliminate? Is there a totally different, better way that I've missed?