Added fb_name helper

This commit is contained in:
Mike Mangino 2010-05-05 12:07:05 -04:00
parent e60e187a05
commit ad3ee1c026
6 changed files with 113 additions and 4 deletions

View File

@ -1,13 +1,25 @@
# Facebooker2
require "mogli"
module Facebooker2
class << self
attr_accessor :api_key, :secret, :app_id
end
def self.cast_to_facebook_id(object)
if object.kind_of?(Mogli::Profile)
object.id
elsif object.respond_to?(:facebook_id)
object.facebook_id
else
object
end
end
end
require "mogli"
require "facebooker2/rails/controller"
require "facebooker2/rails/helpers/facebook_connect"
require "facebooker2/rails/helpers/user"
require "facebooker2/rails/helpers"

View File

@ -0,0 +1,8 @@
module Facebooker2
module Rails
module Helpers
include FacebookConnect
include User
end
end
end

View File

@ -0,0 +1,52 @@
require "ruby-debug"
module Facebooker2
module Rails
module Helpers
module User
# Render an fb:name tag for the given user
# This renders the name of the user specified. You can use this tag as both subject and object of
# a sentence. <em> See </em> http://wiki.developers.facebook.com/index.php/Fb:name for full description.
# Use this tag on FBML pages instead of retrieving the user's info and rendering the name explicitly.
#
def fb_name(user, options={})
options = fb_transform_keys(options,FB_NAME_OPTION_KEYS_TO_TRANSFORM)
fb_assert_valid_keys(options, FB_NAME_VALID_OPTION_KEYS)
options.merge!(:uid => Facebooker2.cast_to_facebook_id(user))
content_tag("fb:name",nil, fb_stringify_vals(options))
end
FB_NAME_OPTION_KEYS_TO_TRANSFORM = {:first_name_only => :firstnameonly,
:last_name_only => :lastnameonly,
:show_network => :shownetwork,
:use_you => :useyou,
:if_cant_see => :ifcantsee,
:subject_id => :subjectid}
FB_NAME_VALID_OPTION_KEYS = [:firstnameonly, :linked, :lastnameonly, :possessive, :reflexive,
:shownetwork, :useyou, :ifcantsee, :capitalize, :subjectid]
def fb_stringify_vals(hash)
result={}
hash.each do |key,value|
result[key]=value.to_s
end
result
end
def fb_transform_keys(options,transformation_hash)
new_hash = {}
options.each do |key,value|
new_key = transformation_hash[key]||key
new_hash[new_key]=value
end
new_hash
end
FB_ALWAYS_VALID_OPTION_KEYS = [:class, :style]
def fb_assert_valid_keys(options,*valid_keys)
unknown_keys = options.keys - [valid_keys + FB_ALWAYS_VALID_OPTION_KEYS].flatten
raise(ArgumentError, "Unknown key(s): #{unknown_keys.join(", ")}") unless unknown_keys.empty?
end
end
end
end
end

View File

@ -17,4 +17,18 @@ describe Facebooker2 do
Facebooker2.app_id.should == "12345"
end
end
describe "Casting to facebook_id" do
it "grabs the id from Mogli::User objects" do
Facebooker2.cast_to_facebook_id(Mogli::User.new(:id=>1234)).should == 1234
end
it "checks for facebook_id and calls that" do
Facebooker2.cast_to_facebook_id(mock("name",:facebook_id=>1234)).should == 1234
end
it "returns the passed object otherwise" do
Facebooker2.cast_to_facebook_id("1234").should == "1234"
end
end
end

View File

@ -1,5 +1,4 @@
require "spec_helper"
require "ruby-debug"
describe Facebooker2::Rails::Helpers::FacebookConnect, :type=>:helper do
include Facebooker2::Rails::Helpers::FacebookConnect
describe "fb_login_and_redirect" do

24
spec/helpers/user_spec.rb Normal file
View File

@ -0,0 +1,24 @@
require "spec_helper"
describe Facebooker2::Rails::Helpers::User, :type=>:helper do
include Facebooker2::Rails::Helpers::User
describe "name" do
it "has an fb_name tag" do
fb_name("1").should == "<fb:name uid=\"1\"></fb:name>"
end
it "translates keys from uderscore to facebook" do
fb_name("loggedinuser",:use_you=>true).should == "<fb:name uid=\"loggedinuser\" useyou=\"true\"></fb:name>"
end
it "raises an error on invalid keys" do
lambda do
fb_name("loggedinuser",:invalid=>true)
end.should raise_error(ArgumentError)
end
it "casts the user to a facebook id" do
fb_name(Mogli::User.new(:id=>"123")).should =~ /uid="123"/
end
end
end