MS IZ DG completed crud for friend

This commit is contained in:
maxwell 2010-06-14 13:12:17 -07:00
parent 63f5fe15d6
commit 960db53b9b
11 changed files with 154 additions and 0 deletions

View file

@ -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

View file

@ -3,6 +3,7 @@ class StatusMessagesController < ApplicationController
def index
@status_messages = StatusMessage.all
@friends = Friend.all
end
def create

View file

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

10
app/models/friend.rb Normal file
View file

@ -0,0 +1,10 @@
class Friend
include Mongoid::Document
include Mongoid::Timestamps
field :username
field :url
validates_presence_of :username, :url
end

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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?

View file

@ -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,

View file

@ -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

View file

@ -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