Commit af17d044 by Mai Hoang Thai Ha

Rails db seed

parent 37623935
Pipeline #1265 failed with stages
in 0 seconds
......@@ -6,6 +6,7 @@ ruby '3.0.1'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails', branch: 'main'
gem 'rails', '~> 6.1.3', '>= 6.1.3.2'
gem 'bcrypt', '3.1.13'
gem 'faker', '2.1.2'
gem 'bootstrap-sass', '3.4.1'
gem 'autoprefixer-rails'
# Use Puma as the app server
......
......@@ -84,10 +84,12 @@ GEM
xpath (~> 3.2)
childprocess (3.0.0)
coderay (1.1.3)
concurrent-ruby (1.1.8)
concurrent-ruby (1.1.9)
crass (1.0.6)
erubi (1.10.0)
execjs (2.8.1)
faker (2.1.2)
i18n (>= 0.8)
ffi (1.15.1)
formatador (0.2.5)
globalid (0.4.2)
......@@ -112,7 +114,7 @@ GEM
listen (3.5.1)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
loofah (2.9.1)
loofah (2.10.0)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
lumberjack (1.2.8)
......@@ -130,7 +132,7 @@ GEM
msgpack (1.4.2)
nenv (0.3.0)
nio4r (2.5.7)
nokogiri (1.11.6-x86_64-linux)
nokogiri (1.11.7-x86_64-linux)
racc (~> 1.4)
notiffany (0.1.3)
nenv (~> 0.1)
......@@ -233,7 +235,7 @@ GEM
rack-proxy (>= 0.6.1)
railties (>= 5.2)
semantic_range (>= 2.3.0)
websocket-driver (0.7.4)
websocket-driver (0.7.5)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
xpath (3.2.0)
......@@ -250,6 +252,7 @@ DEPENDENCIES
bootstrap-sass (= 3.4.1)
byebug
capybara (>= 3.26)
faker (= 2.1.2)
guard (= 2.16.2)
guard-minitest (= 2.4.6)
jbuilder (~> 2.7)
......
......@@ -211,3 +211,15 @@ input {
}
}
}
// Users index
.users {
list-style: none;
margin: 0;
li {
overflow: auto;
padding: 10px 0;
border-bottom: 1px solid $gray-medium-light;
}
}
\ No newline at end of file
......@@ -7,7 +7,7 @@ class SessionsController < ApplicationController
if user && user.authenticate(params[:session][:password])
log_in user
params[:session][:remember_me] == '1'? remember(user) : forget(user)
redirect_to user
redirect_back_or user
else
flash.now[:danger] = 'Invalid email/password combination'
render 'new'
......
class UsersController < ApplicationController
before_action :logged_in_user, only: [:index, :edit, :update]
before_action :correct_user, only: [:edit, :update]
def index
@users = User.all
end
def show
@user = User.find(params[:id])
......@@ -20,10 +26,40 @@ class UsersController < ApplicationController
end
end
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
if @user.update(user_params)
flash[:success] = "Profile updated"
redirect_to @user
else
render 'edit'
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation)
end
# Before filters
# Confirm a logged_in user
def logged_in_user
unless logged_in?
store_location
flash[:danger] = "Please log in."
redirect_to login_url
end
end
# Confirms the correct user
def correct_user
@user = User.find(params[:id])
redirect_to(root_url) unless current_user?(@user)
end
end
......@@ -25,6 +25,11 @@ module SessionsHelper
end
end
# Returns true if the given user is the current user
def current_user?(user)
user && user == current_user
end
# Return true if the user is loffed in, false otherwise
def logged_in?
!current_user.nil?
......@@ -41,4 +46,15 @@ module SessionsHelper
session.delete(:user_id)
@current_user = nil
end
# Redirects to stored location (or to the default)
def redirect_back_or(default)
redirect_to(session[:forwarding_url] || default)
session.delete(:forwarding_url)
end
# Stores the URL trying to be accessed
def store_location
session[:forwarding_url] = request.original_url if request.get?
end
end
module UsersHelper
# Return the Gravatar for the given user
def gravatar_for(user)
def gravatar_for(user, options = { size: 80 })
size = options[:size]
gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}"
gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}?s=#{size}"
image_tag(gravatar_url, alt: user.name, class: "gravatar")
end
end
......@@ -7,7 +7,7 @@ class User < ApplicationRecord
format: {with: VALID_EMAIL_REGEX},
uniqueness: true
has_secure_password
validates :password, presence: true, length: {minimum: 6}
validates :password, presence: true, length: { minimum: 6 }, allow_nil: true
# Returns the hash digest of the given string
def User.digest(string)
......
......@@ -20,14 +20,14 @@
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Help", help_path %></li>
<% if logged_in? %>
<li><%= link_to "Users", '#' %></li>
<li><%= link_to "Users", users_path %></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle='dropdown'>
Account <b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><%= link_to "Profile", current_user %></li>
<li><%= link_to "Settings", '#' %></li>
<li><%= link_to "Settings", edit_user_path(current_user) %></li>
<li class="divider"></li>
<li>
<%= link_to "Log out", logout_path, method: :delete %>
......
<% provide(:title, "Edit user") %>
<h1>Update your profile</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_with(model: @user, local: true) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control' %>
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
<%= f.submit "Save changes", class: "btn btn-primary"%>
<% end %>
<div class="gravatar_edit">
<%= gravatar_for @user %>
<a href="https://gravatar.com/emails" target="_blank">Change</a>
</div>
</div>
</div>
\ No newline at end of file
<%= provide(:title, 'All users') %>
<h1>All users</h1>
<ul class="users">
<% @users.each do |user| %>
<li>
<%= gravatar_for user, size: 50 %>
<%= link_to user.name, user%>
</li>
<% end %>
</ul>
\ No newline at end of file
......@@ -8,6 +8,7 @@ if !defined?(Spring) && [nil, "development", "test"].include?(ENV["RAILS_ENV"])
Gem.use_paths Gem.dir, Bundler.bundle_path.to_s, *Gem.path
gem "spring", spring.version
require "spring/binstub"
rescue Gem::LoadError
# Ignore when Spring is not installed.
end
......
# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the bin/rails db:seed command (or created alongside the database with db:setup).
#
# Examples:
#
# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
# Character.create(name: 'Luke', movie: movies.first)
User.create!( name: "Example User",
email: "example@railstutorial.org",
password: "foobar",
password_confirmation: "foobar")
99.times do |num|
name = Faker::Name.name
email = "example-#{num+1}@railstutorials.org"
password = "password"
User.create!(name: name,
email: email,
password: password,
password_confirmation: password)
end
\ No newline at end of file
require "test_helper"
class UsersControllerTest < ActionDispatch::IntegrationTest
def setup
@user = users(:michael)
@other_user = users(:archer)
end
test "should get new" do
get signup_path
assert_response :success
end
test "should redirect index when not logged in" do
get users_path
assert_redirected_to login_url
end
test "should redirect edit when not logged in" do
get edit_user_path(@user)
assert_not flash.empty?
assert_redirected_to login_url
end
test "should redirect update when not logged in" do
patch user_path(@user), params: { user: { name: @user.name,
email: @user.email } }
assert_not flash.empty?
assert_redirected_to login_url
end
test "should redirect edit when logged in as wrong user" do
log_in_as(@other_user)
get edit_user_path(@user)
assert flash.empty?
assert_redirected_to root_url
end
test "should redirect update when logged in as wrong user" do
log_in_as(@other_user)
patch user_path(@user), params: { user: { name: @user.name,email: @user.email } }
assert flash.empty?
assert_redirected_to root_url
end
end
\ No newline at end of file
......@@ -2,3 +2,8 @@ michael:
name: Michael Example
email: michael@example.com
password_digest: <%= User.digest('password') %>
archer:
name: Sterling Archer
email: duchess@example.gov
password_digest: <%= User.digest('password') %>
require "test_helper"
class UsersEditTest < ActionDispatch::IntegrationTest
def setup
@user = users(:michael)
end
test "unsuccessful edit" do
log_in_as(@user)
get edit_user_path(@user)
assert_template 'users/edit'
patch user_path(@user), params: { user: { name: "",
email: "foo@invalid",
password: "foo",
password_confirmation: "bar" } }
assert_template 'users/edit'
end
test "successful edit with friendly forwarding" do
get edit_user_path(@user)
log_in_as(@user)
assert_redirected_to edit_user_url(@user)
name = "Foo Bar"
email = "foo@bar.com"
patch user_path(@user), params: { user: { name: name,
email: email,
password: "",
password_confirmation: "" } }
assert_not flash.empty?
assert_redirected_to @user
@user.reload
assert_equal name, @user.name
assert_equal email, @user.email
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