FEATURE: Add a location field to a user's profile

This commit is contained in:
Robin Ward 2014-05-27 13:54:04 -04:00
parent 1efa113bea
commit 7c22d738b6
10 changed files with 64 additions and 2 deletions

View File

@ -191,6 +191,7 @@ Discourse.User = Discourse.Model.extend({
var data = this.getProperties('auto_track_topics_after_msecs',
'bio_raw',
'website',
'location',
'name',
'locale',
'email_digests',

View File

@ -127,10 +127,17 @@
</div>
</div>
<div class="control-group">
<label class="control-label">{{i18n user.location}}</label>
<div class="controls">
{{input type="text" value=location class="input-xxlarge"}}
</div>
</div>
<div class="control-group">
<label class="control-label">{{i18n user.website}}</label>
<div class="controls">
{{textField value=website classNames="input-xxlarge"}}
{{input type="text" value=website class="input-xxlarge"}}
</div>
</div>

View File

@ -86,6 +86,9 @@
<div class='secondary'>
<dl>
{{#if location}}
<dt>{{i18n user.location}}</dt><dd>{{location}}</a></dd>
{{/if}}
{{#if websiteName}}
<dt>{{i18n user.website}}</dt><dd><a {{bind-attr href="website"}} target="_blank">{{websiteName}}</a></dd>
{{/if}}

View File

@ -39,6 +39,7 @@ class User < ActiveRecord::Base
has_one :github_user_info, dependent: :destroy
has_one :oauth2_user_info, dependent: :destroy
has_one :user_stat, dependent: :destroy
has_one :user_profile, dependent: :destroy
has_one :single_sign_on_record, dependent: :destroy
belongs_to :approved_by, class_name: 'User'
belongs_to :primary_group, class_name: 'Group'
@ -73,6 +74,7 @@ class User < ActiveRecord::Base
after_create :create_email_token
after_create :create_user_stat
after_create :create_user_profile
after_save :refresh_avatar
before_destroy do
@ -656,6 +658,10 @@ class User < ActiveRecord::Base
end
end
def create_user_profile
UserProfile.create(user_id: id)
end
def create_user_stat
stat = UserStat.new(new_since: Time.now)
stat.user_id = id

View File

@ -0,0 +1,2 @@
class UserProfile < ActiveRecord::Base
end

View File

@ -9,6 +9,7 @@ class UserSerializer < BasicUserSerializer
:created_at,
:website,
:profile_background,
:location,
:can_edit,
:can_edit_username,
:can_edit_email,
@ -113,6 +114,13 @@ class UserSerializer < BasicUserSerializer
scope.can_edit_name?(object)
end
def location
object.user_profile.try(:location)
end
def include_location?
location.present?
end
def stats
UserAction.stats(object.id, scope)
end

View File

@ -18,6 +18,10 @@ class UserUpdater
:disable_jump_reply
]
PROFILE_ATTR = [
:location
]
def initialize(actor, user)
@user = user
@guardian = Guardian.new(actor)
@ -55,7 +59,15 @@ class UserUpdater
end
end
user.save
user_profile = user.user_profile
PROFILE_ATTR.each do |attribute|
user_profile.send("#{attribute.to_s}=", attributes[attribute])
end
User.transaction do
user_profile.save
user.save
end
end
private

View File

@ -358,6 +358,7 @@ en:
last_seen: "Seen"
created: "Joined"
log_out: "Log Out"
location: "Location"
website: "Web Site"
email_settings: "Email"
email_digests:

View File

@ -0,0 +1,14 @@
class CreateUserProfiles < ActiveRecord::Migration
def up
create_table :user_profiles, id: false do |t|
t.references :user
t.string :location
end
execute "ALTER TABLE user_profiles ADD PRIMARY KEY (user_id)"
execute "INSERT INTO user_profiles (user_id) SELECT id FROM users"
end
def down
drop_table :user_profiles
end
end

View File

@ -0,0 +1,8 @@
require 'spec_helper'
describe UserProfile do
it "is created automatically when a user is created" do
user = Fabricate(:evil_trout)
user.user_profile.should be_present
end
end