docbrown/app/models/profile.rb
Daniel Uber c842d1a0c6
Rename table_exists? to table_available? to indicate behavior (#13426)
We added a check for ConnectionNotEstablished, which causes the check
to return false if there is no database available (the case in
buildkite for rake tasks like assets:precompile), this changed the
implementation from an existence check (I can connect to the database,
but the table is not present, the situation when we have created a db,
but not yet populated the schema) to an availability check (either the
db is live but the table is absent, or the db is absent, in either
case we know we can't read from the table yet).

Change table_exists? to table_available? to indicate the new
behavior and update the two call sites.
2021-04-16 09:53:01 -05:00

84 lines
2.1 KiB
Ruby

class Profile < ApplicationRecord
belongs_to :user
validates :data, presence: true
validates :user_id, uniqueness: true
validates_with ProfileValidator
has_many :custom_profile_fields, dependent: :destroy
store_attribute :data, :custom_attributes, :json, default: {}
SPECIAL_DISPLAY_ATTRIBUTES = %w[
summary
employment_title
employer_name
employer_url
location
].freeze
SPECIAL_SOCIAL_LINK_ATTRIBUTES = %w[
twitter_url
github_url
facebook_url
linkedin_url
youtube_url
instagram_url
behance_url
medium_url
stackoverflow_url
gitlab_url
twitch_url
mastodon_url
website_url
dribbble_url
].freeze
# NOTE: @citizen428 This is a temporary mapping so we don't break DEV during
# profile migration/generalization work.
MAPPED_ATTRIBUTES = {
brand_color1: :bg_color_hex,
brand_color2: :text_color_hex,
display_email_on_profile: :email_public,
education: :education,
git_lab_url: :gitlab_url,
linked_in_url: :linkedin_url,
skills_languages: :mostly_work_with,
stack_overflow_url: :stackoverflow_url
}.with_indifferent_access.freeze
# Generates typed accessors for all currently defined profile fields.
def self.refresh_attributes!
return if ENV["ENV_AVAILABLE"] == "false"
return unless Database.table_available?("profiles")
ProfileField.find_each do |field|
store_attribute :data, field.attribute_name.to_sym, field.type
end
end
# Set up all profile attributes when this class loads so all store_attribute
# accessors get defined immediately.
refresh_attributes!
# Returns an array of all currently defined `store_attribute`s on `data`.
def self.attributes
(stored_attributes[:data] || []).map(&:to_s)
end
def self.special_attributes
SPECIAL_DISPLAY_ATTRIBUTES + SPECIAL_SOCIAL_LINK_ATTRIBUTES
end
def self.special_social_link_attributes
SPECIAL_SOCIAL_LINK_ATTRIBUTES.freeze
end
def custom_profile_attributes
custom_profile_fields.pluck(:attribute_name)
end
def clear!
update(data: {})
end
end