include account_users in user_account_associations. fixes #4451

Change-Id: I9a24ab4f5612a66fc2c59c28211de28a88b61dad
Reviewed-on: https://gerrit.instructure.com/3427
Tested-by: Hudson <hudson@instructure.com>
Reviewed-by: Brian Palmer <brianp@instructure.com>
This commit is contained in:
Cody Cutrer 2011-05-02 16:48:20 -06:00
parent ceb3fa0c49
commit 39588b17be
4 changed files with 32 additions and 3 deletions

View File

@ -23,8 +23,23 @@ class AccountUser < ActiveRecord::Base
has_many :all_account_courses, :class_name => 'Course', :foreign_key => 'root_account_id', :primary_key => 'account_id'
has_a_broadcast_policy
before_save :infer_defaults
before_save :set_update_account_associations_if_changed
after_save :touch_user
after_save :update_account_associations_if_changed
def set_update_account_associations_if_changed
@should_update_account_associations = (self.account_id_changed? || self.user_id_changed?) && !self.user_id.nil?
true
end
def update_account_associations_if_changed
send_later_if_production(:update_account_associations) if @should_update_account_associations
end
def update_account_associations
self.user.update_account_associations if self.user
end
def infer_defaults
self.membership_type ||= 'AccountAdmin'
end

View File

@ -221,11 +221,14 @@ class User < ActiveRecord::Base
# User -> Enrollment -> Course -> Account
# Through pseudonyms:
# User -> Pseudonym -> Account
# Through account_users
# User -> AccountUser -> Account
starting_points = []
self.enrollments.find(:all, :include => {:course_section => [:course, :course_account_associations], :course => :course_account_associations}).each do |enrollment|
starting_points << enrollment.course << enrollment.course_section.try(:course) << enrollment.course_section.try(:nonxlist_course)
end
starting_points += self.pseudonym_accounts.reload
starting_points += account_users.map(&:account)
# For each Course and Account, make sure an association exists.
starting_points.compact.each do |entity|

View File

@ -22,7 +22,8 @@ describe UsersController do
it "should filter account users by term" do
a = Account.default
a.add_user(user(:active_all => true))
u = user(:active_all => true)
a.add_user(u)
user_session(@user)
t1 = a.default_enrollment_term
t2 = a.enrollment_terms.create!(:name => 'Term 2')
@ -39,7 +40,7 @@ describe UsersController do
User.update_account_associations(User.all.map(&:id))
get 'index', :account_id => a.id
assigns[:users].map(&:id).sort.should == [e1.user, c1.teachers.first, e2.user, c2.teachers.first, c3.teachers.first].map(&:id).sort
assigns[:users].map(&:id).sort.should == [u, e1.user, c1.teachers.first, e2.user, c2.teachers.first, c3.teachers.first].map(&:id).sort
get 'index', :account_id => a.id, :enrollment_term_id => t1.id
assigns[:users].map(&:id).sort.should == [e1.user, c1.teachers.first, c3.teachers.first].map(&:id).sort # 1 student, enrolled twice, and 2 teachers

View File

@ -126,6 +126,16 @@ describe User do
user.associated_accounts[0].should eql(account1)
user.associated_accounts[1].should eql(account2)
end
it "should update account associations when a user is associated to an account just by account_users" do
account = account_model
@user = User.create
account.add_user(@user)
@user.reload
@user.associated_accounts.length.should eql(1)
@user.associated_accounts.first.should eql(account)
end
it "should populate dashboard_messages" do
Notification.create(:name => "Assignment Created")