In one of our Rails application, we needed to generate password protected PDFs. We used wicked_pdf (an awesome gem that generates PDFs from HTML template) to generate PDFs but it doesn't provide feature to secure it.

While searching for solution to secure PDFs we came across PDFtk (PDF toolkit). It is a cross-platform tool for manipulating PDF documents. It has feature to add password to PDF document using "user password" as well as "master password" (owner password) which is great.

...

Read complete blog post

Almost all Ruby on Rails developers might come across scenario where they need to add a new column with a default value to one of the database tables. Most of us (including me) would write following migration statement -

add_column :table_name, :column_name, :boolean, default: false

This is a good practice but would cause downtime if the table has large number of records. It took 3 secs when I ran the migration for a table having 50k records.

-- add_column(:table_name, :column_name,...

Read complete blog post

Sometimes you’ll want to refer to your image assets from inside of your JavaScript or CoffeeScript files. We have nice rails helpers that would allow us to do so but we need to append .erb to every .js or .coffee file we want to reference images.

I didn’t like it that way, because ERB inside of CoffeeScript looks odd and having the file end with .erb messes up syntax highlighting.

A way around this is by adding following piece of code (not the beautiful one) to one single file that ends on ...

Read complete blog post

Sendgrid provides email service which we can use for sending emails from our app and we can even track every details of any email sent through our Sendgrid account like whether it is delivered, opened, dropped and details of unsubscribed email addresses etc.

There are also API endpoints provided by Sendgrid to access user subscribe/unsubscribe related data and email event data with respect to emails sent through your sendgrid account for which documentation link is provided at bottom.

To...

Read complete blog post

Active Admin is a great plugin that helps us to get administration functions with elegant and simple interface up and live quickly without much of a hassle or efforts. One can also make use of various customisations as per their need with very little effort.

In this article, I will be explaining how we can add custom search bar in Active Admin. Adding custom search bar is not as easy and straight forward as other Active Admin customisations. I will be making use of jQuery to do this.

Here, I...

Read complete blog post

Ruby is too dynamic. We can do almost anything in runtime, from creating the classes at runtime to creating methods dynamically.

If you are coming from some other language, it would be shocking for you too know that nothing is private in ruby. You can access private and protected method from anywhere you want.

You can call this a flexibility or a curse. But a smart developer knows when to utilize the power of extreme flexibility that Ruby provides and when to stay away from it.

Is this all...

Read complete blog post

Recently we developed one feature where we wanted to present a new tab view for an existing smaller form interface whereby users can use the full screen of the new window to move around and use the feature with ease. While we were developing the feature, we came across few challenges and thereby few learnings which we thought would be good to share -

1. Caching existing values.

We have around 8-9 forms from where we can use this feature but each of them have different implementation based on...

Read complete blog post

We were debugging one issue in our application which was caching the scope we had in where clause for firstorcreate in model callbacks - and found one interesting issue -

Theconditions in the where clause were cached in after_create callback if we use where().first_or_create, please check example below -

Example:

Contact.where(first_name: 'Kiprosh').first_or_create

Contact Model:
after_create :sanitize

def sanitize
 Contact.where(creator_id:...

Read complete blog post

Rails has provision to provide order of indexing on a column for better performance of like queries. The varchar_pattern_ops improves the performance of like queries by 4x.

For example, lets have this like query on name column (that has sequential index.)

Select * from users where name like 'John%'

We might have added a regular Rails index in migration for this name column as

add_index :users, :name - This will generate a sequential index.

The same can be made 4 times faster using btree index...

Read complete blog post

Recently in one of our project there was a requirement to send SMS notification to users and task was to perform this functionality as background job using delayed job service. I wrote following code for sending SMS notifications to user and it was working fine. Then I just did one liner change in my code to process send_sms method asynchronously using handle_asynchronously method of delayed job and thought it will work as expected.

 attr_reader :client, :sender, :recepient,...

Read complete blog post

Through this blog post, I would like to share few tips and tricks for getting involved into the existing Rails application. It's hard to lay the architecture of the project and start it from scratch, but it's equally difficult to get involved into existing project. Things may go bad if the project is not well documented and ample amount of tests are not present. But even getting into an existing project with good documentation and well-written test are difficult. Well documented project will...

Read complete blog post

Recently I was getting an issue while inserting a new record to one of the table in our database. I was getting this specific error -

ActiveRecord::RecordNotUnique (PG::Error: ERROR: duplicate key value violates unique constraint "canonical_cities_pkey" DETAIL: Key (id)=(*****) already exists.

Understanding the error

The table currently has several thousand entities for canonical cities. But when am trying to insert a new record it builds an canonical_city record and assign it an 'id', say...

Read complete blog post

In our current app, there was a requirement to have a nice editor which will provide the basic functionalities like bold,italic and also image upload. We decided to use redactor-rails gem Github-Documentation-Link(Github Doc) .

So i took following steps:

  • In Gemfile i included:
gem 'redactor-rails'
  • Then for image uploading and storing i included two more gems as:
gem 'carrierwave'
gem 'mini_magik'
  • Bundle it.
rails generate redactor:install
rails generate redactor:config
  • In...

Read complete blog post

Yesterday I was working on one of our portal (It's in Ruby on Rails and we have used quite alot jquery as well). Was integrating a nice Photo Gallery plugin on one of the page. Applcation is having many pages. Every page has some different behaviour and we have different jquery calls for that as per need.

For a performant application, we always want to avoid any unwanted calls to different methdods/functions. In my case, as I mentioned earlier I wanted to show Photo Gallery on a page and...

Read complete blog post

If you want to send any notification message from your rails application, its easy to send it with following steps:

1) To send notification to your flowdock group, you will need email-address of flowdock grouYou can get it from flow-settings of your group.

example: "xxxx@xxxx.flowdock.com"

2) In your application, define mailer class and send email-message to above email.

class UserMailer < ActionMailer::Base
 default from: "xxxx@example.com"

 def flowdock_notification(post)
 flow_email = "xxxx@xxxx.flowdock.com"
 body = post.user.name + " posted a new article: \n#{post.subject}"
 mail(to: flow_email, subject: "New Post") do |format|
 format.text { render text: body }
 end
 end
end

Thanks.

Read complete blog post

How to work with time-zone in rails

Time-zone leaks:

This is an issue where time-zone of one request is passed to another request.
Ex Scenario:
Lets say we have web-server running on a single dyno and we have three different users with different timezones('Mumbai', 'Central America' and 'London').

Here is my code setup that will produce time-zone leaks,

class UserController < ApplicationController
before_filter :set_user_time_zone, only: [:method_one_with_time_zone] 
def method_one_with_time_zone puts Time.zone #before_filter sets the time zone for this method...< ApplicationController before_filter :set_user_time_zone, only: [:method_one_with_time_zone]

Read complete blog post

Apipie-rails is a DSL and Rails engine for documenting you RESTful API. Instead of traditional use of #comments, Apipie lets you "describe the code by code". This brings advantages like:

  • It uses ruby syntax
  • Reusable doc for other purposes (Best for validations)
  • Easier to extend and maintain

apipie-rails gem owner has documented it in very nice way with examples. Refer Github Documentaion(github doc)

Path to documentation:

The documentation is available right in your app (by default under...

Read complete blog post

If we need to support additional parameters for devise authentication besides normal(username/email with password). We can do this in devise by overiding an existing method, 'activeforauthentication?'. After authenticating a user and in each request, Devise checks if your model is active by calling model.activeforauthentication?

Consider a feature 'Lock sub user', here we can add a boolean flag 'is_locked', based on this flag we enable or disable the authentication for the sub-user:

 def...

Read complete blog post

While evaluating BatmanJS(BatmanJS) for one of the biggest application here @Kiprosh, I found it very much Rails like and started drawing parallels when I was doing MVP with Rails and BatmanJS.

Note: This is not a tutorial for getting started(Getting Started) or building rails app with BatmanJS(Another getting started) . I personally like Zack hubert's blog(Zack hubert's blog) and Batman for SuperHeroes(Batman for super heroes)

If you are book lover, you will certainly like cookbook(Cookbook...

Read complete blog post

PJAX Related performance improvements

To know about PJAX (Click Here)(Pjax Railscast)

Post from Kavita: How to PJAX

Pjax is a great solution when we want to do partial refreshing of page at the same time maintaining the history of user navigation, But it has few issues when the app grows

Basically PJAX works like this,
1) Issuing a PJAX request to refresh the section of the page( eg: #conainer). Basically it fires an html ajax.
2) Rails server handles the html request and renders the...

Read complete blog post