Background There are many gems for implementing two-factor authentication for Rails app out there. But, most of them don't work properly with Devise out from the box. Some gems offering seamless integration with Devise gem never work smoothly with our existing Rails app. This is why we share our experience and strategy to implement two-factor authentication using Devise and ActiveModel::Otp gems. This implementation, based on our experience, works very well with the latest Google Authenticator app of both iPhone and Android (version 5.10 as of 2020/06). The rqrcode gem does a nice job to provide us with QR code that's always scannable by the Google Authenticator. Oh, we run this on Ruby 2.6.6 and Rails 6. So, we can guarantee it's working with the latest Rails version at the time we write this (2020/06). 1. Prepare Gemfile You need to add several gems to achieve this. If you have an existing Rails app with Devise already, then feel free to skip the devise gem and add the rest of them. In…
Why we write this guide There are several reasons
The official guide provides very little info.
We underestimated the effort to do this. This long post explains our similar experience.
Yet, that long post doesn't give a brief step-by-step guide. So, we decide to fill the gap.
Let's start with custom_loader.js. In this file, we use…
All of us want to visit a fast loading website. No one wants to wait for a slower website to load. In the effort to speed up our website that is running Ruby on Rails, we decide to use the first-party analytics for Rails: ahoy_matey gem. This article shows how to speed up showing the visitor/event counts of ahoy gem from 13 sec to 0.01 sec, more than 1,000 times faster! Why we choose ahoy gem
It claims to be first-party analytics for Rails
We'll have the server's side control. Any third-party analytics won't let us have this.
It supports Rails 6 and its webpacker.
It allows us to specify which pages on our Rails website to track.
And the last one is it works with AMP.
However, the journey with ahoy_matey isn't a smooth one. Contrary to our belief, we notice the pages with ahoy_matey are much slower than those without it. Something is wrong there. InvestigationWe start our journey to investigate this problem. We begin with a page that's showing a list of articles. Every article there has a…
Why do this? As usual, we'll start with why. Why do this to our existing Ruby on Rails application? There are several reasons
We always keep our Ruby on Rails application up to date. Starting from Rails 5.1.0, rails-ujs is a part of the Rails itself.
We hate duplication. Why should we keep using jquery-rails (the gem for jquery-ujs) if Rails itself already has the rails-ujs with similar functionality?
We love to keep our Gemfile clean. We drop any gem that we don't use anymore. Jquery-rails is one of them.
Last but not least, we love to keep everything simple
Recommended requirement Before migrating from jquery-ujs to rails-ujs, we recommend the Ruby on Rails application has the following:
Integration tests. In our project, we use capybara and selenium, with some headless browsers.
Unit tests. We use RSpec for this.
Supported Rails version. At the time we did this, we've upgraded our Rails version to 220.127.116.11
Latest ruby version. We used 2.6.5 when we migrated long ago.
Make sure all the…
Why Bullet Gem? For this Rails optimization, we'll focus on optimizing repeated SQL calls to retrieve objects associated with many relationships: N+1 queries problem. The solution to such an issue is an eager loading association. Unoptimized N+1 affected code takes a significantly longer time as N times additional SQL calls are needed. But, we should be careful when using that eager loading approach. This because unused eager loading can deteriorate our Rails app performance instead of improving it. It's where the Bullet gem comes in handy. This gem will help us to find where
the N+1 queries problem is happening.
the eager loading associate is unused.
How to use Bullet Gem? This depends on the current application's state. In our case, we do these:
Add the bullet gem into Gemfile.
Define a new Rails environment called optimization.
Configure it to install the gem for that environment only
Run the Rails app with that environment
1. Add the Bullet gem into Gemfile. Here, we use its latest…
Accelerated Mobile Page (AMP) is a framework for creating a fast and light-weight mobile website. There are not many ruby gems for creating AMP pages for the existing Rails website. We find one like rails_amp. But it doesn't fit our needs. That's why we come up with our solution. It's a simple but powerful one. Here's how we create AMP pages for our Rails website. Add custom Mime::Type We add custom Mime at config/initializers/mime_types.rb
It's only one line code. The code registers amp as a new custom format for text/html. This way, we'll be able to handle this format on our controllers. This leads us to the next step. Add amp format to the controller There is no need to add this to all of the controllers. We only add it to one of the actions in the controller that serves the article pages. Here's the explanation of that gist
That format.amp will render the AMP pages when the user's browser requested it. So, when Google bots or your user's browser visits your AMP URL handled by that…
Privacy settings changed!
Article is saved. Do you want to continue editing the article or leave and edit later?