Skip to content

Commit

Permalink
Add user microposts
Browse files Browse the repository at this point in the history
  • Loading branch information
mhartl committed Mar 8, 2012
1 parent 5c7dec7 commit 04a039c
Show file tree
Hide file tree
Showing 29 changed files with 374 additions and 26 deletions.
Binary file added app/.DS_Store
Binary file not shown.
30 changes: 29 additions & 1 deletion app/assets/stylesheets/custom.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,32 @@ input, textarea, select, .uneditable-input {
border-bottom: 1px solid $grayLighter
}
}
}
}

/* microposts */

.microposts {
list-style: none;
margin: 10px 0 0 0;

li {
padding: 10px 0;
border-top: 1px solid #e8e8e8;
}
}
.content {
display: block;
}
.timestamp {
color: $grayLight;
}
.gravatar {
float: left;
margin-right: 10px;
}
aside {
textarea {
height: 100px;
margin-bottom: 5px;
}
}
27 changes: 27 additions & 0 deletions app/controllers/microposts_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class MicropostsController < ApplicationController
before_filter :signed_in_user
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_by_id(params[:id])
redirect_to root_path if @micropost.nil?
end
end
7 changes: 6 additions & 1 deletion app/controllers/static_pages_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
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
end

Expand Down
8 changes: 1 addition & 7 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def index

def show
@user = User.find(params[:id])
@microposts = @user.microposts.paginate(page: params[:page])
end

def new
Expand Down Expand Up @@ -47,13 +48,6 @@ def destroy

private

def signed_in_user
unless signed_in?
store_location
redirect_to signin_path, notice: "Please sign in."
end
end

def correct_user
@user = User.find(params[:id])
redirect_to(root_path) unless current_user?(@user)
Expand Down
7 changes: 7 additions & 0 deletions app/helpers/sessions_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ def current_user?(user)
user == current_user
end

def signed_in_user
unless signed_in?
store_location
redirect_to signin_path, notice: "Please sign in."
end
end

def sign_out
cookies.delete(:remember_token)
end
Expand Down
9 changes: 9 additions & 0 deletions app/models/micropost.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Micropost < ActiveRecord::Base
attr_accessible :content
belongs_to :user

validates :content, presence: true, length: { maximum: 140 }
validates :user_id, presence: true

default_scope order: 'microposts.created_at DESC'
end
7 changes: 7 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
has_secure_password
has_many :microposts, dependent: :destroy

before_save :create_remember_token

validates :name, presence: true, length: { maximum: 50 }
Expand All @@ -10,6 +12,11 @@ class User < ActiveRecord::Base
validates :password, length: { minimum: 6 }
validates :password_confirmation, presence: true


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

private

def create_remember_token
Expand Down
11 changes: 11 additions & 0 deletions app/views/microposts/_micropost.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<li>
<span class="content"><%= micropost.content %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(micropost.created_at) %> ago.
</span>
<% if current_user?(micropost.user) %>
<%= link_to "delete", micropost, method: :delete,
confirm: "You sure?",
title: micropost.content %>
<% end %>
</li>
8 changes: 4 additions & 4 deletions app/views/shared/_error_messages.html.erb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<% if @user.errors.any? %>
<% if object.errors.any? %>
<div id="error_explanation">
<div class="alert alert-error">
The form contains <%= pluralize(@user.errors.count, "error") %>.
The form contains <%= pluralize(object.errors.count, "error") %>.
</div>
<ul>
<% @user.errors.full_messages.each do |msg| %>
<% object.errors.full_messages.each do |msg| %>
<li>* <%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<% end %>
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? %>
<ol class="microposts">
<%= render partial: 'shared/feed_item', collection: @feed_items %>
</ol>
<%= will_paginate @feed_items %>
<% end %>
15 changes: 15 additions & 0 deletions app/views/shared/_feed_item.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<li id="<%= feed_item.id %>">
<%= link_to gravatar_for(feed_item.user), feed_item.user %>
<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>
<% if current_user?(feed_item.user) %>
<%= link_to "delete", feed_item, method: :delete,
confirm: "You sure?",
title: feed_item.content %>
<% end %>
</li>
7 changes: 7 additions & 0 deletions app/views/shared/_micropost_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<%= form_for(@micropost) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_area :content, placeholder: "Compose new micropost..." %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
12 changes: 12 additions & 0 deletions app/views/shared/_user_info.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<a href="<%= user_path(current_user) %>">
<%= gravatar_for current_user, size: 52 %>
</a>
<h1>
<%= current_user.name %>
</h1>
<span>
<%= link_to "view my profile", current_user %>
</span>
<span>
<%= pluralize(current_user.microposts.count, "micropost") %>
</span>
38 changes: 28 additions & 10 deletions app/views/static_pages/home.html.erb
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
<div class="center hero-unit">
<h1>Welcome to the Sample App</h1>
<% if signed_in? %>
<div class="row">
<aside class="span4">
<section>
<%= render 'shared/user_info' %>
</section>
<section>
<%= render 'shared/micropost_form' %>
</section>
</aside>
<div class="span8">
<h3>Micropost Feed</h3>
<%= render 'shared/feed' %>
</div>
</div>
<% else %>
<div class="center hero-unit">
<h1>Welcome to the Sample App</h1>

<h2>
This is the home page for the
<a href="http://railstutorial.org/">Ruby on Rails Tutorial</a>
sample application.
</h2>
<h2>
This is the home page for the
<a href="http://railstutorial.org/">Ruby on Rails Tutorial</a>
sample application.
</h2>

<%= link_to "Sign up now!", '#', class: "btn btn-large btn-primary" %>
</div>
<%= link_to "Sign up now!", signup_path,
class: "btn btn-large btn-primary" %>
</div>

<%= link_to image_tag("rails.png", alt: "Rails"), 'http://rubyonrails.org/' %>
<%= link_to image_tag("rails.png", alt: "Rails"), 'http://rubyonrails.org/' %>
<% end %>
Binary file added app/views/users/.DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion app/views/users/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<div class="row">
<div class="span6 offset3">
<%= form_for(@user) do |f| %>
<%= render 'shared/error_messages' %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :name %>
<%= f.text_field :name %>
Expand Down
9 changes: 9 additions & 0 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,13 @@
</h1>
</section>
</aside>
<div class="span8">
<% if @user.microposts.any? %>
<h3>Microposts (<%= @user.microposts.count %>)</h3>
<ol class="microposts">
<%= render @microposts %>
</ol>
<%= will_paginate @microposts %>
<% end %>
</div>
</div>
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
SampleApp::Application.routes.draw do
resources :users
resources :sessions, only: [:new, :create, :destroy]

resources :microposts, only: [:create, :destroy]

root to: 'static_pages#home'

match '/signup', to: 'users#new'
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20120308210452_create_microposts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateMicroposts < ActiveRecord::Migration
def change
create_table :microposts do |t|
t.string :content
t.integer :user_id

t.timestamps
end
add_index :microposts, [:user_id, :created_at]
end
end
11 changes: 10 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,16 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20120308193644) do
ActiveRecord::Schema.define(:version => 20120308210452) do

create_table "microposts", :force => true do |t|
t.string "content"
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

add_index "microposts", ["user_id", "created_at"], :name => "index_microposts_on_user_id_and_created_at"

create_table "users", :force => true do |t|
t.string "name"
Expand Down
6 changes: 6 additions & 0 deletions lib/tasks/sample_data.rake
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,11 @@ namespace :db do
password: password,
password_confirmation: password)
end

users = User.all(limit: 6)
50.times do
content = Faker::Lorem.sentence(5)
users.each { |user| user.microposts.create!(content: content) }
end
end
end
5 changes: 5 additions & 0 deletions spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@
admin true
end
end

factory :micropost do
content "Lorem ipsum"
user
end
end
36 changes: 36 additions & 0 deletions spec/models/micropost_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'spec_helper'

describe Micropost do

let(:user) { FactoryGirl.create(:user) }
before { @micropost = user.microposts.build(content: "Lorem ipsum") }

subject { @micropost }

it { should respond_to(:content) }
it { should respond_to(:user_id) }
it { should respond_to(:user) }
its(:user) { should == user }

it { should be_valid }

describe "when user_id is not present" do
before { @micropost.user_id = nil }
it { should_not be_valid }
end

describe "when user_id is not present" do
before { @micropost.user_id = nil }
it { should_not be_valid }
end

describe "with blank content" do
before { @micropost.content = " " }
it { should_not be_valid }
end

describe "with content that is too long" do
before { @micropost.content = "a" * 141 }
it { should_not be_valid }
end
end
Loading

0 comments on commit 04a039c

Please sign in to comment.