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.