2014-05-27 12:54:04 -05:00
|
|
|
class UserProfile < ActiveRecord::Base
|
2014-06-10 00:19:08 -05:00
|
|
|
belongs_to :user, inverse_of: :user_profile
|
|
|
|
|
2014-09-08 14:17:31 -05:00
|
|
|
validates :bio_raw, length: { maximum: 3000 }
|
2014-06-10 00:19:08 -05:00
|
|
|
validates :user, presence: true
|
|
|
|
before_save :cook
|
2014-07-22 20:42:24 -05:00
|
|
|
after_save :trigger_badges
|
2014-06-10 00:19:08 -05:00
|
|
|
|
2014-08-05 01:37:56 -05:00
|
|
|
BAKED_VERSION = 1
|
|
|
|
|
2014-06-10 00:19:08 -05:00
|
|
|
def bio_excerpt
|
|
|
|
excerpt = PrettyText.excerpt(bio_cooked, 350)
|
2014-09-05 00:20:39 -05:00
|
|
|
return excerpt if excerpt.blank? || user.has_trust_level?(TrustLevel[1])
|
2014-06-10 00:19:08 -05:00
|
|
|
PrettyText.strip_links(excerpt)
|
|
|
|
end
|
|
|
|
|
|
|
|
def bio_processed
|
2014-09-05 00:20:39 -05:00
|
|
|
return bio_cooked if bio_cooked.blank? || user.has_trust_level?(TrustLevel[1])
|
2014-06-10 00:19:08 -05:00
|
|
|
PrettyText.strip_links(bio_cooked)
|
|
|
|
end
|
|
|
|
|
|
|
|
def bio_summary
|
|
|
|
return nil unless bio_cooked.present?
|
|
|
|
Summarize.new(bio_cooked).summary
|
|
|
|
end
|
|
|
|
|
|
|
|
def recook_bio
|
|
|
|
self.bio_raw_will_change!
|
|
|
|
cook
|
|
|
|
end
|
|
|
|
|
2014-10-16 14:05:36 -05:00
|
|
|
def upload_expansion_background(upload)
|
|
|
|
self.expansion_background = upload.url
|
|
|
|
self.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
def clear_expansion_background
|
|
|
|
self.expansion_background = ""
|
|
|
|
self.save!
|
|
|
|
end
|
|
|
|
|
2014-06-11 20:52:50 -05:00
|
|
|
def upload_profile_background(upload)
|
|
|
|
self.profile_background = upload.url
|
|
|
|
self.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
def clear_profile_background
|
|
|
|
self.profile_background = ""
|
|
|
|
self.save!
|
|
|
|
end
|
|
|
|
|
2014-08-05 01:37:56 -05:00
|
|
|
def self.rebake_old(limit)
|
|
|
|
problems = []
|
|
|
|
UserProfile.where('bio_cooked_version IS NULL OR bio_cooked_version < ?', BAKED_VERSION)
|
|
|
|
.limit(limit).each do |p|
|
|
|
|
begin
|
|
|
|
p.rebake!
|
|
|
|
rescue => e
|
|
|
|
problems << {profile: p, ex: e}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
problems
|
|
|
|
end
|
|
|
|
|
|
|
|
def rebake!
|
|
|
|
update_columns(bio_cooked: cooked, bio_cooked_version: BAKED_VERSION)
|
|
|
|
end
|
|
|
|
|
2014-07-03 02:29:44 -05:00
|
|
|
protected
|
|
|
|
|
2014-07-22 20:42:24 -05:00
|
|
|
def trigger_badges
|
|
|
|
BadgeGranter.queue_badge_grant(Badge::Trigger::UserChange, user: self)
|
2014-07-03 02:29:44 -05:00
|
|
|
end
|
|
|
|
|
2014-06-10 00:19:08 -05:00
|
|
|
private
|
|
|
|
|
2014-08-05 01:37:56 -05:00
|
|
|
def cooked
|
|
|
|
if self.bio_raw.present?
|
2014-09-05 00:20:39 -05:00
|
|
|
PrettyText.cook(self.bio_raw, omit_nofollow: user.has_trust_level?(TrustLevel[3]) && !SiteSetting.tl3_links_no_follow)
|
2014-08-05 01:37:56 -05:00
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-10 00:19:08 -05:00
|
|
|
def cook
|
|
|
|
if self.bio_raw.present?
|
2014-08-05 01:37:56 -05:00
|
|
|
if bio_raw_changed?
|
|
|
|
self.bio_cooked = cooked
|
|
|
|
self.bio_cooked_version = BAKED_VERSION
|
|
|
|
end
|
2014-06-10 00:19:08 -05:00
|
|
|
else
|
|
|
|
self.bio_cooked = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-27 12:54:04 -05:00
|
|
|
end
|
2014-05-28 23:59:14 -05:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: user_profiles
|
|
|
|
#
|
2014-07-03 02:29:44 -05:00
|
|
|
# user_id :integer not null, primary key
|
|
|
|
# location :string(255)
|
|
|
|
# website :string(255)
|
|
|
|
# bio_raw :text
|
|
|
|
# bio_cooked :text
|
|
|
|
# dismissed_banner_key :integer
|
|
|
|
# profile_background :string(255)
|
2014-10-16 14:05:36 -05:00
|
|
|
# expansion_background :string(255)
|
2014-08-06 22:33:11 -05:00
|
|
|
# bio_cooked_version :integer
|
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_user_profiles_on_bio_cooked_version (bio_cooked_version)
|
2014-05-28 23:59:14 -05:00
|
|
|
#
|