Permalink
Showing
with
271 additions
and 29 deletions.
- +29 −1 app/assets/stylesheets/static_pages.css.scss
- +30 −0 app/controllers/microposts_controller.rb
- +5 −0 app/controllers/static_pages_controller.rb
- +1 −7 app/controllers/users_controller.rb
- +6 −1 app/helpers/sessions_helper.rb
- +6 −0 app/models/micropost.rb
- +6 −0 app/models/user.rb
- +11 −0 app/views/microposts/_micropost.html.erb
- +3 −3 app/views/shared/_error_messages.html.erb
- +6 −0 app/views/shared/_feed.html.erb
- +15 −0 app/views/shared/_feed_item.html.erb
- +7 −0 app/views/shared/_micropost_form.html.erb
- +10 −0 app/views/shared/_user_info.html.erb
- +28 −10 app/views/static_pages/home.html.erb
- +1 −1 app/views/users/edit.html.erb
- +1 −1 app/views/users/new.html.erb
- +9 −0 app/views/users/show.html.erb
- +2 −1 config/routes.rb
- +11 −0 db/migrate/20131107052554_create_microposts.rb
- +10 −1 db/schema.rb
- +11 −3 lib/tasks/sample_data.rake
- +9 −0 spec/factories.rb
- +21 −0 spec/models/micropost_spec.rb
- +33 −0 spec/requests/micropost_pages_spec.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 |
@@ -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 |
@@ -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> |
@@ -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 %> |
@@ -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 |
@@ -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 |

Oops, something went wrong.