PatientBank/Core — pull request #36 by pfletch1023 (2014-03-18 23:36:28+00:00)
Title: Timeline events v1

Okay so I've the afternoon researching and working on a way to store events (or activities) and associate them with models so that they can be quickly and easily queried. This is what I've come up with.

##### Event class
An `event` has three primary attributes:
* `actor`: entity performing the action
* `action`: equivalent to the verb, what the subject is doing
* `eventable`: entity being acted upon
The purpose of the class is to store a record of what happened in a way that's easily converted into a sentence. For example, `[Provider, :created, RecordUpdate].to_s => "Provider created RecordUpdate"`.

##### Querying events
Getting a timeline of events needs to be super fast, so I've implemented a simple Redis queue for each entity that might have events. The `RedisKeyed` and `Eventable` modules add the relevant methods to interact with a Redis queue. Basically, we want to associate a certain event with a bunch of `recipients` (entities where this event is relevant), but doing this with a PostgreSQL table is costly (on both read and write). So each entity that has events also has a Redis queue where we shove the ids of the relevant events. `Eventable` includes a method called `events` that retrieves those ids and we can do `Event.where(id: event_ids)` to get the relevant events. We might also want to consider doing access controls or event updates this way, but that's for another pull request :smile:

##### Not all events are created equal
Different types of events are going to have different sentence structures and recipients. Therefore, we can use single-table inheritance (STI) to customize particular `Event` instances (`class RecordUpdateEvent < Event` for example). Each can have custom `recipients` or `to_s` methods, and more complex applications for batching and rolling up events abound (coming up next).

##### What's next?
This pull request doesn't handle any sort of rolling up (consolidation) yet, so a timeline won't group record updates for the same record update grouping yet. This is coming soon.

##### Setting up Redis
I'll add this to a Wiki page as well, but you have to run a few extra commands to setup Redis. Download Redis from http://redis.io/download then...
```
cd ~/Downloads/redis-2.8.7
make install
make test
redis-server
```
It should be running on port `6379` and you're all set. Test it out by typing `$redis` in your Rails console.