Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add user microposts
  • Loading branch information
rcb09003 committed Nov 8, 2013
1 parent 699a045 commit 35a2c11
Show file tree
Hide file tree
Showing 24 changed files with 271 additions and 29 deletions.
30 changes: 29 additions & 1 deletion app/assets/stylesheets/static_pages.css.scss
Expand Up @@ -191,4 +191,32 @@ 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;
}
}
30 changes: 30 additions & 0 deletions app/controllers/microposts_controller.rb
@@ -0,0 +1,30 @@
class MicropostsController < ApplicationController
before_action :signed_in_user

def create
@micropost = current_user.microposts.build(micropost_params)
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 micropost_params
params.require(:micropost).permit(:content)
end
def correct_user
@micropost = current_user.microposts.find_by(id: params[:id])
redirect_to root_url if @micropost.nil?
end

end
5 changes: 5 additions & 0 deletions app/controllers/static_pages_controller.rb
@@ -1,5 +1,10 @@
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 @@ -9,6 +9,7 @@ class UsersController < ApplicationController

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

def new
Expand Down Expand Up @@ -51,13 +52,6 @@ class UsersController < ApplicationController
def admin_user
redirect_to(root_url) unless current_user.admin?
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])
Expand Down
7 changes: 6 additions & 1 deletion app/helpers/sessions_helper.rb
Expand Up @@ -25,7 +25,12 @@ module SessionsHelper
def current_user?(user)
user == current_user
end

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

def sign_out
self.current_user = nil
Expand Down
6 changes: 6 additions & 0 deletions app/models/micropost.rb
@@ -0,0 +1,6 @@
class Micropost < ActiveRecord::Base
belongs_to :user
default_scope -> { order('created_at DESC') }
validates :content, presence: true, length: { maximum: 140 }
validates :user_id, presence: true
end
6 changes: 6 additions & 0 deletions app/models/user.rb
@@ -1,4 +1,5 @@
class User < ActiveRecord::Base
has_many :microposts, dependent: :destroy
before_save { self.email = email.downcase }
before_create :create_remember_token
validates :name, presence: true, length: { maximum: 50 }
Expand All @@ -17,6 +18,11 @@ class User < ActiveRecord::Base
Digest::SHA1.hexdigest(token.to_s)
end

def feed
# This is preliminary. See "Following users" for the full implementation.
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
@@ -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>
6 changes: 3 additions & 3 deletions app/views/shared/_error_messages.html.erb
@@ -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>
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 %>
15 changes: 15 additions & 0 deletions app/views/shared/_feed_item.html.erb
@@ -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>
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, 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 @@
<%= link_to gravatar_for(current_user, size: 52), current_user %>
<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
@@ -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!", 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 %>
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 @@ -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 %>
Expand Down
9 changes: 9 additions & 0 deletions app/views/users/show.html.erb
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
Expand Up @@ -2,9 +2,10 @@ SampleApp::Application.routes.draw do
resources :products
resources :users
resources :sessions, only: [:new, :create, :destroy]
resources :microposts, only: [:create, :destroy]
root 'static_pages#home'
match '/signup', to: 'users#new', via: 'get'
match '/signin', to: 'sessions#new', via: 'get'
match '/signin', to: 'sessions#new', via: 'get'
match '/signout', to: 'sessions#destroy', via: 'delete'
match '/help', to: 'static_pages#help', via: 'get'
match '/about', to: 'static_pages#about', via: 'get'
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20131107052554_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 that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20131105022512) do
ActiveRecord::Schema.define(version: 20131107052554) 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 "products", force: true do |t|
t.string "name"
Expand Down
14 changes: 11 additions & 3 deletions lib/tasks/sample_data.rake
@@ -1,7 +1,9 @@
namespace :db do

desc "Fill database with sample data"
task populate: :environment do
User.create!(name: "Example User",

User.create!(name: "Example User",
email: "example@railstutorial.org",
password: "foobar",
password_confirmation: "foobar")
Expand All @@ -14,5 +16,11 @@ namespace :db do
password: password,
password_confirmation: password)
end
end
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

9 changes: 9 additions & 0 deletions spec/factories.rb
Expand Up @@ -4,5 +4,14 @@ FactoryGirl.define do
sequence(:email) { |n| "person_#{n}@example.com"}
password "foobar"
password_confirmation "foobar"

factory :admin do
admin true
end
end

factory :micropost do
content "Lorem ipsum"
user
end
end
21 changes: 21 additions & 0 deletions spec/models/micropost_spec.rb
@@ -0,0 +1,21 @@
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 eq user }

it { should be_valid }

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

0 comments on commit 35a2c11

Please sign in to comment.