Adding Custom Search Bar To ActiveAdmin

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 will be taking an example of searching resources with tags

Dynamic Ruby

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 Ruby can do in terms of flexibility? Definitely not. Ruby have

Javascript Browser Window Operations in Rails

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 the requirement. So before we open the new tab - we wanted

first_or_create caches the existing scope and applies to the model callbacks

We were debugging one issue in our application which was caching the scope we had in where clause for first_or_create 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 Contact.where(creator_id: 123) but instead fires Contact.where(first_name: 'Kiprosh', creator_id: 123) On further

Delayed job object serialization and deserialization issue

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 initialize(recipient_ph_number, message_details) @client = Twilio::REST::Client.new ACCOUNT_SID, AUTH_TOKEN @sender = TWILIO_PHONE_

Better performance with Index type `varchar_pattern_ops` operator_class in Rails

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 4 times i.e 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 by adding xxx_pattern_ops options add_index

ActiveRecord::RecordNotUnique (duplicate key value violates unique constraint). Key already exists.

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 111, but that 'id' already exist. The primary key which is suppose to auto-increment and generate an unique id

Integration of Redactor Rails in Active Admin

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 application.js //= require redactor-rails In active_admin.scss on the top include: *= require redactor-rails In ActiveAdmin model i wrote: f.input :name, input_html: {class: 'redactor'}

Use Jquery - document.ready() to load page specific javascript in Rails App

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 photo gallery loads as soon as page is loaded. Gallery

Send notification to flowdock-group from rails application

If you want to send any notification message from your rails application, its easy to send it with following steps: 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" In your application, define mailer class and send email-message to above email. class UserMailer Thanks.