Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Finished 10.3.1

  • Loading branch information...
commit 2bf77c15774bd6574726bd95a65579e2f0f3bee4 1 parent ff1a81d
@mashbash authored
View
28 app/assets/stylesheets/custom.css.scss
@@ -183,4 +183,32 @@ input, textarea, select, .uneditable-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;
+ }
}
View
12 app/controllers/micropost_controller.rb
@@ -0,0 +1,12 @@
+class MicropostsController < ApplicationController
+ before_filter :signed_in_user, only: [:create, :destroy]
+
+ def index
+ end
+
+ def create
+ end
+
+ def destroy
+ end
+end
View
8 app/controllers/users_controller.rb
@@ -9,6 +9,7 @@ def index
def show
@user = User.find(params[:id])
+ @microposts = @user.microposts.paginate(page: params[:page])
end
def new
@@ -47,13 +48,6 @@ def destroy
private
- def signed_in_user
- unless signed_in?
- store_location
- redirect_to signin_path, notice: "Please sign in."
- end
- end
-
def correct_user
@user = User.find(params[:id])
redirect_to(root_path) unless current_user?(@user)
View
7 app/helpers/sessions_helper.rb
@@ -35,4 +35,11 @@ def store_location
session[:return_to] = request.fullpath
end
+ def signed_in_user
+ unless signed_in?
+ store_location
+ redirect_to signin_path, notice: "Please sign in."
+ end
+ end
+
end
View
6 app/models/micropost.rb
@@ -1,6 +1,10 @@
class Micropost < ActiveRecord::Base
attr_accessible :content
+
belongs_to :user
- validates :user_id, presence: true
+ validates :content, presence: true, length: { maximum: 140 }
+ validates :user_id, presence: true
+
+ default_scope order: 'microposts.created_at DESC'
end
View
2  app/models/user.rb
@@ -12,7 +12,7 @@
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
has_secure_password
- has_many :microposts
+ has_many :microposts, dependent: :destroy
before_save { |user| user.email = email.downcase }
before_save :create_remember_token
View
6 app/views/microposts/_micropost.html.erb
@@ -0,0 +1,6 @@
+<li>
+ <span class="content"><%= micropost.content %></span>
+ <span class="timestamp">
+ Posted <%= time_ago_in_words(micropost.created_at) %> ago.
+ </span>
+</li>
View
9 app/views/users/show.html.erb
@@ -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
1  config/routes.rb
@@ -1,6 +1,7 @@
SampleApp::Application.routes.draw do
resources :users
resources :sessions, only: [:new, :create, :destroy]
+ resources :microposts, only: [:create, :destroy]
root to: 'static_pages#home'
View
7 lib/tasks/sample_data.rake
@@ -15,5 +15,12 @@ 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
8 spec/factories.rb
@@ -9,4 +9,10 @@
admin true
end
end
-end
+
+ factory :micropost do
+ content "Lorem ipsum"
+ user
+ end
+end
+
View
13 spec/models/micropost_spec.rb
@@ -23,9 +23,20 @@
end
end
- describe "when user_id is not present" do
+ describe "when user_id is not present" do
before { @micropost.user_id = nil }
it { should_not be_valid }
end
+ describe "with blank content" do
+ before { @micropost.content = " " }
+ it { should_not be_valid }
+ end
+
+ describe "with content that is too long" do
+ before { @micropost.content = "a" * 141 }
+ it { should_not be_valid }
+ end
+
+
end
View
24 spec/models/user_spec.rb
@@ -137,4 +137,28 @@
its(:remember_token) { should_not be_blank }
end
+ describe "micropost associations" do
+
+ before { @user.save }
+ let!(:older_micropost) do
+ FactoryGirl.create(:micropost, user: @user, created_at: 1.day.ago)
+ end
+ let!(:newer_micropost) do
+ FactoryGirl.create(:micropost, user: @user, created_at: 1.hour.ago)
+ end
+
+ it "should have the right microposts in the right order" do
+ @user.microposts.should == [newer_micropost, older_micropost]
+ end
+
+ it "should destroy associated microposts" do
+ microposts = @user.microposts
+ @user.destroy
+ microposts.each do |micropost|
+ Micropost.find_by_id(micropost.id).should be_nil
+ end
+ end
+
+ end
+
end
View
13 spec/requests/authentication_pages_spec.rb
@@ -72,6 +72,19 @@
it { should have_selector('title', text: 'Sign in') }
end
+ describe "in the Microposts controller" do
+
+ describe "submitting to the create action" do
+ before { post microposts_path }
+ specify { response.should redirect_to(signin_path) }
+ end
+
+ describe "submitting to the destroy action" do
+ before { delete micropost_path(FactoryGirl.create(:micropost)) }
+ specify { response.should redirect_to(signin_path) }
+ end
+ end
+
describe "as wrong user" do
let(:user) { FactoryGirl.create(:user) }
let(:wrong_user) { FactoryGirl.create(:user, email: "wrong@example.com") }
View
11 spec/requests/user_pages_spec.rb
@@ -61,12 +61,21 @@
it { should have_selector('title', text: full_title('Sign up')) }
end
- describe "profile page" do
+ describe "profile page" do
let(:user) { FactoryGirl.create(:user) }
+ let!(:m1) { FactoryGirl.create(:micropost, user: user, content: "Foo") }
+ let!(:m2) { FactoryGirl.create(:micropost, user: user, content: "Bar") }
+
before { visit user_path(user) }
it { should have_selector('h1', text: user.name) }
it { should have_selector('title', text: user.name) }
+
+ describe "microposts" do
+ it { should have_content(m1.content) }
+ it { should have_content(m2.content) }
+ it { should have_content(user.microposts.count) }
+ end
end
describe "signup" do
Please sign in to comment.
Something went wrong with that request. Please try again.