DG RS; added blog posts, cleaned up unnecessary warden mocks in all controller specs
This commit is contained in:
parent
99d9e50ed6
commit
45c926f790
17 changed files with 278 additions and 29 deletions
47
app/controllers/blogs_controller.rb
Normal file
47
app/controllers/blogs_controller.rb
Normal file
|
|
@ -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
|
||||||
2
app/helpers/blogs_helper.rb
Normal file
2
app/helpers/blogs_helper.rb
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
module BlogsHelper
|
||||||
|
end
|
||||||
32
app/models/blog.rb
Normal file
32
app/models/blog.rb
Normal file
|
|
@ -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
|
||||||
|
|
@ -15,7 +15,7 @@ class StatusMessage
|
||||||
before_create :set_default_owner
|
before_create :set_default_owner
|
||||||
|
|
||||||
def self.newest(owner_email)
|
def self.newest(owner_email)
|
||||||
message = StatusMessage.last(:conditions => {:owner => owner_email})
|
StatusMessage.last(:conditions => {:owner => owner_email})
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.my_newest
|
def self.my_newest
|
||||||
|
|
|
||||||
12
app/views/blogs/_form.html.haml
Normal file
12
app/views/blogs/_form.html.haml
Normal file
|
|
@ -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
|
||||||
8
app/views/blogs/edit.html.haml
Normal file
8
app/views/blogs/edit.html.haml
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
- title "Edit Blog"
|
||||||
|
|
||||||
|
= render 'form'
|
||||||
|
|
||||||
|
%p
|
||||||
|
= link_to "Show", blog_path(@blog)
|
||||||
|
|
|
||||||
|
= link_to "View All", blogs_path
|
||||||
17
app/views/blogs/index.html.haml
Normal file
17
app/views/blogs/index.html.haml
Normal file
|
|
@ -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
|
||||||
5
app/views/blogs/new.html.haml
Normal file
5
app/views/blogs/new.html.haml
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
- title "New Blog"
|
||||||
|
|
||||||
|
= render 'form'
|
||||||
|
|
||||||
|
%p= link_to "Back to List", blogs_path
|
||||||
18
app/views/blogs/show.html.haml
Normal file
18
app/views/blogs/show.html.haml
Normal file
|
|
@ -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
|
||||||
|
|
@ -32,6 +32,7 @@
|
||||||
%li= link_to "Status Messages", status_messages_path
|
%li= link_to "Status Messages", status_messages_path
|
||||||
%li= link_to "Friends", friends_path
|
%li= link_to "Friends", friends_path
|
||||||
%li= link_to "Bookmarks", bookmarks_path
|
%li= link_to "Bookmarks", bookmarks_path
|
||||||
|
%li= link_to "Blogs", blogs_path
|
||||||
|
|
||||||
.container
|
.container
|
||||||
- if show_title?
|
- if show_title?
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,6 @@
|
||||||
Diaspora::Application.routes.draw do |map|
|
Diaspora::Application.routes.draw do |map|
|
||||||
|
resources :blogs
|
||||||
|
|
||||||
resources :bookmarks
|
resources :bookmarks
|
||||||
|
|
||||||
resources :friends
|
resources :friends
|
||||||
|
|
|
||||||
67
spec/controllers/blogs_controller_spec.rb
Normal file
67
spec/controllers/blogs_controller_spec.rb
Normal file
|
|
@ -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
|
||||||
|
|
@ -3,7 +3,7 @@ require File.dirname(__FILE__) + '/../spec_helper'
|
||||||
describe BookmarksController do
|
describe BookmarksController do
|
||||||
before do
|
before do
|
||||||
#TODO(dan) Mocking Warden; this is a temp fix
|
#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")
|
User.create(:email => "bob@aol.com", :password => "secret")
|
||||||
Bookmark.create(:link => "http://www.yahooligans.com/")
|
Bookmark.create(:link => "http://www.yahooligans.com/")
|
||||||
end
|
end
|
||||||
|
|
@ -11,47 +11,35 @@ describe BookmarksController do
|
||||||
render_views
|
render_views
|
||||||
|
|
||||||
it "index action should render index template" do
|
it "index action should render index template" do
|
||||||
request.env['warden'].should_receive(:authenticate?).at_least(:once)
|
|
||||||
|
|
||||||
get :index
|
get :index
|
||||||
response.should render_template(:index)
|
response.should render_template(:index)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "edit action should render edit template" do
|
it "edit action should render edit template" do
|
||||||
request.env['warden'].should_receive(:authenticate?).at_least(:once)
|
|
||||||
|
|
||||||
get :edit, :id => Bookmark.first.id
|
get :edit, :id => Bookmark.first.id
|
||||||
response.should render_template(:edit)
|
response.should render_template(:edit)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "update action should render edit template when model is invalid" do
|
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)
|
Bookmark.any_instance.stubs(:valid?).returns(false)
|
||||||
put :update, :id => Bookmark.first.id
|
put :update, :id => Bookmark.first.id
|
||||||
response.should render_template(:edit)
|
response.should render_template(:edit)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "update action should redirect when model is valid" do
|
it "update action should redirect when model is valid" do
|
||||||
|
|
||||||
#TODO(dan) look into why we need to create a new bookmark object here
|
#TODO(dan) look into why we need to create a new bookmark object here
|
||||||
Bookmark.any_instance.stubs(:valid?).returns(true)
|
Bookmark.any_instance.stubs(:valid?).returns(true)
|
||||||
n = Bookmark.create(:link => "http://hotub.com")
|
n = Bookmark.create
|
||||||
puts n.inspect
|
|
||||||
put :update, :id => n.id
|
put :update, :id => n.id
|
||||||
response.should redirect_to(bookmark_url(assigns[:bookmark]))
|
response.should redirect_to(bookmark_url(assigns[:bookmark]))
|
||||||
end
|
end
|
||||||
|
|
||||||
it "show action should render show template" do
|
it "show action should render show template" do
|
||||||
request.env['warden'].should_receive(:authenticate?).at_least(:once)
|
|
||||||
|
|
||||||
get :show, :id => Bookmark.first.id
|
get :show, :id => Bookmark.first.id
|
||||||
response.should render_template(:show)
|
response.should render_template(:show)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "create action should render new template when model is invalid" do
|
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)
|
Bookmark.any_instance.stubs(:valid?).returns(false)
|
||||||
post :create
|
post :create
|
||||||
response.should render_template(:new)
|
response.should render_template(:new)
|
||||||
|
|
@ -64,8 +52,6 @@ describe BookmarksController do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "new action should render new template" do
|
it "new action should render new template" do
|
||||||
request.env['warden'].should_receive(:authenticate?).at_least(:once)
|
|
||||||
|
|
||||||
get :new
|
get :new
|
||||||
response.should render_template(:new)
|
response.should render_template(:new)
|
||||||
end
|
end
|
||||||
|
|
@ -75,6 +61,5 @@ describe BookmarksController do
|
||||||
delete :destroy, :id => bookmark.id
|
delete :destroy, :id => bookmark.id
|
||||||
response.should redirect_to(bookmarks_url)
|
response.should redirect_to(bookmarks_url)
|
||||||
Bookmark.first(:conditions => {:id => bookmark.id }).nil?.should be true
|
Bookmark.first(:conditions => {:id => bookmark.id }).nil?.should be true
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -4,18 +4,16 @@ describe FriendsController do
|
||||||
render_views
|
render_views
|
||||||
before do
|
before do
|
||||||
#TODO(dan) Mocking Warden; this is a temp fix
|
#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/")
|
Friend.create(:username => "max", :url => "http://max.com/")
|
||||||
end
|
end
|
||||||
|
|
||||||
it "index action should render index template" do
|
it "index action should render index template" do
|
||||||
request.env['warden'].should_receive(:authenticate?).at_least(:once)
|
|
||||||
get :index
|
get :index
|
||||||
response.should render_template(:index)
|
response.should render_template(:index)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "show action should render show template" do
|
it "show action should render show template" do
|
||||||
request.env['warden'].should_receive(:authenticate?).at_least(:once)
|
|
||||||
get :show, :id => Friend.first.id
|
get :show, :id => Friend.first.id
|
||||||
response.should render_template(:show)
|
response.should render_template(:show)
|
||||||
end
|
end
|
||||||
|
|
@ -28,13 +26,11 @@ describe FriendsController do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "new action should render new template" do
|
it "new action should render new template" do
|
||||||
request.env['warden'].should_receive(:authenticate?).at_least(:once)
|
|
||||||
get :new
|
get :new
|
||||||
response.should render_template(:new)
|
response.should render_template(:new)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "create action should render new template when model is invalid" do
|
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)
|
Friend.any_instance.stubs(:valid?).returns(false)
|
||||||
post :create
|
post :create
|
||||||
response.should render_template(:new)
|
response.should render_template(:new)
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ require File.dirname(__FILE__) + '/../spec_helper'
|
||||||
describe StatusMessagesController do
|
describe StatusMessagesController do
|
||||||
before do
|
before do
|
||||||
#TODO(dan) Mocking Warden; this is a temp fix
|
#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")
|
User.create(:email => "bob@aol.com", :password => "secret")
|
||||||
StatusMessage.create(:message => "yodels.")
|
StatusMessage.create(:message => "yodels.")
|
||||||
end
|
end
|
||||||
|
|
@ -11,13 +11,11 @@ describe StatusMessagesController do
|
||||||
render_views
|
render_views
|
||||||
|
|
||||||
it "index action should render index template" do
|
it "index action should render index template" do
|
||||||
request.env['warden'].should_receive(:authenticate?).at_least(:once)
|
|
||||||
get :index
|
get :index
|
||||||
response.should render_template(:index)
|
response.should render_template(:index)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "create action should render new template when model is invalid" do
|
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)
|
StatusMessage.any_instance.stubs(:valid?).returns(false)
|
||||||
post :create
|
post :create
|
||||||
|
|
@ -31,7 +29,6 @@ describe StatusMessagesController do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "new action should render new template" do
|
it "new action should render new template" do
|
||||||
request.env['warden'].should_receive(:authenticate?).at_least(:once)
|
|
||||||
|
|
||||||
get :new
|
get :new
|
||||||
response.should render_template(:new)
|
response.should render_template(:new)
|
||||||
|
|
@ -45,7 +42,6 @@ describe StatusMessagesController do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "show action should render show template" do
|
it "show action should render show template" do
|
||||||
request.env['warden'].should_receive(:authenticate?).at_least(:once)
|
|
||||||
get :show, :id => StatusMessage.first.id
|
get :show, :id => StatusMessage.first.id
|
||||||
response.should render_template(:show)
|
response.should render_template(:show)
|
||||||
end
|
end
|
||||||
|
|
|
||||||
61
spec/models/blogs_spec.rb
Normal file
61
spec/models/blogs_spec.rb
Normal file
|
|
@ -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 = "<blog>\n <title>yessir</title>\n <body>I hate WALRUSES!</body>\n <owner>Bob</owner>\n</blog>"
|
||||||
|
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
|
||||||
Loading…
Reference in a new issue