DG MS completed bookmark story

This commit is contained in:
maxwell 2010-06-14 20:20:19 -07:00
parent 58dfefc3ca
commit 1a1887f77f
15 changed files with 232 additions and 8 deletions

View file

@ -0,0 +1,46 @@
class BookmarksController < ApplicationController
before_filter :authenticate_user!
def index
@bookmarks = Bookmark.all
end
def edit
@bookmark = Bookmark.first(:conditions => {:id => params[:id]})
end
def update
@bookmark = Bookmark.first(:conditions => {:id => params[:id]})
if @bookmark.update_attributes(params[:bookmark])
flash[:notice] = "Successfully updated bookmark."
redirect_to @bookmark
else
render :action => 'edit'
end
end
def show
@bookmark = Bookmark.first(:conditions => {:id => params[:id]})
end
def create
@bookmark = Bookmark.new(params[:bookmark])
if @bookmark.save
flash[:notice] = "Successfully created bookmark."
redirect_to @bookmark
else
render :action => 'new'
end
end
def new
@bookmark = Bookmark.new
end
def destroy
@bookmark = Bookmark.first(:conditions => {:id => params[:id]})
@bookmark.destroy
flash[:notice] = "Successfully destroyed bookmark."
redirect_to bookmarks_url
end
end

View file

@ -10,7 +10,7 @@ class StatusMessagesController < ApplicationController
@status_message = StatusMessage.new(params[:status_message])
if @status_message.save
flash[:notice] = "Successfully created status message."
redirect_to @status_message
redirect_to status_messages_url
else
render :action => 'new'
end

View file

@ -0,0 +1,2 @@
module BookmarksHelper
end

20
app/models/bookmark.rb Normal file
View file

@ -0,0 +1,20 @@
class Bookmark
include Mongoid::Document
include Mongoid::Timestamps
field :owner
field :link
field :title
validates_presence_of :link
before_create :set_default_owner
protected
def set_default_owner
self.owner ||= User.first.email
end
end

View file

@ -12,7 +12,7 @@ class StatusMessage
validates_presence_of :message
before_create :add_owner
before_create :set_default_owner
def self.newest(owner_email)
StatusMessage.last(:conditions => {:owner => owner_email})
@ -24,8 +24,7 @@ class StatusMessage
protected
def add_owner
def set_default_owner
self.owner ||= User.first.email
end
end

View file

@ -0,0 +1,12 @@
= form_for @bookmark do |f|
= f.error_messages
%p
= f.label :title
%br
= f.text_field :title
%p
= f.label :link
%br
= f.text_field :link
%p
= f.submit

View file

@ -0,0 +1,8 @@
- title "Edit Bookmark"
= render 'form'
%p
= link_to "Show", bookmark_path(@bookmark)
|
= link_to "View All", bookmarks_path

View file

@ -0,0 +1,17 @@
- title "Bookmarks"
%table
%tr
%th Title
%th Link
%th Owner
- for bookmark in @bookmarks
%tr
%td= bookmark.title
%td= link_to bookmark.link, bookmark.link
%td= bookmark.owner
%td= link_to 'Show', bookmark
%td= link_to 'Edit', edit_bookmark_path(bookmark)
%td= link_to 'Destroy', bookmark, :confirm => 'Are you sure?', :method => :delete
%p= link_to "New Bookmark", new_bookmark_path

View file

@ -0,0 +1,5 @@
- title "New Bookmark"
= render 'form'
%p= link_to "Back to List", bookmarks_path

View file

@ -0,0 +1,18 @@
- title "Bookmark"
%p
%strong Title:
= @bookmark.title
%p
%strong Link:
= link_to @bookmark.link
%p
%strong Owner:
= @bookmark.owner
%p
= link_to "Edit", edit_bookmark_path(@bookmark)
|
= link_to "Destroy", @bookmark, :confirm => 'Are you sure?', :method => :delete
|
= link_to "View All", bookmarks_path

View file

@ -30,6 +30,7 @@
%li= link_to "Users", users_path
%li= link_to "Status Messages", status_messages_path
%li= link_to "Friends", friends_path
%li= link_to "Bookmarks", bookmarks_path
.container
- if show_title?

View file

@ -1,4 +1,6 @@
Diaspora::Application.routes.draw do |map|
resources :bookmarks
resources :friends
resources :status_messages

View file

@ -0,0 +1,80 @@
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)
User.create(:email => "bob@aol.com", :password => "secret")
Bookmark.create(:link => "http://www.yahooligans.com/")
end
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
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)
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
request.env['warden'].should_receive(:authenticate?).at_least(:once)
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

View file

@ -8,7 +8,6 @@ describe StatusMessagesController do
StatusMessage.create(:message => "yodels.")
end
#fixtures :all
render_views
it "index action should render index template" do
@ -28,7 +27,7 @@ describe StatusMessagesController do
it "create action should redirect when model is valid" do
StatusMessage.any_instance.stubs(:valid?).returns(true)
post :create
response.should redirect_to(status_message_url(assigns[:status_message]))
response.should redirect_to(status_messages_url)
end
it "new action should render new template" do
@ -47,7 +46,6 @@ describe StatusMessagesController do
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

View file

@ -0,0 +1,16 @@
require File.dirname(__FILE__) + '/../spec_helper'
describe Bookmark do
it "should have a link" do
bookmark = Bookmark.new
bookmark.valid?.should be false
bookmark.link = "http://angjoo.com/"
bookmark.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 = Bookmark.create(:link => "http://www.validurl.com/")
n.owner.should == "bob@aol.com"
end
end