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,22 +24,41 @@ module Facebooker2
FB_NAME_VALID_OPTION_KEYS = [:firstnameonly, :linked, :lastnameonly, :possessive, :reflexive,
: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)
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
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
new_hash
end
FB_ALWAYS_VALID_OPTION_KEYS = [:class, :style]
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

View File

@ -21,4 +21,32 @@ describe Facebooker2::Rails::Helpers::User, :type=>:helper do
fb_name(Mogli::User.new(:id=>"123")).should =~ /uid="123"/
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