Commit 16cf476d by phuctmZigexn

Add user microposts

parent 5871244e
...@@ -28,4 +28,5 @@ ...@@ -28,4 +28,5 @@
# Ignore master key for decrypting credentials and more. # Ignore master key for decrypting credentials and more.
/config/master.key /config/master.key
/assets/images/ app/assets/images
public/uploads
...@@ -43,6 +43,7 @@ group :test do ...@@ -43,6 +43,7 @@ group :test do
end end
group :production do group :production do
gem 'pg', '~> 1.0' gem 'pg', '~> 1.0'
gem 'fog', '~> 2.0'
end end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem # Windows does not include zoneinfo files, so bundle the tzinfo-data gem
...@@ -52,4 +53,6 @@ gem 'bcrypt', '~> 3.1', '>= 3.1.12' ...@@ -52,4 +53,6 @@ gem 'bcrypt', '~> 3.1', '>= 3.1.12'
gem 'jquery-rails', '~> 4.3', '>= 4.3.3' gem 'jquery-rails', '~> 4.3', '>= 4.3.3'
gem 'faker', '~> 1.9', '>= 1.9.1' gem 'faker', '~> 1.9', '>= 1.9.1'
gem 'will_paginate', '~> 3.1', '>= 3.1.6' gem 'will_paginate', '~> 3.1', '>= 3.1.6'
gem 'bootstrap-will_paginate', '~> 1.0' gem 'bootstrap-will_paginate', '~> 1.0'
\ No newline at end of file gem 'carrierwave', '~> 1.2', '>= 1.2.3'
gem 'mini_magick', '~> 4.8'
\ No newline at end of file
GEM GEM
remote: https://rubygems.org/ remote: https://rubygems.org/
specs: specs:
CFPropertyList (2.3.6)
actioncable (5.2.0) actioncable (5.2.0)
actionpack (= 5.2.0) actionpack (= 5.2.0)
nio4r (~> 2.0) nio4r (~> 2.0)
...@@ -67,6 +68,10 @@ GEM ...@@ -67,6 +68,10 @@ GEM
rack (>= 1.6.0) rack (>= 1.6.0)
rack-test (>= 0.6.3) rack-test (>= 0.6.3)
xpath (~> 3.1) xpath (~> 3.1)
carrierwave (1.2.3)
activemodel (>= 4.0.0)
activesupport (>= 4.0.0)
mime-types (>= 1.16)
childprocess (0.9.0) childprocess (0.9.0)
ffi (~> 1.0, >= 1.0.11) ffi (~> 1.0, >= 1.0.11)
chromedriver-helper (1.2.0) chromedriver-helper (1.2.0)
...@@ -74,20 +79,184 @@ GEM ...@@ -74,20 +79,184 @@ GEM
nokogiri (~> 1.8) nokogiri (~> 1.8)
concurrent-ruby (1.0.5) concurrent-ruby (1.0.5)
crass (1.0.4) crass (1.0.4)
domain_name (0.5.20180417)
unf (>= 0.0.5, < 1.0.0)
erubi (1.7.1) erubi (1.7.1)
excon (0.62.0)
execjs (2.7.0) execjs (2.7.0)
faker (1.9.1) faker (1.9.1)
i18n (>= 0.7) i18n (>= 0.7)
ffi (1.9.25) ffi (1.9.25)
fission (0.5.0)
CFPropertyList (~> 2.2)
fog (2.0.0)
fog-aliyun (>= 0.1.0)
fog-atmos
fog-aws (>= 0.6.0)
fog-brightbox (~> 0.4)
fog-cloudatcost (~> 0.1.0)
fog-core (~> 1.45)
fog-digitalocean (>= 0.3.0)
fog-dnsimple (~> 1.0)
fog-dynect (~> 0.0.2)
fog-ecloud (~> 0.1)
fog-google (<= 0.1.0)
fog-internet-archive
fog-joyent
fog-json
fog-local
fog-openstack
fog-ovirt
fog-powerdns (>= 0.1.1)
fog-profitbricks
fog-rackspace
fog-radosgw (>= 0.0.2)
fog-riakcs
fog-sakuracloud (>= 0.0.4)
fog-serverlove
fog-softlayer
fog-storm_on_demand
fog-terremark
fog-vmfusion
fog-voxel
fog-vsphere (>= 0.4.0)
fog-xenserver
fog-xml (~> 0.1.1)
ipaddress (~> 0.5)
json (~> 2.0)
fog-aliyun (0.3.2)
fog-core
fog-json
ipaddress (~> 0.8)
xml-simple (~> 1.1)
fog-atmos (0.1.0)
fog-core
fog-xml
fog-aws (2.0.1)
fog-core (~> 1.38)
fog-json (~> 1.0)
fog-xml (~> 0.1)
ipaddress (~> 0.8)
fog-brightbox (0.15.0)
fog-core (~> 1.22)
fog-json
inflecto (~> 0.0.2)
mime-types
fog-cloudatcost (0.1.2)
fog-core (~> 1.36)
fog-json (~> 1.0)
fog-xml (~> 0.1)
ipaddress (~> 0.8)
fog-core (1.45.0)
builder
excon (~> 0.58)
formatador (~> 0.2)
fog-digitalocean (0.4.0)
fog-core
fog-json
fog-xml
ipaddress (>= 0.5)
fog-dnsimple (1.0.0)
fog-core (~> 1.38)
fog-json (~> 1.0)
fog-dynect (0.0.3)
fog-core
fog-json
fog-xml
fog-ecloud (0.3.0)
fog-core
fog-xml
fog-google (0.1.0)
fog-core
fog-json
fog-xml
fog-internet-archive (0.0.1)
fog-core
fog-json
fog-xml
fog-joyent (0.0.1)
fog-core (~> 1.42)
fog-json (>= 1.0)
fog-json (1.2.0)
fog-core
multi_json (~> 1.10)
fog-local (0.5.0)
fog-core (>= 1.27, < 3.0)
fog-openstack (0.1.27)
fog-core (~> 1.45.0)
fog-json (>= 1.0)
ipaddress (>= 0.8)
fog-ovirt (1.1.1)
fog-core
fog-json
fog-xml
ovirt-engine-sdk (>= 4.1.3)
rbovirt (~> 0.1.5)
fog-powerdns (0.2.0)
fog-core
fog-json
fog-xml
fog-profitbricks (4.1.1)
fog-core (~> 1.42)
fog-json (~> 1.0)
fog-rackspace (0.1.6)
fog-core (>= 1.35)
fog-json (>= 1.0)
fog-xml (>= 0.1)
ipaddress (>= 0.8)
fog-radosgw (0.0.5)
fog-core (>= 1.21.0)
fog-json
fog-xml (>= 0.0.1)
fog-riakcs (0.1.0)
fog-core
fog-json
fog-xml
fog-sakuracloud (1.7.5)
fog-core
fog-json
fog-serverlove (0.1.2)
fog-core
fog-json
fog-softlayer (1.1.4)
fog-core
fog-json
fog-storm_on_demand (0.1.1)
fog-core
fog-json
fog-terremark (0.1.0)
fog-core
fog-xml
fog-vmfusion (0.1.0)
fission
fog-core
fog-voxel (0.1.0)
fog-core
fog-xml
fog-vsphere (2.3.0)
fog-core
rbvmomi (~> 1.9)
fog-xenserver (0.3.0)
fog-core
fog-xml
fog-xml (0.1.3)
fog-core
nokogiri (>= 1.5.11, < 2.0.0)
formatador (0.2.5)
globalid (0.4.1) globalid (0.4.1)
activesupport (>= 4.2.0) activesupport (>= 4.2.0)
http-cookie (1.0.3)
domain_name (~> 0.5)
i18n (1.0.1) i18n (1.0.1)
concurrent-ruby (~> 1.0) concurrent-ruby (~> 1.0)
inflecto (0.0.2)
io-like (0.3.0) io-like (0.3.0)
ipaddress (0.8.3)
jquery-rails (4.3.3) jquery-rails (4.3.3)
rails-dom-testing (>= 1, < 3) rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0) railties (>= 4.2.0)
thor (>= 0.14, < 2.0) thor (>= 0.14, < 2.0)
json (2.1.0)
listen (3.1.5) listen (3.1.5)
rb-fsevent (~> 0.9, >= 0.9.4) rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7) rb-inotify (~> 0.9, >= 0.9.7)
...@@ -100,14 +269,22 @@ GEM ...@@ -100,14 +269,22 @@ GEM
marcel (0.3.2) marcel (0.3.2)
mimemagic (~> 0.3.2) mimemagic (~> 0.3.2)
method_source (0.9.0) method_source (0.9.0)
mime-types (3.2.2)
mime-types-data (~> 3.2015)
mime-types-data (3.2018.0812)
mimemagic (0.3.2) mimemagic (0.3.2)
mini_magick (4.8.0)
mini_mime (1.0.0) mini_mime (1.0.0)
mini_portile2 (2.3.0) mini_portile2 (2.3.0)
minitest (5.11.3) minitest (5.11.3)
msgpack (1.2.4) msgpack (1.2.4)
multi_json (1.13.1)
netrc (0.11.0)
nio4r (2.3.1) nio4r (2.3.1)
nokogiri (1.8.4) nokogiri (1.8.4)
mini_portile2 (~> 2.3.0) mini_portile2 (~> 2.3.0)
ovirt-engine-sdk (4.2.4)
json (>= 1, < 3)
pg (1.0.0) pg (1.0.0)
public_suffix (3.0.2) public_suffix (3.0.2)
puma (3.12.0) puma (3.12.0)
...@@ -146,6 +323,18 @@ GEM ...@@ -146,6 +323,18 @@ GEM
rb-fsevent (0.10.3) rb-fsevent (0.10.3)
rb-inotify (0.9.10) rb-inotify (0.9.10)
ffi (>= 0.5.0, < 2) ffi (>= 0.5.0, < 2)
rbovirt (0.1.7)
nokogiri
rest-client (> 1.7.0)
rbvmomi (1.13.0)
builder (~> 3.0)
json (>= 1.8)
nokogiri (~> 1.5)
trollop (~> 2.1)
rest-client (2.0.2)
http-cookie (>= 1.0.2, < 2.0)
mime-types (>= 1.16, < 4.0)
netrc (~> 0.8)
ruby_dep (1.5.0) ruby_dep (1.5.0)
rubyzip (1.2.1) rubyzip (1.2.1)
sass (3.5.7) sass (3.5.7)
...@@ -178,10 +367,14 @@ GEM ...@@ -178,10 +367,14 @@ GEM
thor (0.20.0) thor (0.20.0)
thread_safe (0.3.6) thread_safe (0.3.6)
tilt (2.0.8) tilt (2.0.8)
trollop (2.1.3)
tzinfo (1.2.5) tzinfo (1.2.5)
thread_safe (~> 0.1) thread_safe (~> 0.1)
uglifier (4.1.17) uglifier (4.1.17)
execjs (>= 0.3.0, < 3) execjs (>= 0.3.0, < 3)
unf (0.1.4)
unf_ext
unf_ext (0.0.7.5)
web-console (3.6.2) web-console (3.6.2)
actionview (>= 5.0) actionview (>= 5.0)
activemodel (>= 5.0) activemodel (>= 5.0)
...@@ -191,6 +384,7 @@ GEM ...@@ -191,6 +384,7 @@ GEM
websocket-extensions (>= 0.1.0) websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.3) websocket-extensions (0.1.3)
will_paginate (3.1.6) will_paginate (3.1.6)
xml-simple (1.1.5)
xpath (3.1.0) xpath (3.1.0)
nokogiri (~> 1.8) nokogiri (~> 1.8)
...@@ -204,10 +398,13 @@ DEPENDENCIES ...@@ -204,10 +398,13 @@ DEPENDENCIES
bootstrap-will_paginate (~> 1.0) bootstrap-will_paginate (~> 1.0)
byebug byebug
capybara (>= 2.15, < 4.0) capybara (>= 2.15, < 4.0)
carrierwave (~> 1.2, >= 1.2.3)
chromedriver-helper chromedriver-helper
faker (~> 1.9, >= 1.9.1) faker (~> 1.9, >= 1.9.1)
fog (~> 2.0)
jquery-rails (~> 4.3, >= 4.3.3) jquery-rails (~> 4.3, >= 4.3.3)
listen (>= 3.0.5, < 3.2) listen (>= 3.0.5, < 3.2)
mini_magick (~> 4.8)
pg (~> 1.0) pg (~> 1.0)
puma (~> 3.11) puma (~> 3.11)
rails (~> 5.2.0) rails (~> 5.2.0)
......
//= require jquery //= require jquery
//= require bootstrap //= require bootstrap
//= require rails-ujs //= require rails-ujs
//= require_tree . //= require home
\ No newline at end of file
$(document).ready(function() {
$('#micropost_picture').bind('change', function() {
var size_in_megabytes = this.files[0].size/1024/1024;
if (size_in_megabytes > 5) {
alert("Maximum file size is 5MB. Please choose a smaller file.");
}
}).trigger("change");
});
...@@ -195,4 +195,50 @@ input { ...@@ -195,4 +195,50 @@ input {
padding: 10px 0; padding: 10px 0;
border-bottom: 1px solid $gray-lighter; border-bottom: 1px solid $gray-lighter;
} }
}
/* microposts */
.microposts {
list-style: none;
padding: 0;
li {
padding: 10px 0;
border-top: 1px solid #e8e8e8;
}
.user {
margin-top: 5em;
padding-top: 0;
}
.content {
display: block;
margin-left: 60px;
img {
display: block;
padding: 5px 0;
}
}
.timestamp {
color: $gray-light;
display: block;
margin-left: 60px;
}
.gravatar {
float: left;
margin-right: 10px;
margin-top: 5px;
}
}
aside {
textarea {
height: 100px;
margin-bottom: 5px;
}
}
span.picture {
margin-top: 10px;
input {
border: 0;
}
} }
\ No newline at end of file
class ApplicationController < ActionController::Base class ApplicationController < ActionController::Base
protect_from_forgery with: :exception protect_from_forgery with: :exception
include SessionsHelper include SessionsHelper
private
#confirm a logged-in user
# if user don't login and they're in GET request, it store this request to session[:forwarding_url]
def logged_in_user
unless logged_in?
store_location
flash[:danger] = "Please log in"
redirect_to login_url
end
end
end end
class MicropostsController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
before_action :correct_user, only: :destroy
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
flash[:success] = "Micropost deleted"
redirect_to request.referrer || root_url
end
private
def micropost_params
params.require(:micropost).permit(:content, :picture)
end
def correct_user
@micropost = current_user.microposts.find_by(id: params[:id])
redirect_to root_url if @micropost.nil?
end
end
class StaticPagesController < ApplicationController class StaticPagesController < ApplicationController
def home
if logged_in?
@micropost = current_user.microposts.build if logged_in?
@feed_items = current_user.feed.paginate(page: params[:page])
end
end
end end
...@@ -9,6 +9,7 @@ class UsersController < ApplicationController ...@@ -9,6 +9,7 @@ class UsersController < ApplicationController
end end
def show def show
@microposts = @user.microposts.paginate(page: params[:page])
end end
def new def new
...@@ -53,16 +54,6 @@ class UsersController < ApplicationController ...@@ -53,16 +54,6 @@ class UsersController < ApplicationController
@user = User.find(params[:id]) @user = User.find(params[:id])
end end
#confirm a logged-in user
# if user don't login and they're in GET request, it store this request to session[:forwarding_url]
def logged_in_user
unless logged_in?
store_location
flash[:danger] = "Please log in"
redirect_to login_url
end
end
# confirm the correct user # confirm the correct user
def correct_user def correct_user
redirect_to root_url unless current_user? find_user redirect_to root_url unless current_user? find_user
......
module MicropostsHelper
end
class Micropost < ApplicationRecord
belongs_to :user
default_scope -> { order("created_at DESC").includes(:user) }
mount_uploader :picture, PictureUploader
validates :user_id, presence: true
validates :content, presence: true, length: { maximum: 140 }
validate :picture_size
def picture_size
if picture.size > 5.megabytes
errors.add(:picture, "should be less than 5MB")
end
end
end
\ No newline at end of file
class User < ApplicationRecord class User < ApplicationRecord
has_many :microposts, dependent: :destroy
attr_accessor :remember_token, :activation_token, :reset_token attr_accessor :remember_token, :activation_token, :reset_token
before_save :downcase_email before_save :downcase_email
...@@ -63,6 +65,10 @@ class User < ApplicationRecord ...@@ -63,6 +65,10 @@ class User < ApplicationRecord
reset_sent_at < 2.hours.ago reset_sent_at < 2.hours.ago
end end
def feed
Micropost.where("user_id = ?", id)
end
private private
def downcase_email def downcase_email
self.email = email.downcase self.email = email.downcase
......
class PictureUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
include CarrierWave::MiniMagick
process resize_to_limit: [400, 400]
# Choose what kind of storage to use for this uploader:
if Rails.env.production?
storage :fog
else
storage :file
end
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
# Provide a default URL as a default if there hasn't been a file uploaded:
# def default_url(*args)
# # For Rails 3.1+ asset pipeline compatibility:
# # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
#
# "/images/fallback/" + [version_name, "default.png"].compact.join('_')
# end
# Process files as they are uploaded:
# process scale: [200, 300]
#
# def scale(width, height)
# # do something
# end
# Create different versions of your uploaded files:
# version :thumb do
# process resize_to_fit: [50, 50]
# end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_whitelist
%w(jpg jpeg gif png)
end
# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
# def filename
# "something.jpg" if original_filename
# end
end
<li id="micropost-<%= micropost.id %>">
<%= link_to gravatar_for(micropost.user, size: 50), micropost.user %>
<span class="user"><%= link_to micropost.user.name, micropost.user %></span>
<span class="content">
<%= micropost.content %>
<%= image_tag micropost.picture.url if micropost.picture? %>
</span>
<span class="timestamp">
Posted <%= time_ago_in_words(micropost.created_at) %> ago.
<% if current_user? micropost.user %>
<%= link_to "delete", micropost, method: :delete, data: { confirm: "You sure?" } %>
<% end %>
</span>
</li>
\ No newline at end of file
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
<div class="row"> <div class="row">
<div class="col-md-6 col-md-offet-3"> <div class="col-md-6 col-md-offet-3">
<%= form_for @user, url: password_reset_path(params[:id]) do |f| %> <%= form_for @user, url: password_reset_path(params[:id]) do |f| %>
<%= render 'shared/error_messages', user: @user %> <%= render 'shared/error_messages', object: f.object %>
<%= hidden_field_tag :email, @user.email %> <%= hidden_field_tag :email, @user.email %>
......
<% if user.errors.any? %> <% if object.errors.any? %>
<div id="error_explanation"> <div id="error_explanation">
<div class="alert alert-danger"> <div class="alert alert-danger">
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>
......
<% if @feed_items.any? %>
<ol class="microposts">
<%= render @feed_items %>
</ol>
<%= will_paginate @feed_items %>
<% end %>
\ No newline at end of file
<%= 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-priamary" %>
<span class="picture">
<%= f.file_field :picture, accept: "image/jpeg,image/gif,image/png" %>
</span>
<% end %>
<%= link_to gravatar_for(current_user, size: 50), 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 jumbotron"> <% if logged_in? %>
<h1>Welcome to the Sample App</h1> <div class="row">
<aside class="col-md-4">
<section class="user_info">
<%= render 'shared/user_info' %>
</section>
<section class="micropost_form">
<%= render "shared/micropost_form" %>
</section>
</aside>
<div class="col-md-8">
<h3>Micropost Feed</h3>
<%= render 'shared/feed' %>
</div>
</div>
<% else %>
<div class="center jumbotron">
<h1>Welcome to the Sample App</h1>
<h2> <h2>
This is the home page for the This is the home page for the
<a href="http://www.rails tutorial.org">Ruby on Rails Tutorial</a> <a href="http://www.rails tutorial.org">Ruby on Rails Tutorial</a>
sample application. sample application.
</h2> </h2>
<%= link_to "Sign up now", signup_path, class: "btn btn-lg btn-primary" %> <%= link_to "Sign up now", signup_path, class: "btn btn-lg btn-primary" %>
</div> </div>
<%= link_to image_tag("rails.png", alt: "Rails logo"), "http://rubyonrails.org/" %> <%= link_to image_tag("rails.png", alt: "Rails logo"), "http://rubyonrails.org/" %>
\ No newline at end of file <% end %>
\ No newline at end of file
<%= form_for @user do |f| %> <%= form_for @user do |f| %>
<%= render 'shared/error_messages', user: @user %> <%= render 'shared/error_messages', object: f.object %>
<%= f.label :name %> <%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %> <%= f.text_field :name, class: 'form-control' %>
......
...@@ -8,5 +8,15 @@ ...@@ -8,5 +8,15 @@
</h1> </h1>
</section> </section>
</aside> </aside>
<div class="col-md-8">
<% if @user.microposts.any? %>
<h3>Microposts (<%= @user.microposts.count %>)</h3>
<ol class="microposts">
<%= render @microposts %>
</ol>
<%= will_paginate @microposts %>
<% end %>
</div>
</div> </div>
...@@ -10,7 +10,6 @@ require "active_record/railtie" ...@@ -10,7 +10,6 @@ require "active_record/railtie"
require "action_controller/railtie" require "action_controller/railtie"
require "action_mailer/railtie" require "action_mailer/railtie"
require "action_view/railtie" require "action_view/railtie"
require "action_cable/engine"
require "sprockets/railtie" require "sprockets/railtie"
require "rails/test_unit/railtie" require "rails/test_unit/railtie"
......
...@@ -16,6 +16,7 @@ Rails.application.routes.draw do ...@@ -16,6 +16,7 @@ Rails.application.routes.draw do
resources :users resources :users
resources :account_activations, only: [:edit] resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update] resources :password_resets, only: [:new, :create, :edit, :update]
resources :microposts, only: [:create, :destroy]
end end
class CreateMicroposts < ActiveRecord::Migration[5.2]
def change
create_table :microposts do |t|
t.text :content
t.references :user, foreign_key: true
t.timestamps
end
add_index :microposts, [:user_id, :created_at]
end
end
class AddPictureToMicroposts < ActiveRecord::Migration[5.2]
def change
add_column :microposts, :picture, :string
end
end
...@@ -10,7 +10,17 @@ ...@@ -10,7 +10,17 @@
# #
# 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: 2018_08_13_074348) do ActiveRecord::Schema.define(version: 2018_08_15_014854) do
create_table "microposts", force: :cascade do |t|
t.text "content"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "picture"
t.index ["user_id", "created_at"], name: "index_microposts_on_user_id_and_created_at"
t.index ["user_id"], name: "index_microposts_on_user_id"
end
create_table "users", force: :cascade do |t| create_table "users", force: :cascade do |t|
t.string "name" t.string "name"
......
...@@ -16,4 +16,10 @@ User.create! name: "Example User", ...@@ -16,4 +16,10 @@ User.create! name: "Example User",
password_confirmation: password, password_confirmation: password,
activated: true, activated: true,
activated_at: Time.zone.now activated_at: Time.zone.now
end
users = User.order(:created_at).take(6)
50.times do
content = Faker::Lorem.sentence(5)
users.each { |user| user.microposts.create!(content: content) }
end end
\ No newline at end of file
require 'test_helper'
class MicropostsControllerTest < ActionDispatch::IntegrationTest
def setup
@micropost = microposts :orange
end
test "should redirect create when not logged in" do
assert_no_difference "Micropost.count" do
post microposts_path, params: { micropost: { content: "Lorem ipsum" } }
end
assert_redirected_to login_url
end
test "should redirect destroy when not logged in" do
assert_no_difference "Micropost.count" do
delete micropost_path(@micropost)
end
assert_redirected_to login_url
end
test "should redirect destroy for wrong micropost" do
log_in_as(users(:michael))
micropost = microposts(:ants)
assert_no_difference "Micropost.count" do
delete micropost_path(micropost)
end
assert_redirected_to root_url
end
end
orange:
content: "I just ate an orange!"
created_at: <%= 10.minutes.ago %>
user: michael
tau_manifesto:
content: "Check out the @tauday site by @mhartl: http://tauday.com"
created_at: <%= 3.years.ago %>
user: michael
cat_video:
content: "Sad cats are sad: http://youtu.be/PKffm2uI4dk"
created_at: <%= 2.hours.ago %>
user: michael
most_recent:
content: "Writing a short test"
created_at: <%= Time.zone.now %>
user: michael
<% 30.times do |n| %>
micropost_<%= n %>:
content: <%= Faker::Lorem.sentence(5) %>
created_at: <%= 42.days.ago %>
user: michael
<% end %>
ants:
content: "Oh, is that what you want? Because that's how you get ants!"
created_at: <%= 2.years.ago %>
user: archer
zone:
content: "Danger zone!"
created_at: <%= 3.days.ago %>
user: archer
tone:
content: "I'm sorry. Your words made sense, but your sarcastic tone did not."
created_at: <%= 10.minutes.ago %>
user: lana
van:
content: "Dude, this van's, like, rolling probable cause."
created_at: <%= 4.hours.ago %>
user: lana
\ No newline at end of file
require 'test_helper'
class MicropostsInterfaceTest < ActionDispatch::IntegrationTest
def setup
@user = users(:michael)
end
test "micropost interface" do
log_in_as(@user)
get root_path
assert_select 'div.pagination'
# Invalid submission
assert_no_difference 'Micropost.count' do
post microposts_path, params: { micropost: { content: "" } }
end
assert_select 'div#error_explanation'
# Valid submission
content = "This micropost really ties the room together"
assert_difference 'Micropost.count', 1 do
post microposts_path, params: { micropost: { content: content } }
end
assert_redirected_to root_url
follow_redirect!
assert_match content, response.body
# Delete post
assert_select 'a', text: 'delete'
first_micropost = @user.microposts.paginate(page: 1).first
assert_difference 'Micropost.count', -1 do
delete micropost_path(first_micropost)
end
# Visit different user (no delete links)
get user_path(users(:archer))
assert_select 'a', text: 'delete', count: 0
end
end
require 'test_helper'
class UsersProfileTest < ActionDispatch::IntegrationTest
include ApplicationHelper
def setup
@user = users(:michael)
end
test "profile display" do
get user_path(@user)
assert_template 'users/show'
assert_select 'title', full_title(@user.name)
assert_select 'h1', text: @user.name
assert_select 'h1>img.gravatar'
assert_match @user.microposts.count.to_s, response.body
assert_select 'div.pagination'
@user.microposts.paginate(page: 1).each do |micropost|
assert_match micropost.content, response.body
end
end
end
\ No newline at end of file
require 'test_helper'
class MicropostTest < ActiveSupport::TestCase
def setup
@user = users :michael
@micropost = @user.microposts.build(content: "Lorem ipsum")
end
test "should be valid" do
assert @micropost.valid?
end
test "user id should be presence" do
@micropost.user_id = nil
assert_not @micropost.valid?
end
test "content should be presence" do
@micropost.content = nil
assert_not @micropost.valid?
end
test "content should be at most 140 characters" do
@micropost.content = "a" * 141
assert_not @micropost.valid?
end
test "order should be most recent first" do
assert_equal microposts(:most_recent), Micropost.first
end
end
...@@ -75,4 +75,12 @@ class UserTest < ActiveSupport::TestCase ...@@ -75,4 +75,12 @@ class UserTest < ActiveSupport::TestCase
test "authenticated? should return false for a user with nil digest" do test "authenticated? should return false for a user with nil digest" do
assert_not @user.authenticated?(:remember, '') assert_not @user.authenticated?(:remember, '')
end end
test "associated microposts should be destroyed" do
@user.save
@user.microposts.create(content: "lorem ipsum")
assert_difference "Micropost.count", -1 do
@user.destroy
end
end
end end
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