From 40e8482825187b61538ebedaf8431c7713f54138 Mon Sep 17 00:00:00 2001
From: Andy Allan
Date: Wed, 22 Dec 2021 14:48:07 +0000
Subject: [PATCH] Alias the user creation_time column
This allows rails to set the created_at automatically, and so avoids
us from having to do so in a callback. It also hides the unusual
db column name from the rest of the app.
---
app/models/user.rb | 11 ++++-------
app/views/api/users/_user.json.jbuilder | 2 +-
app/views/api/users/_user.xml.builder | 2 +-
app/views/users/_user.html.erb | 4 ++--
app/views/users/show.html.erb | 2 +-
5 files changed, 9 insertions(+), 12 deletions(-)
diff --git a/app/models/user.rb b/app/models/user.rb
index 8b8f31676..8f02e35cf 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -114,7 +114,8 @@ class User < ApplicationRecord
validates_email_format_of :email, :if => proc { |u| u.email_changed? }
validates_email_format_of :new_email, :allow_blank => true, :if => proc { |u| u.new_email_changed? }
- after_initialize :set_defaults
+ alias_attribute :created_at, :creation_time
+
before_save :encrypt_password
before_save :update_tile
after_save :spam_check
@@ -308,7 +309,7 @@ class User < ApplicationRecord
end
def max_messages_per_hour
- account_age_in_seconds = Time.now.utc - creation_time
+ account_age_in_seconds = Time.now.utc - created_at
account_age_in_hours = account_age_in_seconds / 3600
recent_messages = messages.where("sent_on >= ?", Time.now.utc - 3600).count
active_reports = issues.with_status(:open).sum(:reports_count)
@@ -317,7 +318,7 @@ class User < ApplicationRecord
end
def max_friends_per_hour
- account_age_in_seconds = Time.now.utc - creation_time
+ account_age_in_seconds = Time.now.utc - created_at
account_age_in_hours = account_age_in_seconds / 3600
recent_friends = Friendship.where(:befriendee => self).where("created_at >= ?", Time.now.utc - 3600).count
active_reports = issues.with_status(:open).sum(:reports_count)
@@ -327,10 +328,6 @@ class User < ApplicationRecord
private
- def set_defaults
- self.creation_time = Time.now.getutc unless attribute_present?(:creation_time)
- end
-
def encrypt_password
if pass_crypt_confirmation
self.pass_crypt, self.pass_salt = PasswordHash.create(pass_crypt)
diff --git a/app/views/api/users/_user.json.jbuilder b/app/views/api/users/_user.json.jbuilder
index 7659e4e11..15f0685ac 100644
--- a/app/views/api/users/_user.json.jbuilder
+++ b/app/views/api/users/_user.json.jbuilder
@@ -1,7 +1,7 @@
json.user do
json.id user.id
json.display_name user.display_name
- json.account_created user.creation_time.xmlschema
+ json.account_created user.created_at.xmlschema
json.description user.description if user.description
if current_user && current_user == user && can?(:details, User)
diff --git a/app/views/api/users/_user.xml.builder b/app/views/api/users/_user.xml.builder
index 7d6b177f2..1791c60ef 100644
--- a/app/views/api/users/_user.xml.builder
+++ b/app/views/api/users/_user.xml.builder
@@ -1,6 +1,6 @@
xml.tag! "user", :id => user.id,
:display_name => user.display_name,
- :account_created => user.creation_time.xmlschema do
+ :account_created => user.created_at.xmlschema do
xml.tag! "description", user.description if user.description
if current_user && current_user == user && can?(:details, User)
xml.tag! "contributor-terms", :agreed => user.terms_agreed.present?,
diff --git a/app/views/users/_user.html.erb b/app/views/users/_user.html.erb
index f01f8d1ed..e419aed1d 100644
--- a/app/views/users/_user.html.erb
+++ b/app/views/users/_user.html.erb
@@ -8,11 +8,11 @@
<%= t "users.index.summary_html",
:name => link_to(user.display_name, user_path(user)),
:ip_address => link_to(user.creation_ip, :ip => user.creation_ip),
- :date => l(user.creation_time, :format => :friendly) %>
+ :date => l(user.created_at, :format => :friendly) %>
<% else %>
<%= t "users.index.summary_no_ip_html",
:name => link_to(user.display_name, user_path(user)),
- :date => l(user.creation_time, :format => :friendly) %>
+ :date => l(user.created_at, :format => :friendly) %>
<% end %>
<%= user.description.to_html %>
diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb
index b55e61f26..95878402b 100644
--- a/app/views/users/show.html.erb
+++ b/app/views/users/show.html.erb
@@ -120,7 +120,7 @@
- <%= t ".mapper since" %>
- - <%= l @user.creation_time.to_date, :format => :long %>
+ - <%= l @user.created_at.to_date, :format => :long %>
<% unless @user.terms_agreed %>
- <%= t ".ct status" %>
-
--
2.39.5