Permalink
Browse files

Add user microposts, end of section 10.3

  • Loading branch information...
wdk122 committed Feb 5, 2013
1 parent fb515c6 commit 99b7ae48c85aca539faa2ec15f0e67860b7ea0d7
@@ -186,6 +186,35 @@ 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;
+ }
+}
+
+
@@ -0,0 +1,27 @@
+class MicropostsController < ApplicationController
+ 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_url
+ else
+ @feed_items = []
+ render 'static_pages/home'
+ end
+ end
+
+ def destroy
+ @micropost.destroy
+ redirect_to root_url
+ end
+
+ private
+
+ def correct_user
+ @micropost = current_user.microposts.find_by_id(params[:id])
+ redirect_to root_url if @micropost.nil?
+ end
+end
@@ -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
@@ -6,6 +6,7 @@ class UsersController < ApplicationController
def show
@user = User.find(params[:id])
+ @microposts = @user.microposts.paginate(page: params[:page])
end
def new
@@ -48,12 +49,12 @@ def destroy
private
- def signed_in_user
- unless signed_in?
- store_location
- redirect_to signin_url, notice: "Please sign in."
- end
- end
+ # def signed_in_user
+ # unless signed_in?
+ # store_location
+ # redirect_to signin_url, notice: "Please sign in."
+ # end
+ # end
def correct_user
@user = User.find(params[:id])
@@ -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
self.current_user = nil
cookies.delete(:remember_token)
View
@@ -0,0 +1,10 @@
+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
View
@@ -12,6 +12,7 @@
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
has_secure_password
+ has_many :microposts, dependent: :destroy
before_save { |user| user.email = email.downcase }
before_save :create_remember_token
@@ -24,6 +25,11 @@ class User < ActiveRecord::Base
validates :password, presence: true, length: { minimum: 6 }
validates :password_confirmation, presence: true
+ 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
@@ -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,
+ data: { confirm: "You sure?" },
+ title: micropost.content %>
+ <% end %>
+</li>
@@ -1,10 +1,10 @@
-<% 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>
@@ -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 %>
@@ -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,
+ data: { confirm: "You sure?" },
+ title: feed_item.content %>
+ <% end %>
+</li>
@@ -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 %>
@@ -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>
@@ -1,13 +1,30 @@
-<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!", signup_path, 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 %>
@@ -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 %>
@@ -21,7 +21,7 @@
<%= f.submit "Save changes", class: "btn btn-large btn-primary" %>
<% end %>
- <%= gravatar_for @user %>
+ <%= gravatar_for(@user) %>
<a href="http://gravatar.com/emails">change</a>
</div>
</div>
@@ -5,7 +5,7 @@
<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 %>
@@ -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>
View
@@ -2,6 +2,7 @@
resources :users
resources :sessions, only: [:new, :create, :destroy]
+ resources :microposts, only: [:create, :destroy]
@@ -0,0 +1,11 @@
+class CreateMicroposts < ActiveRecord::Migration
+ def change
+ create_table :microposts do |t|
+ t.string :content
+ t.integer :user_id
+
+ t.timestamps # adds created_at and updated_at columns
+ end
+ add_index :microposts, [:user_id, :created_at]
+ end
+end
View
@@ -11,7 +11,16 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20130203071345) do
+ActiveRecord::Schema.define(:version => 20130203214717) 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"
@@ -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
View
@@ -9,4 +9,9 @@
admin true
end
end
+
+ factory :micropost do
+ content "Lorem ipsum"
+ user
+ end
end
Oops, something went wrong.

0 comments on commit 99b7ae4

Please sign in to comment.