Update remaining references to profile fields

This commit is contained in:
Karina Kwiatek 2021-12-19 23:51:06 +01:00
parent ab720f3f55
commit 00496a1596
10 changed files with 53 additions and 37 deletions

View File

@ -14,11 +14,11 @@ module ApplicationHelper::GraphMethods
# @param user [User] # @param user [User]
def user_opengraph(user) def user_opengraph(user)
opengraph_meta_tags({ opengraph_meta_tags({
'og:title': user.safe_name, 'og:title': user.profile.safe_name,
'og:type': 'profile', 'og:type': 'profile',
'og:image': full_profile_picture_url(user), 'og:image': full_profile_picture_url(user),
'og:url': show_user_profile_url(user.screen_name), 'og:url': show_user_profile_url(user.screen_name),
'og:description': user.bio, 'og:description': user.profile.description,
'og:site_name': APP_CONFIG['site_name'], 'og:site_name': APP_CONFIG['site_name'],
'profile:username': user.screen_name 'profile:username': user.screen_name
}) })
@ -29,8 +29,8 @@ module ApplicationHelper::GraphMethods
meta_tags({ meta_tags({
'twitter:card': 'summary', 'twitter:card': 'summary',
'twitter:site': '@retrospring', 'twitter:site': '@retrospring',
'twitter:title': user.motivation_header.presence || "Ask me anything!", 'twitter:title': user.profile.motivation_header.presence || "Ask me anything!",
'twitter:description': "Ask #{user.safe_name} anything on Retrospring", 'twitter:description': "Ask #{user.profile.safe_name} anything on Retrospring",
'twitter:image': full_profile_picture_url(user) 'twitter:image': full_profile_picture_url(user)
}) })
end end
@ -38,7 +38,7 @@ module ApplicationHelper::GraphMethods
# @param answer [Answer] # @param answer [Answer]
def answer_opengraph(answer) def answer_opengraph(answer)
opengraph_meta_tags({ opengraph_meta_tags({
'og:title': "#{answer.user.safe_name} answered: #{answer.question.content}", 'og:title': "#{answer.user.profile.safe_name} answered: #{answer.question.content}",
'og:type': 'article', 'og:type': 'article',
'og:image': full_profile_picture_url(answer.user), 'og:image': full_profile_picture_url(answer.user),
'og:url': show_user_answer_url(answer.user.screen_name, answer.id), 'og:url': show_user_answer_url(answer.user.screen_name, answer.id),

View File

@ -3,9 +3,9 @@ class Profile < ApplicationRecord
attr_readonly :user_id attr_readonly :user_id
validates :display_name, length: { maximum: 32 } validates :display_name, length: { maximum: 50 }
validates :location, length: { maximum: 72 } validates :location, length: { maximum: 72 }
validates :description, length: { maximum: 256 } validates :description, length: { maximum: 200 }
before_save do before_save do
unless website.blank? unless website.blank?
@ -24,6 +24,6 @@ class Profile < ApplicationRecord
end end
def safe_name def safe_name
self.display_name.presence || self.screen_name display_name.presence || user.screen_name
end end
end end

View File

@ -58,9 +58,6 @@ class User < ApplicationRecord
validates :email, fake_email: true validates :email, fake_email: true
validates :screen_name, presence: true, format: { with: SCREEN_NAME_REGEX }, uniqueness: { case_sensitive: false }, screen_name: true validates :screen_name, presence: true, format: { with: SCREEN_NAME_REGEX }, uniqueness: { case_sensitive: false }, screen_name: true
validates :display_name, length: { maximum: 50 }
validates :bio, length: { maximum: 200 }
mount_uploader :profile_picture, ProfilePictureUploader, mount_on: :profile_picture_file_name mount_uploader :profile_picture, ProfilePictureUploader, mount_on: :profile_picture_file_name
process_in_background :profile_picture process_in_background :profile_picture
mount_uploader :profile_header, ProfileHeaderUploader, mount_on: :profile_header_file_name mount_uploader :profile_header, ProfileHeaderUploader, mount_on: :profile_header_file_name
@ -77,6 +74,10 @@ class User < ApplicationRecord
end end
end end
after_create do
Profile.create(user_id: id) if Profile.where(user_id: id).count.zero?
end
def login=(login) def login=(login)
@login = login @login = login
end end

View File

@ -1,9 +1,9 @@
.card .card
.card-header .card-header
- if user.motivation_header.blank? - if user.profile.motivation_header.blank?
= t 'views.questionbox.title' = t 'views.questionbox.title'
- else - else
= user.motivation_header = user.profile.motivation_header
.card-body .card-body
- if user.banned? - if user.banned?
.text-center .text-center

View File

@ -2,8 +2,6 @@
.card-body .card-body
= bootstrap_form_for(current_user, url: { action: :edit }, html: { multipart: true }, method: :patch) do |f| = bootstrap_form_for(current_user, url: { action: :edit }, html: { multipart: true }, method: :patch) do |f|
= f.text_field :display_name, label: t('views.settings.profile.displayname')
.media#profile-picture-media .media#profile-picture-media
.pull-left .pull-left
%img.avatar-lg.mr-3{ src: current_user.profile_picture.url(:medium) } %img.avatar-lg.mr-3{ src: current_user.profile_picture.url(:medium) }
@ -38,14 +36,6 @@
%button.btn.btn-inverse#cropper-header-zoom-in{ type: :button } %button.btn.btn-inverse#cropper-header-zoom-in{ type: :button }
%i.fa.fa-search-plus %i.fa.fa-search-plus
= f.text_field :motivation_header, label: t('views.settings.profile.motivation'), placeholder: t('views.settings.profile.placeholder.motivation')
= f.text_field :website, label: t('views.settings.profile.website'), placeholder: 'https://example.com'
= f.text_field :location, label: t('views.settings.profile.location'), placeholder: t('views.settings.profile.placeholder.location')
= f.text_area :bio, label: t('views.settings.profile.bio'), placeholder: t('views.settings.profile.placeholder.bio')
= f.check_box :show_foreign_themes, label: 'Render other user themes when visiting their profile' = f.check_box :show_foreign_themes, label: 'Render other user themes when visiting their profile'
- %i[profile_picture_x profile_picture_y profile_picture_w profile_picture_h].each do |attrib| - %i[profile_picture_x profile_picture_y profile_picture_w profile_picture_h].each do |attrib|
@ -55,3 +45,19 @@
= f.hidden_field attrib, id: attrib = f.hidden_field attrib, id: attrib
= f.submit t('views.actions.save'), class: 'btn btn-primary' = f.submit t('views.actions.save'), class: 'btn btn-primary'
.card
.card-body
= bootstrap_form_for(current_user.profile, url: { action: :edit }, html: { multipart: true }, method: :patch) do |f|
= f.text_field :display_name, label: t('views.settings.profile.displayname')
= f.text_field :motivation_header, label: t('views.settings.profile.motivation'), placeholder: t('views.settings.profile.placeholder.motivation')
= f.text_field :website, label: t('views.settings.profile.website'), placeholder: 'https://example.com'
= f.text_field :location, label: t('views.settings.profile.location'), placeholder: t('views.settings.profile.placeholder.location')
= f.text_area :description, label: t('views.settings.profile.bio'), placeholder: t('views.settings.profile.placeholder.bio')
= f.submit t('views.actions.save'), class: 'btn btn-primary'

View File

@ -2,10 +2,11 @@ class CreateProfiles < ActiveRecord::Migration[5.2]
def change def change
create_table :profiles do |t| create_table :profiles do |t|
t.references :user, index: true, foreign_key: true t.references :user, index: true, foreign_key: true
t.string :display_name, length: 32 t.string :display_name, length: 50
t.string :description, length: 256 t.string :description, length: 200, null: false, default: ''
t.string :location, length: 72 t.string :location, length: 72, null: false, default: ''
t.string :website t.string :website, null: false, default: ''
t.string :motivation_header, null: false, default: ''
t.timestamps t.timestamps
end end

View File

@ -119,9 +119,10 @@ ActiveRecord::Schema.define(version: 2021_12_19_153054) do
create_table "profiles", force: :cascade do |t| create_table "profiles", force: :cascade do |t|
t.bigint "user_id" t.bigint "user_id"
t.string "display_name" t.string "display_name"
t.string "description" t.string "description", default: "", null: false
t.string "location" t.string "location", default: "", null: false
t.string "website" t.string "website", default: "", null: false
t.string "motivation_header", default: "", null: false
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.index ["user_id"], name: "index_profiles_on_user_id" t.index ["user_id"], name: "index_profiles_on_user_id"
@ -136,7 +137,9 @@ ActiveRecord::Schema.define(version: 2021_12_19_153054) do
t.datetime "created_at" t.datetime "created_at"
t.datetime "updated_at" t.datetime "updated_at"
t.integer "answer_count", default: 0, null: false t.integer "answer_count", default: 0, null: false
t.datetime "discarded_at"
t.boolean "direct", default: false, null: false t.boolean "direct", default: false, null: false
t.index ["discarded_at"], name: "index_questions_on_discarded_at"
t.index ["user_id", "created_at"], name: "index_questions_on_user_id_and_created_at" t.index ["user_id", "created_at"], name: "index_questions_on_user_id_and_created_at"
end end

View File

@ -6,16 +6,20 @@ FactoryBot.define do
sequence(:email) { |i| "#{i}#{Faker::Internet.email}" } sequence(:email) { |i| "#{i}#{Faker::Internet.email}" }
password { 'P4s5w0rD' } password { 'P4s5w0rD' }
confirmed_at { Time.now.utc } confirmed_at { Time.now.utc }
display_name { Faker::Name.name }
transient do transient do
roles { [] } roles { [] }
profile { { display_name: Faker::Name.name } }
end end
after(:create) do |user, evaluator| after(:create) do |user, evaluator|
evaluator.roles.each do |role| evaluator.roles.each do |role|
user.add_role role user.add_role role
end end
evaluator.profile.each do |key, value|
user.profile.public_send(:"#{key}=", value)
end
end end
end end
end end

View File

@ -55,8 +55,8 @@ describe ApplicationHelper, :type => :helper do
describe "#user_opengraph" do describe "#user_opengraph" do
context "sample user" do context "sample user" do
let(:user) { FactoryBot.create(:user, let(:user) { FactoryBot.create(:user,
display_name: 'Cunes', profile: { display_name: 'Cunes',
bio: 'A bunch of raccoons in a trenchcoat.', description: 'A bunch of raccoons in a trenchcoat.' },
screen_name: 'raccoons') } screen_name: 'raccoons') }
subject { user_opengraph(user) } subject { user_opengraph(user) }
@ -79,12 +79,12 @@ EOS
describe "#user_twitter_card" do describe "#user_twitter_card" do
context "sample user" do context "sample user" do
let(:user) { FactoryBot.create(:user, let(:user) { FactoryBot.create(:user,
display_name: '', profile: {
bio: 'A bunch of raccoons in a trenchcoat.', display_name: '',
description: 'A bunch of raccoons in a trenchcoat.'},
screen_name: 'raccoons') } screen_name: 'raccoons') }
subject { user_twitter_card(user) } subject { user_twitter_card(user) }
it 'should generate a matching OpenGraph structure for a user' do it 'should generate a matching OpenGraph structure for a user' do
expect(subject).to eq(<<-EOS.chomp) expect(subject).to eq(<<-EOS.chomp)
<meta name="twitter:card" content="summary" /> <meta name="twitter:card" content="summary" />

View File

@ -1,3 +1,4 @@
# frozen_string_literal: true # frozen_string_literal: true
require "factory_bot_rails" require "factory_bot_rails"