From 7db31adf35b3943a0749103b2b69f589baf06cdf Mon Sep 17 00:00:00 2001 From: Andrew Bezzub Date: Fri, 6 Jun 2014 21:54:32 -0700 Subject: [PATCH] move website from User to UserProfile --- app/models/user.rb | 1 - app/models/user_profile.rb | 1 + app/serializers/user_serializer.rb | 7 +++++++ app/services/user_updater.rb | 4 ++-- ...140607035234_add_website_to_user_profiles.rb | 17 +++++++++++++++++ spec/services/user_updater_spec.rb | 4 ++-- 6 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 db/migrate/20140607035234_add_website_to_user_profiles.rb diff --git a/app/models/user.rb b/app/models/user.rb index 8ef6111c1ae..a1cbfa42fc4 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -770,7 +770,6 @@ end # username_lower :string(60) not null # auth_token :string(32) # last_seen_at :datetime -# website :string(255) # admin :boolean default(FALSE), not null # last_emailed_at :datetime # email_digests :boolean not null diff --git a/app/models/user_profile.rb b/app/models/user_profile.rb index e762563147c..35bd8459211 100644 --- a/app/models/user_profile.rb +++ b/app/models/user_profile.rb @@ -7,4 +7,5 @@ end # # user_id :integer not null, primary key # location :string(255) +# website :string(255) # diff --git a/app/serializers/user_serializer.rb b/app/serializers/user_serializer.rb index d9eb48a831b..f6b23e21e19 100644 --- a/app/serializers/user_serializer.rb +++ b/app/serializers/user_serializer.rb @@ -116,6 +116,13 @@ class UserSerializer < BasicUserSerializer location.present? end + def website + object.user_profile.try(:website) + end + def include_website? + website.present? + end + def stats UserAction.stats(object.id, scope) end diff --git a/app/services/user_updater.rb b/app/services/user_updater.rb index aa27f5dcdf7..e6c90a390b8 100644 --- a/app/services/user_updater.rb +++ b/app/services/user_updater.rb @@ -28,7 +28,8 @@ class UserUpdater end def update(attributes = {}) - user.website = format_url(attributes.fetch(:website) { user.website }) + user_profile = user.user_profile + user_profile.website = format_url(attributes.fetch(:website) { user_profile.website }) user.bio_raw = attributes.fetch(:bio_raw) { user.bio_raw } user.name = attributes.fetch(:name) { user.name } @@ -59,7 +60,6 @@ class UserUpdater end end - user_profile = user.user_profile PROFILE_ATTR.each do |attribute| user_profile.send("#{attribute.to_s}=", attributes[attribute]) end diff --git a/db/migrate/20140607035234_add_website_to_user_profiles.rb b/db/migrate/20140607035234_add_website_to_user_profiles.rb new file mode 100644 index 00000000000..4194dcf1393 --- /dev/null +++ b/db/migrate/20140607035234_add_website_to_user_profiles.rb @@ -0,0 +1,17 @@ +class AddWebsiteToUserProfiles < ActiveRecord::Migration + def up + add_column :user_profiles, :website, :string + + execute "UPDATE user_profiles SET website = (SELECT website FROM users where user_profiles.user_id = users.id)" + + remove_column :users, :website + end + + def down + add_column :users, :website, :string + + execute "UPDATE users SET website = (SELECT website FROM user_profiles where user_profiles.user_id = users.id)" + + remove_column :user_profiles, :website + end +end diff --git a/spec/services/user_updater_spec.rb b/spec/services/user_updater_spec.rb index b6b7bf1a48c..f43ef1f4453 100644 --- a/spec/services/user_updater_spec.rb +++ b/spec/services/user_updater_spec.rb @@ -68,7 +68,7 @@ describe UserUpdater do updater.update(website: 'http://example.com') - expect(user.reload.website).to eq 'http://example.com' + expect(user.reload.user_profile.website).to eq 'http://example.com' end end @@ -79,7 +79,7 @@ describe UserUpdater do updater.update(website: 'example.com') - expect(user.reload.website).to eq 'http://example.com' + expect(user.reload.user_profile.website).to eq 'http://example.com' end end end