From 673eb059e2a451c9d19f84e81d8b9b8c33a9626a Mon Sep 17 00:00:00 2001 From: Michael Hartl Date: Sun, 29 Aug 2010 17:59:06 -0700 Subject: [PATCH] Showing microposts --- app/controllers/users_controller.rb | 1 + app/views/microposts/_micropost.html.erb | 8 +++ app/views/users/show.html.erb | 7 +++ lib/tasks/sample_data.rake | 7 ++- public/stylesheets/custom.css | 59 ++++++++++++++++++++++- spec/controllers/users_controller_spec.rb | 21 ++++++++ 6 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 app/views/microposts/_micropost.html.erb diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 2f87813..e047a6e 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -10,6 +10,7 @@ def index def show @user = User.find(params[:id]) + @microposts = @user.microposts.paginate(:page => params[:page]) @title = @user.name end diff --git a/app/views/microposts/_micropost.html.erb b/app/views/microposts/_micropost.html.erb new file mode 100644 index 0000000..9270799 --- /dev/null +++ b/app/views/microposts/_micropost.html.erb @@ -0,0 +1,8 @@ + + + <%= micropost.content %> + + Posted <%= time_ago_in_words(micropost.created_at) %> ago. + + + \ No newline at end of file diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index 4d4768d..ae7a50d 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -5,10 +5,17 @@ <%= gravatar_for @user %> <%= @user.name %> + <% if @user.microposts.any? %> + + <%= render @microposts %> +
+ <%= will_paginate @microposts %> + <% end %> Name <%= @user.name %>
URL <%= link_to user_path(@user), @user %> + Microposts <%= @user.microposts.count %> diff --git a/lib/tasks/sample_data.rake b/lib/tasks/sample_data.rake index 94ab172..df789bc 100644 --- a/lib/tasks/sample_data.rake +++ b/lib/tasks/sample_data.rake @@ -18,6 +18,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 \ No newline at end of file diff --git a/public/stylesheets/custom.css b/public/stylesheets/custom.css index 1b3f968..d3a3de5 100644 --- a/public/stylesheets/custom.css +++ b/public/stylesheets/custom.css @@ -189,4 +189,61 @@ ul.users { ul.users li { list-style: none; -} \ No newline at end of file +} + +/* Micropost styling */ + +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; +} diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index caf6484..ff8d944 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -104,6 +104,27 @@ response.should have_selector('td>a', :content => user_path(@user), :href => user_path(@user)) end + + it "should show the user's microposts" do + mp1 = Factory(:micropost, :user => @user, :content => "Foo bar") + mp2 = Factory(:micropost, :user => @user, :content => "Baz quux") + get :show, :id => @user + response.should have_selector('span.content', :content => mp1.content) + response.should have_selector('span.content', :content => mp2.content) + end + + it "should paginate microposts" do + 35.times { Factory(:micropost, :user => @user, :content => "foo") } + get :show, :id => @user + response.should have_selector('div.pagination') + end + + it "should display the micropost count" do + 10.times { Factory(:micropost, :user => @user, :content => "foo") } + get :show, :id => @user + response.should have_selector('td.sidebar', + :content => @user.microposts.count.to_s) + end end describe "GET 'new'" do