Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: jinscoe/sample_app
base: 4c8d07ce2b
...
head fork: jinscoe/sample_app
compare: e8c961c938
  • 4 commits
  • 22 files changed
  • 0 commit comments
  • 1 contributor
View
29 app/controllers/microposts_controller.rb
@@ -0,0 +1,29 @@
+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
View
4 app/controllers/pages_controller.rb
@@ -1,6 +1,10 @@
class PagesController < ApplicationController
def home
+ if signed_in?
+ @micropost = Micropost.new
+ @feed_items = current_user.feed.paginate(:page => params[:page])
+ end
end
def contact
View
6 app/controllers/users_controller.rb
@@ -10,6 +10,7 @@ def index
def show
@user = User.find(params[:id])
+ @microposts = @user.microposts.paginate :page => params[:page]
end
def new
@@ -52,10 +53,7 @@ def destroy
private
- def authenticate
- deny_access unless signed_in?
- end
-
+
def correct_user
@user = User.find(params[:id])
View
4 app/helpers/sessions_helper.rb
@@ -28,6 +28,10 @@ def current_user?(user)
user == current_user
end
+ def authenticate
+ deny_access unless signed_in?
+ end
+
def deny_access
store_location
redirect_to signin_path, :notice => "Please sign in to access this page."
View
4 app/models/user.rb
@@ -25,6 +25,10 @@ def has_password?(submitted_password)
encrypted_password == encrypt(submitted_password)
end
+ def feed
+ Micropost.where("user_id = ?", id)
+ end
+
def self.authenticate(email, submitted_password)
user = find_by_email(email)
(user && user.has_password?(submitted_password)) ? user : nil
View
13 app/views/microposts/_micropost.html.erb
@@ -0,0 +1,13 @@
+<tr>
+ <td class="micropost">
+ <span class="content"><%= micropost.content %></span>
+ <span class="timestamp">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 => "Are you sure?", :title => micropost.content %>
+ </td>
+ <% end %>
+
+</tr>
View
20 app/views/pages/home.html.erb
@@ -1,6 +1,26 @@
+<% if signed_in? %>
+
+<table class="front" summary ="for signed in users">
+ <tr>
+ <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' %>
+ </td>
+ </tr>
+
+</table>
+
+<% else %>
+
<% @page_title = "Home" %>
<h1>Sample App</h1>
<p>This is the home page</p>
<%= link_to "Sign up now!", signup_path, :class => "signup_button round" %>
+
+<% end %>
View
8 app/views/shared/_error_messages.html.erb
@@ -1,12 +1,12 @@
-<% if @user.errors.any? %>
+<% if object.errors.any? %>
<div id="error_explanation">
<h2>
- <%= pluralize(@user.errors.count, "error") %>
- prohibited this user from being saved:
+ <%= pluralize(object.errors.count, "error") %>
+ prohibited this <%= object.class.to_s.underscore.humanize.downcase %> from being saved:
</h2>
<p>There were problem with the following fields:</p>
<ul>
- <% @user.errors.full_messages.each do |error| %>
+ <% object.errors.full_messages.each do |error| %>
<li><%= error %></li>
<% end %>
</ul>
View
7 app/views/shared/_feed.html.erb
@@ -0,0 +1,7 @@
+<% if @feed_items.any? %>
+<table class = "microposts" summary ="Status Feed">
+ <%= render :partial => 'shared/feed_item', :collection => @feed_items %>
+</table>
+<%= will_paginate @feed_items %>
+
+<% end %>
View
15 app/views/shared/_feed_item.html.erb
@@ -0,0 +1,15 @@
+<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 => "Are you sure?", :title => feed_item.content %>
+ </td>
+ <% end %>
+</tr>
View
10 app/views/shared/_micropost_form.html.erb
@@ -0,0 +1,10 @@
+<%= form_for(@micropost) do |f| %>
+<%= render 'shared/error_messages', :object => f.object%>
+<div class="field">
+ <%= f.text_area :content %>
+</div>
+
+<div class="actions">
+ <%= f.submit "Submit!" %>
+</div>
+<% end %>
View
11 app/views/shared/_user_info.html.erb
@@ -0,0 +1,11 @@
+<div class="user_info">
+<a href="<%= user_path(current_user) %>">
+ <%= gravatar_for current_user, :size => 30 %>
+ <span class="user_name">
+ <%= current_user.name %>
+ </span>
+ <span class="microposts">
+ <%= pluralize(current_user.microposts.count, "micropost") %>
+ </span>
+ </a>
+</div>
View
1  app/views/users/_fields.html.erb
@@ -1,4 +1,3 @@
-<%= render :partial => 'shared/error_messages' %>
<div class = "field">
<%= f.label :name %>
View
2  app/views/users/edit.html.erb
@@ -3,7 +3,7 @@
<h1>Edit user</h1>
<%= form_for(@user) do |f| %>
-
+<%= render 'shared/error_messages', :object => f.object%>
<%= render :partial => 'fields', :locals => {:f => f} %>
<div class = "actions">
View
2  app/views/users/new.html.erb
@@ -4,6 +4,8 @@
<%= form_for(@user) do |f| %>
+<%= render 'shared/error_messages', :object => f.object%>
+
<%= render :partial => 'fields', :locals => {:f => f} %>
<div class = "actions">
View
9 app/views/users/show.html.erb
@@ -7,10 +7,17 @@
<%= gravatar_for @user %>
<%= @user.name %>
</h1>
+ <% if @user.microposts.any? %>
+ <table class="microposts" summary ="user microposts">
+ <%= render @microposts %>
+ </table>
+ <%= will_paginate @microposts %>
+ <% end %>
</td>
<td class="sidebar round">
<strong>Name</strong> <%= @user.name %><br />
- <strong>URL</strong> <%= link_to user_path(@user), @user %>
+ <strong>URL</strong> <%= link_to user_path(@user), @user %><br />
+ <strong>Microposts</strong> <%= @user.microposts.count %>
</td>
</tr>
</table>
View
1  config/routes.rb
@@ -2,6 +2,7 @@
resources :users
resources :sessions, :only => [:new, :create, :destroy]
+ resources :microposts, :only => [:create, :destroy]
root :to => "pages#home"
View
6 lib/tasks/sample_data.rake
@@ -15,6 +15,12 @@ namespace :db do
User.create!(:name => name, :email => email, :password => password, :password_confirmation => password)
end
+ User.all(:limit => 6).each do |user|
+ 50.times do
+ user.microposts.create!(:content => Faker::Lorem.sentence(5))
+ end
+ end
+
end
View
55 public/stylesheets/custom.css
@@ -188,4 +188,59 @@ ul.users {
.users li {
list-style: none;
+}
+
+h1.micropost {
+ margin-bottom: 0.3em;
+}
+
+table.microposts {
+ margin-top: 1em;
+}
+
+table.microposts tr {
+ height: 70px;
+}
+
+table.microposts tr td.gravatar {
+ border-top: 1px solid #ccc;
+ vertical-align: top;
+ width: 50px;
+}
+
+table.microposts tr td.micropost {
+ border-top: 1px solid #ccc;
+ vertical-align: top;
+ padding-top: 10px;
+}
+
+table.microposts tr td.micropost span.timestamp {
+ display: block;
+ font-size: 85%;
+ color: #666;
+}
+
+div.user_info img {
+ padding-right: 0.1em;
+}
+
+div.user_info a {
+ text-decoration: none;
+}
+
+div.user_info span.user_name {
+ position: absolute;
+}
+
+div.user_info span.microposts {
+ font-size: 80%;
+}
+
+form.new_micropost {
+ margin-bottom: 2em;
+}
+
+form.new_micropost textarea {
+ height: 4em;
+ margin-bottom: 0;
}
View
110 spec/controllers/microposts_controller_spec.rb
@@ -0,0 +1,110 @@
+require 'spec_helper'
+
+describe MicropostsController do
+
+render_views
+
+ describe "Access control" do
+
+ it "should deny aces to create" do
+ post :create
+ response.should redirect_to(signin_path)
+ end
+
+ it "should deny acess to destroy" do
+ delete :create, :id => 1
+ response.should redirect_to(signin_path)
+ end
+
+ end
+
+ describe "POST create" do
+
+ before(:each) do
+ @user = test_sign_in(Factory(:user))
+ end
+
+ describe "failure" do
+
+ before(:each) do
+ @attr = {:content => ""}
+ end
+
+ it "should not create a micropost" do
+ lambda do
+ post :create, :micropost => @attr
+ end.should_not change(Micropost, :count)
+ end
+
+ it "should re-render the home page" do
+
+ post :create, :micropost => @attr
+ response.should render_template('pages/home')
+
+ end
+
+ end
+
+
+
+ describe "success" do
+
+ before(:each) do
+ @attr = {:content => "foo bar"}
+ end
+
+ it "should create a micropost" do
+ lambda do
+ post :create, :micropost => @attr
+ end.should change(Micropost, :count).by(1)
+ end
+
+ it "shoudl redirect to root path" do
+ post :create, :micropost => @attr
+ response.should redirect_to(root_path)
+ end
+
+ it "should have a flash success message" do
+ post :create, :micropost => @attr
+ 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
+ end.should change(Micropost, :count).by(-1)
+ end
+ end
+ end
+ end
View
20 spec/controllers/users_controller_spec.rb
@@ -111,6 +111,26 @@
response.should have_selector('td>a', :content => user_path(@user), :href => user_path(@user))
end
+ it "should show the users microposts" do
+ mp1 = Factory(:micropost, :user => @user, :content => "Foo bar" )
+ mp2 = Factory(:micropost, :user => @user, :content => "Baz qux" )
+ get :show, :id => @user
+ response.should have_selector('span.content', :content => mp1.content)
+ response.should have_selector('span.content', :content => mp2.content)
+ end
+
+ it "should paginate microposts" do
+ 35.times{Factory(:micropost, :user => @user, :content => "foo" )}
+ get :show, :id => @user
+ response.should have_selector('div.pagination')
+ end
+
+ it "should display the micropost count" do
+ 10.times{Factory(:micropost, :user => @user, :content => "foo" )}
+ get :show, :id => @user
+ response.should have_selector('td.sidebar', :content => @user.microposts.count.to_s)
+ end
+
end
describe "GET 'new'" do
View
17 spec/models/user_spec.rb
@@ -181,6 +181,23 @@
@mp2 = Factory(:micropost, :user => @user, :created_at => 1.hour.ago )
end
+ describe "status feed" do
+ it "should have a feed" do
+ @user.should respond_to(:feed)
+ end
+
+ it "should include users microposts" do
+ @user.feed.include?(@mp1)
+ @user.feed.include?(@mp2)
+ end
+
+ it "should not include a different users micro posts" do
+ mp3 = Factory(:micropost, :user => Factory(:user, :email => Factory.next(:email)))
+ @user.feed.should_not include(mp3)
+ end
+
+ end
+
it "should have a microposts attribute" do
@user.should respond_to(:microposts)
end

No commit comments for this range

Something went wrong with that request. Please try again.