Skip to content

Commit 7d3fd82

Browse files
authored
Merge pull request #370 from Trim/track-account-last-seen-on
Track account last seen on
2 parents 3a80bb1 + e71a744 commit 7d3fd82

File tree

7 files changed

+582
-2
lines changed

7 files changed

+582
-2
lines changed

app/controllers/application_controller.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class ApplicationController < ActionController::Base
55
protect_from_forgery with: :reset_session, prepend: true
66
before_action :seo_filter
77
before_action :configure_permitted_parameters, if: :devise_controller?
8+
before_action :set_account_last_seen, if: :account_signed_in?
89
helper_method :url_for_content, :path_for_content, :current_user, :current_stylesheet
910
rescue_from Canable::Transgression, with: :error_403
1011

@@ -44,6 +45,15 @@ def seo_filter
4445
@dont_index = params.has_key?(:order) || (request.headers["User-Agent"] =~ /AppEngine-Google/i)
4546
end
4647

48+
def set_account_last_seen
49+
# Update last seen only once a day to avoid too many database update
50+
if Date.today - current_account.last_seen_on >= 1
51+
# Use update_columns to ensure updated_at value is not changed with this
52+
# automatic update
53+
current_account.update_columns(last_seen_on: Date.today);
54+
end
55+
end
56+
4757
def dont_index?
4858
!!@dont_index
4959
end

app/views/admin/accounts/index.html.haml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
%th Adresse IP courante / précédente
1717
%th Commentaires / contenus / étiquettes
1818
%th Date de création
19+
%th Dernière visite
1920
%th Actions
2021
%th Changer de rôle
2122
- @accounts.each do |account|
@@ -73,6 +74,7 @@
7374
#{ image_tag "/images/icones/denied.png", alt: "masqués", title: "masqués", width: "16px" }
7475
%br
7576
%td= account.created_at.to_s(:posted)
77+
%td= account.last_seen_on ? account.last_seen_on.to_s(:date) : "-"
7678
%td
7779
- if account.inactive? && account.user_id != 1
7880
= button_to "Activer", [:admin, account], method: :put, class: "ok_button"

app/views/users/_recent.html.haml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
%li Rôle : #{a.display_role(@user.nodes.where(public: true).count>0)}
1111
%li Dernière connexion : #{a.current_sign_in_at ? l(@user.account.current_sign_in_at) : "-"}
1212
%li Dernière action : #{a.updated_at ? l(@user.account.updated_at) : "-"}
13+
%li Dernière visite : #{a.last_seen_on ? l(@user.account.last_seen_on, format: :long) : "-"}
1314
%li Karma : #{a.karma} (minimum : #{a.min_karma}, maximum : #{a.max_karma})
1415
- if @user.homesite.present?
1516
%li Site perso : #{@user.homesite}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class AddLastSeenOnToAccounts < ActiveRecord::Migration[5.2]
2+
def change
3+
add_column :accounts, :last_seen_on, :date
4+
5+
reversible do |set_last_seen_on|
6+
set_last_seen_on.up do
7+
# Set default value only for confirmed accounts
8+
execute <<-SQL
9+
UPDATE accounts
10+
SET last_seen_on =
11+
CASE
12+
WHEN confirmed_at is NULL
13+
THEN NULL
14+
WHEN login in ("Anonyme", "Collectif")
15+
THEN NULL
16+
ELSE
17+
CURRENT_DATE()
18+
END
19+
SQL
20+
end
21+
set_last_seen_on.down do
22+
# Column will be dropped nothing to do
23+
end
24+
end
25+
end
26+
end

db/schema.drawio

Lines changed: 541 additions & 1 deletion
Large diffs are not rendered by default.

db/schema.png

87 KB
Loading

db/schema.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 2023_02_25_172700) do
13+
ActiveRecord::Schema.define(version: 2023_05_06_121910) do
1414

1515
create_table "accounts", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci", force: :cascade do |t|
1616
t.integer "user_id"
@@ -38,6 +38,7 @@
3838
t.integer "min_karma", default: 20
3939
t.integer "max_karma", default: 20
4040
t.string "uploaded_stylesheet"
41+
t.date "last_seen_on"
4142
t.index ["confirmation_token"], name: "index_accounts_on_confirmation_token", unique: true
4243
t.index ["email"], name: "index_accounts_on_email", unique: true
4344
t.index ["login"], name: "index_accounts_on_login"

0 commit comments

Comments
 (0)