use course nicknames in notifications

test plan:
 - set up multiple users in a course
 - give some of these users different nicknames for the course
 - set up and confirm communication channels for these users
 - do things that cause notification messages to be dispatched
   to the users
 - ensure each user sees the correct course name in his/her
   notification messages

closes CNVS-24168

Change-Id: Ic49c888ae86b40ba42004e3c2dbc205e49480b79
Reviewed-on: https://gerrit.instructure.com/65544
Reviewed-by: Clay Diffrient <cdiffrient@instructure.com>
Tested-by: Jenkins
QA-Review: Ryan Allen <rallen@instructure.com>
Product-Review: Cosme Salazar <cosme@instructure.com>
This commit is contained in:
Jeremy Stanley 2015-10-21 12:45:51 -06:00
parent 1a2f3f85b4
commit 9aff943389
4 changed files with 79 additions and 0 deletions

View File

@ -2825,4 +2825,13 @@ class Course < ActiveRecord::Base
def refresh_content_participation_counts(_progress) def refresh_content_participation_counts(_progress)
content_participation_counts.each(&:refresh_unread_count) content_participation_counts.each(&:refresh_unread_count)
end end
def name
return @nickname if @nickname
read_attribute(:name)
end
def apply_nickname_for!(user)
@nickname = nickname_for(user, nil)
end
end end

View File

@ -483,6 +483,9 @@ class Message < ActiveRecord::Base
user_time_zone = self.user.try(:time_zone) || original_time_zone user_time_zone = self.user.try(:time_zone) || original_time_zone
Time.zone = user_time_zone Time.zone = user_time_zone
# (temporarily) override course name with user's nickname for the course
hacked_course = apply_course_nickname_to_asset(self.context, self.user)
# Ensure we have a path_type # Ensure we have a path_type
path_type = 'dashboard' if to == 'summary' path_type = 'dashboard' if to == 'summary'
unless path_type unless path_type
@ -518,6 +521,8 @@ class Message < ActiveRecord::Base
# Set the timezone back to what it originally was # Set the timezone back to what it originally was
Time.zone = original_time_zone if original_time_zone.present? Time.zone = original_time_zone if original_time_zone.present?
hacked_course.apply_nickname_for!(nil) if hacked_course
@i18n_scope = nil @i18n_scope = nil
end end
@ -818,4 +823,16 @@ class Message < ActiveRecord::Base
@_message_context ||= Messages::AssetContext.new(context, notification_name) @_message_context ||= Messages::AssetContext.new(context, notification_name)
end end
def apply_course_nickname_to_asset(asset, user)
hacked_course = if asset.is_a?(Course)
asset
elsif asset.respond_to?(:context) && asset.context.is_a?(Course)
asset.context
elsif asset.respond_to?(:course) && asset.course.is_a?(Course)
asset.course
end
hacked_course.apply_nickname_for!(user) if hacked_course
hacked_course
end
end end

View File

@ -4305,3 +4305,25 @@ describe Course, '#update_enrolled_users' do
expect(@user.associated_accounts).to be_blank expect(@user.associated_accounts).to be_blank
end end
end end
describe Course, "#apply_nickname_for!" do
before(:once) do
@course = Course.create! :name => 'some terrible name'
@user = User.new
@user.course_nicknames[@course.id] = 'nickname'
@user.save!
end
it "sets name to user's nickname (non-persistently)" do
@course.apply_nickname_for!(@user)
expect(@course.name).to eq 'nickname'
@course.save!
expect(Course.find(@course).name).to eq 'some terrible name'
end
it "undoes the change with nil user" do
@course.apply_nickname_for!(@user)
@course.apply_nickname_for!(nil)
expect(@course.name).to eq 'some terrible name'
end
end

View File

@ -99,6 +99,37 @@ describe Message do
msg = generate_message(:account_user_notification, :email, @au) msg = generate_message(:account_user_notification, :email, @au)
expect(msg.html_body).to include('awesomelogo.jpg') expect(msg.html_body).to include('awesomelogo.jpg')
end end
describe "course nicknames" do
before(:once) do
course_with_student(:active_all => true, :course_name => 'badly-named-course')
@student.course_nicknames[@course.id] = 'student-course-nick'
@student.save!
end
def check_message(message, asset)
msg = generate_message(message, :email, asset, :user => @student)
expect(msg.html_body).not_to include 'badly-named-course'
expect(msg.html_body).to include 'student-course-nick'
expect(@course.name).to eq 'badly-named-course'
msg = generate_message(message, :email, asset, :user => @teacher)
expect(msg.html_body).to include 'badly-named-course'
expect(msg.html_body).not_to include 'student-course-nick'
end
it "applies nickname to asset" do
check_message(:grade_weight_changed, @course)
end
it "applies nickname to asset.course" do
check_message(:enrollment_registration, @student.enrollments.first)
end
it "applies nickname to asset.context" do
check_message(:assignment_changed, @course.assignments.create!)
end
end
end end
context "named scopes" do context "named scopes" do