Exporting large data to CSV using mysql outfile in multi-host Rails env.

In one of our current project we had a requirement where we needed to allow the site admin to download raw DB data as CSV file for some offline analysis. So, according to the requirement, we allowed the admin to select a database table and a date range to download the generated CSV data.

Our initial approach was to send the CSV file asynchronously using delayed job. It worked like this:
Admin submits the request, system queues it and sends admin an email with the attached CSV file. The reason we did this asynchronously was to avoid timeout during CSV generation.

While testing & benchmarking this approach, we ecountered two issues when we had a large data to export.

  • Too much memory was used by the ruby process when generating the CSV
  • CSV size could be larger than the supported attachment size by the email service provider, hence could result in an undelivered email

Read more


[Press Release] VinSol Releases Four Spree Commerce Extensions

The following is a press release announcing launch of open source Spree commerce extensions from our eCommerce team.

New Delhi, October 10, 2013 – VinSol, a New Delhi, India based premier Ruby on Rails studio and Spree Commerce premier partner, today announced the release of four useful extensions for the Spree Commerce community. The extensions include Spree Wallet, Spree Favorite Products, Spree Bank Transfer and Spree Admin Roles. VinSol believe that the extensions would provide substantial return of investment to Spree Commerce Developers as well as Store Owners.

When contacted, Manik Juneja from VinSol said, “The open source model of Spree Commerce allows VinSol to offer tools and methodologies that have proven successful for us while also encouraging feedback and contributions from the Spree Commerce community.”

Spree Wallet extension would enable the option of digital wallet. Using the same, the customers are able to make a purchase using the money stored on the website for their account.

Spree Bank Transfer extension creates an additional payment method for the stores that allows customers to pay via bank transfers.

The Spree Favorite Products extension allows users to tag their favorite products. Customers can create a list of their favorite products which motivates them to come back to your website and place more orders.

The Spree Admin Roles extension will be particularly useful for stores that need to allow various store admins with restricted permissions.

VinSol confirmed that they tested the extensions before releasing it for the open source community. In addition, these extensions are compatible with 2.0.x version of Spree commerce, easy to use and can be further customized in accordance to ones requirements and preferences.

Akhil Bansal, Rails business unit head of the company added, “In more than eight years of building eCommerce application with the Ruby on Rails framework, we have learnt the ins and outs of the required development patterns and platform. Our goal is to spend our time on high-value features for our client's stores, and our team has evolved several frameworks and utilities in Spree Commerce that allow us to be the utmost efficient with our development process.”

About VinSol

Based in New Delhi, India, VinSol is a Ruby on Rails development studio specializing in e-commerce, native mobile application and web development.  The company is also recognized as a Premier Spree Commerce Partner.

Contact Number: +1-415-839-0057

Email:  info at vinsol dot com

Website: http://vinsol.com/spree-commerce  

 


Drawing tool in iOS ( openGLES or UIBezierPath )

New to the problem of making a drawing/painting tool for an iOS application many developers get confused on using either OpenGLES or UIBeizerPath. Sometimes sample code on apples developer website GLPAINT  points many developers to use OpenGL Views for making paint layers for an application. A major issue with this that the developers do not understand is that OpenGL Views leave very heavy memory footprints and on devices like ipad1 having 24mb of memory to use per view , this can be a pain.

Trying to make a transparent openGL view which allows the user to paint on a transparent canvas with the background views visible is a problem I faced. Often crashes start occuring once you add multiple openGL views on a single view i.e adding multiple transparent paint canvases on a single view. These crashes are shown to be occuring because of the application receiving memory warning and the delegate “didReceiveMemoryWarning” being called.

After spending a lot of time with OpenGL , trying to figure out  a way to reduce the memory footprint (using instruments in Xcode), one gives up. The solution thus thought turns out that ipad1 cannot support multiple paint layers and the functionality has to be restricted for user to be able to add only a single layer per view, which indeed is not true.

Now comes the boon of UIBeizerPath class. This class introduced in iOS 4 SDK  makes it a lot easier for a developer to make multiple transparent paint canvases and add it to a single view without having a high memory footprint.

Steps on how to make use of UIBezierPath :

-       Add a new UIView class to the project , name it paintCanvasView.

-       Add two objects  in header file,

-       Initialize these objects in the init method by overridding it.

-       Override drawRect

-       Implement Touches Began

-       Implement Touches Moved

That is it.

You can use this PaintCanvasView Object in your code like any other UIView Object.  I hope this post would help some developers who are unaware of UIBeizerPath class and its uses. A further implementation of this is adding pattern to the rendered strokes. This pattern can be rendered on the strokes using pattern image , assuming pattern.png. Replacing the bColor with a pattern image in init method would do this trick.

 

Update 16May’12

Adding infinite undo and Redo managers:

-   Add two arrays to paintCanvasView.h

-   Add two methods to paintCanvasView.h

-   Allocate the arrays in init method

-   Modify drawRect in paintCanvasView.m to draw from pathArray

-   Add bPath to pathArray  in touchesBegan

-   Define undoButtonClicked and redoButtonClicked to modify pathArray as desired.

That is it.

There is no rocket science to make an infinite undo manager for paint or sketch. One can simply maintain two arrays, buffer and path. By easily maintaining all items removed from path array in buffer array, redo functionality is achieved.


VSCircularDial - open source iOS component

VSCircularDial is an UIView enhancement which provides you a 360 degree scrollable dial. This component can be dropped into your project and used as it is or can be customized as per your need.

It can be downloaded from github.

Features

1. Easy to use, just like normal UIView.
2. Fully customizable
3. Optimized for different resolutions, including iPhone, iPad and iPhone 4 (Retina Display)
4. Callbacks can be implemented to call specific methods on rotating the view.

Usage

Very similar to UIView. For VSRotatingView, you can initialize with "new" method:

VSRotatingView *rv = [VSRotatingView new];
[view addSubview:rv];

Initial Steps

1. Download and add the complete "VSRotatingView" folder in your project from here.
2. You need to add three frameworks in your project - QuartzCore, AVFoundation and Security.
3. Add #import "VSRotatingView.h" to the top of view controller in which you are going to use the above code and you are ready to go.
4. Function "- (void)viewCirculatedToSegmentIndex:(NSUInteger)index;" can be called in its delegate which can be used to know when view is beeing rotated and which segment is the current selected one.

Customizing this view is a lot simple. Variables could be modified in VSConstants.h to get desired functionality and the main image of dial can be replaced easily with image named "pain-cycle.png". Complete code can be accessed in VSRotatingView.h/.m files and full modification is possible.

Two demo applications are included in the project. Project#1 shows the default functionality of this component. Demo project#2 shows how you can customize this view and show a 360 degree view like the one below:

This component can be used to make various applications like wheel of fortune, etc.


A Rails Developer's impression of CoffeeScript - Part 2

We had a look at the basics of CoffeeScript in the previous post. In this tutorial, we'll see how it is supported in Rails and how can we use it. Ruby CoffeeScript is a ruby bridge to the CoffeeScript compiler. The coffee-script library will automatically choose the best JavaScript engine for our platform. The available engines are V8, Node.js, and JavaScriptCore.

To use CoffeeScript with Rails 3.0.x

We can use either of the two gems available:
- Barista – gem that provides CoffeeScript support for Rails and Rack apps
- BistroCar - another gem that serves up CoffeeScript from within our Rails application

The latest versions of Barista depends on the coffee-script gem. It auto-detects the available compilers on our system as there are several ways to install CoffeeScript. We can install via node.js as the official documentation suggests or else we can use the therubyracer gem and we won’t have to install anything else! therubyracer includes V8 (the fast Javascript engine from Google).

Finally the coffee-script gem depends on the coffee-script-source gem. This gem contains the CoffeeScript source code and it is synced with official releases.

RubyInside has a good tutorial for installing necessary gems and get CoffeeScript working in Rails projects. It also describes how to use CoffeeScript with jQuery.

Here is what we need to add to our Gemfile :

Next, we need to generate barista_config.rb file which is stored in config/initializers/ that contains a set of options to configure Barista options

The two basic settings for barista are the root and output_root configurations. The default directory where barista looks for your coffeescript files is the coffeescripts folder in your app directory which you need to manually create. We can change this default location by making changes in barista_config.rb.

Similarly, default location for compiled javascript files generated by Barista gem is the public/javascripts directory. Lets say, we want Barista to compile CoffeeScript files into public/javascripts/compiled, we’ll need to add this line to Barista configuration file :

CoffeeScript with JQuery

To use CoffeeScript with JQuery, the initial setup is as follows -

Lets start by writing the simplest JS statement in CoffeeScript



Generated JS -

Well, parenthesis for arguments are optional in CoffeeScript, so we can write

So far, so good.

Executing something on document.ready with Jquery

Doing it this way, the .bind call would executing too soon, before the document is ready and thus it doesn't do anything. Wrap it in a call to $(document).ready like this

Global functions

If we want to make a global function in coffeescript, we can explicitly assign it as a property of the global window object

Sample code to highlight(in red color) all required but empty fields, on clicking the form. Example -

We give 'required' class to all the required input text fields.

Generated JS -

CoffeeScript Demo (GitHub Repo)

This sample Rails application lets us create posts, each post can have multiple sections. It uses Ryan Bates's Nifty Generator Plugin to generate form layouts and basic skeleton for controllers and models.

Add_More_coffeescript

Clicking the “Add more” link lets us create more sections. This JavaScript functionality has been created using CoffeeScript.

If we clone the repo and run server.. notice the log -

On every request, Barista compiles the coffee files and generates equivalent JavaScript code. Every coffee file in app/coffeescript has an auto-generated corresponding file in public/javascript directory.

Other support

rack-coffee - Serves up coffeescript from rack middleware
After we've required 'rack/coffee', we put this in the Rails initializer config block:

coffee-haml-filter Custom haml filter for rendering CoffeeScript inside our Haml templates. Sample Code -


CoffeeScript in the wild - Is a comprehensive list of all projects that have adopted CoffeeScript.

Coffeebeans gives us inline CoffeeScript

Generated JavaScript -

Rails 3.1 and CoffeeScript

Rails 3.1 will have native CoffeeScript supports(via Sprockets 2). RubyInside has another good tutorial on how to start using CoffeeScript in Rails 3.1. This default can be turned off by commenting out the depedndencies in the generated Gemfile. If we want to write code in JS, we still can - just leave off the .coffee suffix from the filename. The application.js file is no longer shipping with .coffee added for the simple reason that it'll have no code. But if coffee is available, we'll be generating JS stubs for new controllers (like helpers) with the coffee extension on). Write .js files, like we were doing before. Be sure not to write coffeescript in .coffee files if we don't want to use coffeescript. By doing so, we will be using coffeescript. Instead we should only write javascript in .js files.