From 930e4d683c83f36d645f0d5d5575a2c3317d1e03 Mon Sep 17 00:00:00 2001 From: Mike Mangino Date: Wed, 5 May 2010 13:52:05 -0400 Subject: [PATCH] Added new user helpers --- lib/facebooker2/rails/helpers/user.rb | 39 ++++++++++++++++++++------- spec/helpers/user_spec.rb | 28 +++++++++++++++++++ 2 files changed, 57 insertions(+), 10 deletions(-) diff --git a/lib/facebooker2/rails/helpers/user.rb b/lib/facebooker2/rails/helpers/user.rb index c69717a..5d60c00 100644 --- a/lib/facebooker2/rails/helpers/user.rb +++ b/lib/facebooker2/rails/helpers/user.rb @@ -23,23 +23,42 @@ module Facebooker2 :subject_id => :subjectid} 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 diff --git a/spec/helpers/user_spec.rb b/spec/helpers/user_spec.rb index 7338749..c389455 100644 --- a/spec/helpers/user_spec.rb +++ b/spec/helpers/user_spec.rb @@ -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 == "" + end + + it "translates keys" do + fb_profile_pic(1,:facebook_logo=>true).should == + "" + 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 == "" + + end + end end