Since Rails 5 is now in beta and ActionCable has been merged in, I wanted to give it a try. At my current job, we have a few reports that take a while to build and export (usually to PDF). For this we have a background job that will compile the report together and polling from the front end to see whether or not the file is ready for download. I'm not a fan of polling as it creates a lot of wasteful requests. Using WebSockets to get broadcasted messages from the backend seemed like a much more elequent approach. This post will use this simple use case to demonstrate ActionCable
To demonstrate this use case, we will be searching the PubMed database and returning a list of publications based on a search term. When the user enters a search term, a background job is created (I'm using Sidekiq to handle this through ActiveJob) and on the front end a modal is displayed with a spinner (although you could display a progress bar or whatever and update it with information received from the backround job). The background job will query the PubMed database and add each entry to a CSV. For this example I've capped the number of records to 5000 because the search could take an extremely long time depending on how general the search term the user has entered. As the search results are retrieved, the background job broadcasts what offset in the search it is currently working on (as mentioned earlier, this could be used to display and updated). Once the search is complete, a record is created and the file location is attached. A link is generated for the search result record and a success is broadcasted on the socket. The front end then displays the link so the user can download the file. Let's look at the code!
Been pretty busy this past year (so much for trying to make an effort to post more :-/). Thought I make a quick post about some useful hash methods that I've been using lately.
This past summer, I worked on a project building out an API for a mobile application. I thought it would be cool to use token based authentication in order to secure the API. To accomplish this authentication scheme I used JSON Web Tokens and Redis.
About the Competition
Last weekend I participating in a 48 hour rails development competition called Rails Rumble. The goal of the competition is to develop a rails based application. After the 48 hour period the applications are given feedback by other developers, than by the judges. The judges choose their top 10 favorite applications and the top 10 are declared winners. There’s also a winner for the best public favorite application and one for the best application built by a single person. Judging is base on originality, usefulness, appearance, and completeness.
The project that my team, which consisted of my brother and I, was to build a platform for collaborative brainstorming. Users create themes around a project, or question to get ideas or more questions on. Members are added to the theme to add their ideas. One goal of this project was to make everything realtime. To accomplish this we used Pusher, which is a publish/subscribe based system. You can check out the application here, feel free to leave comments here or on the rails rumble site, any feedback will be appreciated.
I find myself repeating my configuration all the time when creating a new rails application. I decided to give rails templates a try after watching this railscast on app generators. In it Ryan Bates goes over having a railsrc file, application templates, and application builders. One problem I did run into while trying out the strategies from the railscast was the application builder, I’m using rails 4 and it seems like they have gotten rid of this option, application templates still exist.
Here’s a strategy for merging branches:
git checkout <branch_to_merge_into> git pull --rebase git checkout <feature_branch> git rebase <branch_to_merge_into>
Here’s my approach to testing ActionMailer email. I want to test for two things: that the email will contain the correct content and that the email will be delivered. I also want to make sure that the email looks properly formatted, and for this I use the gem letter_opener
has_scope is a nice gem for Rails applications created by plataformatec. You can find it on github. The purpose of has_scope is to dynamically retrieve data from a model by applying the model’s scopes.
Ran into an interesting pitfall at work today with a has_many association. I had was using a condition where it was constraining the returned objects by date.
Sinatra is a simple DSL for Ruby to write web applications. Sinatra would be good for applications that don’t require a ton of data and don’t want the overhead that would come with Rails. For instance, if I just wanted to have a form and when the data was submitted it just went to a simple database table, I would probably use Sinatra.
I have a problem with coffeescript, it’s not necessary. I see no benefit from having to learn a new syntax that just compiles (really just converted) to another version that I already know. Coffeescript tries to fit into the same paradigm as less, sass or haml. But I actually find these tools to be useful, why? Because they take syntax that can’t be logical and makes it logical like less or sass, or can reduce the amount of text needed (from text heavy markup languages) and simplifies it like haml.
I recently started a new job as a rails developer. It’s my first foray into enterprise rails development in three years. One thing I noticed when looking into the new codebases I would be working with, too many migrations! In my opinion, migration files should be archived once the database schema has stabilized. Now I wouldn’t delete them, just in case something goes horribly wrong (but that would be very unlikely). Migration files should only be used to make deltas to the database, if the database is stable then there’s no reason to hold on to the deltas that built up to that version.