How to setup Sprockets without Rails. Self referencial association with DataMapper. Delayed job for Sinatra.
In the previous post, we have seen how to embed a chat widget with Coffescript.
pusher. In this post we will see how to setup
Sprockets without Rails, we’ll cover self referential associations for friendship
relations, and finally we’ll setup Delayed Job with Sinatra to fetch user’s friends
To do so, we setup a watchr file, and install the gem watchr.
Relationship status: It’s not complicated
We use Datamapper for out database models. It’s easy to use and
we don’t have to worry about migrations (and that’s ok given this simplicity of this project). Documentation
is really good, we reuse part of Self referential many to many relationships section in
and we add few utility methods.
We have added online and offline methods, a method to get online friends and
the inverse_friendship relation. If user A is friend with user B, then it means B is also
friend with A. We store this relation only once in the Friendship table and use the inverse
Omniauth Twitter strategy using Sinatra
Omniauth makes it very easy to authenticate with Twitter.
We just need to provide a callback method, and store the user informations.
Sinatra and Delayed Job
In the previous code snippet we use Delayed Job to fetch our friends. We need to setup
with Sinatra and define a couple of tasks to start and clear the job queue in our Rakefile.rb.
Note that the following tasks are useful when we deploy the code to heroku.
Delayed Job is now ready. We need to setup the actual worker:
In the previous post we’ve seen how to embed a chat widget inside a Twitter page,
using pusher and CORS requests.
Behind the scenes though there are few tricks to talk about. The widgets is a coffescript single page
mini application. Sprockets is an important tool to glue Coffescript files together. The database used
is necessary to check for online or offline users. Finally, we’ve seen how to setup Delayed Job to
fetch data in background, and populate the database with friends data.