FEATURE: custom fields on User

This commit is contained in:
Sam
2014-04-22 13:52:13 +10:00
parent 25860622b7
commit a3b2b4baca
9 changed files with 139 additions and 15 deletions

View File

@ -8,10 +8,10 @@ end
#
# Table name: badge_types
#
# id :integer not null, primary key
# name :string(255) not null
# created_at :datetime
# updated_at :datetime
# id :integer not null, primary key
# name :string(255) not null
# created_at :datetime
# updated_at :datetime
#
# Indexes
#

View File

@ -60,6 +60,10 @@ class DiscourseSingleSignOn < SingleSignOn
user.enqueue_welcome_message('welcome_user')
end
custom_fields.each do |k,v|
user.custom_fields[k] = v
end
# optionally save the user and sso_record if they have changed
user.save!
sso_record.save!

View File

@ -32,6 +32,7 @@ class User < ActiveRecord::Base
has_many :invites, dependent: :destroy
has_many :topic_links, dependent: :destroy
has_many :uploads
has_many :user_custom_fields, dependent: :destroy
has_one :facebook_user_info, dependent: :destroy
has_one :twitter_user_info, dependent: :destroy
@ -68,6 +69,7 @@ class User < ActiveRecord::Base
after_save :update_tracked_topics
after_save :clear_global_notice_if_needed
after_save :save_custom_fields
after_create :create_email_token
after_create :create_user_stat
@ -587,8 +589,35 @@ class User < ActiveRecord::Base
nil
end
def custom_fields
@custom_fields ||= begin
@custom_fields_orig = Hash[*user_custom_fields.pluck(:name,:value).flatten]
@custom_fields_orig.dup
end
end
protected
def save_custom_fields
if @custom_fields && @custom_fields_orig != @custom_fields
dup = @custom_fields.dup
user_custom_fields.each do |f|
if dup[f.name] != f.value
f.destroy
else
dup.remove[f.name]
end
end
dup.each do |k,v|
user_custom_fields.create(name: k, value: v)
end
@custom_fields_orig = @custom_fields
end
end
def cook
if bio_raw.present?
self.bio_cooked = PrettyText.cook(bio_raw, omit_nofollow: self.has_trust_level?(:leader)) if bio_raw_changed?

View File

@ -0,0 +1,19 @@
class UserCustomField < ActiveRecord::Base
belongs_to :user
end
# == Schema Information
#
# Table name: user_custom_fields
#
# id :integer not null, primary key
# user_id :integer not null
# name :string(256) not null
# value :text
# created_at :datetime
# updated_at :datetime
#
# Indexes
#
# index_user_custom_fields_on_user_id_and_name (user_id,name)
#