Skip to content

Commit 2e59598

Browse files
committed
speed up account pages a bit
* avoid joining with users just to get the count of associated_users Change-Id: I79635e7bd189d361fb9f1deee73806ffc40d40db Reviewed-on: https://gerrit.instructure.com/5321 Tested-by: Hudson <hudson@instructure.com> Reviewed-by: Zach Wily <zach@instructure.com>
1 parent 33de0f4 commit 2e59598

4 files changed

Lines changed: 36 additions & 3 deletions

File tree

app/models/account.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ def inherited_assessment_question_banks(include_self = false, *additional_contex
8585
has_many :account_notifications
8686
has_many :alerts, :as => :context, :include => :criteria
8787
has_many :associated_alerts, :through => :associated_courses, :source => :alerts, :include => :criteria
88+
has_many :user_account_associations
8889

8990
before_validation :verify_unique_sis_source_id
9091
before_save :ensure_defaults
@@ -783,7 +784,12 @@ def sub_account_count
783784
self.sub_accounts.active.count
784785
end
785786
memoize :sub_account_count
786-
787+
788+
def user_count
789+
self.user_account_associations.count
790+
end
791+
memoize :user_count
792+
787793
def current_sis_batch
788794
if (current_sis_batch_id = self.read_attribute(:current_sis_batch_id)) && current_sis_batch_id.present?
789795
self.sis_batches.find_by_id(current_sis_batch_id)

app/views/shared/_accounts_right_side_shared.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
serviceUrl: $("#new_user")[0].action + ".json",
1919
onSelect: function(value, data){
2020
window.location = $("#new_user")[0].action + "/" + data
21-
<% if @account.all_users.count < 500 %>
21+
<% if @account.user_count < 500 %>
2222
},
2323
lookup: <%= raw({
2424
:suggestions => @account.fast_all_users.map{ |u| u.last_name_first || ""},

app/views/shared/_accounts_secondary_content.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
serviceUrl: $newUserForm[0].action + ".json",
3030
onSelect: function(value, data){
3131
window.location = data
32-
<% if @account.all_users.count < 250 %>
32+
<% if @account.user_count < 250 %>
3333
},
3434

3535
lookup: <%= raw({

spec/models/account_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,4 +443,31 @@ def account_with_admin_and_restricted_user(account)
443443
["&nbsp;&nbsp;&nbsp;&nbsp;sub2-1", sub2_1.id]
444444
]
445445
end
446+
447+
it "should return the correct user count" do
448+
a = Account.default
449+
a.all_users.count.should == a.user_count
450+
a.user_count.should == 0
451+
452+
u = User.create!
453+
a.add_user(u)
454+
a.all_users.count.should == a.user_count(:reload)
455+
a.user_count.should == 1
456+
457+
course_with_teacher
458+
@teacher.update_account_associations
459+
a.all_users.count.should == a.user_count(:reload)
460+
a.user_count.should == 2
461+
462+
a2 = a.sub_accounts.create!
463+
course_with_teacher(:account => a2)
464+
@teacher.update_account_associations
465+
a.all_users.count.should == a.user_count(:reload)
466+
a.user_count.should == 3
467+
468+
user_with_pseudonym
469+
a.all_users.count.should == a.user_count(:reload)
470+
a.user_count.should == 4
471+
472+
end
446473
end

0 commit comments

Comments
 (0)