Active Admin - customize default CSV export
ActiveAdmin is great framework for managing website administration interfaces. It allows us to list records of a table, filter them, sort them and also support exporting them in CSV, XML or JSON. By default, records that are being displayed are exported. For example, say we have a products table where each product is distinguished by their type. We can apply a filter to list all electronics products. Clicking on CSV link below the list will export all electronics products only. But sometimes we need to download specific records coming from a custom query or scope using the default csv link.
Upload files on S3 with Elixir/Phoenix using ex_aws
In one of our Rails 4 app, we decided to move file and image uploads to another microservice so that the load on server is reduced when a big file is uploaded. We decided to do this in Phoenix. In Phoenix, we have ex_aws package which makes file uploads to S3 very simple just like Rails. So lets get started. Add ex_aws Update mix.exs to include following dependencies. defp deps do [ ..., {:ex_aws, "~> 1.0"}, {:poison, "~> 2.0"}, {:hackney, "~> 1.6"}, {:sweet_xml, "~> 0.6"
Accessing cross domain iFrame contents
Recently we improved performance of one of our Ruby on Rails application. We used NewRelic tool to identify areas that were slow. One of which was file and image uploads to S3 from a text editor (Redactor). When a user uploads a big file or image - the app's server response time shoots up degrading the performance and overall throughput because the files and images were being uploaded via rails action. The server process was busy uploading file on S3 while other requests to server were being queued increasing the request processing time. To improve this, we decided to delegate
Encoding Problems in Ruby
String encoding is something that we don't really think until we see Encoding::CompatibilityError: incompatible character encodings: UTF-8 and ASCII-8BIT Or when users complains about missing special characters like "’" (apostrophe copied from Microsoft Word) or when "菜医生" becomes "иЏњеЊ»з”џ". Before we go into encoding problems, lets understand what encoding is. A string can be considered as an array of bytes: irb(main):001:0> "world".bytes => [119, 111, 114, 108, 100] Here 119 means w, 111 means o and so on. This relationship between bytes and characters is
Password protect PDFs
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. Installing PDFtk You need to install "PDFtk Server" which is a command-line tool
Regular Expressions - Greedy vs non-greedy
By default, regular expression matching is greedy, which means they try to match as many matches as possible in a given string. Lets see an example considering HTML snippet - <p>Hello</p><span>Awesome</span><p>World</p>. Our task is to extract first p tag. i.e pattern matching should return <p>Hello</p>. Immediate solution is to write regex - /<p>.*<\/p>/. But it would match the whole string. Greedy The reason it matches whole string is
Adding new column with default value to high volume database table
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, :boolean, {:default=>false}) -> 3.3695s 3 secs is a long
Referencing Rails Assets in Javascript / CoffeeScript files
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 .erb (i.e. js_assets.js.
Writing binary file in Cordova and uploading it to server using Cordova File Transfer plugin
Cordova File Transfer plugin allows you to transfer / upload photos from mobile to server. You generally need a file with path for transferring / uploading files via File Transfer plugin. More information on how to use Cordova File transfer plugin can be found here. This plugin won't help much when we have a Base64 image (usually a drawing or signature drawn on canvas). To upload such images we can do: take your base64. write it to a file. then just use file transfer plugin to upload it to server. In this article, I would explain how we can write Base64 image
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