Jumat, 16 Desember 2011

Parsing XML with Nokogiri

There are so many related articles about this things.

But i want to write down the spesific about how to parsing xml on nokogiri,


   require 'open-uri'
   doc = Nokogiri::HTML(open("http://www.w3schools.com/xml/note.xml"))   
  

This is the result example:
results

Click it. This is the sample xml:

         The Xml File
  

and you want to get the "Jani" from the xml.

here what we do

because Jani is children from note node.
so we have to call the note first.

we will get the note's children.:

      result = doc.xpath('//note')            
  
and last, after we get the children. we parse with :

           
      result = result.xpath('//from').text           
  

Or, we could make it simplifier by:

     doc = Nokogiri::HTML(open("http://www.w3schools.com/xml/note.xml")) 
     result = doc.xpath('//note/from').text      
  
# in this case, we are find for the root of "note", and then search the children node, named "from". and this will be "JANI"


Hope it will helps

Kamis, 15 Desember 2011

making a gem

if you want to make gem , or it is same as our own library,
and you don't know how to do it.

here some reference how to build that

Railscats :
http://railscasts.com/episodes/135-making-a-gem

rubyonrails.org

http://guides.rubyonrails.org/plugins.html

Eager Loading

When we are building a website, a small website, it wont be feel slow, if we are querying a large of data from database.

But.. if you re building a large scale rails application. This is one of the methods you have to use, Eager Loading

Eager loading is One way to improve performance is to cut down on the number of SQL queries.
It will improve query on rails application

I dont quite understand how rails do it.
But simple, programmer doesnt have to know all the things(code) that written down. lol.

This is the example


      class Post < ActiveRecord::Base
           belongs_to :author
           has_many   :comments
       end
  

Then you are querying it on the view.


for post in Post.all
  puts "Post:            " + post.title
  puts "Written by:      " + post.author.name
  puts "Last comment on: " + post.comments.first.created_on
end
 

What happened?
they will querying to comments table, as much as data on the posts tables.
So it's really painfull.

So with code below :

for post in Post.find(:all, :include => :author)
 

There will be just 1 query to the database.

And it will make your application lightener..

Rabu, 14 Desember 2011

basic gem for building rails application

There are some various gem for building application.
but here i will list, the most common rails application gem used.


1. Devise
Used for authentication

gem install devise

source : https://github.com/plataformatec/devise

2. Easy Roles
Used for user roles.


gem install easy_roles


source : https://github.com/platform45/easy_roles

Minggu, 11 Desember 2011

jquery bublle popup for help

There are many jquery plugins help, for adding a bubble/ popup for help button,
but for me , this is the simplyest and easy to configure

This is the link
Jquery bubble popup

This is the example:
Examples

This is the example code:
 $(document).ready(function(){  
 //create a bubble popup for each DOM element with class attribute as "text", "button" or "link" and LI, P, IMG elements.  
 $('.text, .button, .link, li, p.paragraph, #myimage1, #myimage2').CreateBubblePopup({  
 position : 'top',  
 align : 'center',  
 innerHtml: 'Take a look to the HTML source of this page  
 \  
 to learn how the plugin works!',  
 innerHtmlStyle: {  
 color:'#FFFFFF',  
 'text-align':'center'  
 },  
 themeName: 'all-black',  
 themePath: 'images/jquerybubblepopup-theme'  
 });  
 });  
 
Copyright 2009 ruby on rails. Powered by Blogger
Blogger Templates created by Deluxe Templates
Wordpress by Wpthemescreator