urls are now unique, and a user can not add a friend with the exact same url
This commit is contained in:
parent
a0a0b9d0ad
commit
77c0bb5c4e
2 changed files with 19 additions and 2 deletions
|
|
@ -6,7 +6,7 @@ class Person
|
||||||
xml_accessor :url
|
xml_accessor :url
|
||||||
|
|
||||||
key :email, String
|
key :email, String
|
||||||
key :url, String, :unique => true
|
key :url, String
|
||||||
|
|
||||||
one :profile, :class_name => 'Profile', :foreign_key => :person_id
|
one :profile, :class_name => 'Profile', :foreign_key => :person_id
|
||||||
many :posts, :class_name => 'Post', :foreign_key => :person_id
|
many :posts, :class_name => 'Post', :foreign_key => :person_id
|
||||||
|
|
@ -16,15 +16,26 @@ class Person
|
||||||
validates_presence_of :url
|
validates_presence_of :url
|
||||||
validates_format_of :url, :with =>
|
validates_format_of :url, :with =>
|
||||||
/^(https?):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*(\.[a-z]{2,5})?(:[0-9]{1,5})?(\/.*)?$/ix
|
/^(https?):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*(\.[a-z]{2,5})?(:[0-9]{1,5})?(\/.*)?$/ix
|
||||||
validates_presence_of :email
|
|
||||||
|
|
||||||
|
validates_true_for :url, :logic => lambda { self.url_unique?}
|
||||||
|
|
||||||
|
|
||||||
|
validates_presence_of :email
|
||||||
|
|
||||||
before_validation :clean_url
|
before_validation :clean_url
|
||||||
|
|
||||||
def real_name
|
def real_name
|
||||||
self.profile.first_name + " " + self.profile.last_name
|
self.profile.first_name + " " + self.profile.last_name
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
|
def url_unique?
|
||||||
|
same_url = Person.first(:url => self.url)
|
||||||
|
return same_url.nil? || same_url.id == self.id
|
||||||
|
end
|
||||||
|
|
||||||
def clean_url
|
def clean_url
|
||||||
self.url ||= "http://localhost:3000/" if self.class == User
|
self.url ||= "http://localhost:3000/" if self.class == User
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,10 @@
|
||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
|
||||||
describe Person do
|
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
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue