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 a23325a7f..7d78ce805 100644 --- a/app/models/status_message.rb +++ b/app/models/status_message.rb @@ -15,9 +15,7 @@ class StatusMessage before_create :set_default_owner def self.newest(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 32604ec0e..37183c34b 100644 --- a/spec/controllers/bookmarks_controller_spec.rb +++ b/spec/controllers/bookmarks_controller_spec.rb @@ -13,48 +13,36 @@ 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 = Factory.create(:bookmark, :link => "http://hotub.com") n.save - #puts n.inspect put :update, :id => Bookmark.first.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) @@ -67,8 +55,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 @@ -78,6 +64,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 4c51d7497..7f6fe491a 100644 --- a/spec/controllers/friends_controller_spec.rb +++ b/spec/controllers/friends_controller_spec.rb @@ -9,7 +9,6 @@ describe FriendsController do 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 @@ -29,13 +28,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 516de5aab..22d080fe0 100644 --- a/spec/controllers/status_messages_controller_spec.rb +++ b/spec/controllers/status_messages_controller_spec.rb @@ -13,13 +13,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 @@ -33,7 +31,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) 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