13 March 2008

Facebooker Publisher and URL Fixes for Rails 1.2.x Use

I'm using Facebooker, and specifically the new Publisher class it has, with a Rails 1.2.6 app (hopefully I'll get us on Rails 2 sooner than later). But, Publisher uses some methods that are only available in Rails 2 it appears, as well as the mechanism it uses to look up Publisher view templates doesn't work properly in Rails 1.2. Also, link generation doesn't work quite right in all cases, so I have a fix for that too. Documenting my changes here for my own reference, as well as anyone else it may help.

For Publisher, I've made two changes to remedy these issues, both in facebooker/lib/facebooker/rails/publisher.rb:

In the initialize_template_class method, change the line:

returning ActionView::Base.new([template_root,File.join(template_root,self.class.controller_path)], assigns, self) do |template|

to instead be:
returning ActionView::Base.new(template_root, assigns, self) do |template|

This fixes the problem where the Publisher would look for views in a directory path that contained your publisher's name twice.

The second one is to change the inherited method's call to send! to instead simply call send.

For the link generation, I tweaked the implementation of Facebooker's UrlRewriter#link_to_canvas? method, shown in entirety here:

def link_to_canvas?(params, options)
option_override = options[:canvas]
options[:only_path] = false if !option_override.nil?
return false if option_override == false # important to check for false. nil should use default behavior
option_override || @request.parameters["fb_sig_in_canvas"] == "1" || @request.parameters[:fb_sig_in_canvas] == "1"
end

The result is that if the canvas parameter is specified, then we force a full URL, instead of only a path (which is the default). This covers apps that have both a regular web application and a Facebook app, where you are generating links that point to one from the other (e.g. you're in Facebook, but generating a link that points to your regular web app).

2 comments:

Jatinder Singh said...

Hi Chris,

I am on Edge Rails and have created a publisher in models.

I use "profile" method to update profiles of facebook users. The call to profile looks like following,
profile render :partial => "/users/profile", :assigns => {:user =>
user_to_be_updated }

But I always get an error,
"ActionView::TemplateFinder::InvalidViewPath
(Unprocessed view path found:
"/home/jatinder/rails_apps/app-name/app/views/."


Any clue whats happening here?

Chris said...

What version of Rails are you on? Also, where are you storing your "_profile.rhtml" (or _profile.html.erb if Rails 2)? With my change, it will be expecting your partial to be in .../app/views/publisher controller name/. So, let's say your profile method for your Publisher is in a "FacebookPublisher" publisher, your views would go in .../app/views/facebook_publisher. And then, because you have a path specified for your partial, it'll look for .../app/views/facebook_publisher/users/_profile.rhtml. I don't use sub-paths or full paths to my partials, I just put them directly into the publisher's view directory. So, instead, I have what would equate to .../app/views/facebook_publisher/_profile.rhtml, and my profile publisher method would be:

render :partial => "profile"...