users: set default settings in ruby instead of in database.

Specify the default settings for new users inside the User model instead
of inside the database. This makes it easier to change defaults, and it
makes the code clearer.
This commit is contained in:
evazion
2021-01-14 20:01:23 -06:00
parent 99d447279b
commit 4719a5ed1c
6 changed files with 71 additions and 27 deletions

View File

@@ -63,6 +63,7 @@ class UsersController < ApplicationController
@user = authorize User.new(
last_ip_addr: CurrentUser.ip_addr,
last_logged_in_at: Time.zone.now,
requires_verification: user_verifier.requires_verification?,
level: user_verifier.initial_level,
name: params[:user][:name],

View File

@@ -69,7 +69,36 @@ class User < ApplicationRecord
is_verified
)
DEFAULT_BLACKLIST = ["spoilers", "guro", "scat", "furry -rating:s"].join("\n")
attribute :id
attribute :created_at
attribute :updated_at
attribute :name
attribute :level, default: Levels::MEMBER
attribute :bcrypt_password_hash
attribute :inviter_id
attribute :last_logged_in_at, default: -> { Time.zone.now }
attribute :last_forum_read_at, default: "1960-01-01 00:00:00"
attribute :last_ip_addr
attribute :comment_threshold, default: 0
attribute :default_image_size, default: "large"
attribute :favorite_tags
attribute :blacklisted_tags, default: DEFAULT_BLACKLIST
attribute :time_zone, default: "Eastern Time (US & Canada)"
attribute :custom_style
attribute :post_upload_count, default: 0
attribute :post_update_count, default: 0
attribute :note_update_count, default: 0
attribute :unread_dmail_count, default: 0
attribute :favorite_count, default: 0
attribute :per_page, default: 20
attribute :theme, default: :light
attribute :upload_points, default: 1000
attribute :bit_prefs, default: 0
has_bit_flags BOOLEAN_ATTRIBUTES, :field => "bit_prefs"
enum theme: { light: 0, dark: 100 }, _suffix: true
attr_reader :password
@@ -122,7 +151,6 @@ class User < ApplicationRecord
belongs_to :inviter, class_name: "User", optional: true
accepts_nested_attributes_for :email_address, reject_if: :all_blank, allow_destroy: true
enum theme: { light: 0, dark: 100 }, _suffix: true
# UserDeletion#rename renames deleted users to `user_<1234>~`. Tildes
# are appended if the username is taken.

View File

@@ -0,0 +1,22 @@
class RemoveDefaultsFromUsers < ActiveRecord::Migration[6.1]
def change
change_table(:users) do |t|
t.change_default :level, from: 20, to: nil
t.change_default :last_logged_in_at, from: "now()", to: nil
t.change_default :last_forum_read_at, from: "1960-01-01 00:00:00", to: nil
t.change_default :comment_threshold, from: 0, to: nil
t.change_default :default_image_size, from: "large", to: nil
t.change_default :blacklisted_tags, from: "spoilers\nguro\nscat\nfurry -rating:s", to: nil
t.change_default :time_zone, from: "Eastern Time (US & Canada)", to: nil
t.change_default :post_update_count, from: 0, to: nil
t.change_default :note_update_count, from: 0, to: nil
t.change_default :favorite_count, from: 0, to: nil
t.change_default :post_upload_count, from: 0, to: nil
t.change_default :per_page, from: 20, to: nil
t.change_default :bit_prefs, from: 0, to: nil
t.change_default :unread_dmail_count, from: 0, to: nil
t.change_default :theme, from: 0, to: nil
t.change_default :upload_points, from: 1000, to: nil
end
end
end

View File

@@ -2236,32 +2236,29 @@ ALTER TABLE ONLY public.posts ALTER COLUMN tag_index SET STATISTICS 2000;
CREATE TABLE public.users (
id integer NOT NULL,
name character varying NOT NULL,
level integer DEFAULT 20 NOT NULL,
level integer NOT NULL,
inviter_id integer,
created_at timestamp without time zone NOT NULL,
last_logged_in_at timestamp without time zone DEFAULT now(),
last_forum_read_at timestamp without time zone DEFAULT '1960-01-01 00:00:00'::timestamp without time zone,
comment_threshold integer DEFAULT 0 NOT NULL,
last_logged_in_at timestamp without time zone,
last_forum_read_at timestamp without time zone,
comment_threshold integer NOT NULL,
updated_at timestamp without time zone,
default_image_size character varying DEFAULT 'large'::character varying NOT NULL,
default_image_size character varying NOT NULL,
favorite_tags text,
blacklisted_tags text DEFAULT 'spoilers
guro
scat
furry -rating:s'::text,
time_zone character varying DEFAULT 'Eastern Time (US & Canada)'::character varying NOT NULL,
post_update_count integer DEFAULT 0 NOT NULL,
note_update_count integer DEFAULT 0 NOT NULL,
favorite_count integer DEFAULT 0 NOT NULL,
post_upload_count integer DEFAULT 0 NOT NULL,
blacklisted_tags text,
time_zone character varying NOT NULL,
post_update_count integer NOT NULL,
note_update_count integer NOT NULL,
favorite_count integer NOT NULL,
post_upload_count integer NOT NULL,
bcrypt_password_hash text,
per_page integer DEFAULT 20 NOT NULL,
per_page integer NOT NULL,
custom_style text,
bit_prefs bigint DEFAULT 0 NOT NULL,
bit_prefs bigint NOT NULL,
last_ip_addr inet,
unread_dmail_count integer DEFAULT 0 NOT NULL,
theme integer DEFAULT 0 NOT NULL,
upload_points integer DEFAULT 1000 NOT NULL
unread_dmail_count integer NOT NULL,
theme integer NOT NULL,
upload_points integer NOT NULL
);
@@ -7885,6 +7882,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20210108030723'),
('20210108030724'),
('20210110015410'),
('20210110090656');
('20210110090656'),
('20210115015308');

View File

@@ -2,13 +2,8 @@ FactoryBot.define do
factory(:user, aliases: [:creator, :updater]) do
name { SecureRandom.uuid }
password {"password"}
default_image_size {"large"}
level {20}
created_at {Time.now}
last_logged_in_at {Time.now}
favorite_count {0}
bit_prefs {0}
last_forum_read_at {nil}
factory(:banned_user) do
transient { ban_duration {3} }

View File

@@ -72,7 +72,7 @@ class ForumTopicsControllerTest < ActionDispatch::IntegrationTest
should "not record a topic visit for non-html requests" do
get_auth forum_topic_path(@forum_topic), @user, params: {format: :json}
@user.reload
assert_nil(@user.last_forum_read_at)
assert_equal(Time.zone.parse("1960-01-01"), @user.last_forum_read_at)
end
should "render for atom feed" do