Skip to content

Commit

Permalink
Finished microposts
Browse files Browse the repository at this point in the history
  • Loading branch information
mhartl committed Aug 30, 2010
1 parent 4bcc03b commit bc7c1c0
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 1 deletion.
11 changes: 11 additions & 0 deletions app/controllers/microposts_controller.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
class MicropostsController < ApplicationController
before_filter :authenticate
before_filter :authorized_user, :only => :destroy

def create
@micropost = current_user.microposts.build(params[:micropost])
if @micropost.save
redirect_to root_path, :flash => { :success => "Micropost created!" }
else
@feed_items = []
render 'pages/home'
end
end

def destroy
@micropost.destroy
redirect_to root_path, :flash => { :success => "Micropost deleted!" }
end

private

def authorized_user
@micropost = Micropost.find(params[:id])
redirect_to root_path unless current_user?(@micropost.user)
end
end
5 changes: 4 additions & 1 deletion app/controllers/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ class PagesController < ApplicationController

def home
@title = "Home"
@micropost = Micropost.new if signed_in?
if signed_in?
@micropost = Micropost.new
@feed_items = current_user.feed.paginate(:page => params[:page])
end
end

def contact
Expand Down
4 changes: 4 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class User < ActiveRecord::Base
def has_password?(submitted_password)
encrypted_password == encrypt(submitted_password)
end

def feed
Micropost.where("user_id = ?", id)
end

class << self
def authenticate(email, submitted_password)
Expand Down
8 changes: 8 additions & 0 deletions app/views/microposts/_micropost.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,12 @@
Posted <%= time_ago_in_words(micropost.created_at) %> ago.
</span>
</td>
<% user = micropost.user rescue User.find(micropost.user_id) %>
<% if current_user?(user) %>
<td>
<%= link_to "delete", micropost, :method => :delete,
:confirm => "You sure?",
:title => micropost.content %>
</td>
<% end %>
</tr>
1 change: 1 addition & 0 deletions app/views/pages/home.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<td class="main">
<h1 class="micropost">What's up?</h1>
<%= render 'shared/micropost_form' %>
<%= render 'shared/feed' %>
</td>
<td class="sidebar round">
<%= render 'shared/user_info' %>
Expand Down
6 changes: 6 additions & 0 deletions app/views/shared/_feed.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<% if @feed_items.any? %>
<table class="microposts" summary="Status feed">
<%= render :partial => 'shared/feed_item', :collection => @feed_items %>
</table>
<%= will_paginate @feed_items %>
<% end %>
21 changes: 21 additions & 0 deletions app/views/shared/_feed_item.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<tr>
<td class="gravatar">
<%= link_to gravatar_for(feed_item.user), feed_item.user %>
</td>
<td class="micropost">
<span class="user">
<%= link_to feed_item.user.name, feed_item.user %>
</span>
<span class="content"><%= feed_item.content %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(feed_item.created_at) %> ago.
</span>
</td>
<% if current_user?(feed_item.user) %>
<td>
<%= link_to "delete", feed_item, :method => :delete,
:confirm => "You sure?",
:title => feed_item.content %>
</td>
<% end %>
</tr>
33 changes: 33 additions & 0 deletions spec/controllers/microposts_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,39 @@
flash[:success].should =~ /micropost created/i
end
end
end

describe "DELETE 'destroy'" do

describe "for an unauthorized user" do

before(:each) do
@user = Factory(:user)
wrong_user = Factory(:user, :email => Factory.next(:email))
@micropost = Factory(:micropost, :user => @user)
test_sign_in(wrong_user)
end

it "should deny access" do
delete :destroy, :id => @micropost
response.should redirect_to(root_path)
end
end

describe "for an authorized user" do

before(:each) do
@user = test_sign_in(Factory(:user))
@micropost = Factory(:micropost, :user => @user)
end

it "should destroy the micropost" do
lambda do
delete :destroy, :id => @micropost
flash[:success].should =~ /deleted/i
response.should redirect_to(root_path)
end.should change(Micropost, :count).by(-1)
end
end
end
end
17 changes: 17 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,5 +197,22 @@
end.should raise_error(ActiveRecord::RecordNotFound)
end
end

describe "status feed" do
it "should have a feed" do
@user.should respond_to(:feed)
end

it "should include the user's microposts" do
@user.feed.should include(@mp1)
@user.feed.should include(@mp2)
end

it "should not include a different user's microposts" do
mp3 = Factory(:micropost,
:user => Factory(:user, :email => Factory.next(:email)))
@user.feed.should_not include(mp3)
end
end
end
end
39 changes: 39 additions & 0 deletions spec/requests/microposts_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'spec_helper'

describe "Microposts" do

before(:each) do
user = Factory(:user)
visit signin_path
fill_in :email, :with => user.email
fill_in :password, :with => user.password
click_button
end

describe "creation" do

describe "failure" do
it "should not make a new micropost" do
lambda do
visit root_path
fill_in :micropost_content, :with => ""
click_button
response.should render_template('pages/home')
response.should have_selector('div#error_explanation')
end.should_not change(Micropost, :count)
end
end

describe "success" do
it "should make a new micropost" do
content = "Lorem ipsum dolor sit amet"
lambda do
visit root_path
fill_in :micropost_content, :with => content
click_button
response.should have_selector('span.content', :content => content)
end.should change(Micropost, :count).by(1)
end
end
end
end

0 comments on commit bc7c1c0

Please sign in to comment.