Monday 20 April 2015

Difference between += and .concat


example:

2.1.1 :009 > x="saritha"
 => "saritha"
2.1.1 :010 > x+="srinivas"
 => "sarithasrinivas"
2.1.1 :011 > x.object_id
 => 19461520
2.1.1 :012 > x+="sree"
 => "sarithasrinivassree"
2.1.1 :013 > x.object_id
 => 19365720
2.1.1 :014 > x="hello"
 => "hello"
2.1.1 :015 > x.object_id
 => 19016160
2.1.1 :016 > x.concat("hi")
 => "hellohi"
2.1.1 :017 > x.object_id
 => 19016160
 2.1.1 :018 > x.concat(2)
 => "hellohi\u0002"
2.1.1 :019 > x.object_id
 => 19016160
2.1.1 :020 > i=10
 => 10

Based on above exple, += always creates a new object to perform string operation and .concat work on existing object only. so concat() is faster than +.
another exmple:
2.1.1 :021 > x = "hello"
 => "hello"
2.1.1 :022 > x.object_id
 => 19923680
2.1.1 :023 > x << "joy"
 => "hellojoy"
2.1.1 :024 > x.object_id
 => 19923680
2.1.1 :025 > x << "2015"
 => "hellojoy2015"
2.1.1 :026 > x.object_id
 => 19923680

<< alias for .concat
Fair question. The plus symbol, it seems, creates an intermediary copy of the variables before combining them, whereas << and concat directly concatenate the variables to each other without first producing an intermediary copy.

Thursday 16 April 2015

Pagination With Ajax Using Will Paginate in Ruby on Rails



step1: I have and index page. TO display all records.

controller method:
    def index
      @products = Product.paginate(:order =>"name ASC" ,:page => params[:page], :per_page => 14)
      respond_to do |format|
        format.html # index.html.erb
        format.json { render json: @products }
        format.js
      end
    end


step2: i have a dorp down with type1 ans type2 categories.
index.html.erb

<h1>Products</h1>
<%= select_tag :type, options_for_select([["type1",1],["type2",2]]), { :prompt => 'Select Category', :class => 'form-control', :id =>"type", :style => "width: 24%;margin-bottom: 7px;"} %>
<div id="products">
 <%= render "products/products" %>
</div>

<script>
$( document ).ready(function() {
    $("#type").change(function() {
        var type = $('select#type :selected').val();
        $.ajax({
            url: '/admin/index/'+type,
                dataType: "script"
        });
    });
    $(function(){
           $('.pagination a').attr('data-remote', 'true')
    });
});
</script>

step 3:
index.js.erb

jQuery('#products').html(" 'products/products' )%>");
$('.pagination a').attr('data-remote', 'true');

step 4:
_products.html.erb
<div class="listing">
      <% @products.each do |product| %>
        <div>
     <span> <%= product.title %> </span>
           <span> <%= product.price %>  </span>
        </div>
     <% end %>
</div>
<%= will_paginate @products %>


Thats all. ajax based pagination is done....

Thursday 9 April 2015

Mongoid scopes usage

class Person
  include Mongoid::Document
  field :occupation, type: String
  field :age, type: Integer

  scope :rock_n_rolla, -> { where(occupation: "Rockstar") }
  scope :washed_up, where(:age.gt => 30)
  scope :age_or_occupation, ->  {any_of({occupation: "Rockstar"}, {age: 18}) }
  scope :over, ->(limit) { where(:age.gt => limit) }
end

# Find all the rockstars.
Person.rock_n_rolla

# Find all rockstars age greater than 30.
Person.washed_up.rock_n_rolla

# Find all rockstars or other persons have their age as 18 (or operation)
Person.age_or_occupation

# Find a criteria rockstart with 60 as their aget.
Person.rock_n_rolla.over(60)