Skip to content

Commit

Permalink
Add user microposts
Browse files Browse the repository at this point in the history
  • Loading branch information
windless committed Oct 16, 2012
1 parent 7a5006a commit 090c1c7
Show file tree
Hide file tree
Showing 34 changed files with 406 additions and 28 deletions.
3 changes: 3 additions & 0 deletions app/assets/javascripts/microposts.js.coffee
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
28 changes: 28 additions & 0 deletions app/assets/stylesheets/custom.css.scss
Expand Up @@ -183,3 +183,31 @@ input, textarea, select, .uneditable-input {
}
}
}

/* 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;
}
}
3 changes: 3 additions & 0 deletions app/assets/stylesheets/microposts.css.scss
@@ -0,0 +1,3 @@
// Place all the styles related to the microposts controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
29 changes: 29 additions & 0 deletions app/controllers/microposts_controller.rb
@@ -0,0 +1,29 @@
class MicropostsController < ApplicationController
before_filter :signed_in_user, :only => [:create, :destroy]
before_filter :correct_user, :only => :destroy

def index
end

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

def destroy
@micropost.destroy
redirect_to 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
4 changes: 4 additions & 0 deletions app/controllers/static_pages_controller.rb
@@ -1,5 +1,9 @@
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
Expand Down
8 changes: 1 addition & 7 deletions app/controllers/users_controller.rb
Expand Up @@ -13,6 +13,7 @@ def new

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

def create
Expand Down Expand Up @@ -46,13 +47,6 @@ def destroy
end

private
def signed_in_user
unless signed_in?
store_location
redirect_to signin_path, :notice => "Pleas sign in."
end
end

def correct_user
@user = User.find(params[:id])
redirect_to root_path unless current_user?(@user)
Expand Down
2 changes: 2 additions & 0 deletions app/helpers/microposts_helper.rb
@@ -0,0 +1,2 @@
module MicropostsHelper
end
7 changes: 7 additions & 0 deletions app/helpers/sessions_helper.rb
Expand Up @@ -33,4 +33,11 @@ def redirect_back_or(default)
def store_location
session[:return_to] = request.url
end

def signed_in_user
unless signed_in?
store_location
redirect_to signin_path, :notice => "Pleas sign in."
end
end
end
7 changes: 4 additions & 3 deletions app/helpers/users_helper.rb
@@ -1,7 +1,8 @@
module UsersHelper
def gravatar_for(user)
def gravatar_for(user, options = { :size => 50 })
gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}"
image_tag(gravatar_url, :alt => user.name, :class => "gravatar")
size = options[:size]
gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}?s=#{size}"
image_tag(gravatar_url, :alt => user.name, :class => "gravatar")
end
end
23 changes: 23 additions & 0 deletions app/models/micropost.rb
@@ -0,0 +1,23 @@
# == Schema Information
#
# Table name: microposts
#
# id :integer not null, primary key
# content :string(255)
# user_id :integer
# created_at :datetime not null
# updated_at :datetime not null
#

class Micropost < ActiveRecord::Base
attr_accessible :content
belongs_to :user

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

default_scope :order => 'created_at DESC'
end
7 changes: 7 additions & 0 deletions app/models/user.rb
Expand Up @@ -9,12 +9,15 @@
# updated_at :datetime not null
# password_digest :string(255)
# remember_token :string(255)
# admin :boolean default(FALSE)
#

class User < ActiveRecord::Base
attr_accessible :email, :name, :password, :password_confirmation
has_secure_password

has_many :microposts, :dependent => :destroy

validates :name,
:presence => true,
:length => { :maximum => 50 }
Expand All @@ -35,6 +38,10 @@ class User < ActiveRecord::Base
before_save { |user| user.email = email.downcase }
before_save :create_remember_token

def feed
microposts
end

private
def create_remember_token
self.remember_token = SecureRandom.urlsafe_base64
Expand Down
12 changes: 12 additions & 0 deletions app/views/microposts/_micropost.html.erb
@@ -0,0 +1,12 @@
<li>
<span class="content"><%= micropost.content %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(micropost.created_at) %>
</span>
<% if current_user?(micropost.user) %>
<%= link_to 'delete', micropost, :method => :delete,
:data => { :confirm => 'You sure?' },
:title => micropost.content
%>
<% end %>
</li>
2 changes: 2 additions & 0 deletions app/views/microposts/create.html.erb
@@ -0,0 +1,2 @@
<h1>Microposts#create</h1>
<p>Find me in app/views/microposts/create.html.erb</p>
2 changes: 2 additions & 0 deletions app/views/microposts/destroy.html.erb
@@ -0,0 +1,2 @@
<h1>Microposts#destroy</h1>
<p>Find me in app/views/microposts/destroy.html.erb</p>
6 changes: 3 additions & 3 deletions app/views/shared/_error_messages.html.erb
@@ -1,10 +1,10 @@
<div id="error_explanation">
<% if @user.errors.any? %>
<% if object.errors.any? %>
<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>
Expand Down
6 changes: 6 additions & 0 deletions app/views/shared/_feed.html.erb
@@ -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 %>
13 changes: 13 additions & 0 deletions app/views/shared/_feed_item.html.erb
@@ -0,0 +1,13 @@
<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) %>
</span>
<% if current_user?(feed_item.user) %>
<%= link_to 'delete', feed_item, :method => :delete, :data => { :confirm => "You sure?" } %>
<% end %>
</li>
7 changes: 7 additions & 0 deletions app/views/shared/_micropost_form.html.erb
@@ -0,0 +1,7 @@
<%= form_for(@micropost) do |f| %>
<%= render 'shared/error_messages', :object => f.object %>
<div class="field">
<%= f.text_area :content, :rows => 5, :placeholder => "Compose new micropost..." %>
</div>
<%= f.submit 'Post', :class => 'btn btn-large btn-primary' %>
<% end %>
10 changes: 10 additions & 0 deletions app/views/shared/_user_info.html.erb
@@ -0,0 +1,10 @@
<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>
37 changes: 27 additions & 10 deletions app/views/static_pages/home.html.erb
@@ -1,11 +1,28 @@
<div class="center hero-unit">
<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>
<%= link_to 'Sign up now!', signup_path, :class => 'btn btn-large btn-primary' %>
</div>
<% if signed_in? %>
<div class="row">
<div class="span4">
<section>
<%= render :partial => 'shared/user_info' %>
</section>
<section>
<%= render :partial => 'shared/micropost_form' %>
</section>
</div>
<div class="span8">
<h3>Micropost Feed</h3>
<%= render 'shared/feed' %>
</div>
</div>
<% else %>
<div class="center hero-unit">
<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>
<%= 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 %>
2 changes: 1 addition & 1 deletion app/views/users/edit.html.erb
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
2 changes: 1 addition & 1 deletion app/views/users/new.html.erb
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
10 changes: 10 additions & 0 deletions app/views/users/show.html.erb
Expand Up @@ -8,4 +8,14 @@
</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>
1 change: 1 addition & 0 deletions config/routes.rb
@@ -1,6 +1,7 @@
SampleApp::Application.routes.draw do
resources :users
resources :sessions, :only => [:new, :create, :destroy]
resources :microposts, :only => [:create, :destroy]

match '/signin' => 'sessions#new'
match '/signout' => 'sessions#destroy'
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20121016074701_create_microposts.rb
@@ -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
Expand Up @@ -11,7 +11,16 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20121016070552) do
ActiveRecord::Schema.define(:version => 20121016074701) 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
Expand Up @@ -19,5 +19,11 @@ namespace :db do
:password => password,
:password_confirmation => password)
end

users = User.all(:limit => 6)
50.times do |n|
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
Expand Up @@ -9,4 +9,9 @@
admin true
end
end

factory :micropost do
content "Lorem ipsum"
user
end
end

0 comments on commit 090c1c7

Please sign in to comment.