Friday, 27 June 2014

Comparing two html files using nokogiri

How ever in practice if you want to compare two html files and the output will be customized according to your requirement like you want to change color or add some extra html tags in the modified tags or add any class to the modified node,the awesome way to do it by using nokogiri. here is an example of doing this:

Example: In this example I am adding the <del> tag before the deleted or modified element and just add the newly added elements to the first html file.

1. install nokogiri and nokogiri-diff.
    (nokogiri provide the efficient way to modify and access the elements of the htmls)
2. require 'nokogiri/diff' in your code.
3. parsing html by nokogiri, you can read any html file and can parse by nikogiri to get the nokogiri doc for operations.I am just giving the small example by parsing a html string to the nikogiri.

_first_doc =  Nokogiri::HTML('<p>hello</p><p>this is demo example</p><p>byee</p>')

_second_doc =  Nokogiri::HTML('<p>hii</p><p>this is demo example</p><p> good byee</p>')

4. Get the difference:

 _first_doc.diff(_second_doc,:removed => true) do |change,node|
  node.replace("<del>#{node.to_html}</del>")
 end // adding <del> tag to modified elements(node).

 _first_doc.diff(_second_doc,:added => true) do |change,node|
  _parent = _first_doc.search(node.parent.path).first
  _parent.add_child(node)
 end // adding newly added elements in the result.

5.Get the output : now you can see the changes by printing  _first_doc.to_html

output will be like:

<html><body>
<del><p>hello</p></del>
<p>hiii</p> 
<p>this is demo example</p>
<del><p>byee</p></del>
<p>good byee</p>
</body></html>









Saturday, 21 June 2014

Three ways to do eager loading in Rails

There are three ways to do eager load in rails.they are:

1. includes
2. preload and
3. eager_load

includes delegates the job to preload or eager_load depending on the presence or absence of condition related to one of the preloaded table.

preload is using separate DB queries to get the data.

eager_load is using one big query with LEFT JOIN for each eager loaded table.

In Rails 4 you should use #references combined with includes if you have the additional condition for one of the eager loaded table.

  Typically, when you want to use the eager loading feature you would use the includes method, which Rails encouraged you to use since Rails2 or maybe even Rails1 ;). And that works like a charm doing 2 queries:
    
User.includes(:addresses)
#  SELECT "users".* FROM "users" 
#  SELECT "addresses".* FROM "addresses" WHERE "addresses"."user_id"
IN (1, 2)
doing one query. So what is #includes for? It decides for you which 
way it is going to be.
User.preload(:addresses)
#  SELECT "users".* FROM "users" 
#  SELECT "addresses".* FROM "addresses" WHERE "addresses"."user_id" 
IN (1, 2)
 Apparently #preload behave just like #includes.
If you use #preload, it means you always want separate queries. If 
you use #eager_load you arelet Rails handle that decision.What is
query conditions. Let's see an example where #includes delegates to 
#eager_load so that there is one big query only.the decision based
on, you might ask. It is based on