alphagov/whitehall — pull request #928 by tekin (2013-10-09 09:55:11+00:00)
Title: 56857726 editions service objects

This is part of a wider set of refactoring that will move us towards having separate service objects to perform actions on editions. The main thrust of this pull request is to untangle permissions from within the Edition model.

We need to enforce two things in the app before performing a given action on an Edition:

1) Does the current user have permission to perform the action?
2) Is the Edition in a state where the action can be performed?

Historically, these two concepts were tied together in the model. But thanks to the work done by @hlame earlier this year, we now have `Whitehall::Authority` which encapsulates many of the rules that govern access rights to Editions and the actions that can be performed on them. Therefor, we no longer need to enforce these permissions in the model; The controller can enforce permissions and the model can get on with performing the action, without worrying about the user's credentials.

I've done my best to make the commits on this pull request as self-contained as possible, so you may find it easier to review by going through the individual commits.

*Notes*

There are some inconsistency in the predicate checks on edition actions. Some use the state-machine supplied transition check method, e.g. can_unschedule? and can_delete?, whereas in other cases (where more checks than just the transition validity is required) an explicit method has been defined, e.g. schedulable? and force_schedulable?. I've left this inconsistency in place for now as they are only really used on the admin/editions#show template and the logic will ultimately be encapsulated in the service objects.

The tests around the edition actions are spread far and wide and could do with a bid tidy up, but I felt it was better to avoid changing them too much in this pull request to minimise the chance of regressions.