Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added user microposts

  • Loading branch information...
commit ae788ac71bada475255c9bbd2ff14aa9c76722c5 1 parent 0310d4e
@chadkoh authored
View
31 app/controllers/microposts_controller.rb
@@ -0,0 +1,31 @@
+class MicropostsController < ApplicationController
+ before_filter :authenticate, :only => [:create, :destroy]
+ before_filter :authorized_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 'pages/home'
+ end
+ end
+
+ def destroy
+ @micropost.destroy
+ flash[:success] = "Micropost deleted (;_;)"
+ redirect_back_or root_path
+ end
+
+ private
+
+ def authorized_user
+ #@micropost = current_user.microposts.find_by_id(params[:id])
+ #redirect_to root_path if @microposts.nil?
+ @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
@title = "Home"
+ if signed_in?
+ @micropost = Micropost.new if signed_in?
+ @feed_items = current_user.feed.paginate(:page => params[:page])
+ end
end
def about
View
4 app/controllers/users_controller.rb
@@ -11,6 +11,7 @@ def index
def show
@user = User.find(params[:id])
+ @microposts = @user.microposts.paginate(:page => params[:page])
@title = @user.name
end
@@ -57,9 +58,6 @@ 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
@@ -26,6 +26,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
11 app/helpers/users_helper.rb
@@ -1,9 +1,8 @@
module UsersHelper
- def gravatar_for(user, options = { size => 50 })
- gravatar_image_tag(user.email.downcase, :alt => h(user.name),
- :class => 'gravatar',
- :gravatar => options)
+ def gravatar_for(user, options = { :size => 50 })
+ gravatar_image_tag(user.email.downcase, :alt => h(user.name),
+ :class => 'gravatar',
+ :gravatar => options)
end
-
-end
+end
View
10 app/models/micropost.rb
@@ -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
9 app/models/user.rb
@@ -13,6 +13,15 @@ class User < ActiveRecord::Base
attr_accessor :password
attr_accessible :name, :email, :password, :password_confirmation
+ has_many :microposts, :dependent => :destroy
+
+
+
+def feed
+ # This is preliminary. See Chapter 12 for the full implementation.
+ Micropost.where("user_id = ?", id)
+ end
+
# Validations for :name and :email
View
1  app/views/layouts/application.html.erb
@@ -5,6 +5,7 @@
<%= csrf_meta_tag %>
<%= render 'layouts/stylesheets' %>
<%= javascript_include_tag :defaults %>
+
</head>
<body>
<div class="container">
View
15 app/views/microposts/_micropost.html.erb
@@ -0,0 +1,15 @@
+<tr>
+ <td class="micropost">
+ <span class="content"><%= micropost.content %></span>
+ <span class="timestamp">
+ 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
27 app/views/pages/home.html.erb
@@ -1,9 +1,22 @@
-<h1>Sample App</h1>
+<% 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>
+ </tr>
+ </table>
+<% else %>
-<p>
- This is the home page for the
- <a href="http://railstutorial.org/">Ruby on Rails Tutorial</a>
- sample application.
-</p>
+ <h1>Sample App</h1>
-<%= link_to "Sign up now!", signup_path, :class => "signup_button round" %>
+ <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" %>
+<% end %>
View
6 app/views/shared/_feed.html.erb
@@ -0,0 +1,6 @@
+<% unless @feed_items.empty? %>
+ <table class="microposts" summary="User microposts">
+ <%= render :partial => 'shared/feed_item', :collection => @feed_items %>
+ </table>
+ <%= will_paginate @feed_items %>
+<% end %>
View
21 app/views/shared/_feed_item.html.erb
@@ -0,0 +1,21 @@
+<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
6 app/views/users/_user.html.erb
@@ -1,8 +1,8 @@
<li>
<%= gravatar_for user, :size => 30 %>
<%= link_to user.name, user %>
- <% if current_user.admin? %>
- | <%= link_to "delete", user, :method => :delete, :confirm => "You sure?",
+ <%# if current_user.admin? %>
+ | <%#= link_to "delete", user, :method => :delete, :confirm => "You sure?",
:title => "Delete #{user.name}" %>
- <% end %>
+ <%# end %>
</li>
View
16 app/views/users/show.html.erb
@@ -11,4 +11,18 @@
<strong>URL</strong> <%= link_to user_path(@user), @user %>
-<p><%= @user.email %></p>
+
+<p><%= @user.email %></p>
+
+
+
+<p><strong>Total number of microposts</strong> <%= @user.microposts.count %></p>
+
+
+<% unless @user.microposts.empty? %>
+ <table class="microposts" summary="User microposts">
+ <%= render @microposts %>
+ </table>
+ <%= will_paginate @microposts %>
+<% end %>
+
View
1  config/routes.rb
@@ -4,6 +4,7 @@
resources :users
resources :sessions, :only => [:new, :create, :destroy]
+ resources :microposts, :only => [:create, :destroy]
match '/signup', :to => 'users#new'
View
15 db/migrate/20111228231649_create_microposts.rb
@@ -0,0 +1,15 @@
+class CreateMicroposts < ActiveRecord::Migration
+ def self.up
+ create_table :microposts do |t|
+ t.string :content
+ t.integer :user_id
+
+ t.timestamps
+ end
+ add_index :microposts, [:user_id, :created_at]
+ end
+
+ def self.down
+ drop_table :microposts
+ end
+end
View
11 db/schema.rb
@@ -10,7 +10,16 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20111221025906) do
+ActiveRecord::Schema.define(:version => 20111228231649) do
+
+ create_table "microposts", :force => true do |t|
+ t.string "content"
+ t.integer "user_id"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ 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"
View
7 lib/tasks/sample_data.rake
@@ -18,6 +18,13 @@ namespace :db do
:email => email,
:password => password,
:password_confirmation => password)
+
+ 50.times do
+ User.all(:limit => 6).each do |user|
+ user.microposts.create!(:content => Faker::Lorem.sentence(5))
+ end
+
+ end
end
end
end
View
1  public/javascripts/application.js
@@ -1,2 +1,3 @@
// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
+
View
55 public/stylesheets/custom.css
@@ -173,3 +173,58 @@ div.field, div.actions {
}
/* FORMS END */
+
+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
5 spec/models/micropost_spec.rb
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe Micropost do
+ pending "add some examples to (or delete) #{__FILE__}"
+end
Please sign in to comment.
Something went wrong with that request. Please try again.