urls are now unique, and a user can not add a friend with the exact same url

This commit is contained in:
maxwell 2010-06-29 16:04:15 -07:00
parent a0a0b9d0ad
commit 77c0bb5c4e
2 changed files with 19 additions and 2 deletions

View file

@ -6,7 +6,7 @@ class Person
xml_accessor :url
key :email, String
key :url, String, :unique => true
key :url, String
one :profile, :class_name => 'Profile', :foreign_key => :person_id
many :posts, :class_name => 'Post', :foreign_key => :person_id
@ -16,6 +16,11 @@ class Person
validates_presence_of :url
validates_format_of :url, :with =>
/^(https?):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*(\.[a-z]{2,5})?(:[0-9]{1,5})?(\/.*)?$/ix
validates_true_for :url, :logic => lambda { self.url_unique?}
validates_presence_of :email
before_validation :clean_url
@ -24,8 +29,14 @@ class Person
self.profile.first_name + " " + self.profile.last_name
end
protected
def url_unique?
same_url = Person.first(:url => self.url)
return same_url.nil? || same_url.id == self.id
end
def clean_url
self.url ||= "http://localhost:3000/" if self.class == User
if self.url

View file

@ -1,4 +1,10 @@
require 'spec_helper'
describe Person do
it 'should not allow a friend with the same url as the user' do
user = Factory.create(:user)
friend = Factory.build(:friend, :url => user.url)
friend.valid?.should == false
end
end