From ad3ee1c0267aac970c9b331b37532b73ed9abe4a Mon Sep 17 00:00:00 2001 From: Mike Mangino Date: Wed, 5 May 2010 12:07:05 -0400 Subject: [PATCH] Added fb_name helper --- lib/facebooker2.rb | 18 ++++++++-- lib/facebooker2/rails/helpers.rb | 8 +++++ lib/facebooker2/rails/helpers/user.rb | 52 +++++++++++++++++++++++++++ spec/facebooker2_spec.rb | 14 ++++++++ spec/helpers/facebook_connect_spec.rb | 1 - spec/helpers/user_spec.rb | 24 +++++++++++++ 6 files changed, 113 insertions(+), 4 deletions(-) create mode 100644 lib/facebooker2/rails/helpers.rb create mode 100644 lib/facebooker2/rails/helpers/user.rb create mode 100644 spec/helpers/user_spec.rb diff --git a/lib/facebooker2.rb b/lib/facebooker2.rb index 0f6294d..9b2342f 100644 --- a/lib/facebooker2.rb +++ b/lib/facebooker2.rb @@ -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" \ No newline at end of file +require "facebooker2/rails/helpers/facebook_connect" +require "facebooker2/rails/helpers/user" +require "facebooker2/rails/helpers" \ No newline at end of file diff --git a/lib/facebooker2/rails/helpers.rb b/lib/facebooker2/rails/helpers.rb new file mode 100644 index 0000000..bf2a227 --- /dev/null +++ b/lib/facebooker2/rails/helpers.rb @@ -0,0 +1,8 @@ +module Facebooker2 + module Rails + module Helpers + include FacebookConnect + include User + end + end +end \ No newline at end of file diff --git a/lib/facebooker2/rails/helpers/user.rb b/lib/facebooker2/rails/helpers/user.rb new file mode 100644 index 0000000..c69717a --- /dev/null +++ b/lib/facebooker2/rails/helpers/user.rb @@ -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. See 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 \ No newline at end of file diff --git a/spec/facebooker2_spec.rb b/spec/facebooker2_spec.rb index 71c7601..55ac964 100644 --- a/spec/facebooker2_spec.rb +++ b/spec/facebooker2_spec.rb @@ -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 \ No newline at end of file diff --git a/spec/helpers/facebook_connect_spec.rb b/spec/helpers/facebook_connect_spec.rb index 90a1379..49ac810 100644 --- a/spec/helpers/facebook_connect_spec.rb +++ b/spec/helpers/facebook_connect_spec.rb @@ -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 diff --git a/spec/helpers/user_spec.rb b/spec/helpers/user_spec.rb new file mode 100644 index 0000000..7338749 --- /dev/null +++ b/spec/helpers/user_spec.rb @@ -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 == "" + + end + + it "translates keys from uderscore to facebook" do + fb_name("loggedinuser",:use_you=>true).should == "" + 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