create migration for populating terms

Change-Id: I13eddbe900070b8f50ff5c8f6f016daa27ec87d1
Reviewed-on: https://gerrit.instructure.com/128447
Tested-by: Jenkins
QA-Review: Steven Burnett <sburnett@instructure.com>
Reviewed-by: Simon Williams <simon@instructure.com>
Reviewed-by: Steven Burnett <sburnett@instructure.com>
Product-Review: James Williams  <jamesw@instructure.com>
Reviewed-by: James Williams  <jamesw@instructure.com>
This commit is contained in:
Steven Burnett 2017-10-04 10:10:05 -06:00 committed by James Williams
parent 1846d1843e
commit b48d3260ff
5 changed files with 70 additions and 1 deletions

View File

@ -1649,6 +1649,7 @@ class Account < ActiveRecord::Base
work = -> do
default_enrollment_term
enable_canvas_authentication
TermsOfService.ensure_terms_for_account(self) if self.root_account?
end
return work.call if Rails.env.test?
self.class.connection.after_transaction_commit(&work)

View File

@ -21,4 +21,27 @@ class TermsOfService < ActiveRecord::Base
belongs_to :account
belongs_to :terms_of_service_content
validates :terms_type, :passive, presence: true
validate :validate_account_is_root
cattr_accessor :skip_automatic_terms_creation
def validate_account_is_root
if self.account_id_changed? && !self.account.root_account?
self.errors.add(:account, "must be root account")
end
end
def self.ensure_terms_for_account(account)
unless !self.table_exists? || self.skip_automatic_terms_creation || account.terms_of_service
account.shard.activate do
account.create_terms_of_service!(term_options_for_account(account))
end
end
end
DEFAULT_OPTIONS = {:terms_type => "default_url"}.freeze
def self.term_options_for_account(account)
DEFAULT_OPTIONS
end
end

View File

@ -0,0 +1,31 @@
#
# Copyright (C) 2017 - 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/>.
#
#
class PopulateTermsOfService < ActiveRecord::Migration[4.2]
tag :postdeploy
disable_ddl_transaction!
def up
Account.root_accounts.each do |ra|
TermsOfService.ensure_terms_for_account(ra)
end
end
def down
end
end

View File

@ -14,7 +14,7 @@
# 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_relative '../spec_helper'
require_relative '../sharding_spec_helper'
describe TermsOfService do
before :once do
@ac = account_model
@ -43,4 +43,17 @@ describe TermsOfService do
account: ac2)
expect(tos.passive).to eq true
end
describe "#ensure_terms_for_account" do
before :each do
TermsOfService.skip_automatic_terms_creation = false
end
it "should create a default terms_of_service on root account creation" do
ac2 = account_model
expect(ac2.terms_of_service.terms_type).to eq TermsOfService.term_options_for_account(ac2)[:terms_type]
sub = ac2.sub_accounts.create!
expect(sub.terms_of_service).to be nil
end
end
end

View File

@ -349,6 +349,7 @@ RSpec.configure do |config|
RequestStore.clear!
MultiCache.reset
Course.enroll_user_call_count = 0
TermsOfService.skip_automatic_terms_creation = true
$spec_api_tokens = {}
end