Sharing

Although nowadays we have Email, SMS services integrated in most of web applications, sometimes there could be need of documentation for future reference which users may want to reuse later based on their need. Similar requirement we had in one of our web application where we wanted to provide option to user to generate single or bulk PDF files out of any HTML templates they have in their account. There was different features around the generation of PDF document but major work was involved in…

Read complete blog post

Recently, we have to build an Admin dashboard for an e-commerce application. This involves adding stores, items their photos and other data typically needed for managing the data from admin side for non technical people. Since, we are using rails framework so we had two options that are best suitable as per our need administrate and active admin. The former is very popular recently as it has several advantages over the latter. Unlike active admin you don't have to understand the DSL. It's very…

Read complete blog post

Use spring to run RSpec -

with spring -

bundle exec spring rspec spec/controllers/api/v1/
Finished in 47.14 seconds (files took 1.58 seconds to load)
72 examples, 0 failures

without spring -

bundle exec rspec spec/controllers/api/v1/
Finished in 48.84 seconds (files took 29.23 seconds to load)
72 examples, 0 failures

One of the reasons for RSpec tests being slow is the initial application boot time. This happens every time you make changes in your code and takes few seconds every time…

Read complete blog post

In the previous article, we have seen how to configure DigitalOcean Droplet. Now let's proceed further to set up our application repository and Ngnix with Phusion Passenger on the DigitalOcean droplet.

1. Install RVM and Ruby

We will install ruby using Ruby Version Manager(RVM) as it gives more flexibility to manage multiple ruby versions on the same system over rbenv, chruby, etc. Import RVM GPG key, before installing RVM:

gpg --keyserver hkp://keys.gnupg.net --recv-keys

Read complete blog post

Nginx is a lightweight, high-performance web server. Passenger used with Nginx gives us many advantages. It automatically serves static assets from the public subdirectory to improve performance. Capistrano is used to automate deployment process to your remote server. Using Capistrano, you can deploy your web application on many servers simultaneously in sequence or as a rolling set.

In this article, we will see basic steps that need to be followed to configure your DigitalOcean Ubuntu Droplet.

Read complete blog post

Since quite some time, we use Apache Solr in one of our project for indexing data to search it faster from Solr server instead of always searching it from main database and creating bottleneck. We use Sunspot gem which is ruby library for rails application to implement Apache solr. Recently we thought to implement Apache SolrCloud architecture in same application to manage our daily growing Solr data more efficiently with this powerful feature of Apache Solr.

In brief, you can consider it as a…

Read complete blog post

In rails one of the way to execute raw query is use of ActiveRecord::Base.connection.execute

We are going to see if we use this option to execute raw query and we are working with pg database then what result it provides and what operations can be performed on that result.

results = ActiveRecord::Base.connection.execute('select * from purposes')

It returns PG::Result object :

#<PG::Result:0x007fd5a09686d8 status=PGRES_TUPLES_OK ntuples=3 nfields=5 cmd_tuples=3>

We can verify it simply…

Read complete blog post

Speed

  • Method Missing

Method Missing is one of the concepts of metaprogramming ruby. Although metaprogramming is very powerful it too has some shortcomings especially speed. A normal method is comparatively 1.5x times faster than a missing method. One of our users complained about our calendar page being very slow - on inspecting we found request was taking more than 30 seconds for loading the month view of the calendar and eventually request timed out. The user had lots of activities on that…

Read complete blog post

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

In software development you are destined to deal with lots of code that is not written by you. What would be more stressful than debugging the code that you barely understand. You might have been in the situation where you have been assigned a bug and you have to traverse through lots of file which you have no idea of, just get to root cause of the issue. Being in these situations many times, I have learned few tips and tricks for debugging and would like to share it here.

Checking where the

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 .er

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: 123).solr_index
end

This should fire

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

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, :message

 def

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