You could move all (or most) of this into the controller and do:

```
render CSV.generate { .. }
```