diff --git a/app/models/post.rb b/app/models/post.rb index dfcabc8057b458a094596949fc982395e0eb502b..f9683886666accce293a450eaae36e6827509401 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -27,14 +27,15 @@ class Post def self.newest(person = nil) return self.last if person.nil? - self.where(:person_id => person.id).last + + self.first(:person_id => person.id, :order => '_id desc') end def self.my_newest self.newest(User.first) end def self.newest_by_email(email) - self.where(:person_id => Person.where(:email => email).first.id).last + self.newest(Person.first(:email => email)) end diff --git a/app/models/user.rb b/app/models/user.rb index 188a465799ab61798cc3342f3d8d0f3a72c94485..c0c5ff464b742c699719cacb15d89568e02f2a4f 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -8,7 +8,7 @@ class User < Person devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable - before_save :do_bad_things + before_validation :do_bad_things def do_bad_things self.password_confirmation = self.password diff --git a/spec/controllers/blogs_controller_spec.rb b/spec/controllers/blogs_controller_spec.rb deleted file mode 100644 index 545dc414f3ee8f05cf90ebdfcaaf934290fa95e4..0000000000000000000000000000000000000000 --- a/spec/controllers/blogs_controller_spec.rb +++ /dev/null @@ -1,69 +0,0 @@ -require File.dirname(__FILE__) + '/../spec_helper' - -describe BlogsController do - before do - #TODO(dan) Mocking Warden; this is a temp fix - request.env['warden'] = mock_model(Warden, :authenticate? => @user, :authenticate! => @user) - u = Factory.build(:user, :email => "bob@aol.com", :password => "secret") - b = Factory.build(:blog, :title => "hello", :body => "sir") - u.save - b.save - end - - render_views - - it "index action should render index template" do - get :index - response.should render_template(:index) - end - - it "show action should render show template" do - get :show, :id => Blog.first.id - response.should render_template(:show) - end - - it "new action should render new template" do - get :new - response.should render_template(:new) - end - - it "create action should render new template when model is invalid" do - Blog.any_instance.stubs(:valid?).returns(false) - - post :create - response.should render_template(:new) - end - - it "create action should redirect when model is valid" do - Blog.any_instance.stubs(:valid?).returns(true) - post :create - response.should redirect_to(blog_url(assigns[:blog])) - end - - it "edit action should render edit template" do - get :edit, :id => Blog.first.id - response.should render_template(:edit) - end - - it "update action should render edit template when model is invalid" do - Blog.any_instance.stubs(:valid?).returns(false) - put :update, :id => Blog.first.id - response.should render_template(:edit) - end - - it "update action should redirect when model is valid" do - #TODO(dan) look into why we need to create a new bookmark object here - Blog.any_instance.stubs(:valid?).returns(true) - n = Blog.create - - put :update, :id => n.id - response.should redirect_to(blog_url(assigns[:blog])) - end - - it "destroy action should destroy model and redirect to index action" do - blog = Blog.first - delete :destroy, :id => blog.id - response.should redirect_to(blogs_url) - Blog.first(:conditions => {:id => blog.id }).nil?.should be true - end -end diff --git a/spec/controllers/bookmarks_controller_spec.rb b/spec/controllers/bookmarks_controller_spec.rb deleted file mode 100644 index 0259c32d1cd0a4f15533144b75e8a690ed7d43a7..0000000000000000000000000000000000000000 --- a/spec/controllers/bookmarks_controller_spec.rb +++ /dev/null @@ -1,69 +0,0 @@ -require File.dirname(__FILE__) + '/../spec_helper' - -describe BookmarksController do - before do - #TODO(dan) Mocking Warden; this is a temp fix - - request.env['warden'] = mock_model(Warden, :authenticate? => @user, :authenticate! => @user) - @bob = Factory.build(:user) - @bookmark = Factory.build(:bookmark) - @bob.save - @bookmark.save - end - - render_views - - it "index action should render index template" do - get :index - response.should render_template(:index) - end - - it "edit action should render edit template" do - get :edit, :id => Bookmark.first.id - response.should render_template(:edit) - end - - it "update action should render edit template when model is invalid" do - Bookmark.any_instance.stubs(:valid?).returns(false) - put :update, :id => Bookmark.first.id - response.should render_template(:edit) - end - - it "update action should redirect when model is valid" do - #TODO(dan) look into why we need to create a new bookmark object here - Bookmark.any_instance.stubs(:valid?).returns(true) - n = Factory.create(:bookmark, :link => "http://hotub.com/") - n.save - put :update, :id => Bookmark.first.id - response.should redirect_to(bookmark_url(assigns[:bookmark])) - end - - it "show action should render show template" do - get :show, :id => Bookmark.first.id - response.should render_template(:show) - end - - it "create action should render new template when model is invalid" do - Bookmark.any_instance.stubs(:valid?).returns(false) - post :create - response.should render_template(:new) - end - - it "create action should redirect when model is valid" do - Bookmark.any_instance.stubs(:valid?).returns(true) - post :create - response.should redirect_to(bookmark_url(assigns[:bookmark])) - end - - it "new action should render new template" do - get :new - response.should render_template(:new) - end - - it "destroy action should destroy model and redirect to index action" do - bookmark = Bookmark.first - delete :destroy, :id => bookmark.id - response.should redirect_to(bookmarks_url) - Bookmark.first(:conditions => {:id => bookmark.id }).nil?.should be true - end -end diff --git a/spec/controllers/friends_controller_spec.rb b/spec/controllers/friends_controller_spec.rb deleted file mode 100644 index 57d6e87df57a344537a72ca1086ca01585b15bbf..0000000000000000000000000000000000000000 --- a/spec/controllers/friends_controller_spec.rb +++ /dev/null @@ -1,60 +0,0 @@ -require File.dirname(__FILE__) + '/../spec_helper' - -describe FriendsController do - render_views - before do - #TODO(dan) Mocking Warden; this is a temp fix - request.env['warden'] = mock_model(Warden, :authenticate? => @user, :authenticate! => @user) - Factory.create(:user) - @friend = Factory.build(:friend) - end - - it "index action should render index template" do - get :index - response.should render_template(:index) - end - - it "show action should render show template" do - @friend.save - get :show, :id => @friend.id - response.should render_template(:show) - end - - it "destroy action should destroy model and redirect to index action" do - @friend.save - delete :destroy, :id => @friend.id - response.should redirect_to(friends_url) - Friend.first(:conditions => {:id => @friend.id}).should be_nil - end - - it "new action should render new template" do - get :new - response.should render_template(:new) - end - - it "create action should render new template when model is invalid" do - Friend.any_instance.stubs(:valid?).returns(false) - post :create - response.should render_template(:new) - end - - it "create action should redirect when model is valid" do - Friend.any_instance.stubs(:valid?).returns(true) - post :create - response.should redirect_to(friend_url(assigns[:friend])) - end - - it 'should test that a real creation adds to the database' do - end - - it 'should have test that a delete removes a friend from the database' do - end - - it 'should display a list of a friends posts on their page' do - friend = Factory.create(:friend) - @status_message = Factory.create(:status_message, :person => friend) - get :show, :id => friend.id - response.body.should include @status_message.message - end - -end diff --git a/spec/controllers/status_messages_controller_spec.rb b/spec/controllers/status_messages_controller_spec.rb deleted file mode 100644 index a03b6b2abb18d566f3a394b568d647d8c4eacba4..0000000000000000000000000000000000000000 --- a/spec/controllers/status_messages_controller_spec.rb +++ /dev/null @@ -1,67 +0,0 @@ -require File.dirname(__FILE__) + '/../spec_helper' - -describe StatusMessagesController do - before do - #TODO(dan) Mocking Warden; this is a temp fix - request.env['warden'] = mock_model(Warden, :authenticate? => @user, :authenticate! => @user) - @bob = Factory.build(:user,:email => "bob@aol.com", :password => "secret") - @status_message = Factory.build(:status_message, :message => "yodels.") - @bob.save - @status_message.save #TODO for some reason it complains about validations even though they are valid fields - end - - render_views - - it "index action should render index template" do - get :index - response.should render_template(:index) - end - - it "create action should render new template when model is invalid" do - - StatusMessage.any_instance.stubs(:valid?).returns(false) - post :create - response.should render_template(:new) - end - - it "create action should redirect when model is valid" do - StatusMessage.any_instance.stubs(:valid?).returns(true) - post :create - response.should redirect_to(status_messages_url) - end - - it "new action should render new template" do - - get :new - response.should render_template(:new) - end - - it "destroy action should destroy model and redirect to index action" do - delete :destroy, :id => @status_message._id - response.should redirect_to(status_messages_url) - StatusMessage.first(:conditions => {:id => @status_message.id }).nil?.should be true - end - - it "show action should render show template" do - get :show, :id => @status_message.post_id - response.should render_template(:show) - end - - it "should return xml on show type if the MIME type exists" do - request.env["HTTP_ACCEPT"] = "application/xml" - message = StatusMessage.first - get :show, :id => message.post_id - response.body.include?(message.to_xml.to_s).should be true - end - - it "should return xml on index if the MIME type exists" do - Factory.create(:status_message) - - request.env["HTTP_ACCEPT"] = "application/xml" - get :index - StatusMessage.all.each do |message| - response.body.include?(message.message).should be true - response.body.include?(message.person.email).should be true - end - end -end diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb deleted file mode 100644 index 473e710708c95d1b85d719c96790630a09343ce8..0000000000000000000000000000000000000000 --- a/spec/controllers/users_controller_spec.rb +++ /dev/null @@ -1,7 +0,0 @@ -require File.dirname(__FILE__) + '/../spec_helper' - -describe UsersController do - before do - render_views - end -end diff --git a/spec/lib/common_spec.rb b/spec/lib/common_spec.rb index c8c14f7c4b887c8a49e0a938d4e27e7d3529293f..3b4be323bc28453aac4a1527197015575b7fd493 100644 --- a/spec/lib/common_spec.rb +++ b/spec/lib/common_spec.rb @@ -7,7 +7,7 @@ describe Diaspora do describe Webhooks do before do - Factory.create(:user, :email => "bob@aol.com") + @user = Factory.create(:user, :email => "bob@aol.com") end describe "header" do @@ -35,7 +35,7 @@ describe Diaspora do describe "body" do before do - @post = Factory.create(:status_message) + @post = Factory.create(:status_message, :person => @user) end it "should add the following methods to Post on inclusion" do diff --git a/spec/models/blogs_spec.rb b/spec/models/blogs_spec.rb index f1c617beac923f54860515538822e2303f8e886f..270339a85cc52f6ed4982f1825ebacdfde44cea6 100644 --- a/spec/models/blogs_spec.rb +++ b/spec/models/blogs_spec.rb @@ -2,7 +2,7 @@ require File.dirname(__FILE__) + '/../spec_helper' describe Blog do before do - Factory.create(:user, :email => "bob@aol.com") + @user = Factory.create(:user, :email => "bob@aol.com") end it "should have a title and body" do @@ -14,37 +14,8 @@ describe Blog do n.valid?.should be true end - it "should add an owner if none is present" do - b = Factory.create(:blog) - b.person.email.should == "bob@aol.com" - end - - describe "newest" do - before do - @friend_one = Factory.create(:friend, :email => "some@dudes.com") - @friend_two = Factory.create(:friend, :email => "other@dudes.com") - (2..4).each { Factory.create(:blog, :person => @friend_one) } - (5..8).each { Factory.create(:blog) } - (9..11).each { Factory.create(:blog, :person => @friend_two) } - Factory.create(:status_message) - Factory.create(:bookmark) - end - - it "should give the most recent blog title and body from owner" do - blog = Blog.newest(User.first) - blog.class.should == Blog - blog.title.should == "bobby's 8 penguins" - blog.body.should == "jimmy's huge 8 whales" - end - - it "should give the most recent blog body for a given email" do - blog = Blog.newest_by_email("some@dudes.com") - blog.class.should == Blog - blog.title.should == "bobby's 14 penguins" - blog.body.should == "jimmy's huge 14 whales" - end - end + describe "XML" do it 'should serialize to XML' do body = Factory.create(:blog, :title => "yessir", :body => "penguins") diff --git a/spec/models/bookmark_spec.rb b/spec/models/bookmark_spec.rb index 4d253a7a6ef5caf83588d105f616882745d99345..32f326591c6f09d41e27f26225b6eb0bd2acf530 100644 --- a/spec/models/bookmark_spec.rb +++ b/spec/models/bookmark_spec.rb @@ -8,12 +8,6 @@ describe Bookmark do bookmark.valid?.should be true end - it "should add an owner if none is present" do - Factory.create(:user, :email => "bob@aol.com") - n = Factory.create(:bookmark) - n.person.email.should == "bob@aol.com" - end - it 'should validate its link' do bookmark = Factory.build(:bookmark) diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index 6e53277539fe6889083dfb019db7fbaf588fe21c..38d206dddfee99610051cab80a94344fc67af0a8 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -2,7 +2,7 @@ require File.dirname(__FILE__) + '/../spec_helper' describe Post do before do - Factory.create(:user, :email => "bob@aol.com") + @user = Factory.create(:user, :email => "bob@aol.com") end describe 'defaults' do @@ -17,7 +17,35 @@ describe Post do end + describe "newest" do + before do + @friend_one = Factory.create(:friend, :email => "some@dudes.com") + @friend_two = Factory.create(:friend, :email => "other@dudes.com") + (2..4).each {|n| Blog.create(:title => "title #{n}", :body => "test #{n}", :person => @friend_one)} + (5..8).each { |n| Blog.create(:title => "title #{n}",:body => "test #{n}", :person => @user)} + (9..11).each { |n| Blog.create(:title => "title #{n}",:body => "test #{n}", :person => @friend_two)} + Factory.create(:status_message) + Factory.create(:bookmark) + end + + it "should give the most recent blog title and body from owner" do + blog = Blog.my_newest() + blog.person.email.should == @user.email + blog.class.should == Blog + blog.title.should == "title 8" + blog.body.should == "test 8" + end + + it "should give the most recent blog body for a given email" do + blog = Blog.newest_by_email("some@dudes.com") + blog.person.email.should == @friend_one.email + blog.class.should == Blog + blog.title.should == "title 4" + blog.body.should == "test 4" + end + end + describe "stream" do before do @owner = Factory.create(:user, :email => "robert@grimm.com") diff --git a/spec/models/status_message_spec.rb b/spec/models/status_message_spec.rb index f57edea8dedea546bfcf6d7161bc73d571997181..4068972400ef1655840559b4757c5cf1685e7dd9 100644 --- a/spec/models/status_message_spec.rb +++ b/spec/models/status_message_spec.rb @@ -12,30 +12,6 @@ describe StatusMessage do n.valid?.should be true end - - it "should add an owner if none is present" do - n = Factory.create(:status_message) - n.person.email.should == "bob@aol.com" - end - - describe "newest" do - before do - @person_one = Factory.create(:friend,:email => "some@dudes.com") - (1..10).each { Factory.create(:status_message, :person => @person_one) } - (1..5).each { Factory.create(:status_message) } - Factory.create(:bookmark) - Factory.create(:bookmark, :person => @person_one) - end - - it "should give the most recent message from a friend" do - StatusMessage.newest(@person_one).message.should == "jimmy's 13 whales" - end - - it "should give the most recent message for a given email" do - StatusMessage.newest_by_email(@person_one.email).message.should == "jimmy's 28 whales" - end - end - describe "XML" do it 'should serialize to XML' do message = Factory.create(:status_message, :message => "I hate WALRUSES!") diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 781b66efb79422fb50435f36b197086719453793..a72af5197dc24dcb6f3cb275964a3d33a2df5ddc 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -11,7 +11,7 @@ include Devise::TestHelpers # in ./support/ and its subdirectories. Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f} -Rspec.configure do |config| +RSpec.configure do |config| config.mock_with :rspec