mirror of
https://github.com/Retrospring/retrospring.git
synced 2024-11-20 14:19:53 +01:00
Create Profile model
This commit is contained in:
parent
2c7d7b7028
commit
ff410773dd
4 changed files with 66 additions and 19 deletions
29
app/models/profile.rb
Normal file
29
app/models/profile.rb
Normal file
|
@ -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 :totp_recovery_codes, dependent: :destroy
|
||||
|
||||
has_one :profile, dependent: :destroy
|
||||
has_one :theme, dependent: :destroy
|
||||
|
||||
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
|
||||
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
|
||||
before_destroy do
|
||||
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
|
||||
end
|
||||
|
||||
def display_website
|
||||
website.match(/https?:\/\/([A-Za-z.\-0-9]+)\/?(?:.*)/i)[1]
|
||||
rescue NoMethodError
|
||||
website
|
||||
end
|
||||
|
||||
def comment(answer, content)
|
||||
Comment.create!(user: self, answer: answer, content: content)
|
||||
end
|
||||
|
@ -253,10 +240,6 @@ class User < ApplicationRecord
|
|||
!self.export_processing
|
||||
end
|
||||
|
||||
def safe_name
|
||||
self.display_name.presence || self.screen_name
|
||||
end
|
||||
|
||||
# %w[admin moderator].each do |m|
|
||||
# define_method(m) { raise "not allowed: #{m}" }
|
||||
# define_method(m+??) { raise "not allowed: #{m}?"}
|
||||
|
|
23
db/migrate/20211219153054_create_profiles.rb
Normal file
23
db/migrate/20211219153054_create_profiles.rb
Normal file
|
@ -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.
|
||||
|
||||
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
|
||||
enable_extension "plpgsql"
|
||||
|
@ -116,6 +116,17 @@ ActiveRecord::Schema.define(version: 2021_08_11_133004) do
|
|||
t.datetime "updated_at"
|
||||
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|
|
||||
t.string "content"
|
||||
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"
|
||||
end
|
||||
|
||||
add_foreign_key "profiles", "users"
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue