Monday, 18 May 2015

Rails 4 WITH NO Database

Sometimes, you may want to create a Rails application without ActiveRecord or a database. There might be situations like, Your application only store data using third party APIs, or storing in to files or might not have any persistent data at all. Since Rails stands for the common case (database backed application), by default it fails to start the server without a database connection.

You can simply build a Rails app without a database:
  1. Comment / Remove any database gems ( mysql, mysql2, sqlite, pg, mongoid, etc..) from your Gemfile and then run bundle. 
  2. Open your application.rb. By default, in Rails 4.0 you can see one line which requires all of Rails:
    require 'rails/all'
    This includes ActiveRecord also, and requires a database connection. Instead, you can include the specific parts of Rails that you gonna use.
    See below:
    require 'action_controller/railtie'
    require 'action_mailer/railtie'
    require 'active_model/railtie'
    require 'sprockets/railtie'
    require 'rails/test_unit/railtie'
  3. The default app generated by Rails 4.0 has one reference to active record which must be removed, in config/environments/development.rb:
    config.active_record.migration_error = :page_load 
    4. Remove any references to active_record in your configuration 
Now run your application with out any backend :)

Tuesday, 5 May 2015

Conference Track Management - Ruby solution

I got to solve this problem as the part of an interview process.
It wont look good if I paste the whole code here.

So just sharing the link to my gitbub account. Conference Track Management

Wednesday, 22 April 2015

Binary Search implementation in Ruby

  1. def binary_search(array, value, from=0, to=nil)
  2.     to = array.count - 1 unless to
  3.     mid = (from + to) / 2
  4.     if value < array[mid]
  5.       return binary_search(array, value, from, mid - 1)
  6.     elsif value > array[mid]
  7.       return binary_search(array, value, mid + 1, to)
  8.     else
  9.       return mid
  10.     end
  11. end

puts binary_search([1,2,3,4,5,7,8,9,10,12,15,16,17,18], 15)

Program to perform divison operation of two numbers without using /, %, and modules operator

def devide(divisor, dividend)
  quotient = 0
  until(dividend < divisor) do
    dividend -= divisor
    quotient += 1

puts devide(15, 100)

Wednesday, 1 April 2015

Ruby Count, Length & Size

These methods can be applied on Arrays, Hashes or Objects.  Here I'm just trying to illustrate the difference between count, length and size.


Basically length method returns number of elements in the array.
Example, suppose we have array arr as,
arr = [1,2,3,4,5]
Calling Array#length method on array will give result as,

 => 5 


Array#size is just an alias to the Array#length method. This method executes same as that of Array#length method internally.

 => 5 


It has more functionalities than length or size. This method can be used for getting number of elements based on some condition. Unlike length/size, we can pass block or an argument to count.
This can be called in three ways:
  • Array#count => without condition, Returns number of elements in Array
  • Array#count(n) => passing value, Returns number of elements having value n in Array
  • Array#count{|i|} => passing block, Returns count based on condition invoked on each element array

Way 1:
=> 5
Way 2:
arr.count 3
=> 1
Way 3:
c.count{|n| n < 4}
=> 3

Friday, 5 July 2013

Rails4 , Faster loading with turbolinks

The Ruby on Rails version 4.0.0 has been just released. The team took one and half years for development, Rails 4 comes with some interesting new features such as  Turbolinks, improved caching and thread-safe.

Turbolinks changes your Rails application into a single page JavaScript application, that is, it doesn't load new pages but instead replaces the current page with new content from the server.

[ This is similar to pjax, but here we dont need to worry about which element on the page to replace, and combining the server-side response to add, The entire body will be replaced ]

Few major points on Turbolinks

1. Less Execution time : -
  1. Turbolinks makes following links in your web application faster. Turbolinks  won't allow the browser to recompile the JavaScript and CSS between each page change/loading, it keeps the current page there itself and replaces only the body and the title in the head.
  2. Execution time of code depends. The more CSS and JavaScript you have, the more benefit of not throwing away the browser instance and recompiling all of it for each and every page.
  3. In any case, the benefit can be up to two times faster in apps with lots of JS and CSS. Of course, your app's speed may vary, It'll be dependent on your browser version and all other factors affecting performance of your application.
2. No jQuery or any other framework : -

  1.   Turbolinks is built as light-weight.
  2.   It does not require jQuery or any other framework to work. But it works fine with jQuery or Prototype or whatever else like these frameworks.
3. Events :-
  1. Turbolinks changes the pages without a full page reload, so you can't rely on DOMContentLoaded or jQuery.ready() to trigger your code.
  2.  Instead Turbolinks triggers events on document to provide hooks into the lifecycle of the current page
4. Initialization
  1.   Turbolinks will be enabled whenever the server has rendered a GET request.
Eg :-
  • POST :create => resource successfully created => redirect to GET :show
    • Turbolinks ENABLED
  • POST :create => resource creation failed => render :new
    • Turbolinks DISABLED
5. Opting out of Turbolinks

  1. By default, Turbolinks is enabled for all internal HTML links. 
  2. You can opt out by marking links or their parent container with data-no-turbolink attribute
  3. After giving data-no-turbolink attr with in a div, then all links inside of that div will be treated as regular links.
Eg :- 
<a href="#">Home (via Turbolinks)</a>
<div data-no-turbolink>
  <a href="#">Home (without Turbolinks)</a>
6.  Visit links manually using turbolinks
You can use Turbolinks.visit(path) to go to a URL through Turbolinks.

Tuesday, 11 June 2013

Rails Kaminari - Ajax pagination

Here is a simple way to implement AJAX pagination using Kaminari gem.

Suppose, We are having a Product Model with fields id and name.

So, In the ProductsController,

def index
  @products =[:page]).per(params[:per])
   respond_to do |format|

in view file index.html.haml

    = render 'products'
    = paginate @products, :remote => true

And create _products.html.haml(this will be rendered from index) with content
    @products.each do |product|

Finally, We have to create one more  file _index.js.haml with content

$('#products').html("#{escape_javascript(render 'products')}");
$('#paginator').html("#{escape_javascript(paginate(@products, :remote => true)) }");

This post may help you. Please comment on the same, If you guys have any queries.