AgileVentures/LocalSupport — pull request #50 by johnnymo87 (2014-05-21 16:47:34+00:00)
Title: Batch Invite Job

Here is the result of slurping all the logic out of the controller and slapping into a service. If we ever want to modify this invitations system (e.g. to allow users to invite other users), we will need to break this logic down into smaller, more composable units. As of now, though, this highly cohesive and highly coupled implementation will suffice.

Previously, the invitation system never tried to associate organizations with their invited users until after the user accepted. When the user accepted their invite, the system would use the user's email to look up the organization that invited it, but this method proved itself unreliable when it became clear that user emails were being mutated in the creation process, making this lookup fail.

Now, the association between the user and organization is set as soon as the invite is successfully sent.

The service processes batches of invitations as such:
* Incoming params are checked for having the required keys
* The boolean for resending invitations is passed to `Devise`
* The invitation data comes and goes as a hash
* `{'1'=>'e@mail.org'} => {'1'=>'Invited!'}`
* Each invite is attempted, and a `user` is associated with an `organization` if it succeeds
* The outcome of each invite is recorded, and sent back with an organization id