Skip to content
Permalink
Browse files

Add user microposts

  • Loading branch information
rcb09003 committed Nov 8, 2013
1 parent 699a045 commit 35a2c1114ac1bbb6e46a5eb6ba4df6891596a0ab
@@ -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;
}
}
@@ -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
@@ -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
@@ -9,6 +9,7 @@ def index

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

def new
@@ -51,13 +52,6 @@ def user_params
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])
@@ -25,7 +25,12 @@ def current_user=(user)
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
@@ -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
@@ -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 }
@@ -17,6 +18,11 @@ def User.encrypt(token)
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
@@ -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,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>
@@ -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 %>
@@ -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 %>
@@ -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>
@@ -2,9 +2,10 @@
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'
@@ -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,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"
@@ -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")
@@ -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

@@ -4,5 +4,14 @@
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
@@ -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.
You can’t perform that action at this time.