From 1936c90c951ba69a1528d53ce3178d42620bf25a Mon Sep 17 00:00:00 2001 From: Jyri-Petteri Paloposki Date: Mon, 10 Aug 2020 16:54:48 +0300 Subject: [PATCH] Record and show the last time users have logged in --- app/controllers/login_controller.rb | 1 + app/views/users/index.html.erb | 2 ++ db/migrate/20200810123316_add_lastlogin_to_user.rb | 5 +++++ db/schema.rb | 3 ++- 4 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20200810123316_add_lastlogin_to_user.rb diff --git a/app/controllers/login_controller.rb b/app/controllers/login_controller.rb index 402f7a4b..2ca0b1cb 100644 --- a/app/controllers/login_controller.rb +++ b/app/controllers/login_controller.rb @@ -14,6 +14,7 @@ class LoginController < ApplicationController case request.method when 'POST' if @user = User.authenticate(params['user_login'], params['user_password']) + @user.update_attribute(:last_login_at, Time.now) return handle_post_success else handle_post_failure diff --git a/app/views/users/index.html.erb b/app/views/users/index.html.erb index 9cf49d00..f463466f 100644 --- a/app/views/users/index.html.erb +++ b/app/views/users/index.html.erb @@ -14,6 +14,7 @@ <%= t('users.total_projects') %> <%= t('users.total_notes') %> <%= User.human_attribute_name('created_at') %> + <%= User.human_attribute_name('last_login_at') %>   <% for user in @users %> @@ -28,6 +29,7 @@ <%= h user.projects.size %> <%= h user.notes.size %> <%= format_date(user.created_at) %> + <%= format_date(user.last_login_at) %> <%= !user.is_admin? ? remote_delete_user(user) : " ".html_safe %> <% end %> diff --git a/db/migrate/20200810123316_add_lastlogin_to_user.rb b/db/migrate/20200810123316_add_lastlogin_to_user.rb new file mode 100644 index 00000000..c7ec36d9 --- /dev/null +++ b/db/migrate/20200810123316_add_lastlogin_to_user.rb @@ -0,0 +1,5 @@ +class AddLastloginToUser < ActiveRecord::Migration[5.2] + def change + add_column :users, :last_login_at, :datetime + end +end diff --git a/db/schema.rb b/db/schema.rb index 68ea91d1..b0f92d33 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2020_08_07_175610) do +ActiveRecord::Schema.define(version: 2020_08_10_123316) do create_table "attachments", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.integer "todo_id" @@ -223,6 +223,7 @@ ActiveRecord::Schema.define(version: 2020_08_07_175610) do t.string "email" t.datetime "created_at" t.datetime "updated_at" + t.datetime "last_login_at" t.index ["login"], name: "index_users_on_login" end