Friday, 30 May 2014

Understanding JavaScript:NaN



NaN,not a number, is a special value used to denote an unrepresentable value. With JavaScript, NaN can cause some confusion, starting from its typeof and all to the way the comparison is handled.

Several operations can lead to NaN as the result. Here are some examples:

Math.sqrt(-2)          //NaN

Math.log(-1)           //NaN

parseFloat('foo')    //NaN

0/0                          //NaN


The first trap is usually the unexpected result of calling typeof:

console.log(typeof NaN); // 'number'

In a way, while NaN isn’t supposed to be a number, its type is number. Got it?
Let’s compare two NaNs:

var x = Math.sqrt(-2);

var y = Math.log(-1);

console.log(x == y);         // false

Maybe that’s because we’re supposed to use strict equal (===) operator instead? Apparently not. 

var x = Math.sqrt(-2);

var y = Math.log(-1);

console.log(x === y); // false

even if we test like this:

console.log(x == x);       // false 

if we compare real NaNs then:

console.log(NaN === NaN); // false

Because there are many ways to represent a NaN, it makes sense that one NaN will not be equal to another NaN.


Friday, 23 May 2014

Using Named Scopes Across Models with ActiveRecord #Merge

If you want to use named scopes on joined models, then there are two ways to find the solution.


1. Define a named scope inside each model : you can define a different named scope in each model. lets have an example:

we have two models named user and project. these model have associations lke this,

class User < ActiveRecord::Base

  has_many :projects


end
 --------------------------------------------------------------------------------------------------------------------------------
class Project < ActiveRecord::Base

  belongs_to :user


  scope :available, -> {where(available: true)}
  scope :unavailable, -> {where(available: false)}



end

now if we to get the all projects that are available by the scope then we have to define a named scope inside the user model.

 scope :available_projects, -> {joins(:projects).where("projects.available = ?" ,true)}



2. Use Merge to use defined named scope in another model: 
Other way to use named scope just use merge in your query as like:

 User.joins(:projects).merge(Project.available)
User.joins(:projects).merge(Project.unavailable)








there is no need to have an extra named scope inside the user only one query can can do the same thing as the scope available_projects do.

Friday, 16 May 2014

JQuery: width() vs css('width') and height() vs css('height')

jQuery provides two ways to set width and height of any element. You can set using css or you can use jQuery provided methods.

 If you want to set the width or height of any DOM then

$('#div_1').css('width','100px');
$('#div_1').width(100); 


Then what is the difference?

The difference lies in datatype. As its clear in code that with width() method you need to append 'px' to the width value and with css('width') you don't need to specify.  


When you want to read width of any element then css('width') method will return you string value like '100px' while width() will return an integer value.

$('#div_1').css('width'); // will return '100px'


$('#div_1').width(100);  // will return 100

So if you want to do any kind of manipulation then width() function is the best option.


note: same with the height() and css('height') methods.

 


 

Saturday, 10 May 2014

Using pre-compiled assets in Rails Development Environment

 if you want to use the pre-compiled assets in Development Environment then follow these steps:

1. Edit development.rb : Add the following lines inside your config/environments/development.rb


  config.assets.debug = false
  # Disable Rails's static asset server (Apache or nginx will already do this).
  config.serve_static_assets = true

  # Compress JavaScripts and CSS.
  config.assets.js_compressor = :uglifier
  # config.assets.css_compressor = :sass

  # Do not fallback to assets pipeline if a precompiled asset is missed.
  config.assets.compile = false

  # Generate digests for assets URLs.
  config.assets.digest = true


if you want to compile your other assets like assets/your_dir/ style.css ,assets/your_dir/ custom.js etc then add the following line in your  config/environments/development.rb

 config.assets.precompile += %w( your_dir/style.css, your_dir/custom.js)




2. Compile your assets in Development Environment: compile your assets by the following command

rake assets:precompile RAILS_ENV=development

3. Restart your Rails server: Now restart your server and your application will serve compile assets in development environment.

Friday, 18 April 2014

Adding title to Rails view

You can add your title for views in two ways.for adding title in views you have to follow these steps:

1-> add <title><%= yield(:title) %></title> inside the header of your layout.
2-> now you can add your title inside your view.there are following ways to do that:

  a-> use this line in your view:
      <%= content_for(:title, "Title for specific page") %>

  b-> use helper method:
  •  add a helper method in application_helper

       def title(page_title)
            content_for :title, page_title.to_s
      end


  • inside your view:
           <%= title "your custom title" %>
  c-> if you want to write title for view in controller then add this method inside   your application_controller.

  def view_context
    super.tap do |view|
      view.content_for :title, "your title"
    end
  end


if you want to add title according to your controller and action names pair then:

  def view_context
    super.tap do |view|
      view.content_for :title, "#{controller_name}_#{action_name}"
    end
  end

 

Saturday, 12 April 2014

Converting Html to Pdf using wkhtmltopdf

wkhtmltopdf is open source (LGPL) command line tools to render HTML into PDF and various image formats using the QT Webkit rendering engine. These run entirely "headless" and do not require a display or display service.

wkhtmltopdf convert a html to pdf with all images and css.

1-Install wkhtmltopdf: download from link.
or
sudo apt-get install wkhtmltopdf  // in ubuntu.

2.Conversion: 
convert your html file to pdf by the following command:

wkhtmltopdf html_file_name.html pdf_name.pdf

this will create the pdf file named pdf_name.pdf inside the current directory.

Example:


 wkhtmltopdf www.yahoo.com yahoo.pdf

this command create a yahoo.pdf inside the current working directory and looks like same as html file.

 

Friday, 4 April 2014

JavaScript functions slice and splice

In JavaScript, mistaking slice for splice (or vice versa) is a common mistake. These two functions, although they have similar names, are doing two completely different things.

slice:Array’s slice is quite similar to String’s slice. According to the specification, slice needs to accept two arguments, start and end. It will return a new array containing the elements from the given start index up the one right before the specified end index. It’s not very difficult to understand what slice does:

Example:
'abcdefg' .slice(1,2)       // 'b'
'abcdefg' .slice(1,3)       // 'bc'
[1,2,3,4,5] .slice(1,2)     //  [2]
[1,2,3,4,5] .slice(1,3)     //  [2,3]


An important aspect of slice is that it does not change the array which invokes it. The following code fragment illustrates the behavior. As you can see, arr1 keeps its elements and arr2 gets the sliced version thereof.

var arr1 = [1,2,3,4,5];
var arr2 = arr1.slice(1, 2);
console.log(arr1);          // [1,2,3,4,5]
console.log(arr1);          // [2]




splice: Although splice also takes two arguments (at minimum), the meaning is very different:


[1,2,3,4,5] .splice(1,2)     //  [2, 3]



splice also mutates the array that calls it. This is not supposed to be a surprise, after all the name splice implies it.


var arr1 = [1,2,3,4,5];
var arr2 = arr1.splice(1, 2);
console.log(arr1);          //
[1, 4, 5]
 console.log(arr1);          // [2,3]