Sunday, April 17, 2011

Passing target="_top" to will_paginate rails plugin for Facebook iFrame application.

First things first You cannot pass option of target in default will_paginate plugin that is shipped. So you have to modify the plugin.
There is a parameter called "renderer" that you can pass to specify which renderer class to use for will_paginate function. The default is LinkRenderer. In my solution you have to write a new class which will be child of LinkRenderer class. And you will pass this class as a parameter to will_paginate function. Simple.

Here how you define drive the class. The name of file is fb_link_renderer.rb and I placed it under lib folder.

class FBLinkRenderer < WillPaginate::ViewHelpers::LinkRenderer

and than you rewrite in link function. The link function is a private function as it is in LinkRenderer class.


def link(text, target, attributes = {})
if target.is_a? Fixnum
attributes[:rel] = rel_value(target)
target = url(target)
end
attributes[:href] = target
attributes[:target] = "_top"
tag(:a, text, attributes)
end



Here is the complete code for the new class


class FBLinkRenderer < WillPaginate::ViewHelpers::LinkRenderer private def link(text, target, attributes = {}) if target.is_a? Fixnum attributes[:rel] = rel_value(target) target = url(target) end attributes[:href] = target attributes[:target] = "_top" tag(:a, text, attributes) end end



and here is how you use it


= will_paginate(data, :params => "apps.facebook.com/fb-app-name", :renderer => 'FBLinkRenderer')

Saturday, April 16, 2011

Facebook Friend Selector in Ruby on Rails (ROR) using jquery autocomplete plugin

I am using Rails 3 and Facebooker2 with Mogli. So you should have those if you are making a facebook project. Also this is a iFrame app. Since FBML apps so soon be deprecated.

First of all you should have a Controller with a action that gets all your friends with there names and id, because we will need those for our custom Friend Selector. I had to make it because Facebook's Friend selector wasn't behaving well, some time it will show up and most of the times it wont show up. So back to the code .
Here is code snippet that gets you friend list and convert to JSON to be consumed on client side.
I wont discuss how to setup Facebooker2 and Mogli.

@friends = Array.new
current_facebook_user.friends(:id,:name).each do|friend|
@friends << {:name => friend.name, :id => friend.id}
end
@friends = @friends.to_json


So thats it for the Rails 3 code.

Now we need to add references for jquery javascript and css. Here what I added

%script{:src => "http://code.jquery.com/jquery-latest.js", :type => "text/javascript"}
%link{:href => "http://dev.jquery.com/view/trunk/plugins/autocomplete/jquery.autocomplete.css", :rel => "stylesheet", :type => "text/css"}/
%script{:src => "http://dev.jquery.com/view/trunk/plugins/autocomplete/lib/jquery.bgiframe.min.js", :type => "text/javascript"}
%script{:src => "http://dev.jquery.com/view/trunk/plugins/autocomplete/lib/jquery.dimensions.js", :type => "text/javascript"}
%script{:src => "http://dev.jquery.com/view/trunk/plugins/autocomplete/jquery.autocomplete.js", :type => "text/javascript"}



I am using haml instead of normal html. Haml is more elegant and easy to code.

Ok now to code View for the action.


= form_tag(friend_selector_url(:host => "apps.facebook.com/app-name-at-facebook"), :method => "get", :target => "_top", :autocomplete => "off") do
= hidden_field_tag('friend_sel')
.identify-friend
%p{:style => "float:left;width:180px"} Identify the friend that you are asking about:
= text_field_tag("friend_uid")
= submit_tag('Ask', :name => 'btn')



We use will set the value of 'friend_sel' hidden field equal to friend id ,so that when form is submitted we can get friend id from params at the action of controller,using jquery autocomplete plugin.
Here is the last part of code. The jquery autoplugin.

:javascript
$("#friend_uid").autocomplete(#{@friends}, {
minChars: 0,
width: 310,
matchContains: true,
highlightItem: false,
formatItem: function (row, i, max) {
return row.name ;
},
formatMatch: function(row, i, max) {
return row.name + " " + row.id;
},
formatResult:function (row){
return row.name;
}
}).result(function(e, item) {
$('#friend_sel').val(item.id);
});



@friends is instance variable that we set in controller's action. 'friend_uid' is the id for text field that will be our autocomplete field.

Thats it for autocomplete friend selector.

Thanks

Friday, April 1, 2011

Famous Quotes

Dr Abdul Kalaam


It Is Very Easy To Defeat Someone, But It Is Very Hard To Win Some


Shakespeare


Never Play With The Feelings Of Others Because You May Win The Game
But The Risk Is That You Will Surely Lose The Person For A Life Time.




Napoleon


The world suffers a lot. Not because of the violence of bad people,
But because of the silence of good people!




Einstein


I am thankful to all those who said NO to me Its because of them I did it myself




Abraham Lincoln


If friendship is your weakest point then you are the strongest person
in the world




Shakespeare


Laughing Faces Do Not Mean That There Is Absence Of Sorrow! But It Means That They Have The Ability To Deal With It.




William Arthur


Opportunities Are Like Sunrises, If You Wait Too Long You Can Miss Them.




Hitler


When You Are In The Light, Everything Follows You, But When You Enter Into The Dark, Even Your Own Shadow Doesn't Follow You.




Shakespeare


Coin Always Makes Sound But The Currency Notes Are Always Silent. So When Your Value Increases Keep Yourself Calm and Silent