move profile_background from User to UserProfile

This commit is contained in:
Andrew Bezzub 2014-06-11 18:52:50 -07:00 committed by Robin Ward
parent 00910679ad
commit 386d1e231a
8 changed files with 53 additions and 18 deletions

View File

@ -357,7 +357,7 @@ class UsersController < ApplicationController
when "avatar"
upload_avatar_for(user, upload)
when "profile_background"
upload_profile_background_for(user, upload)
upload_profile_background_for(user.user_profile, upload)
end
else
render status: 422, text: upload.errors.full_messages
@ -384,8 +384,7 @@ class UsersController < ApplicationController
user = fetch_user_from_params
guardian.ensure_can_edit!(user)
user.profile_background = ""
user.save!
user.user_profile.clear_profile_background
render nothing: true
end
@ -429,8 +428,8 @@ class UsersController < ApplicationController
render json: { upload_id: upload.id, url: upload.url, width: upload.width, height: upload.height }
end
def upload_profile_background_for(user, upload)
user.upload_profile_background(upload)
def upload_profile_background_for(user_profile, upload)
user_profile.upload_profile_background(upload)
# TODO: add a resize job here
render json: { url: upload.url, width: upload.width, height: upload.height }

View File

@ -6,7 +6,7 @@ module Jobs
def execute(args)
return unless SiteSetting.clean_up_uploads?
uploads_used_as_profile_backgrounds = User.uniq.where("profile_background IS NOT NULL AND profile_background != ''").pluck(:profile_background)
uploads_used_as_profile_backgrounds = UserProfile.uniq.where("profile_background IS NOT NULL AND profile_background != ''").pluck(:profile_background)
grace_period = [SiteSetting.clean_orphan_uploads_grace_period_hours, 1].max

View File

@ -533,14 +533,6 @@ class User < ActiveRecord::Base
created_at > 1.day.ago
end
# TODO this is a MESS
# at most user table should have profile_background_upload_id
# best case is to move this to another table
def upload_profile_background(upload)
self.profile_background = upload.url
self.save!
end
def generate_api_key(created_by)
if api_key.present?
api_key.regenerate!(created_by)
@ -773,7 +765,6 @@ end
# mailing_list_mode :boolean default(FALSE), not null
# primary_group_id :integer
# locale :string(10)
# profile_background :string(255)
# registration_ip_address :inet
# last_redirected_to_top_at :datetime
# disable_jump_reply :boolean default(FALSE), not null

View File

@ -25,6 +25,16 @@ class UserProfile < ActiveRecord::Base
cook
end
def upload_profile_background(upload)
self.profile_background = upload.url
self.save!
end
def clear_profile_background
self.profile_background = ""
self.save!
end
private
def cook
@ -46,4 +56,5 @@ end
# bio_raw :text
# location :string(255)
# website :string(255)
# profile_background :string(255)
#

View File

@ -135,6 +135,13 @@ class UserSerializer < BasicUserSerializer
object.user_profile.bio_processed
end
def profile_background
object.user_profile.profile_background
end
def include_profile_background?
profile_background.present?
end
def stats
UserAction.stats(object.id, scope)
end

View File

@ -0,0 +1,17 @@
class MoveProfileBackgroundToUserProfiles < ActiveRecord::Migration
def up
add_column :user_profiles, :profile_background, :string, limit: 255
execute "UPDATE user_profiles SET profile_background = (SELECT profile_background FROM users WHERE user_profiles.user_id = users.id)"
remove_column :users, :profile_background
end
def down
add_column :users, :profile_background, :string, limit: 255
execute "UPDATE users SET profile_background = (SELECT profile_background FROM user_profiles WHERE user_profiles.user_id = users.id)"
remove_column :user_profiles, :profile_background
end
end

View File

@ -1169,7 +1169,7 @@ describe UsersController do
xhr :post, :upload_user_image, username: user.username, file: user_image, user_image_type: "profile_background"
user.reload
user.profile_background.should == "/uploads/default/1/1234567890123456.png"
user.user_profile.profile_background.should == "/uploads/default/1/1234567890123456.png"
# returns the url, width and height of the uploaded image
json = JSON.parse(response.body)
@ -1218,7 +1218,7 @@ describe UsersController do
Upload.expects(:create_for).returns(upload)
xhr :post, :upload_user_image, username: user.username, file: user_image_url, user_image_type: "profile_background"
user.reload
user.profile_background.should == "/uploads/default/1/1234567890123456.png"
user.user_profile.profile_background.should == "/uploads/default/1/1234567890123456.png"
# returns the url, width and height of the uploaded image
json = JSON.parse(response.body)
@ -1287,7 +1287,7 @@ describe UsersController do
it 'it successful' do
xhr :put, :clear_profile_background, username: user.username
user.reload.profile_background.should == ""
user.reload.user_profile.profile_background.should == ""
response.should be_success
end

View File

@ -32,6 +32,16 @@ describe UserSerializer do
end
end
context "with filled out profile background" do
before do
user.user_profile.profile_background = 'http://background.com'
end
it "has a profile background" do
expect(json[:profile_background]).to eq 'http://background.com'
end
end
context "with filled out website" do
before do
user.user_profile.website = 'http://example.com'