Permalink
Browse files

Add user microposts

  • Loading branch information...
1 parent ba60e46 commit be159fe33d08abbfb5a6f79f67dae5ebcc8b4dd6 @etrainey committed Feb 15, 2012
View
20 app/controllers/microposts_controller.rb
@@ -1,9 +1,27 @@
class MicropostsController < ApplicationController
- before_filter :signed_in_user
+ before_filter :signed_in_user, only: [:create, :destroy]
+ before_filter :correct_user, only: :destroy
def create
+ @micropost = current_user.microposts.build(params[:micropost])
+ if @micropost.save
+ flash[:success] = "Micropost created!"
+ redirect_to root_path
+ else
+ @feed_items = []
+ render 'static_pages/home'
+ end
end
def destroy
+ @micropost.destroy
+ redirect_back_or root_path
end
+
+ private
+
+ def correct_user
+ @micropost = current_user.microposts.find(params[:id])
+ redirect_to root_path if @micropost.nil?
+ end
end
View
5 app/controllers/static_pages_controller.rb
@@ -1,5 +1,10 @@
class StaticPagesController < ApplicationController
+
def home
+ if signed_in?
+ @micropost = current_user.microposts.build
+ @feed_items = current_user.feed.paginate(page: params[:page])
+ end
end
def help
View
5 app/models/user.rb
@@ -11,6 +11,11 @@ class User < ActiveRecord::Base
uniqueness: { case_sensitive: false }
validates :password, length: { minimum: 6}
+ def feed
+ # This is preliminary. See "Following users" for the full implementation.
+ Micropost.where("user_id = ?", id)
+ end
+
private
def create_remember_token
self.remember_token = SecureRandom.urlsafe_base64
View
7 app/views/microposts/_micropost.html.erb
@@ -5,4 +5,11 @@
Posted <%= time_ago_in_words(micropost.created_at) %> ago.
</span>
</td>
+ <% if current_user?(micropost.user) %>
+ <td>
+ <%= link_to "delete", micropost, method: :delete,
+ confirm: "You sure?",
+ title: micropost.content %>
+ </td>
+ <% end %>
</tr>
View
9 app/views/shared/_error_messages.html.erb
@@ -1,10 +1,11 @@
-<% if @user.errors.any? %>
+<% if object.errors.any? %>
<div id="error_explanation">
- <h2><%= pluralize(@user.errors.count, "error") %>
- prohibited this user from being saved:</h2>
+ <h2><%= pluralize(object.errors.count, "error") %>
+ prohibited this <%= object.class.to_s.underscore.humanize.downcase %>
+ from being saved:</h2>
<p>There were problems with the following fields:</p>
<ul>
- <% @user.errors.full_messages.each do |msg| %>
+ <% object.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
View
6 app/views/shared/_feed.html.erb
@@ -0,0 +1,6 @@
+<% if @feed_items.any? %>
+ <table class="microposts">
+ <%= render partial: 'shared/feed_item', collection: @feed_items %>
+ </table>
+ <%= will_paginate @feed_items %>
+<% end %>
View
19 app/views/shared/_feed_item.html.erb
@@ -0,0 +1,19 @@
+<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>
View
9 app/views/shared/_micropost_form.html.erb
@@ -0,0 +1,9 @@
+<%= 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
29 app/views/static_pages/home.html.erb
@@ -1,10 +1,25 @@
<% provide(:title, 'Home') %>
-<h1>Sample App</h1>
+<% if signed_in? %>
+ <table class="front">
+ <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 %>
+ <h1>Sample App</h1>
-<p>
- This is the home page for the
- <a href="http://railstutorial.org/">Ruby on Rails Tutorial</a>
- sample application.
-</p>
+ <p>
+ This is the home page for the
+ <a href="http://railstutorial.org/">Ruby on Rails Tutorial</a>
+ sample application.
+ </p>
-<%= link_to "Sign up now!", signup_path, class: "signup_button round" %>
+ <%= link_to "Sign up now!", signup_path, class: "signup_button round" %>
+<% end %>
View
2 app/views/users/edit.html.erb
@@ -2,7 +2,7 @@
<h1>Edit user</h1>
<%= form_for(@user) do |f| %>
- <%= render 'shared/error_messages' %>
+ <%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
View
2 app/views/users/new.html.erb
@@ -2,7 +2,7 @@
<h1>Sign up</h1>
<%= form_for(@user) do |f| %>
- <%= render 'shared/error_messages' %>
+ <%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
View
13 spec/models/user_spec.rb
@@ -22,6 +22,7 @@
it { should respond_to(:admin) }
it { should respond_to(:authenticate) }
it { should respond_to(:microposts) }
+ it { should respond_to(:feed) }
it { should be_valid }
it { should_not be_admin }
@@ -109,7 +110,7 @@
it { should be_admin }
end
- describe "micropost associations" do
+ describe "micropost associations" do
before { @user.save }
let!(:older_micropost) do
@@ -130,5 +131,15 @@
Micropost.find_by_id(micropost.id).should be_nil
end
end
+
+ describe "status" do
+ let(:unfollowed_post) do
+ FactoryGirl.create(:micropost, user: FactoryGirl.create(:user))
+ end
+
+ its(:feed) { should include(newer_micropost) }
+ its(:feed) { should include(older_micropost) }
+ its(:feed) { should_not include(unfollowed_post) }
+ end
end
end
View
46 spec/requests/micropost_pages_spec.rb
@@ -0,0 +1,46 @@
+require 'spec_helper'
+
+describe "Micropost pages" do
+
+ subject { page }
+
+ let(:user) { FactoryGirl.create(:user) }
+ before { sign_in(user) }
+
+ describe "micropost creation" do
+ before { visit root_path }
+
+ describe "with invalid information" do
+
+ it "should not create a micropost" do
+ expect { click_button "Submit" }.should_not change(Micropost, :count)
+ end
+
+ describe "error messages" do
+ let(:error) { '1 error prohibited this micropost from being saved' }
+ before { click_button "Submit" }
+ it { should have_content(error) }
+ end
+ end
+
+ describe "with valid information" do
+
+ before { fill_in 'micropost_content', with: "Lorem ipsum" }
+ it "should create a micropost" do
+ expect { click_button "Submit" }.should change(Micropost, :count).by(1)
+ end
+ end
+ end
+
+ describe "micropost destruction" do
+ before { FactoryGirl.create(:micropost, user: user) }
+
+ describe "as correct user" do
+ before { visit root_path }
+
+ it "should delete a micropost" do
+ expect { click_link "delete" }.should change(Micropost, :count).by(-1)
+ end
+ end
+ end
+end
View
16 spec/requests/static_pages_spec.rb
@@ -9,6 +9,22 @@
it { should have_selector('h1', text: 'Sample App') }
it { should have_selector('title', text: full_title('Home')) }
+
+ describe "for signed-in users" do
+ let(:user) { FactoryGirl.create(:user) }
+ before do
+ FactoryGirl.create(:micropost, user: user, content: "Lorem ipsum")
+ FactoryGirl.create(:micropost, user: user, content: "Dolor sit amet")
+ sign_in(user)
+ visit root_path
+ end
+
+ it "should render the user's feed" do
+ user.feed.each do |item|
+ page.should have_selector("tr##{item.id}", text: item.content)
+ end
+ end
+ end
end
describe "Help page" do

0 comments on commit be159fe

Please sign in to comment.