From 9fe37b1d8ccaf4be467c50b46f090c137157f3b7 Mon Sep 17 00:00:00 2001
From: Raphael <raphael@joindiaspora.com>
Date: Mon, 2 Aug 2010 16:11:47 -0700
Subject: [PATCH] RS, IZ; Cleaned up user spec, started to add
 current_user.post

---
 app/models/post.rb                 |   5 ++
 app/models/user.rb                 |   7 ++
 spec/models/status_message_spec.rb |   6 +-
 spec/models/user_spec.rb           | 112 ++++++++++++++---------------
 4 files changed, 70 insertions(+), 60 deletions(-)

diff --git a/app/models/post.rb b/app/models/post.rb
index f270666fad..b1002ce917 100644
--- a/app/models/post.rb
+++ b/app/models/post.rb
@@ -26,6 +26,11 @@ class Post
   before_destroy :propagate_retraction
   after_destroy :destroy_comments, :remove_from_view
 
+  def self.instantiate params
+    self.create params
+  end
+
+#Querying
   def self.stream
     Post.sort(:created_at.desc).all
   end
diff --git a/app/models/user.rb b/app/models/user.rb
index a7326c18a9..0f97022d71 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -13,6 +13,13 @@ class User < Person
  
  
   
+  ######## Posting ########
+
+  def post(class_name, options = {})
+    options[:person] = self
+    model_class = class_name.to_s.camelize.constantize
+    post = model_class.instantiate(options)
+  end
 
   ######## Commenting  ########
   def comment(text, options = {})
diff --git a/spec/models/status_message_spec.rb b/spec/models/status_message_spec.rb
index 4068972400..4bab96af29 100644
--- a/spec/models/status_message_spec.rb
+++ b/spec/models/status_message_spec.rb
@@ -11,7 +11,11 @@ describe StatusMessage do
     n.message = "wales"
     n.valid?.should be true
   end
-   
+  
+  it 'should be postable through the user' do
+    status = @user.post(:status_message, :message => "Users do things")
+  end
+
   describe "XML" do
     it 'should serialize to XML' do
       message = Factory.create(:status_message, :message => "I hate WALRUSES!")
diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb
index 1c4f1cb740..e9640d07ad 100644
--- a/spec/models/user_spec.rb
+++ b/spec/models/user_spec.rb
@@ -7,75 +7,69 @@ describe User do
     Person.count.should == n+1
   end
 
-  it "should be able to accept a pending friend request" do
-    @user = Factory.create(:user)
-    @friend = Factory.create(:person, :active => false)
-    r = Request.instantiate(:to => @user.url, :from => @friend)
-    r.save
-    Person.all.count.should == 2
-    Request.for_user(@user).all.count.should == 1
-    @user.accept_friend_request(r.id)
-    Request.for_user(@user).all.count.should == 0
-    Person.where(:id => @friend.id).first.active.should == true
-  end
+  describe 'friend requesting' do
+    before do
+      @user = Factory.create(:user)
 
-  it 'should be able to ignore a pending friend request' do
-    @user = Factory.create(:user)
-    @friend = Factory.create(:person, :active => false)
-    r = Request.instantiate(:to => @user.url, :from => @friend)
-    r.save
+    end
 
-    Person.count.should == 2
-    @friend.active.should == false
+    it "should be able to accept a pending friend request" do
+      friend = Factory.create(:person, :active => false)
+      r = Request.instantiate(:to => @user.url, :from => friend)
+      r.save
+      Person.all.count.should == 2
+      Request.for_user(@user).all.count.should == 1
+      @user.accept_friend_request(r.id)
+      Request.for_user(@user).all.count.should == 0
+      Person.where(:id => friend.id).first.active.should == true
+    end
 
-    @user.ignore_friend_request(r.id)
+    it 'should be able to ignore a pending friend request' do
+      friend = Factory.create(:person, :active => false)
+      r = Request.instantiate(:to => @user.url, :from => friend)
+      r.save
 
-    Person.count.should == 1
-    Request.count.should == 0
-  end
+      Person.count.should == 2
+      friend.active.should == false
 
-  it 'should not be able to friend request an existing friend' do
-    @user = Factory.create(:user)
-    @friend = Factory.create(:person)
+      @user.ignore_friend_request(r.id)
 
-    @user.send_friend_request_to( @friend.url ).should be nil
-  end
+      Person.count.should == 1
+      Request.count.should == 0
+    end
 
-  it 'should be able to give me the terse url for webfinger' do
-    user = Factory.create(:user)
-    user.terse_url.should == 'example.com'
-  end
+    it 'should not be able to friend request an existing friend' do
+      friend = Factory.create(:person)
 
-  it 'should be able to unsubscribe from a status.net user' do
-    @user = Factory.create(:user)
-    author = Factory.create(:author)
-    Author.all.count.should == 1
-    q = Request.send :class_variable_get, :@@queue
-    q.stub!(:add_hub_unsubscribe_request)
-    q.should_receive(:add_hub_unsubscribe_request)
+      @user.send_friend_request_to( friend.url ).should be nil
+    end
 
-    @user.unsubscribe_from_pubsub(author.id)  
-    Author.all.count.should == 0
-  end
-  
-  it 'should be able to update their profile and send it to their friends' do 
-    Factory.create(:person)
-    p = {:profile => {:first_name => 'bob', :last_name => 'billytown', :image_url => "http://clowntown.com"}}
-    
-    @user = Factory.create(:user)
-     p = {:profile => {:first_name => 'bob', :last_name => 'billytown', :image_url => "http://clown.com"}}
-    
-    n = Profile.send :class_variable_get, :@@queue
-    n.should_receive(:process)
-    
-    
-    @user.update_profile(p).should == true
-    
-    @user.profile.image_url.should == "http://clown.com"
-    
-   
+    it 'should be able to give me the terse url for webfinger' do
+      @user.terse_url.should == 'example.com'
+    end
+
+    it 'should be able to unsubscribe from a status.net user' do
+      author = Factory.create(:author)
+      Author.all.count.should == 1
+      q = Request.send :class_variable_get, :@@queue
+      q.stub!(:add_hub_unsubscribe_request)
+      q.should_receive(:add_hub_unsubscribe_request)
 
+      @user.unsubscribe_from_pubsub(author.id)  
+      Author.all.count.should == 0
+    end
+    
+    it 'should be able to update their profile and send it to their friends' do 
+      Factory.create(:person)
+      
+      updated_profile = {:profile => {:first_name => 'bob', :last_name => 'billytown', :image_url => "http://clown.com"}}
+      
+      queue = Profile.send :class_variable_get, :@@queue
+      queue.should_receive(:process)
+      
+      @user.update_profile(updated_profile).should == true
+      @user.profile.image_url.should == "http://clown.com"
+    end
   end
-  
 
 end
-- 
GitLab