Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 11 additions & 20 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,22 @@ git_source(:github){|repo| "https://github.com/#{repo}.git"}

ruby "2.4.1"

gem "rails", "~> 5.2.0"

gem "bootstrap-sass", "3.3.7"

gem "jquery-rails", "~> 4.3", ">= 4.3.3"

gem "bcrypt", "3.1.12"

gem "bootsnap", ">= 1.1.0", require: false
gem "bootstrap-sass", "3.3.7"
gem "bootstrap-will_paginate", "1.0.0"
gem "coffee-rails", "~> 4.2"
gem "config"

gem "sqlite3"

gem "faker", "1.9.1"
gem "jbuilder", "~> 2.5"
gem "jquery-rails", "~> 4.3", ">= 4.3.3"
gem "puma", "~> 3.11"

gem "rails", "~> 5.2.0"
gem "sass-rails", "~> 5.0"

gem "uglifier", ">= 1.3.0"

gem "coffee-rails", "~> 4.2"

gem "sqlite3"
gem "turbolinks", "~> 5"

gem "jbuilder", "~> 2.5"

gem "bootsnap", ">= 1.1.0", require: false
gem "uglifier", ">= 1.3.0"
gem "will_paginate", "3.1.6"

group :development, :test do
gem "byebug", platforms: [:mri, :mingw, :x64_mingw]
Expand Down
8 changes: 8 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ GEM
bootstrap-sass (3.3.7)
autoprefixer-rails (>= 5.2.1)
sass (>= 3.3.4)
bootstrap-will_paginate (1.0.0)
will_paginate
builder (3.2.3)
byebug (10.0.2)
coffee-rails (4.2.2)
Expand Down Expand Up @@ -98,6 +100,8 @@ GEM
dry-types (~> 0.13.1)
erubi (1.7.1)
execjs (2.7.0)
faker (1.9.1)
i18n (>= 0.7)
ffi (1.9.25)
globalid (0.4.1)
activesupport (>= 4.2.0)
Expand Down Expand Up @@ -220,6 +224,7 @@ GEM
websocket-driver (0.7.0)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.3)
will_paginate (3.1.6)

PLATFORMS
ruby
Expand All @@ -228,9 +233,11 @@ DEPENDENCIES
bcrypt (= 3.1.12)
bootsnap (>= 1.1.0)
bootstrap-sass (= 3.3.7)
bootstrap-will_paginate (= 1.0.0)
byebug
coffee-rails (~> 4.2)
config
faker (= 1.9.1)
jbuilder (~> 2.5)
jquery-rails (~> 4.3, >= 4.3.3)
listen (>= 3.0.5, < 3.2)
Expand All @@ -245,6 +252,7 @@ DEPENDENCIES
tzinfo-data
uglifier (>= 1.3.0)
web-console (>= 3.3.0)
will_paginate (= 3.1.6)

RUBY VERSION
ruby 2.4.1p111
Expand Down
10 changes: 10 additions & 0 deletions app/assets/stylesheets/custom.scss
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,13 @@ input {
width: auto;
margin-left: 0;
}

.users {
list-style: none;
margin: 0;
li {
overflow: auto;
padding: 10px 0;
border-bottom: 1px solid $gray-lighter;
}
}
2 changes: 1 addition & 1 deletion app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def create
if @user&.authenticate params[:session][:password]
log_in @user
remember_me
redirect_to @user
redirect_back_or @user
else
flash.now[:danger] = t ".error_login"
render :new
Expand Down
57 changes: 52 additions & 5 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
class UsersController < ApplicationController
before_action :logged_in_user, only: [:index, :edit, :update, :destroy]
before_action :find_user, only: [:edit, :show, :update, :destroy]
before_action :correct_user, only: [:edit, :update]
before_action :admin_user, only: :destroy
def index
@users = User.paginate page: params[:page],
per_page: Settings.total_user_per_page
end

def new
@user = User.new
end
Expand All @@ -14,12 +23,26 @@ def create
end
end

def show
@user = User.find_by id: params[:id]
def show; end

return if @user
flash[:danger] = t "not_found"
redirect_to root_path
def edit; end

def update
if @user.update_attributes user_params
flash[:success] = t "update_mesage"
redirect_to @user
else
render :edit
end
end

def destroy
flash[:success] = if @user.destroy
t "delete_mesage_success"
else
t "delete_mesage_failed"
end
redirect_to users_url
end

private
Expand All @@ -28,4 +51,28 @@ def user_params
params.require(:user)
.permit :name, :email, :password, :password_confirmation
end

def logged_in_user
return if logged_in?
store_location
flash[:danger] = t "login_mesage"
redirect_to login_url
end

def correct_user
redirect_to root_path unless @user.current_user? current_user
end

def admin_user
redirect_to root_url unless current_user.admin?
end

def find_user
@user = User.find_by id: params[:id]

return if @user

flash[:danger] = t "not_found"
redirect_to root_path
end
end
9 changes: 9 additions & 0 deletions app/helpers/sessions_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,13 @@ def log_out
session.delete :user_id
@current_user = nil
end

def redirect_back_or default
redirect_to session[:forwarding_url] || default
session.delete :forwarding_url
end

def store_location
session[:forwarding_url] = request.original_url if request.get?
end
end
6 changes: 5 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@ def forget
update_attributes remember_digest: nil
end

def current_user? user
user == self
end

class << self
def digest string
cost = if ActiveModel::SecurePassword.min_cost
BCrypt::Engine::MIN_COST
else
BCrypt::Engine.cost
end
BCrypt::Password.create(string, cost: cost)
BCrypt::Password.create string, cost: cost
end

def new_token
Expand Down
4 changes: 2 additions & 2 deletions app/views/layouts/_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<li><%= link_to t(".home"), root_path %></li>
<li><%= link_to t(".help"), help_path %></li>
<% if logged_in? %>
<li><%= link_to t(".user"), "#" %></li>
<li><%= link_to t(".user"), users_path %></li>
<li class="dropdown">
<%= link_to "#", class: "dropdown-toggle",
data: {toggle: "dropdown"} do %>
Expand All @@ -15,7 +15,7 @@
<% end %>
<ul class="dropdown-menu">
<li><%= link_to t(".profile"), current_user %></li>
<li><%= link_to t(".settings"), "#" %></li>
<li><%= link_to t(".settings"), edit_user_path(current_user) %></li>
<li class="divider"></li>
<li>
<%= link_to t(".logout"), logout_path, method: :delete %>
Expand Down
17 changes: 17 additions & 0 deletions app/views/users/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<%= form_for @user do |f| %>
<%= render "shared/error_messages", object: @user %>

<%= f.label :name, t(".name") %>
<%= f.text_field :name, class: "form-control" %>

<%= f.label :email, t(".email") %>
<%= f.email_field :email, class: "form-control" %>

<%= f.label :password, t(".password") %>
<%= f.password_field :password, class: "form-control" %>

<%= f.label :password_confirmation, t(".confirmation") %>
<%= f.password_field :password_confirmation, class: "form-control" %>

<%= f.submit yield(:button_text), class: "btn btn-primary" %>
<% end %>
8 changes: 8 additions & 0 deletions app/views/users/_user.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<li>
<%= gravatar_for user, size: Settings.user_picture_thumb_size %>
<%= link_to user.name, @user %>
<% if current_user.admin? && !user.current_user?(current_user) %>
| <%= link_to t(".delete"), user, method: :delete,
data: { confirm: t(".del_confirm") } %>
<% end %>
</li>
13 changes: 13 additions & 0 deletions app/views/users/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<% provide :title, t(".title") %>
<% provide :button_text, t(".button_text") %>
<h1><%= t ".content" %></h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= render "form" %>
<div class="gravatar_edit">
<%= gravatar_for @user %>
<a href="http://gravatar.com/emails" target="_blank">
<%= t ".link_change" %></a>
</div>
</div>
</div>
9 changes: 9 additions & 0 deletions app/views/users/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<% provide :title, t(".title") %>
<h1><% t ".title" %></h1>
<%= will_paginate %>

<ul class="users">
<%= render @users %>
</ul>

<%= will_paginate %>
21 changes: 2 additions & 19 deletions app/views/users/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,25 +1,8 @@
<% provide :title, t(".sign_up") %>
<% provide :button_text, t(".button_text") %>
<h1><%= t ".sign_up" %></h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for @user, url: signup_path do |f| %>
<%= render "shared/error_messages" %>

<%= f.label :name, t(".name") %>
<%= f.text_field :name, class: "form-control" %>

<%= f.label :email, t(".email") %>
<%= f.email_field :email, class: "form-control" %>

<%= f.label :password, t(".password") %>
<%= f.password_field :password, class: "form-control" %>

<%= f.label :password_confirmation, t(".confirmation") %>
<%= f.password_field :password_confirmation,
class: "form-control" %>

<%= f.submit t(".create"),
class: "btn btn-primary" %>
<% end %>
<%= render "form" %>
</div>
</div>
20 changes: 18 additions & 2 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ en:
back: "Back"
en_title: "Ruby on Rails Tutorial"
not_found: "không tìm thấy tài khoản!"
update_message: "Profile updated"
delete_mesage_success: "User deleted"
delete_mesage_failed: "User delete failed"
login_message: "Please log in."
static_pages:
home:
title: "Home"
Expand Down Expand Up @@ -55,12 +59,24 @@ en:
application:
users:
title_sample: "Welcome to the Sample App!"
index:
title: "All user"
user:
delete: "delete"
del_confirm: "You sure?"
new:
error: "error "
sign_up: "Sign up"
button_text: "Sign up"
form_container: "The form contains "
sign_up: "Sign up "
content_sigup: "This will be a signup page for new users. "
create: "Create my account"
edit:
title: "Edit user"
button_text: "Save changes"
content: "Update your profile"
link_change: "Change"
form:
create: "Comfirm"
name: "Name"
email: "Email"
password: "Password"
Expand Down
25 changes: 22 additions & 3 deletions config/locales/vi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ vi:
back: "Trở lại"
en_title: "Hướng dẫn Ruby on Rails"
not_found: "không tìm thấy tài khoản!"
update_mesage: "Hồ sơ đã được cập nhật"
delete_mesage_success: "Người dùng đã xóa"
delete_mesage_failed: "Người dùng chưa được xóa"
login_message: "Làm ơn hay đăng nhập."
will_paginate:
next_label: "Sau"
previous_label: "Trước"
activerecord:
errors:
models:
Expand Down Expand Up @@ -71,14 +78,26 @@ vi:
application:
users:
title_sample: "Chào mừng đến với Ứng dụng mẫu!"
index:
title: "Tất cả người dùng"
user:
delete: "xóa"
del_confirm: "Bạn có chắc không ?"
new:
error: "lỗi "
form_container: "Biểu mẫu có chứa "
sign_up: "Đăng ký"
button_text: "Đăng ký"
form_container: "Biểu mẫu có chứa "
content_sigup: "Trang này sẽ là trang đăng ký cho 1 tài khoản mới ."
create: "Tạo tài khoản của tôi"
edit:
title: "Sửa người dùng"
button_text: "Lưu thay đổi"
content: "Cập nhật hồ sơ của bạn"
link_change: "Thay đổi"
form:
create: "Xác nhận"
name: "Tên"
email: "Email"
email: "Thư"
password: "Mật khẩu"
confirmation: "Xác nhận lại mật khẩu"
shared:
Expand Down
Loading