Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add user microposts
- Loading branch information
Showing
24 changed files
with
271 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.