From 45c926f79036a00620dcf7de8dd506bbb8e0ed2d Mon Sep 17 00:00:00 2001 From: danielvincent Date: Tue, 15 Jun 2010 11:09:47 -0700 Subject: [PATCH] DG RS; added blog posts, cleaned up unnecessary warden mocks in all controller specs --- app/controllers/blogs_controller.rb | 47 +++++++++++++ app/helpers/blogs_helper.rb | 2 + app/models/blog.rb | 32 +++++++++ app/models/status_message.rb | 2 +- app/views/blogs/_form.html.haml | 12 ++++ app/views/blogs/edit.html.haml | 8 +++ app/views/blogs/index.html.haml | 17 +++++ app/views/blogs/new.html.haml | 5 ++ app/views/blogs/show.html.haml | 18 +++++ app/views/layouts/application.html.haml | 1 + config/routes.rb | 2 + spec/controllers/blogs_controller_spec.rb | 67 +++++++++++++++++++ spec/controllers/bookmarks_controller_spec.rb | 19 +----- spec/controllers/friends_controller_spec.rb | 6 +- .../status_messages_controller_spec.rb | 6 +- spec/models/blogs_spec.rb | 61 +++++++++++++++++ spec/models/friend_spec.rb | 2 +- 17 files changed, 278 insertions(+), 29 deletions(-) create mode 100644 app/controllers/blogs_controller.rb create mode 100644 app/helpers/blogs_helper.rb create mode 100644 app/models/blog.rb create mode 100644 app/views/blogs/_form.html.haml create mode 100644 app/views/blogs/edit.html.haml create mode 100644 app/views/blogs/index.html.haml create mode 100644 app/views/blogs/new.html.haml create mode 100644 app/views/blogs/show.html.haml create mode 100644 spec/controllers/blogs_controller_spec.rb create mode 100644 spec/models/blogs_spec.rb diff --git a/app/controllers/blogs_controller.rb b/app/controllers/blogs_controller.rb new file mode 100644 index 000000000..e4bf80484 --- /dev/null +++ b/app/controllers/blogs_controller.rb @@ -0,0 +1,47 @@ +class BlogsController < ApplicationController + before_filter :authenticate_user! + + + def index + @blogs = Blog.all + end + + def show + @blog = Blog.find(params[:id]) + end + + def new + @blog = Blog.new + end + + def create + @blog = Blog.new(params[:blog]) + if @blog.save + flash[:notice] = "Successfully created blog." + redirect_to @blog + else + render :action => 'new' + end + end + + def edit + @blog = Blog.find(params[:id]) + end + + def update + @blog = Blog.find(params[:id]) + if @blog.update_attributes(params[:blog]) + flash[:notice] = "Successfully updated blog." + redirect_to @blog + else + render :action => 'edit' + end + end + + def destroy + @blog = Blog.find(params[:id]) + @blog.destroy + flash[:notice] = "Successfully destroyed blog." + redirect_to blogs_url + end +end diff --git a/app/helpers/blogs_helper.rb b/app/helpers/blogs_helper.rb new file mode 100644 index 000000000..cc0dbd200 --- /dev/null +++ b/app/helpers/blogs_helper.rb @@ -0,0 +1,2 @@ +module BlogsHelper +end diff --git a/app/models/blog.rb b/app/models/blog.rb new file mode 100644 index 000000000..fd49ac248 --- /dev/null +++ b/app/models/blog.rb @@ -0,0 +1,32 @@ +class Blog + include Mongoid::Document + include Mongoid::Timestamps + include ROXML + + xml_accessor :title + xml_accessor :body + xml_accessor :owner + + + field :title + field :body + field :owner + + validates_presence_of :title, :body + + before_create :set_default_owner + + def self.newest(owner_email) + Blog.last(:conditions => {:owner => owner_email}) + end + + def self.my_newest + Blog.newest(User.first.email) + end + + protected + + def set_default_owner + self.owner ||= User.first.email + end +end diff --git a/app/models/status_message.rb b/app/models/status_message.rb index 87b3dea78..20add29c5 100644 --- a/app/models/status_message.rb +++ b/app/models/status_message.rb @@ -15,7 +15,7 @@ class StatusMessage before_create :set_default_owner def self.newest(owner_email) - message = StatusMessage.last(:conditions => {:owner => owner_email}) + StatusMessage.last(:conditions => {:owner => owner_email}) end def self.my_newest diff --git a/app/views/blogs/_form.html.haml b/app/views/blogs/_form.html.haml new file mode 100644 index 000000000..5651e1eb0 --- /dev/null +++ b/app/views/blogs/_form.html.haml @@ -0,0 +1,12 @@ +- form_for @blog do |f| + = f.error_messages + %p + = f.label :title + %br + = f.text_field :title + %p + = f.label :body + %br + = f.text_area :body + %p + = f.submit diff --git a/app/views/blogs/edit.html.haml b/app/views/blogs/edit.html.haml new file mode 100644 index 000000000..29c83d2f4 --- /dev/null +++ b/app/views/blogs/edit.html.haml @@ -0,0 +1,8 @@ +- title "Edit Blog" + += render 'form' + +%p + = link_to "Show", blog_path(@blog) + | + = link_to "View All", blogs_path diff --git a/app/views/blogs/index.html.haml b/app/views/blogs/index.html.haml new file mode 100644 index 000000000..40eccd014 --- /dev/null +++ b/app/views/blogs/index.html.haml @@ -0,0 +1,17 @@ +- title "Blogs" + +%table + %tr + %th Title + %th Body + %th Owner + - for blog in @blogs + %tr + %td= blog.title + %td= blog.body + %td= blog.owner + %td= link_to 'Show', blog + %td= link_to 'Edit', edit_blog_path(blog) + %td= link_to 'Destroy', blog, :confirm => 'Are you sure?', :method => :delete + +%p= link_to "New Blog", new_blog_path diff --git a/app/views/blogs/new.html.haml b/app/views/blogs/new.html.haml new file mode 100644 index 000000000..5f2a42ea5 --- /dev/null +++ b/app/views/blogs/new.html.haml @@ -0,0 +1,5 @@ +- title "New Blog" + += render 'form' + +%p= link_to "Back to List", blogs_path diff --git a/app/views/blogs/show.html.haml b/app/views/blogs/show.html.haml new file mode 100644 index 000000000..0b08a6c73 --- /dev/null +++ b/app/views/blogs/show.html.haml @@ -0,0 +1,18 @@ +- title "Blog" + +%p + %strong Title: + = @blog.title +%p + %strong Body: + = @blog.body +%p + %strong Owner: + = @blog.owner + +%p + = link_to "Edit", edit_blog_path(@blog) + | + = link_to "Destroy", @blog, :confirm => 'Are you sure?', :method => :delete + | + = link_to "View All", blogs_path diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index b0e330ead..7f48e98b5 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -32,6 +32,7 @@ %li= link_to "Status Messages", status_messages_path %li= link_to "Friends", friends_path %li= link_to "Bookmarks", bookmarks_path + %li= link_to "Blogs", blogs_path .container - if show_title? diff --git a/config/routes.rb b/config/routes.rb index edcead33d..9f7998572 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,6 @@ Diaspora::Application.routes.draw do |map| + resources :blogs + resources :bookmarks resources :friends diff --git a/spec/controllers/blogs_controller_spec.rb b/spec/controllers/blogs_controller_spec.rb new file mode 100644 index 000000000..71c503ccb --- /dev/null +++ b/spec/controllers/blogs_controller_spec.rb @@ -0,0 +1,67 @@ +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) + User.create(:email => "bob@aol.com", :password => "secret") + Blog.create(:title => "hello", :body => "sir") + 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 index 61dd7ca20..b674682b7 100644 --- a/spec/controllers/bookmarks_controller_spec.rb +++ b/spec/controllers/bookmarks_controller_spec.rb @@ -3,7 +3,7 @@ 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) + request.env['warden'] = mock_model(Warden, :authenticate? => @user, :authenticate! => @user) User.create(:email => "bob@aol.com", :password => "secret") Bookmark.create(:link => "http://www.yahooligans.com/") end @@ -11,47 +11,35 @@ describe BookmarksController do render_views it "index action should render index template" do - request.env['warden'].should_receive(:authenticate?).at_least(:once) - get :index response.should render_template(:index) end it "edit action should render edit template" do - request.env['warden'].should_receive(:authenticate?).at_least(:once) - get :edit, :id => Bookmark.first.id response.should render_template(:edit) end it "update action should render edit template when model is invalid" do - request.env['warden'].should_receive(:authenticate?).at_least(:once) - 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 = Bookmark.create(:link => "http://hotub.com") - puts n.inspect + n = Bookmark.create put :update, :id => n.id response.should redirect_to(bookmark_url(assigns[:bookmark])) end it "show action should render show template" do - request.env['warden'].should_receive(:authenticate?).at_least(:once) - get :show, :id => Bookmark.first.id response.should render_template(:show) end it "create action should render new template when model is invalid" do - request.env['warden'].should_receive(:authenticate?).at_least(:once) - Bookmark.any_instance.stubs(:valid?).returns(false) post :create response.should render_template(:new) @@ -64,8 +52,6 @@ describe BookmarksController do end it "new action should render new template" do - request.env['warden'].should_receive(:authenticate?).at_least(:once) - get :new response.should render_template(:new) end @@ -75,6 +61,5 @@ describe BookmarksController do 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 index 4637aa15b..53bf2eb54 100644 --- a/spec/controllers/friends_controller_spec.rb +++ b/spec/controllers/friends_controller_spec.rb @@ -4,18 +4,16 @@ 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) + request.env['warden'] = mock_model(Warden, :authenticate? => @user, :authenticate! => @user) Friend.create(:username => "max", :url => "http://max.com/") end it "index action should render index template" do - request.env['warden'].should_receive(:authenticate?).at_least(:once) get :index response.should render_template(:index) end it "show action should render show template" do - request.env['warden'].should_receive(:authenticate?).at_least(:once) get :show, :id => Friend.first.id response.should render_template(:show) end @@ -28,13 +26,11 @@ describe FriendsController do end it "new action should render new template" do - request.env['warden'].should_receive(:authenticate?).at_least(:once) get :new response.should render_template(:new) end it "create action should render new template when model is invalid" do - request.env['warden'].should_receive(:authenticate?).at_least(:once) Friend.any_instance.stubs(:valid?).returns(false) post :create response.should render_template(:new) diff --git a/spec/controllers/status_messages_controller_spec.rb b/spec/controllers/status_messages_controller_spec.rb index 50cba6a45..b860eaded 100644 --- a/spec/controllers/status_messages_controller_spec.rb +++ b/spec/controllers/status_messages_controller_spec.rb @@ -3,7 +3,7 @@ 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) + request.env['warden'] = mock_model(Warden, :authenticate? => @user, :authenticate! => @user) User.create(:email => "bob@aol.com", :password => "secret") StatusMessage.create(:message => "yodels.") end @@ -11,13 +11,11 @@ describe StatusMessagesController do render_views it "index action should render index template" do - request.env['warden'].should_receive(:authenticate?).at_least(:once) get :index response.should render_template(:index) end it "create action should render new template when model is invalid" do - request.env['warden'].should_receive(:authenticate?).at_least(:once) StatusMessage.any_instance.stubs(:valid?).returns(false) post :create @@ -31,7 +29,6 @@ describe StatusMessagesController do end it "new action should render new template" do - request.env['warden'].should_receive(:authenticate?).at_least(:once) get :new response.should render_template(:new) @@ -45,7 +42,6 @@ describe StatusMessagesController do end it "show action should render show template" do - request.env['warden'].should_receive(:authenticate?).at_least(:once) get :show, :id => StatusMessage.first.id response.should render_template(:show) end diff --git a/spec/models/blogs_spec.rb b/spec/models/blogs_spec.rb new file mode 100644 index 000000000..ce3e5f9a5 --- /dev/null +++ b/spec/models/blogs_spec.rb @@ -0,0 +1,61 @@ +require File.dirname(__FILE__) + '/../spec_helper' + +describe Blog do + it "should have a title and body" do + n = Blog.new + n.valid?.should be false + n.title = "jimmy" + n.valid?.should be false + n.body = "wales" + n.valid?.should be true + end + + it "should add an owner if none is present" do + User.create(:email => "bob@aol.com", :password => "big bux") + n = Blog.create(:title => "kittens", :body => "puppies!") + n.owner.should == "bob@aol.com" + end + + + describe "newest" do + before do + User.create(:email => "bob@aol.com", :password => "diggity") + Blog.create(:title => "bone dawg", :body => "wale for jimmy", :owner => "xzibit@dawgz.com") + Blog.create(:title => "dawg bone", :body => "jimmy wales") + Blog.create(:title => "bone dawg", :body => "jimmy your wales", :owner => "some@dudes.com") + Blog.create(:title => "dawg bone", :body => "lions", :owner => "xzibit@dawgz.com") + Blog.create(:title => "bone dawg", :body => "bears") + Blog.create(:title => "dawg bone", :body => "sharks", :owner => "some@dudes.com") + Blog.create(:title => "bone dawg", :body => "roar") + end + + it "should give the most recent blog title and body from owner" do + blog = Blog.my_newest + blog.title.should == "bone dawg" + blog.body.should == "roar" + end + + it "should give the most recent blog body for a given email" do + blog = Blog.newest("some@dudes.com") + blog.title.should == "dawg bone" + blog.body.should == "sharks" + end + end + + describe "XML" do + before do + @xml = "\n yessir\n I hate WALRUSES!\n Bob\n" + end + + it 'should serialize to XML' do + body = Blog.create(:title => "yessir", :body => "I hate WALRUSES!", :owner => "Bob") + body.to_xml.to_s.should == @xml + end + + it 'should marshal serialized XML to object' do + parsed = Blog.from_xml(@xml) + parsed.body.should == "I hate WALRUSES!" + parsed.owner.should == "Bob" + end + end +end diff --git a/spec/models/friend_spec.rb b/spec/models/friend_spec.rb index 1a6345435..d3f9c9c07 100644 --- a/spec/models/friend_spec.rb +++ b/spec/models/friend_spec.rb @@ -7,4 +7,4 @@ describe Friend do n.url = "http://max.com/" n.valid?.should be true end -end \ No newline at end of file +end