From 027d953cf4e8b637fe16c9036db8218192a5781c Mon Sep 17 00:00:00 2001
From: ilya <ilya@laptop.(none)>
Date: Mon, 5 Jul 2010 23:28:14 -0400
Subject: [PATCH] DG IZ added the FriendRequest model.

---
 app/models/friend_request.rb       | 25 ++++++++++++++++
 spec/models/friend_request_spec.rb | 46 ++++++++++++++++++++++++++++++
 spec/models/person_spec.rb         |  2 +-
 3 files changed, 72 insertions(+), 1 deletion(-)
 create mode 100644 app/models/friend_request.rb
 create mode 100644 spec/models/friend_request_spec.rb

diff --git a/app/models/friend_request.rb b/app/models/friend_request.rb
new file mode 100644
index 0000000000..33de9013f2
--- /dev/null
+++ b/app/models/friend_request.rb
@@ -0,0 +1,25 @@
+class FriendRequest
+  include MongoMapper::Document
+  include ROXML
+
+  xml_accessor :sender, :as => Person
+  xml_accessor :recipient, :as => Person
+
+  key :sender, Person
+  key :recipient, Person
+
+  validates_presence_of :sender, :recipient
+
+  def accept
+    f = Friend.new
+    f.email = self.sender.email
+    f.url = self.sender.url
+    f.save
+    self.destroy
+  end
+
+  def reject
+    self.destroy
+  end
+  
+end
diff --git a/spec/models/friend_request_spec.rb b/spec/models/friend_request_spec.rb
new file mode 100644
index 0000000000..ed256fd3cf
--- /dev/null
+++ b/spec/models/friend_request_spec.rb
@@ -0,0 +1,46 @@
+require 'spec_helper'
+
+describe FriendRequest do 
+  before do
+    sender = Factory.build(:user, :email => "bob@aol.com", :url => "http://google.com/")
+    recipient = Factory.build(:person, :email => "robert@grimm.com", :url => "http://robert.com")
+    @r = FriendRequest.create(:sender => sender, :recipient => recipient)
+  end
+  
+  it 'should have sender and recipient credentials after serialization' do
+    xml = @r.to_xml.to_s
+    xml.include?(@r.sender.url).should be true
+    xml.include?(@r.sender.email).should be true
+    xml.include?(@r.recipient.url).should be true
+    xml.include?(@r.recipient.email).should be true
+  end
+
+  describe "acceptance" do
+    it 'should create a friend' do
+      Friend.count.should be 0
+      @r.accept
+      Friend.count.should be 1
+    end
+
+    it 'should remove the request' do
+      FriendRequest.count.should be 1
+      @r.accept
+      FriendRequest.count.should be 0
+    end
+  end
+
+  describe "rejection" do 
+    it 'should not create a friend' do
+      Friend.count.should be 0
+      @r.reject
+      Friend.count.should be 0
+    end
+
+    it 'should remove the request' do
+      FriendRequest.count.should be 1
+      @r.reject
+      FriendRequest.count.should be 0
+    end
+  end
+  
+end
diff --git a/spec/models/person_spec.rb b/spec/models/person_spec.rb
index 59ef0c21c5..74602cebbf 100644
--- a/spec/models/person_spec.rb
+++ b/spec/models/person_spec.rb
@@ -11,8 +11,8 @@ describe Person do
     user = Factory.create(:user)
     friend = Factory.build(:friend, :url => user.url)
     friend.valid?.should == false
-
   end
+
   it 'should serialize to xml' do
     friend_one = Factory.create(:friend)
     xml = friend_one.to_xml.to_s
-- 
GitLab