diff --git a/Gemfile b/Gemfile index b9e6c4f..06f6764 100644 --- a/Gemfile +++ b/Gemfile @@ -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] diff --git a/Gemfile.lock b/Gemfile.lock index 0cb1f1f..03bb1f1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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) @@ -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 @@ -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) @@ -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 diff --git a/app/assets/stylesheets/custom.scss b/app/assets/stylesheets/custom.scss index 8fa267c..27766ac 100644 --- a/app/assets/stylesheets/custom.scss +++ b/app/assets/stylesheets/custom.scss @@ -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; + } +} diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 25e8ef0..3d8a34e 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -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 diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index bf4a682..df99185 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -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 @@ -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 @@ -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 diff --git a/app/helpers/sessions_helper.rb b/app/helpers/sessions_helper.rb index 50e3e82..b093be3 100644 --- a/app/helpers/sessions_helper.rb +++ b/app/helpers/sessions_helper.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 099dd07..c2df066 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -27,6 +27,10 @@ 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 @@ -34,7 +38,7 @@ def digest string else BCrypt::Engine.cost end - BCrypt::Password.create(string, cost: cost) + BCrypt::Password.create string, cost: cost end def new_token diff --git a/app/views/layouts/_header.html.erb b/app/views/layouts/_header.html.erb index b0349f6..7fcc1b1 100644 --- a/app/views/layouts/_header.html.erb +++ b/app/views/layouts/_header.html.erb @@ -6,7 +6,7 @@