Create Profile model
This commit is contained in:
parent
2c7d7b7028
commit
ff410773dd
|
@ -0,0 +1,29 @@
|
||||||
|
class Profile < ApplicationRecord
|
||||||
|
belongs_to :user
|
||||||
|
|
||||||
|
attr_readonly :user_id
|
||||||
|
|
||||||
|
validates :display_name, length: { maximum: 32 }
|
||||||
|
validates :location, length: { maximum: 72 }
|
||||||
|
validates :description, length: { maximum: 256 }
|
||||||
|
|
||||||
|
before_save do
|
||||||
|
unless website.blank?
|
||||||
|
self.website = if website.match %r{\Ahttps?://}
|
||||||
|
website
|
||||||
|
else
|
||||||
|
"http://#{website}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def display_website
|
||||||
|
website.match(/https?:\/\/([A-Za-z.\-0-9]+)\/?(?:.*)/i)[1]
|
||||||
|
rescue NoMethodError
|
||||||
|
website
|
||||||
|
end
|
||||||
|
|
||||||
|
def safe_name
|
||||||
|
self.display_name.presence || self.screen_name
|
||||||
|
end
|
||||||
|
end
|
|
@ -45,6 +45,7 @@ class User < ApplicationRecord
|
||||||
has_many :subscriptions, dependent: :destroy
|
has_many :subscriptions, dependent: :destroy
|
||||||
has_many :totp_recovery_codes, dependent: :destroy
|
has_many :totp_recovery_codes, dependent: :destroy
|
||||||
|
|
||||||
|
has_one :profile, dependent: :destroy
|
||||||
has_one :theme, dependent: :destroy
|
has_one :theme, dependent: :destroy
|
||||||
|
|
||||||
SCREEN_NAME_REGEX = /\A[a-zA-Z0-9_]{1,16}\z/
|
SCREEN_NAME_REGEX = /\A[a-zA-Z0-9_]{1,16}\z/
|
||||||
|
@ -65,14 +66,6 @@ class User < ApplicationRecord
|
||||||
mount_uploader :profile_header, ProfileHeaderUploader, mount_on: :profile_header_file_name
|
mount_uploader :profile_header, ProfileHeaderUploader, mount_on: :profile_header_file_name
|
||||||
process_in_background :profile_header
|
process_in_background :profile_header
|
||||||
|
|
||||||
before_save do
|
|
||||||
self.website = if website.match %r{\Ahttps?://}
|
|
||||||
website
|
|
||||||
else
|
|
||||||
"http://#{website}"
|
|
||||||
end unless website.blank?
|
|
||||||
end
|
|
||||||
|
|
||||||
# when a user has been deleted, all reports relating to the user become invalid
|
# when a user has been deleted, all reports relating to the user become invalid
|
||||||
before_destroy do
|
before_destroy do
|
||||||
rep = Report.where(target_id: self.id, type: 'Reports::User')
|
rep = Report.where(target_id: self.id, type: 'Reports::User')
|
||||||
|
@ -169,12 +162,6 @@ class User < ApplicationRecord
|
||||||
comment.smiles.pluck(:user_id).include? self.id
|
comment.smiles.pluck(:user_id).include? self.id
|
||||||
end
|
end
|
||||||
|
|
||||||
def display_website
|
|
||||||
website.match(/https?:\/\/([A-Za-z.\-0-9]+)\/?(?:.*)/i)[1]
|
|
||||||
rescue NoMethodError
|
|
||||||
website
|
|
||||||
end
|
|
||||||
|
|
||||||
def comment(answer, content)
|
def comment(answer, content)
|
||||||
Comment.create!(user: self, answer: answer, content: content)
|
Comment.create!(user: self, answer: answer, content: content)
|
||||||
end
|
end
|
||||||
|
@ -253,10 +240,6 @@ class User < ApplicationRecord
|
||||||
!self.export_processing
|
!self.export_processing
|
||||||
end
|
end
|
||||||
|
|
||||||
def safe_name
|
|
||||||
self.display_name.presence || self.screen_name
|
|
||||||
end
|
|
||||||
|
|
||||||
# %w[admin moderator].each do |m|
|
# %w[admin moderator].each do |m|
|
||||||
# define_method(m) { raise "not allowed: #{m}" }
|
# define_method(m) { raise "not allowed: #{m}" }
|
||||||
# define_method(m+??) { raise "not allowed: #{m}?"}
|
# define_method(m+??) { raise "not allowed: #{m}?"}
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
class CreateProfiles < ActiveRecord::Migration[5.2]
|
||||||
|
def change
|
||||||
|
create_table :profiles do |t|
|
||||||
|
t.references :user, index: true, foreign_key: true
|
||||||
|
t.string :display_name, length: 32
|
||||||
|
t.string :description, length: 256
|
||||||
|
t.string :location, length: 72
|
||||||
|
t.string :website
|
||||||
|
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
|
||||||
|
User.find_each do |user|
|
||||||
|
Profile.create!(
|
||||||
|
user_id: user.id,
|
||||||
|
display_name: user.display_name,
|
||||||
|
description: user.bio,
|
||||||
|
location: user.location,
|
||||||
|
website: user.website,
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
14
db/schema.rb
14
db/schema.rb
|
@ -10,7 +10,7 @@
|
||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema.define(version: 2021_08_11_133004) do
|
ActiveRecord::Schema.define(version: 2021_12_19_153054) do
|
||||||
|
|
||||||
# These are extensions that must be enabled in order to support this database
|
# These are extensions that must be enabled in order to support this database
|
||||||
enable_extension "plpgsql"
|
enable_extension "plpgsql"
|
||||||
|
@ -116,6 +116,17 @@ ActiveRecord::Schema.define(version: 2021_08_11_133004) do
|
||||||
t.datetime "updated_at"
|
t.datetime "updated_at"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
create_table "profiles", force: :cascade do |t|
|
||||||
|
t.bigint "user_id"
|
||||||
|
t.string "display_name"
|
||||||
|
t.string "description"
|
||||||
|
t.string "location"
|
||||||
|
t.string "website"
|
||||||
|
t.datetime "created_at", null: false
|
||||||
|
t.datetime "updated_at", null: false
|
||||||
|
t.index ["user_id"], name: "index_profiles_on_user_id"
|
||||||
|
end
|
||||||
|
|
||||||
create_table "questions", id: :bigint, default: -> { "gen_timestamp_id('questions'::text)" }, force: :cascade do |t|
|
create_table "questions", id: :bigint, default: -> { "gen_timestamp_id('questions'::text)" }, force: :cascade do |t|
|
||||||
t.string "content"
|
t.string "content"
|
||||||
t.boolean "author_is_anonymous"
|
t.boolean "author_is_anonymous"
|
||||||
|
@ -296,4 +307,5 @@ ActiveRecord::Schema.define(version: 2021_08_11_133004) do
|
||||||
t.index ["user_id"], name: "index_users_roles_on_user_id"
|
t.index ["user_id"], name: "index_users_roles_on_user_id"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
add_foreign_key "profiles", "users"
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue