Skip to content

Commit

Permalink
Populate root account id for favorites
Browse files Browse the repository at this point in the history
Test plan:
-specs should pass
-create a favorite
-inspect in rails console 
-verify root account id is set 

Fixes VICE-343

flag=none

Change-Id: Ib13b7db057bfe1fd7a1d78b30a5ed3a30ae85799
Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/239853
Tested-by: Service Cloud Jenkins <[email protected]>
Reviewed-by: Ben Nelson <[email protected]>
QA-Review: Caleb Guanzon <[email protected]>
Product-Review: Caleb Guanzon <[email protected]>
  • Loading branch information
drakeaharper committed Jun 12, 2020
1 parent fe42ebf commit 68422b7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/models/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Account < ActiveRecord::Base
belongs_to :root_account, :class_name => 'Account'

has_many :courses
has_many :favorites, inverse_of: :root_account
has_many :all_courses, :class_name => 'Course', :foreign_key => 'root_account_id'
has_one :terms_of_service, :dependent => :destroy
has_one :terms_of_service_content, :dependent => :destroy
Expand Down
6 changes: 6 additions & 0 deletions app/models/favorite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@
class Favorite < ActiveRecord::Base
belongs_to :context, polymorphic: [:course, :group]
belongs_to :user
belongs_to :root_account, class_name: "Account", inverse_of: :favorites
validates_inclusion_of :context_type, :allow_nil => true, :in => ['Course', 'Group'].freeze
scope :by, lambda { |type| where(:context_type => type) }

before_create :populate_root_account_id
after_save :touch_user

def touch_user
self.class.connection.after_transaction_commit { user.touch }
end

def populate_root_account_id
self.root_account = self.context.root_account
end
end
26 changes: 26 additions & 0 deletions spec/models/favorite_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#
# Copyright (C) 2020 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.

require 'spec_helper'

describe Favorite do
it 'should populate root account' do
student_in_course()
favorite = @user.favorites.create!(context: @course)
expect(favorite.root_account).to eq @course.root_account
end
end

0 comments on commit 68422b7

Please sign in to comment.