From 960db53b9ba336565371120ffb488900120ac3ea Mon Sep 17 00:00:00 2001 From: maxwell Date: Mon, 14 Jun 2010 13:12:17 -0700 Subject: [PATCH] MS IZ DG completed crud for friend --- app/controllers/friends_controller.rb | 32 ++++++++++++ app/controllers/status_messages_controller.rb | 1 + app/helpers/friends_helper.rb | 2 + app/models/friend.rb | 10 ++++ app/views/friends/index.html.haml | 14 ++++++ app/views/friends/new.html.haml | 17 +++++++ app/views/friends/show.html.haml | 13 +++++ app/views/layouts/application.html.haml | 1 + config/routes.rb | 2 + spec/controllers/friends_controller_spec.rb | 50 +++++++++++++++++++ spec/models/friend_spec.rb | 12 +++++ 11 files changed, 154 insertions(+) create mode 100644 app/controllers/friends_controller.rb create mode 100644 app/helpers/friends_helper.rb create mode 100644 app/models/friend.rb create mode 100644 app/views/friends/index.html.haml create mode 100644 app/views/friends/new.html.haml create mode 100644 app/views/friends/show.html.haml create mode 100644 spec/controllers/friends_controller_spec.rb create mode 100644 spec/models/friend_spec.rb diff --git a/app/controllers/friends_controller.rb b/app/controllers/friends_controller.rb new file mode 100644 index 000000000..000185f6e --- /dev/null +++ b/app/controllers/friends_controller.rb @@ -0,0 +1,32 @@ +class FriendsController < ApplicationController + before_filter :authenticate_user! + + def index + @friends = Friend.all + end + + def show + @friend = Friend.first(:conditions=> {:id => params[:id]}) + end + + def destroy + @friend = Friend.first(:conditions=> {:id => params[:id]}) + @friend.destroy + flash[:notice] = "Successfully destroyed friend." + redirect_to friends_url + end + + def new + @friend = Friend.new + end + + def create + @friend = Friend.new(params[:friend]) + if @friend.save + flash[:notice] = "Successfully created friend." + redirect_to @friend + else + render :action => 'new' + end + end +end diff --git a/app/controllers/status_messages_controller.rb b/app/controllers/status_messages_controller.rb index 0398931c8..e34ee6ce0 100644 --- a/app/controllers/status_messages_controller.rb +++ b/app/controllers/status_messages_controller.rb @@ -3,6 +3,7 @@ class StatusMessagesController < ApplicationController def index @status_messages = StatusMessage.all + @friends = Friend.all end def create diff --git a/app/helpers/friends_helper.rb b/app/helpers/friends_helper.rb new file mode 100644 index 000000000..0b69e9bce --- /dev/null +++ b/app/helpers/friends_helper.rb @@ -0,0 +1,2 @@ +module FriendsHelper +end diff --git a/app/models/friend.rb b/app/models/friend.rb new file mode 100644 index 000000000..6c71adb08 --- /dev/null +++ b/app/models/friend.rb @@ -0,0 +1,10 @@ +class Friend + include Mongoid::Document + include Mongoid::Timestamps + + field :username + field :url + + validates_presence_of :username, :url + +end \ No newline at end of file diff --git a/app/views/friends/index.html.haml b/app/views/friends/index.html.haml new file mode 100644 index 000000000..044ade323 --- /dev/null +++ b/app/views/friends/index.html.haml @@ -0,0 +1,14 @@ +- title "Friends" + +%table + %tr + %th username + %th url + - for friend in @friends + %tr + %td= friend.username + %td= friend.url + %td= link_to 'Show', friend + %td= link_to 'Destroy', friend, :confirm => 'Are you sure?', :method => :delete + +%p= link_to "New Friend", new_friend_path diff --git a/app/views/friends/new.html.haml b/app/views/friends/new.html.haml new file mode 100644 index 000000000..d1dd09a17 --- /dev/null +++ b/app/views/friends/new.html.haml @@ -0,0 +1,17 @@ +- title "New Friend" + +- form_for @friend do |f| + = f.error_messages + %p + = f.label :username + %br + = f.text_field :username + %p + = f.label :url + %br + = f.text_field :url + %p + = f.submit + + +%p= link_to "Back to List", friends_path diff --git a/app/views/friends/show.html.haml b/app/views/friends/show.html.haml new file mode 100644 index 000000000..034eb23ce --- /dev/null +++ b/app/views/friends/show.html.haml @@ -0,0 +1,13 @@ +- title "Friend" + +%p + %strong Username: + = @friend.username +%p + %strong Url: + = @friend.url + +%p + = link_to "Destroy", @friend, :confirm => 'Are you sure?', :method => :delete + | + = link_to "View All", friends_path diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index ba5ba735f..26089e42d 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -27,6 +27,7 @@ %ul.nav %li= link_to "Users", users_url %li= link_to "Status Messages", status_messages_url + %li= link_to "Friends", friends_url .container - if show_title? diff --git a/config/routes.rb b/config/routes.rb index 552d540cb..b454c731f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,6 @@ Diaspora::Application.routes.draw do |map| + resources :friends + resources :status_messages #routes for devise, not really sure you will need to mess with this in the future, lets put default, diff --git a/spec/controllers/friends_controller_spec.rb b/spec/controllers/friends_controller_spec.rb new file mode 100644 index 000000000..8ebffbc8a --- /dev/null +++ b/spec/controllers/friends_controller_spec.rb @@ -0,0 +1,50 @@ +require File.dirname(__FILE__) + '/../spec_helper' + +describe FriendsController do + render_views + before do + #TODO(dan) Mocking Warden; this is a temp fix + request.env['warden'] = mock_model(Warden, :authenticate => @user, :authenticate! => @user) + Friend.create(:username => "max", :url => "http://max.com/") + 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 + + it "show action should render show template" do + request.env['warden'].should_receive(:authenticate?).at_least(:once) + get :show, :id => Friend.first.id + response.should render_template(:show) + end + + it "destroy action should destroy model and redirect to index action" do + friend = Friend.first + delete :destroy, :id => friend.id + response.should redirect_to(friends_url) + Friend.first(:conditions => {:id => friend.id}).should be_nil + 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) + end + + it "create action should redirect when model is valid" do + Friend.any_instance.stubs(:valid?).returns(true) + post :create + response.should redirect_to(friend_url(assigns[:friend])) + end +end diff --git a/spec/models/friend_spec.rb b/spec/models/friend_spec.rb new file mode 100644 index 000000000..602da02c8 --- /dev/null +++ b/spec/models/friend_spec.rb @@ -0,0 +1,12 @@ +require File.dirname(__FILE__) + '/../spec_helper' + +describe Friend do + it 'should have a diaspora username + diaspora url' do + n = Friend.new(:username => 'max') + n.valid?.should == false + n.url = "http://max.com/" + n.valid?.should == true + end + + +end \ No newline at end of file