FEATURE: Implement refresh method for title

Takes preferred pronouns and fun job title and combines them into
a user's Discourse title.

https://github.com/mozilla/discourse/issues/183
This commit is contained in:
Leo McArdle 2019-04-01 16:15:22 +01:00
Родитель 7ef8ce5baa
Коммит 2b49d2b2dd
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 8262833620A64C3F
4 изменённых файлов: 123 добавлений и 1 удалений

Просмотреть файл

@ -105,4 +105,5 @@ require_relative "profile/duplicate_accounts"
require_relative "profile/is_aal_enough"
require_relative "profile/dinopark_enabled"
require_relative "profile/update_name"
require_relative "profile/update_title"
require_relative "profile/update_username"

Просмотреть файл

@ -0,0 +1,19 @@
module MozillaIAM
Profile.class_eval do
during_refresh :update_title
def update_title
return unless dinopark_enabled?
pronouns = attr(:pronouns)
fun_title = attr(:fun_title)
title = ""
title << "(#{pronouns})" unless pronouns.blank?
title << " " unless pronouns.blank? || fun_title.blank?
title << fun_title unless fun_title.blank?
@user.update(title: title) unless @user.title == title
end
end
end

Просмотреть файл

@ -1,6 +1,6 @@
# name: mozilla-iam
# about: A plugin to integrate Discourse with Mozilla's Identity and Access Management (IAM) system
# version: 1.2.0-alpha.4
# version: 1.2.0-alpha.5
# authors: Leo McArdle
# url: https://github.com/mozilla/discourse-mozilla-iam

Просмотреть файл

@ -0,0 +1,102 @@
require_relative '../../../iam_helper'
describe MozillaIAM::Profile do
describe described_class.refresh_methods do
it { should include(:update_title) }
end
describe "#update_title" do
let(:user) { Fabricate(:user) }
let(:profile) { MozillaIAM::Profile.new(user, "uid") }
shared_examples "no change" do
it "does nothing" do
profile.send(:update_title)
user.reload
expect(user.title).to eq title_before
end
end
context "with dinopark_enabled? set to false" do
let!(:title_before) { user.title }
before do
profile.dinopark_enabled = false
profile.expects(:attr).with(:fun_title).never
profile.expects(:attr).with(:pronouns).never
end
include_examples "no change"
end
context "with dinopark_enabled? set to true" do
before do
profile.dinopark_enabled = true
end
context "with empty dinopark fun_title and pronouns" do
before do
profile.expects(:attr).with(:fun_title).returns(" ")
profile.expects(:attr).with(:pronouns).returns(" ")
end
it "makes the user's title blank" do
profile.send(:update_title)
user.reload
expect(user.title).to eq ""
end
end
context "with empty dinopark fun_title and value for pronouns" do
before do
profile.expects(:attr).with(:fun_title).returns(" ")
profile.expects(:attr).with(:pronouns).returns("she/her")
end
it "makes the user's title blank" do
profile.send(:update_title)
user.reload
expect(user.title).to eq "(she/her)"
end
end
context "with empty dinopark pronouns and value for fun_title" do
before do
profile.expects(:attr).with(:fun_title).returns("The World's Best Developer")
profile.expects(:attr).with(:pronouns).returns(" ")
end
it "makes the user's title blank" do
profile.send(:update_title)
user.reload
expect(user.title).to eq "The World's Best Developer"
end
end
context "with values for dinopark pronouns and fun_title" do
before do
profile.expects(:attr).with(:fun_title).returns("The World's Best Developer")
profile.expects(:attr).with(:pronouns).returns("she/her")
end
it "makes the user's title blank" do
profile.send(:update_title)
user.reload
expect(user.title).to eq "(she/her) The World's Best Developer"
end
end
context "with dinopark pronouns and fun_title already set" do
let!(:title_before) { "(she/her) The World's Best Developer" }
before do
profile.expects(:attr).with(:fun_title).returns("The World's Best Developer")
profile.expects(:attr).with(:pronouns).returns("she/her")
user.update(title: "(she/her) The World's Best Developer")
user.reload
user.expects(:update).never
end
include_examples "no change"
end
end
end
end