Skip to content

Commit

Permalink
Added user microposts
Browse files Browse the repository at this point in the history
  • Loading branch information
MrEdwardo committed Sep 6, 2011
1 parent 6612f98 commit cd5f4d1
Show file tree
Hide file tree
Showing 27 changed files with 505 additions and 38 deletions.
Binary file modified app/.DS_Store
Binary file not shown.
29 changes: 29 additions & 0 deletions app/controllers/microposts_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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
redirect_back_or root_path
end

private

def authorized_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/pages_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
class PagesController < ApplicationController
def home
@title = "Home"
if signed_in?
@micropost = Micropost.new
@feed_items = current_user.feed.paginate(:page => params[:page])
end
end

def contact
Expand Down
5 changes: 1 addition & 4 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def new

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

Expand Down Expand Up @@ -64,10 +65,6 @@ def admin_user
redirect_to(root_path) unless current_user.admin?
end

def authenticate
deny_access unless signed_in?
end

def correct_user
@user = User.find(params[:id])
redirect_to(root_path) unless current_user?(@user)
Expand Down
4 changes: 4 additions & 0 deletions app/helpers/sessions_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ def sign_in(user)
self.current_user = 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."
Expand Down
10 changes: 10 additions & 0 deletions app/models/micropost.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
class User < ActiveRecord::Base
attr_accessor :password
attr_accessible :name, :email, :password, :password_confirmation

has_many :microposts, :dependent => :destroy

email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

Expand All @@ -29,6 +31,11 @@ class User < ActiveRecord::Base

before_save :encrypt_password

def feed
#this is preliminary, and will be changed in final implementation
Micropost.where("user_id = ?", id)
end

#return true if user's password matches the submitted password.
def has_password?(submitted_password)
encrypted_password == encrypt(submitted_password)
Expand Down
Binary file modified app/views/.DS_Store
Binary file not shown.
16 changes: 16 additions & 0 deletions app/views/microposts/_micropost.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<tr>
<td class="micropost">
<span class="content"><%= micropost.content %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(micropost.created_at) %> ago.
</span>
</td>
<% user = micropost.user rescue User.find(micropost.user_id) %>
<% if current_user?(user) %>
<td>
<%= link_to "delete", micropost, :method => :delete,
:confirm => "You sure?",
:title => micropost.content %>
</td>
<% end %>
</tr>
33 changes: 25 additions & 8 deletions app/views/pages/home.html.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
<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', :class => "signup_button round" %>
<% 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>
<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>

<%= link_to "Sign up now!", 'signup', :class => "signup_button round" %>

<% 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 @@
<% unless @feed_items.empty? %>
<table class="microposts" summary="User microposts">
<%= render :partial => 'shared/feed_item', :collection => @feed_items %>
</table>
<%= will_paginate @feed_items %>
<% end %>
21 changes: 21 additions & 0 deletions app/views/shared/_feed_item.html.erb
Original file line number Diff line number Diff line change
@@ -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>
9 changes: 9 additions & 0 deletions app/views/shared/_micropost_form.html.erb
Original file line number Diff line number Diff line change
@@ -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 %>
11 changes: 11 additions & 0 deletions app/views/shared/_user_info.html.erb
Original file line number Diff line number Diff line change
@@ -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>
10 changes: 9 additions & 1 deletion app/views/users/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@
<%= gravatar_for @user %>
<%= @user.name %>
</h1>

<% unless @user.microposts.empty? %>
<table class="microposts" summary="User microposts">
<%= render @microposts %>
</table>
<%= will_paginate @microposts %>
<% end %>
</td>
<td class="sidebar round">
<strong>Name</strong> <%= @user.name %><br />
<strong>URL</strong> <%= link_to user_path(@user), @user %>
<strong>URL</strong> <%= link_to user_path(@user), @user %><br />
<strong>Microposts</strong> <%= @user.microposts.count %>
</td>
</tr>
</table>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

resources :users
resources :sessions, :only => [:new, :create, :destroy]
resources :microposts, :only => [:create, :destroy]

match '/signout', :to => 'sessions#destroy'
match '/signin', :to => 'sessions#new'
Expand Down
16 changes: 16 additions & 0 deletions db/migrate/20110905205244_create_microposts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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
add_index :microposts, :created_at
end

def self.down
drop_table :microposts
end
end
12 changes: 11 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,17 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20110905190221) do
ActiveRecord::Schema.define(:version => 20110905205244) 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", ["created_at"], :name => "index_microposts_on_created_at"
add_index "microposts", ["user_id"], :name => "index_microposts_on_user_id"

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 @@ -17,5 +17,11 @@ namespace :db do
:password => password,
:password_confirmation => password)
end

User.all(:limit => 6).each do |user|
50.times do
user.microposts.create!(:content => Faker::Lorem.sentence(5))
end
end
end
end
55 changes: 55 additions & 0 deletions public/stylesheets/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,61 @@ ul.users {
list-style: none;
}

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;
}

#error_explanation {
width: 400px;
border: 2px solid red;
Expand Down
Binary file modified spec/.DS_Store
Binary file not shown.
Loading

0 comments on commit cd5f4d1

Please sign in to comment.