Commit f7e94cee by Tô Ngọc Ánh

10.1 -> 10.3.2

parent e3f8bd4e
Pipeline #676 failed with stages
in 0 seconds
...@@ -189,3 +189,31 @@ input { ...@@ -189,3 +189,31 @@ input {
} }
} }
} }
/* 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;
}
}
\ No newline at end of file
class MicropostsController < ApplicationController
before_action :signed_in_user, only: [:create, :destroy]
def index
end
def create
@micropost = current_user.microposts.build(micropost_params)
if @micropost.save
flash[:success] = "Micropost created!"
redirect_to root_url
else
render 'static_pages/home'
end
end
def destroy
end
private
def micropost_params
params.require(:micropost).permit(:content)
end
end
\ No newline at end of file
class StaticPagesController < ApplicationController class StaticPagesController < ApplicationController
def home def home
@micropost = current_user.microposts.build if signed_in?
end end
def help def help
......
...@@ -9,6 +9,7 @@ class UsersController < ApplicationController ...@@ -9,6 +9,7 @@ class UsersController < ApplicationController
def show def show
@user = User.find(params[:id]) @user = User.find(params[:id])
@microposts = @user.microposts.paginate(page: params[:page])
end end
def new def new
...@@ -54,13 +55,6 @@ class UsersController < ApplicationController ...@@ -54,13 +55,6 @@ class UsersController < ApplicationController
# Before filters # Before filters
def signed_in_user
unless signed_in?
store_location
redirect_to signin_url, notice: "Please sign in."
end
end
def correct_user def correct_user
@user = User.find(params[:id]) @user = User.find(params[:id])
redirect_to(root_url) unless current_user?(@user) redirect_to(root_url) unless current_user?(@user)
......
...@@ -23,6 +23,13 @@ module SessionsHelper ...@@ -23,6 +23,13 @@ module SessionsHelper
user == current_user user == current_user
end end
def signed_in_user
unless signed_in?
store_location
redirect_to signin_url, notice: "Please sign in."
end
end
def sign_out def sign_out
current_user.update_attribute(:remember_token, User.digest(User.new_remember_token)) current_user.update_attribute(:remember_token, User.digest(User.new_remember_token))
cookies.delete(:remember_token) cookies.delete(:remember_token)
......
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
class User < ActiveRecord::Base class User < ActiveRecord::Base
has_many :microposts, dependent: :destroy
validates :name, presence: true, length: { maximum: 50 } validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /[\w+\-.]+@[a-z\d*\-.]+\.[a-z]+/i VALID_EMAIL_REGEX = /[\w+\-.]+@[a-z\d*\-.]+\.[a-z]+/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false } validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }
validates :password, length: { minimum: 6 } validates :password, length: { minimum: 6 }
before_save { self.email = email.downcase } before_save { self.email = email.downcase }
before_create :create_remember_token before_create :create_remember_token
has_secure_password has_secure_password
......
<li>
<span class="content"><%= micropost.content %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(micropost.created_at) %> ago.
</span>
</li>
\ No newline at end of file
<% if @user.errors.any? %> <% if object.errors.any? %>
<div id="error_explanation"> <div id="error_explanation">
<div class="alert alert-error"> <div class="alert alert-error">
The form contains <%= pluralize(@user.errors.count, "error") %>. The form contains <%= pluralize(object.errors.count, "error") %>.
</div> </div>
<ul> <ul>
<% @user.errors.full_messages.each do |msg| %> <% object.errors.full_messages.each do |msg| %>
<li>* <%= msg %></li> <li>* <%= msg %></li>
<% end %> <% end %>
</ul> </ul>
......
<%= 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 %>
\ No newline at end of file
<%= 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>
\ No newline at end of file
<div class="center hero-unit"> <% if signed_in? %>
<div class="row">
<aside class="span4">
<section>
<%= render 'shared/user_info' %>
</section>
<section>
<%= render 'shared/micropost_form' %>
</section>
</aside>
</div>
<% else %>
<div class="center hero-unit">
<h1>Welcome to the Sample App</h1> <h1>Welcome to the Sample App</h1>
<h2> <h2>
This is the home page for the This is the home page for the
...@@ -6,6 +18,7 @@ ...@@ -6,6 +18,7 @@
sample application. sample application.
</h2> </h2>
<%= link_to "Sign up now!", signup_path, class: "btn btn-large btn-primary" %> <%= link_to "Sign up now!", signup_path, class: "btn btn-large btn-primary" %>
</div> </div>
<%= link_to image_tag("rails.png", alt: "Rails"), 'http://rubyonrails.org/' %> <%= link_to image_tag("rails.png", alt: "Rails"), 'http://rubyonrails.org/' %>
<% end %>
\ No newline at end of file
<%= render 'shared/error_messages' %> <%= render 'shared/error_messages', object: f.object %>
<%= f.label :name %> <%= f.label :name %>
<%= f.text_field :name %> <%= f.text_field :name %>
......
...@@ -8,4 +8,13 @@ ...@@ -8,4 +8,13 @@
</h1> </h1>
</section> </section>
</aside> </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> </div>
\ No newline at end of file
...@@ -11,4 +11,5 @@ Rails.application.routes.draw do ...@@ -11,4 +11,5 @@ Rails.application.routes.draw do
delete '/signout', to: 'sessions#destroy' delete '/signout', to: 'sessions#destroy'
resources :users resources :users
resources :sessions, only: [:new, :create, :destroy] resources :sessions, only: [:new, :create, :destroy]
resources :microposts, only: [:create, :destroy]
end end
class CreateMicroposts < ActiveRecord::Migration
def change
create_table :microposts do |t|
t.string :content
t.integer :user_id
t.timestamps null: false
end
add_index :microposts, [:user_id, :created_at]
end
end
...@@ -11,7 +11,16 @@ ...@@ -11,7 +11,16 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20200706063440) do ActiveRecord::Schema.define(version: 20200706085817) do
create_table "microposts", force: :cascade do |t|
t.string "content"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "microposts", ["user_id", "created_at"], name: "index_microposts_on_user_id_and_created_at"
create_table "users", force: :cascade do |t| create_table "users", force: :cascade do |t|
t.string "name" t.string "name"
......
...@@ -15,5 +15,10 @@ namespace :db do ...@@ -15,5 +15,10 @@ namespace :db do
password: password, password: password,
password_confirmation: password) password_confirmation: password)
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
end end
\ No newline at end of file
...@@ -9,4 +9,9 @@ FactoryGirl.define do ...@@ -9,4 +9,9 @@ FactoryGirl.define do
admin true admin true
end end
end end
factory :micropost do
content "Lorem ipsum"
user
end
end end
\ No newline at end of file
require 'spec_helper'
describe Micropost do
let(:user) { FactoryGirl.create(:user) }
before do
#This code is not idiomatically correct.
@micropost = user.microposts.build(content: "Lorem ipsum")
end
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
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
...@@ -13,6 +13,9 @@ describe User do ...@@ -13,6 +13,9 @@ describe User do
it { should respond_to(:password_confirmation) } it { should respond_to(:password_confirmation) }
it { should respond_to(:authenticate) } it { should respond_to(:authenticate) }
it { should respond_to(:admin) }
it { should respond_to(:microposts) }
it { should be_valid } it { should be_valid }
describe "when name is not present" do describe "when name is not present" do
...@@ -115,4 +118,27 @@ describe User do ...@@ -115,4 +118,27 @@ describe User do
before { @user.save } before { @user.save }
its(:remember_token) { should_not be_blank } its(:remember_token) { should_not be_blank }
end 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
expect(@user.microposts.to_a).to eq [newer_micropost, older_micropost]
end
it "should destroy associated microposts" do
microposts = @user.microposts.to_a
@user.destroy
expect(microposts).not_to be_empty
microposts.each do |micropost|
expect(Micropost.where(id: micropost.id)).to be_empty
end
end
end
end end
...@@ -63,6 +63,18 @@ describe "AuthenticationPages" do ...@@ -63,6 +63,18 @@ describe "AuthenticationPages" do
end end
end end
describe "in the Microposts controller" do
describe "submitting to the create action" do
before { post microposts_path }
specify { expect(response).to redirect_to(signin_path) }
end
describe "submitting to the destroy action" do
before { delete micropost_path(FactoryGirl.create(:micropost)) }
specify { expect(response).to redirect_to(signin_path) }
end
end
describe "in the Users controller" do describe "in the Users controller" do
describe "visiting the edit page" do describe "visiting the edit page" do
......
require 'spec_helper'
describe "MicropostPages" do
subject { page }
let(:user) { FactoryGirl.create(:user) }
before { sign_in user }
describe "micropost creation" do
before { visit root_path }
describe "with invalid information" do
it "should not create a micropost" do
expect { click_button "Post" }.not_to change(Micropost, :count)
end
describe "error messages" do
before { click_button "Post" }
it { should have_content('error') }
end
end
describe "with valid information" do
before { fill_in 'micropost_content', with: "Lorem ipsum" }
it "should create a micropost" do
expect { click_button "Post" }.to change(Micropost, :count).by(1)
end
end
end
end
...@@ -13,11 +13,19 @@ describe "User pages" do ...@@ -13,11 +13,19 @@ describe "User pages" do
describe "profile page" do describe "profile page" do
let(:user) { FactoryGirl.create(:user) } let(:user) { FactoryGirl.create(:user) }
let!(:m1) { FactoryGirl.create(:micropost, user: user, content: "Foo") }
let!(:m2) { FactoryGirl.create(:micropost, user: user, content: "Bar") }
# Replace with code to make a user variable # Replace with code to make a user variable
before { visit user_path(user) } before { visit user_path(user) }
it { should have_content(user.name) } it { should have_content(user.name) }
it { should have_title(user.name) } it { should have_title(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 end
describe "signup" do describe "signup" do
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment