IZ merge conflicts
This commit is contained in:
commit
a46b0479f4
16 changed files with 272 additions and 23 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,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
|
||||
|
|
|
|||
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 "Friends", friends_path
|
||||
%li= link_to "Bookmarks", bookmarks_path
|
||||
%li= link_to "Blogs", blogs_path
|
||||
|
||||
.container
|
||||
- if show_title?
|
||||
|
|
|
|||
|
|
@ -1,4 +1,6 @@
|
|||
Diaspora::Application.routes.draw do |map|
|
||||
resources :blogs
|
||||
|
||||
resources :bookmarks
|
||||
|
||||
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
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
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