Thursday, August 26, 2010

Using 'Aroung Alias'

You can write an Around Alias in three simple steps:

1. You alias a method.
2. You redefine it.
3. You call the old method from the new method.

Example:

class String
alias :orig_length :length

def length
"Length of string '#{self}' is: #{orig_length}"
end
end

"abc".length => "Length of string 'abc' is: 3"

Friday, March 20, 2009

Helper method to user ordinal of any number...

# APPLY THE SUPERSCRIPT TO THE NUMBER SELECTED
def number_to_ordinal(num)
num = num.to_i
if (10...20)===num
"#{num}th"
else
g = %w{ th st nd rd th th th th th th }
a = num.to_s
c=a[-1..-1].to_i
a + g[c]
end
end

Tuesday, January 27, 2009

Subdomains and Rails

I was trying to setup the proper environment settings in Rails 2 to support cross-subdomain cookie sharing (so the user doesn’t have to login again if they manage multiple subdomain accounts). I was experiencing some inconsistencies when trying to make sure it was working. Here is a tip:

Clear your cookies (the Firefox Web Developer plug-in is great for viewing/deleting cookies being assigned) before testing this! If you have any old cookies from an old login session before you converted to subdomains, you’ll experience some oddities that will make you think it isn’t working.

BTW, if you are using the Ruby on Rails Wiki page for customizing your session or cookie settings, you’ll notice that the wiki provides multiple ways to change the session domain. This is the line that worked for me to customize the domain using Rails 2.0.2:

ActionController::Base.session_options[:session_domain] = ‘.mydomain.com’

Using constants in shared config file

Constants of rails application can be used in shared config file instead of using it in different development.rb and production.rb for different environments.
You can create ruby config file "shared.rb" in "config/environments/" folder of your rails application.
Then just add one statement in "config/environment.rb" file to use shared file throughout the application as following:

require 'environments/shared'

Tuesday, December 30, 2008

Using line - wrapping after specified limit.

Following method is useful to display line with wrapping after specified limit. So that if you give a limit to 50, then it will insert '
' after 50 characters. Also, it will not break line from middle of any word. So after using this method, you can provide justification method to display text block properly.

Just pass a paragraph to display as 'par' and also provide a limit as 'length'.

def line_wrapping_after_limit(par, length)
    b = []
    res = []
    if par.length > length
      a = par.split(' ')
      len = 0
      0.upto(a.length-1) do |i|
        temp = a[i]
        while(temp.length > length)
          res << (temp[0, length-1] + "-").to_s
          temp = temp[length-1, a[i].length]
        end
        b << (temp + " ")
        len += temp.length + 1
        tot_len = i.eql?(a.length-1) ? len : len + a[i+1].length
        if(tot_len > length)
          b.join('')
          res << (b.to_s)
          len = 0
          b = []
        end
      end
      b.join('')
      res << (b.to_s)
      res.join("<\br/>

")

    else
      par
    end
  end

Thursday, December 25, 2008

Fetching photos from facebook in rails application

If you want to get all photos from each album of any particular facebook user, then try the following code. I have used it and it works in my application. Hope that u can also enjoy it......

## Just initialize two arrays ## 

@albums = []
@fbphotos = []

## Get the facebook user information who is logged in ## 

@fbuser = fbsession.users_getInfo(:uids => fbsession.session_user_id, :fields =>     ["first_name", "last_name", "pic_big"])

## Then collect all album information providing his/her facebook user_id ##

@fbalbums = fbsession.photos_getAlbums(:uid => @fbuser.uid)
        
doc = Hpricot::XML(@fbalbums.to_s)

(doc/:album).each do |a|
          aid = a.at('aid').innerHTML
          @albums <<>
end
@albums.each do |a|
          @fbphotos << uid =""> @fbuser.uid, :aid => a)
end

## Thus, finally you will get all the photos stored in @fbphotos from each album... If u want photos for any particular album, then not need to write entire code, just  use fbsession.photos_get method with passing parameter :aid i.e album_id...

Integrating ruby on rails appliation with facebook

I have created one application which directly integrates with facebook and uses facebook data in ror application.

To implement it, you have to just log in to facebook and from 'Developer' application, you have to add one application. Then facebook will provide you an api_key and secret_key.

You should also have rfacebook gem installed before creating ror application. After installing that gem, you will find 'facebook.yml' file in application's /config folder. In that file, you can insert api_key and secret_key provided from facebook.

Then you can integrate your ror application with facebook. For more help on functions, visit 


Wednesday, December 24, 2008

To resolve problem of rounding-off numbers

Following code is used to rounding-off any floating numbers with specified decimal places. There is also helper method provided by rails to do the same which is "number_with_precision". But if we want to rounding-off the floating number in controller, then following one-liner code may be helpful for that case.

# Specify your number to be round-off instead of FLOATING_NUMBER and how many decimal places you want instead of i.

(FLOATING_NUMBER * 10**i).round.to_f / 10**i