diff --git a/app/controllers/bookmarks_controller.rb b/app/controllers/bookmarks_controller.rb new file mode 100644 index 000000000..8cfd3f066 --- /dev/null +++ b/app/controllers/bookmarks_controller.rb @@ -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 diff --git a/app/controllers/status_messages_controller.rb b/app/controllers/status_messages_controller.rb index 19b62ecca..7c65ed3c7 100644 --- a/app/controllers/status_messages_controller.rb +++ b/app/controllers/status_messages_controller.rb @@ -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 diff --git a/app/helpers/bookmarks_helper.rb b/app/helpers/bookmarks_helper.rb new file mode 100644 index 000000000..2f5878156 --- /dev/null +++ b/app/helpers/bookmarks_helper.rb @@ -0,0 +1,2 @@ +module BookmarksHelper +end diff --git a/app/models/bookmark.rb b/app/models/bookmark.rb new file mode 100644 index 000000000..3480c72fb --- /dev/null +++ b/app/models/bookmark.rb @@ -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 \ No newline at end of file diff --git a/app/models/status_message.rb b/app/models/status_message.rb index c0b27d87c..20add29c5 100644 --- a/app/models/status_message.rb +++ b/app/models/status_message.rb @@ -12,9 +12,9 @@ class StatusMessage 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}) end @@ -24,8 +24,7 @@ class StatusMessage protected - def add_owner + def set_default_owner self.owner ||= User.first.email end - end diff --git a/app/views/bookmarks/_form.html.haml b/app/views/bookmarks/_form.html.haml new file mode 100644 index 000000000..0e02f4082 --- /dev/null +++ b/app/views/bookmarks/_form.html.haml @@ -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 diff --git a/app/views/bookmarks/edit.html.haml b/app/views/bookmarks/edit.html.haml new file mode 100644 index 000000000..11421d0ba --- /dev/null +++ b/app/views/bookmarks/edit.html.haml @@ -0,0 +1,8 @@ +- title "Edit Bookmark" + += render 'form' + +%p + = link_to "Show", bookmark_path(@bookmark) + | + = link_to "View All", bookmarks_path diff --git a/app/views/bookmarks/index.html.haml b/app/views/bookmarks/index.html.haml new file mode 100644 index 000000000..c4ac79f65 --- /dev/null +++ b/app/views/bookmarks/index.html.haml @@ -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 diff --git a/app/views/bookmarks/new.html.haml b/app/views/bookmarks/new.html.haml new file mode 100644 index 000000000..f04d00838 --- /dev/null +++ b/app/views/bookmarks/new.html.haml @@ -0,0 +1,5 @@ +- title "New Bookmark" + += render 'form' + +%p= link_to "Back to List", bookmarks_path diff --git a/app/views/bookmarks/show.html.haml b/app/views/bookmarks/show.html.haml new file mode 100644 index 000000000..2072f4cf9 --- /dev/null +++ b/app/views/bookmarks/show.html.haml @@ -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 diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index 996ea92a5..86bda945a 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -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? diff --git a/config/routes.rb b/config/routes.rb index b454c731f..edcead33d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,6 @@ Diaspora::Application.routes.draw do |map| + resources :bookmarks + resources :friends resources :status_messages diff --git a/spec/controllers/bookmarks_controller_spec.rb b/spec/controllers/bookmarks_controller_spec.rb new file mode 100644 index 000000000..61dd7ca20 --- /dev/null +++ b/spec/controllers/bookmarks_controller_spec.rb @@ -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 diff --git a/spec/controllers/status_messages_controller_spec.rb b/spec/controllers/status_messages_controller_spec.rb index e0fe528d6..50cba6a45 100644 --- a/spec/controllers/status_messages_controller_spec.rb +++ b/spec/controllers/status_messages_controller_spec.rb @@ -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 diff --git a/spec/models/bookmark_spec.rb b/spec/models/bookmark_spec.rb new file mode 100644 index 000000000..309969bf0 --- /dev/null +++ b/spec/models/bookmark_spec.rb @@ -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 \ No newline at end of file