Fix order of before and after hooks.

Also fix passing arguments to tasks executed by before and after hooks.

This fixes both issues and has tests, although somewhat weird tests. I would prefer the tests were refactored, but I'm almost running out of time to work on this.

The code itself seems fine. It uses rake task prerequisites for the before hooks (which already pass arguments) and manually passes the arguments to after hooks (and I included a small variable name change to clarify what I understood to be the formal and real parameters).

Bug report was #1004 and original pull request was #928.