person_image_link and object_path return empty strings if parameter is nil.

This commit is contained in:
Sarah Mei 2010-11-25 12:26:46 -08:00
parent 050918a310
commit ef2fa21d06
2 changed files with 28 additions and 2 deletions

View file

@ -22,8 +22,9 @@ module ApplicationHelper
end end
def object_path(object, opts = {}) def object_path(object, opts = {})
return "" if object.nil?
object = object.person if object.is_a? User object = object.person if object.is_a? User
eval("#{object.class.to_s.underscore}_path(object, opts)") eval("#{object.class.name.underscore}_path(object, opts)")
end end
def object_fields(object) def object_fields(object)
@ -77,10 +78,11 @@ module ApplicationHelper
end end
def person_image_link(person, opts = {}) def person_image_link(person, opts = {})
return "" if person.nil?
if opts[:to] == :photos if opts[:to] == :photos
link_to person_image_tag(person), person_photos_path(person) link_to person_image_tag(person), person_photos_path(person)
else else
link_to person_image_tag(person), object_path(person) link_to person_image_tag(person), person_path(person)
end end
end end

View file

@ -18,6 +18,30 @@ describe ApplicationHelper do
person_url(@user).should == "/users/#{@user.id}" person_url(@user).should == "/users/#{@user.id}"
end end
describe "#object_path" do
it "returns an empty string if object is nil" do
object_path(nil).should == ""
end
it "returns person path if it's a person" do
object_path(@person).should == person_path(@person)
end
it "returns person path if it's a user" do
object_path(@user).should == person_path(@user.person)
end
end
describe "#person_image_link" do
it "returns an empty string if person is nil" do
person_image_link(nil).should == ""
end
it "returns a link containing the person's photo" do
person_image_link(@person).should include(image_or_default(@person))
end
it "returns a link to the person's profile" do
person_image_link(@person).should include("href=\"#{person_path(@person)}\"")
end
end
describe "markdownify" do describe "markdownify" do
describe "autolinks" do describe "autolinks" do
it "should not allow basic XSS/HTML" do it "should not allow basic XSS/HTML" do