From 386d1e231a07acf5ab105077ff3e0aa55ad046aa Mon Sep 17 00:00:00 2001 From: Andrew Bezzub Date: Wed, 11 Jun 2014 18:52:50 -0700 Subject: [PATCH] move profile_background from User to UserProfile --- app/controllers/users_controller.rb | 9 ++++----- app/jobs/scheduled/clean_up_uploads.rb | 2 +- app/models/user.rb | 9 --------- app/models/user_profile.rb | 11 +++++++++++ app/serializers/user_serializer.rb | 7 +++++++ ..._move_profile_background_to_user_profiles.rb | 17 +++++++++++++++++ spec/controllers/users_controller_spec.rb | 6 +++--- spec/serializers/user_serializer_spec.rb | 10 ++++++++++ 8 files changed, 53 insertions(+), 18 deletions(-) create mode 100644 db/migrate/20140612010718_move_profile_background_to_user_profiles.rb diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 1f659c62211..0c2bb9a00d7 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -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 } diff --git a/app/jobs/scheduled/clean_up_uploads.rb b/app/jobs/scheduled/clean_up_uploads.rb index 48150bbec14..71965607705 100644 --- a/app/jobs/scheduled/clean_up_uploads.rb +++ b/app/jobs/scheduled/clean_up_uploads.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index bf2b4b10a53..e71d6988a70 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/app/models/user_profile.rb b/app/models/user_profile.rb index b4e3732cce2..1cc3c48bfdc 100644 --- a/app/models/user_profile.rb +++ b/app/models/user_profile.rb @@ -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) # diff --git a/app/serializers/user_serializer.rb b/app/serializers/user_serializer.rb index 31f52ff9586..279b76b9d8c 100644 --- a/app/serializers/user_serializer.rb +++ b/app/serializers/user_serializer.rb @@ -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 diff --git a/db/migrate/20140612010718_move_profile_background_to_user_profiles.rb b/db/migrate/20140612010718_move_profile_background_to_user_profiles.rb new file mode 100644 index 00000000000..d654ab19a29 --- /dev/null +++ b/db/migrate/20140612010718_move_profile_background_to_user_profiles.rb @@ -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 diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index b5cf1dc822b..f9cd9e78c07 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -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 diff --git a/spec/serializers/user_serializer_spec.rb b/spec/serializers/user_serializer_spec.rb index f6e55bc417e..200c2c4939b 100644 --- a/spec/serializers/user_serializer_spec.rb +++ b/spec/serializers/user_serializer_spec.rb @@ -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'