avoinministerio/avoinministerio — pull request #152 by jyrkive (2012-06-05 13:29:59+00:00)
Title: Improved way to input article author

The admin area contains a page where an admin can add an article to the database. He/she can set arbitrary citizen as the article author. Currently it is done with a dropdown. That approach has two problems:

+ Creating the dropdown requires fetching all the citizens from the database (hopefully tens or hundreds of thousands in the future), which is horribly slow.
+ It is also slow for the admin to find the right citizen from the dropdown. It would be faster to simply write his/her name.

These commits change the dropdown to a text field, which makes entering the author name much faster.

## Implementation notes

### Passing author name to the controller

It isn't possible to convert the name directly into a Citizen object. I have manually implemented the conversion in ArticlesController, but the problem is that when you try to save the object, ActiveRecord will complain that the **author** object is a String rather than a Citizen. The right way to fix that is to save author name outside the model object so that it doesn't cause problems when saving the object. Unfortunately, SimpleForm doesn't allow that. The options I had were

+ dropping SimpleForm from the form partial (requires rewriting the partial from stratch)
+ employing hacks such as replacing the **author** object within the controller

While I don't like rewriting files, I dislike hacks even more. Therefore I rewrote **views/admin/articles/_form.html.haml** from stratch.

### Flash messages

It's beyond me why flash messages aren't currently displayed in admin area. I made them visible. (Aleksi requested that the admin is informed when the citizen wasn't found, and my current implementation of that feature depends on flash.)