Added new user helpers

This commit is contained in:
Mike Mangino 2010-05-05 13:52:05 -04:00
parent ad3ee1c026
commit 930e4d683c
2 changed files with 57 additions and 10 deletions

View File

@ -24,6 +24,25 @@ module Facebooker2
FB_NAME_VALID_OPTION_KEYS = [:firstnameonly, :linked, :lastnameonly, :possessive, :reflexive, FB_NAME_VALID_OPTION_KEYS = [:firstnameonly, :linked, :lastnameonly, :possessive, :reflexive,
:shownetwork, :useyou, :ifcantsee, :capitalize, :subjectid] :shownetwork, :useyou, :ifcantsee, :capitalize, :subjectid]
def fb_profile_pic(user, options={})
options = options.dup
validate_fb_profile_pic_size(options)
options = fb_transform_keys(options,FB_PROFILE_PIC_OPTION_KEYS_TO_TRANSFORM)
fb_assert_valid_keys(options,FB_PROFILE_PIC_VALID_OPTION_KEYS)
options.merge!(:uid => Facebooker2.cast_to_facebook_id(user))
content_tag("fb:profile-pic", nil,fb_stringify_vals(options))
end
FB_PROFILE_PIC_OPTION_KEYS_TO_TRANSFORM = {:facebook_logo => 'facebook-logo'}
FB_PROFILE_PIC_VALID_OPTION_KEYS = [:size, :linked, 'facebook-logo', :width, :height]
VALID_FB_PROFILE_PIC_SIZES = [:thumb, :small, :normal, :square]
def validate_fb_profile_pic_size(options)
if options.has_key?(:size) && !VALID_FB_PROFILE_PIC_SIZES.include?(options[:size].to_sym)
raise(ArgumentError, "Unknown value for size: #{options[:size]}")
end
end
def fb_stringify_vals(hash) def fb_stringify_vals(hash)
result={} result={}
hash.each do |key,value| hash.each do |key,value|

View File

@ -21,4 +21,32 @@ describe Facebooker2::Rails::Helpers::User, :type=>:helper do
fb_name(Mogli::User.new(:id=>"123")).should =~ /uid="123"/ fb_name(Mogli::User.new(:id=>"123")).should =~ /uid="123"/
end end
end end
describe "profile pic" do
it "has an fb_profile_pic tag" do
fb_profile_pic("loggedinuser").should == "<fb:profile-pic uid=\"loggedinuser\"></fb:profile-pic>"
end
it "translates keys" do
fb_profile_pic(1,:facebook_logo=>true).should ==
"<fb:profile-pic facebook-logo=\"true\" uid=\"1\"></fb:profile-pic>"
end
it "validates the size option" do
lambda do
fb_profile_pic(1,:size=>:invalid)
end.should raise_error(ArgumentError)
end
it "raises an error on invalid keys" do
lambda do
fb_profile_pic(1,:invalid=>true)
end.should raise_error(ArgumentError)
end
it "casts the user to a facebook_id" do
fb_profile_pic(Mogli::User.new(:id=>1)).should == "<fb:profile-pic uid=\"1\"></fb:profile-pic>"
end
end
end end