DG MS completed bookmark story
This commit is contained in:
parent
58dfefc3ca
commit
1a1887f77f
15 changed files with 232 additions and 8 deletions
46
app/controllers/bookmarks_controller.rb
Normal file
46
app/controllers/bookmarks_controller.rb
Normal 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
|
||||||
|
|
@ -10,7 +10,7 @@ class StatusMessagesController < ApplicationController
|
||||||
@status_message = StatusMessage.new(params[:status_message])
|
@status_message = StatusMessage.new(params[:status_message])
|
||||||
if @status_message.save
|
if @status_message.save
|
||||||
flash[:notice] = "Successfully created status message."
|
flash[:notice] = "Successfully created status message."
|
||||||
redirect_to @status_message
|
redirect_to status_messages_url
|
||||||
else
|
else
|
||||||
render :action => 'new'
|
render :action => 'new'
|
||||||
end
|
end
|
||||||
|
|
|
||||||
2
app/helpers/bookmarks_helper.rb
Normal file
2
app/helpers/bookmarks_helper.rb
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
module BookmarksHelper
|
||||||
|
end
|
||||||
20
app/models/bookmark.rb
Normal file
20
app/models/bookmark.rb
Normal 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
|
||||||
|
|
@ -12,9 +12,9 @@ class StatusMessage
|
||||||
|
|
||||||
validates_presence_of :message
|
validates_presence_of :message
|
||||||
|
|
||||||
before_create :add_owner
|
before_create :set_default_owner
|
||||||
|
|
||||||
def self.newest(owner_email)
|
def self.newest(owner_email)
|
||||||
StatusMessage.last(:conditions => {:owner => owner_email})
|
StatusMessage.last(:conditions => {:owner => owner_email})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -24,8 +24,7 @@ class StatusMessage
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
def add_owner
|
def set_default_owner
|
||||||
self.owner ||= User.first.email
|
self.owner ||= User.first.email
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
||||||
12
app/views/bookmarks/_form.html.haml
Normal file
12
app/views/bookmarks/_form.html.haml
Normal 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
|
||||||
8
app/views/bookmarks/edit.html.haml
Normal file
8
app/views/bookmarks/edit.html.haml
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
- title "Edit Bookmark"
|
||||||
|
|
||||||
|
= render 'form'
|
||||||
|
|
||||||
|
%p
|
||||||
|
= link_to "Show", bookmark_path(@bookmark)
|
||||||
|
|
|
||||||
|
= link_to "View All", bookmarks_path
|
||||||
17
app/views/bookmarks/index.html.haml
Normal file
17
app/views/bookmarks/index.html.haml
Normal 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
|
||||||
5
app/views/bookmarks/new.html.haml
Normal file
5
app/views/bookmarks/new.html.haml
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
- title "New Bookmark"
|
||||||
|
|
||||||
|
= render 'form'
|
||||||
|
|
||||||
|
%p= link_to "Back to List", bookmarks_path
|
||||||
18
app/views/bookmarks/show.html.haml
Normal file
18
app/views/bookmarks/show.html.haml
Normal 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
|
||||||
|
|
@ -30,6 +30,7 @@
|
||||||
%li= link_to "Users", users_path
|
%li= link_to "Users", users_path
|
||||||
%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
|
||||||
|
|
||||||
.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 :bookmarks
|
||||||
|
|
||||||
resources :friends
|
resources :friends
|
||||||
|
|
||||||
resources :status_messages
|
resources :status_messages
|
||||||
|
|
|
||||||
80
spec/controllers/bookmarks_controller_spec.rb
Normal file
80
spec/controllers/bookmarks_controller_spec.rb
Normal 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
|
||||||
|
|
@ -8,7 +8,6 @@ describe StatusMessagesController do
|
||||||
StatusMessage.create(:message => "yodels.")
|
StatusMessage.create(:message => "yodels.")
|
||||||
end
|
end
|
||||||
|
|
||||||
#fixtures :all
|
|
||||||
render_views
|
render_views
|
||||||
|
|
||||||
it "index action should render index template" do
|
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
|
it "create action should redirect when model is valid" do
|
||||||
StatusMessage.any_instance.stubs(:valid?).returns(true)
|
StatusMessage.any_instance.stubs(:valid?).returns(true)
|
||||||
post :create
|
post :create
|
||||||
response.should redirect_to(status_message_url(assigns[:status_message]))
|
response.should redirect_to(status_messages_url)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "new action should render new template" do
|
it "new action should render new template" do
|
||||||
|
|
@ -47,7 +46,6 @@ describe StatusMessagesController do
|
||||||
|
|
||||||
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)
|
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
|
||||||
|
|
|
||||||
16
spec/models/bookmark_spec.rb
Normal file
16
spec/models/bookmark_spec.rb
Normal 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
|
||||||
Loading…
Reference in a new issue