no more dynamic finders (lib)

refs CNVS-15126

Change-Id: I4087b1c2442c5b8f70ce2138bf4aeef6ef7e59cd
Reviewed-on: https://gerrit.instructure.com/41259
Reviewed-by: Jacob Fugal <jacob@instructure.com>
Tested-by: Jenkins <jenkins@instructure.com>
Product-Review: Cody Cutrer <cody@instructure.com>
QA-Review: Cody Cutrer <cody@instructure.com>
This commit is contained in:
Cody Cutrer 2014-09-17 11:29:06 -06:00
parent 65a620ed8e
commit 5d42d023d2
42 changed files with 102 additions and 103 deletions

View File

@ -131,14 +131,16 @@ class Attachment < ActiveRecord::Base
# by id is deleted but an active attachment in the same context has the same
# path, it'll return that attachment.
module FindInContextAssociation
def find(*a, &b)
return super if a.first.is_a?(Symbol)
def find(*a)
find_with_possibly_replaced(super)
end
def method_missing(method, *a, &b)
return super unless method.to_s =~ /^find(?:_all)?_by_id$/
find_with_possibly_replaced(super)
def find_by_id(id)
find_with_possibly_replaced(where(id: id).first)
end
def find_all_by_id(ids)
find_with_possibly_replaced(where(id: ids).to_a)
end
def find_with_possibly_replaced(a_or_as)

View File

@ -397,7 +397,7 @@ module Api
if match.obj_id
obj = preloaded_attachments[match.obj_id]
obj ||= if context.is_a?(User) || context.nil?
Attachment.find_by_id(match.obj_id)
Attachment.where(id: match.obj_id).first
else
context.attachments.find_by_id(match.obj_id)
end

View File

@ -154,7 +154,7 @@ module Api::V1::Assignment
row_hash["ratings"] = row[:ratings].map do |c|
c.slice(:id, :points, :description)
end
if row[:learning_outcome_id] && outcome = LearningOutcome.find_by_id(row[:learning_outcome_id])
if row[:learning_outcome_id] && outcome = LearningOutcome.where(id: row[:learning_outcome_id]).first
row_hash["outcome_id"] = outcome.id
row_hash["vendor_guid"] = outcome.vendor_guid
end
@ -364,18 +364,18 @@ module Api::V1::Assignment
if update_params.has_key?("assignment_group_id")
ag_id = update_params.delete("assignment_group_id").presence
assignment.assignment_group = assignment.context.assignment_groups.find_by_id(ag_id)
assignment.assignment_group = assignment.context.assignment_groups.where(id: ag_id).first
end
if update_params.has_key?("group_category_id")
gc_id = update_params.delete("group_category_id").presence
assignment.group_category = assignment.context.group_categories.find_by_id(gc_id)
assignment.group_category = assignment.context.group_categories.where(id: gc_id).first
end
if update_params.has_key?("grading_standard_id")
standard_id = update_params.delete("grading_standard_id")
if standard_id.present?
grading_standard = GradingStandard.standards_for(context).find_by_id(standard_id)
grading_standard = GradingStandard.standards_for(context).where(id: standard_id).first
assignment.grading_standard = grading_standard if grading_standard
else
assignment.grading_standard = nil

View File

@ -83,7 +83,7 @@ module Api::V1::CourseEvent
course_ids.concat(events.map do |event|
event.event_data[event.event_type] if event.event_data
end.compact)
courses = Course.find_all_by_id(course_ids) if course_ids.length > 0
courses = Course.where(id: course_ids).to_a if course_ids.length > 0
courses ||= []
page_view_ids = events.map{ |event| event.request_id }.compact
@ -91,7 +91,7 @@ module Api::V1::CourseEvent
page_views ||= []
user_ids = events.map{ |event| event.user_id }.compact
users = User.find_all_by_id(user_ids) if user_ids.length > 0
users = User.where(id: user_ids).to_a if user_ids.length > 0
users ||= []
{

View File

@ -26,8 +26,9 @@ module DataExportsApi
def data_export_json(data_export, user, session, includes = [])
json = api_json(data_export, user, session, :only => %w(id created_at workflow_state))
# TODO update once we have support for more granular contexts than just Account
json["account"] = account_json(::Account.find_by_id(data_export.context_id), user, session, includes) if includes.include?(:account)
json["user"] = user_json(User.find_by_id(data_export.user_id), user, session, includes, Account.find_by_id(data_export.context_id)) if includes.include?(:user)
account = ::Account.where(id: data_export.context_id).first unless (includes & [:account, :user]).empty?
json["account"] = account_json(account, user, session, includes) if includes.include?(:account)
json["user"] = user_json(User.where(id: data_export.user_id).first, user, session, includes, account) if includes.include?(:user)
json
end

View File

@ -73,16 +73,16 @@ module Api::V1::GradeChangeEvent
def linked_json(events, user, session)
course_ids = events.map{ |event| event.course_id }.compact
courses = Course.find_all_by_id(course_ids) if course_ids.length > 0
courses = Course.where(id: course_ids).to_a if course_ids.length > 0
courses ||= []
assignment_ids = events.map{ |event| event.assignment_id }.compact
assignments = Assignment.find_all_by_id(assignment_ids) if assignment_ids.length > 0
assignments = Assignment.where(id: assignment_ids).to_a if assignment_ids.length > 0
assignments ||= []
user_ids = events.map{ |event| event.grader_id }.compact
user_ids.concat(events.map{ |event| event.student_id }.compact)
users = User.find_all_by_id(user_ids) if user_ids.length > 0
users = User.where(id: user_ids).to_a if user_ids.length > 0
users ||= []
page_view_ids = events.map{ |event| event.request_id }.compact

View File

@ -109,7 +109,7 @@ module Api::V1::Quiz
# make sure assignment_group_id belongs to context
if update_params.has_key?("assignment_group_id")
ag_id = update_params.delete("assignment_group_id").presence
ag = quiz.context.assignment_groups.find_by_id(ag_id)
ag = quiz.context.assignment_groups.where(id: ag_id).first
update_params["assignment_group_id"] = ag.try(:id)
end

View File

@ -91,7 +91,7 @@ module AuthenticationMethods
if !@current_pseudonym
if @policy_pseudonym_id
@current_pseudonym = Pseudonym.find_by_id(@policy_pseudonym_id)
@current_pseudonym = Pseudonym.where(id: @policy_pseudonym_id).first
elsif @pseudonym_session = PseudonymSession.find
@current_pseudonym = @pseudonym_session.record
@ -125,7 +125,7 @@ module AuthenticationMethods
# just using an app session
# this basic auth support is deprecated and marked for removal in 2012
if @pseudonym_session.try(:used_basic_auth?) && params[:api_key].present?
Shard.birth.activate { @developer_key = DeveloperKey.find_by_api_key(params[:api_key]) }
Shard.birth.activate { @developer_key = DeveloperKey.where(api_key: params[:api_key]).first }
end
@developer_key ||
request.get? ||
@ -147,7 +147,7 @@ module AuthenticationMethods
if @current_user && %w(become_user_id me become_teacher become_student).any? { |k| params.key?(k) }
request_become_user = nil
if params[:become_user_id]
request_become_user = User.find_by_id(params[:become_user_id])
request_become_user = User.where(id: params[:become_user_id]).first
elsif params.keys.include?('me')
request_become_user = @current_user
elsif params.keys.include?('become_teacher')

View File

@ -31,7 +31,7 @@ module BasicLTI
return false unless tool.id == md[1].to_i
course = Course.find(md[2])
assignment = course.assignments.active.find(md[3])
user = course.student_enrollments.active.find_by_user_id(md[4]).user
user = course.student_enrollments.active.where(user_id: md[4]).first.try(:user)
tag = assignment.external_tool_tag
if !tag || tool != ContextExternalTool.find_external_tool(tag.url, course)
return false # assignment settings have changed, this tool is no longer active

View File

@ -77,7 +77,7 @@ module Canvas::MessageHelper
using[:name] ||= split_txt[0]
end
raise 'Name is required' unless using[:name]
n = Notification.find_or_initialize_by_name(using[:name])
n = Notification.where(name: using[:name]).first_or_initialize
begin
n.update_attributes(:delay_for => using[:delay_for], :category => using[:category])
rescue => e

View File

@ -147,9 +147,9 @@ module Canvas::Migration::Helpers
hash[:title] = item['file_name']
elsif type == 'assessment_question_banks'
if hash[:title].blank? && @migration && @migration.context.respond_to?(:assessment_question_banks)
if hash[:migration_id] && bank = @migration.context.assessment_question_banks.find_by_migration_id(hash[:migration_id])
if hash[:migration_id] && bank = @migration.context.assessment_question_banks.where(migration_id: hash[:migration_id]).first
hash[:title] = bank.title
elsif @migration.question_bank_id && default_bank = @migration.context.assessment_question_banks.find_by_id(@migration.question_bank_id)
elsif @migration.question_bank_id && default_bank = @migration.context.assessment_question_banks.where(id: @migration.question_bank_id).first
hash[:title] = default_bank.title
end
hash[:title] ||= @migration.question_bank_name || AssessmentQuestionBank.default_imported_title

View File

@ -21,7 +21,7 @@ module Canvas::Migration::Validators::CourseCopyValidator
if !options || !options[:source_course_id]
return I18n.t :course_copy_argument_error, 'A course copy requires a source course.'
end
source = Course.find_by_id(options[:source_course_id])
source = Course.where(id: options[:source_course_id]).first
if source
if !(source.grants_right?(user, :read_as_admin) && source.grants_right?(user, :read))
return I18n.t :course_copy_not_allowed_error, 'You are not allowed to copy the source course.'

View File

@ -21,7 +21,7 @@ module Canvas::Migration::Validators::ZipImporterValidator
if !options || !options[:folder_id]
return I18n.t :zip_argument_error, 'A .zip upload requires a folder to upload to.'
end
if !course.folders.find_by_id(options[:folder_id])
if !course.folders.where(id: options[:folder_id]).first
return I18n.t :zip_no_folder_error, "The specified folder couldn't be found in this course."
end

View File

@ -39,7 +39,7 @@ module Canvas::Oauth
def key
return nil unless client_id_is_valid?
@key ||= DeveloperKey.find_by_id(@client_id)
@key ||= DeveloperKey.where(id: @client_id).first
end
# Checks to see if a token has already been issued to this client and

View File

@ -57,7 +57,7 @@ module CC
rules.each do |rule|
a = nil
if rule.first == 'never_drop'
a = @course.assignments.find_by_id(rule.last)
a = @course.assignments.where(id: rule.last).first
next unless a
end
rules_node.rule do |rule_node|

View File

@ -178,7 +178,7 @@ module CCHelper
if @course && match.obj_class == Attachment
obj = @course.attachments.find_by_id(match.obj_id)
else
obj = match.obj_class.find_by_id(match.obj_id)
obj = match.obj_class.where(id: match.obj_id).first
end
next(match.url) unless obj && @rewriter.user_can_view_content?(obj)
folder = obj.folder.full_name.gsub(/course( |%20)files/, WEB_CONTENT_TOKEN)
@ -196,9 +196,9 @@ module CCHelper
# WikiPagesController allows loosely-matching URLs; fix them before exporting
if match.obj_id.present?
url_or_title = match.obj_id
page = @course.wiki.wiki_pages.deleted_last.find_by_url(url_or_title) ||
@course.wiki.wiki_pages.deleted_last.find_by_url(url_or_title.to_url) ||
@course.wiki.wiki_pages.find_by_id(url_or_title.to_i)
page = @course.wiki.wiki_pages.deleted_last.where(url: url_or_title).first ||
@course.wiki.wiki_pages.deleted_last.where(url: url_or_title.to_url).first ||
@course.wiki.wiki_pages.where(id: url_or_title.to_i).first
end
if page
"#{WIKI_TOKEN}/#{match.type}/#{page.url}"
@ -216,7 +216,7 @@ module CCHelper
@rewriter.set_default_handler do |match|
new_url = match.url
if match.obj_id && match.obj_class
obj = match.obj_class.find_by_id(match.obj_id)
obj = match.obj_class.where(id: match.obj_id).first
if obj && @rewriter.user_can_view_content?(obj)
# for all other types,
# create a migration id for the object, and use that as the new link

View File

@ -17,7 +17,7 @@
#
class Canvas::Migration::Worker::CCWorker < Struct.new(:migration_id)
def perform(cm=nil)
cm ||= ContentMigration.find_by_id migration_id
cm ||= ContentMigration.where(id: migration_id).first
cm.job_progress.start unless cm.skip_job_progress
begin
cm.update_conversion_progress(1)

View File

@ -291,7 +291,7 @@ module CC
pick_count = group['pick_count'].to_i
chosen = 0
if group[:assessment_question_bank_id]
if bank = @course.assessment_question_banks.find_by_id(group[:assessment_question_bank_id])
if bank = @course.assessment_question_banks.where(id: group[:assessment_question_bank_id]).first
bank.assessment_questions.each do |question|
# try adding questions until the pick count is reached
chosen += 1 if add_cc_question(node, question)
@ -319,9 +319,9 @@ module CC
bank = nil
if group[:assessment_question_bank_id]
if bank = @course.assessment_question_banks.find_by_id(group[:assessment_question_bank_id])
if bank = @course.assessment_question_banks.where(id: group[:assessment_question_bank_id]).first
sel_node.sourcebank_ref create_key(bank)
elsif bank = AssessmentQuestionBank.find_by_id(group[:assessment_question_bank_id])
elsif bank = AssessmentQuestionBank.where(id: group[:assessment_question_bank_id]).first
sel_node.sourcebank_ref bank.id
is_external = true
end

View File

@ -42,7 +42,7 @@ module CC
def add_ref_or_question(node, question)
aq = nil
unless question[:assessment_question_id].blank?
if aq = AssessmentQuestion.find_by_id(question[:assessment_question_id])
if aq = AssessmentQuestion.where(id: question[:assessment_question_id]).first
if aq.deleted? ||
!aq.assessment_question_bank ||
aq.assessment_question_bank.deleted? ||

View File

@ -45,7 +45,7 @@ module CopyAuthorizedLinks
html.scan(re) do |match|
ids << match[0]
end
Attachment.find_all_by_id(ids).uniq.each do |file|
Attachment.where(id: ids.uniq).each do |file|
html = html.gsub(Regexp.new("/#{context.class.to_s.pluralize.underscore}/#{context.id}/files/#{file.id}"), "/#{file.context_type.pluralize.underscore}/#{file.context_id}/files/#{file.id}")
end
self.write_attribute(column, html) if html && !html.empty?

View File

@ -39,14 +39,14 @@ module DataFixup::FixBrokenFileLinksInAssignments
end
if file_id
if att = Attachment.find_by_id(file_id)
if att = Attachment.where(id: file_id).first
# this find returns the passed in att if nothing found in the context
# and sometimes URI.unescape errors so ignore that
att = att.context.attachments.find(att.id) rescue att
if att.context_type == "Course" && att.context_id == assignment.context_id
course_id = assignment.context_id
file_id = att.id
elsif att.cloned_item_id && cloned_att = assignment.context.attachments.find_by_cloned_item_id(att.cloned_item_id)
elsif att.cloned_item_id && cloned_att = assignment.context.attachments.where(cloned_item_id: att.cloned_item_id).first
course_id = assignment.context_id
file_id = assignment.context.attachments.find(cloned_att.id).id rescue cloned_att.id
elsif att.context_type == "Course"

View File

@ -31,7 +31,7 @@ module DataFixup
def self.other_namespace(attachment)
account_id = attachment.namespace.sub('account_', '').to_i
if account_id.to_s.length > 8
namespace_account_id = Account.find_by_id(account_id).try(:local_id)
namespace_account_id = Account.where(id: account_id).first.try(:local_id)
else
namespace_account_id = attachment.shard.global_id_for(account_id)
end

View File

@ -30,7 +30,7 @@ class EnrollmentsFromUserList
@enrollment_type = opts[:enrollment_type] || 'StudentEnrollment'
@role_name = opts[:role_name]
@limit = opts[:limit]
@section = (opts[:course_section_id].present? ? @course.course_sections.active.find_by_id(opts[:course_section_id].to_i) : nil) || @course.default_section
@section = (opts[:course_section_id].present? ? @course.course_sections.active.where(id: opts[:course_section_id].to_i).first : nil) || @course.default_section
@limit_privileges_to_course_section = opts[:limit_privileges_to_course_section]
@enrolled_users = {}
end

View File

@ -50,13 +50,13 @@ class ImportedHtmlConverter
#todo: refactor migration systems to use new $CANVAS...$ flags
#todo: FLAG UNFOUND REFERENCES TO re-attempt in second loop?
if wiki_migration_id = $1
if linked_wiki = context.wiki.wiki_pages.find_by_migration_id(wiki_migration_id)
if linked_wiki = context.wiki.wiki_pages.where(migration_id: wiki_migration_id).first
new_url = "#{course_path}/wiki/#{linked_wiki.url}"
end
end
elsif val =~ /discussion_topic_migration_id=(.*)/
if topic_migration_id = $1
if linked_topic = context.discussion_topics.find_by_migration_id(topic_migration_id)
if linked_topic = context.discussion_topics.where(migration_id: topic_migration_id).first
new_url = URI::escape("#{course_path}/discussion_topics/#{linked_topic.id}")
end
end
@ -73,11 +73,11 @@ class ImportedHtmlConverter
if type == 'pages'
new_url = "#{course_path}/#{context.feature_enabled?(:draft_state) ? 'pages' : 'wiki'}/#{migration_id}"
elsif type == 'attachments'
if att = context.attachments.find_by_migration_id(migration_id)
if att = context.attachments.where(migration_id: migration_id).first
new_url = URI::escape("#{course_path}/files/#{att.id}/preview")
end
elsif context.respond_to?(type) && context.send(type).respond_to?(:find_by_migration_id)
if object = context.send(type).find_by_migration_id(migration_id)
elsif context.respond_to?(type) && context.send(type).respond_to?(:where)
if object = context.send(type).where(migration_id: migration_id).first
new_url = URI::escape("#{course_path}/#{type_for_url}/#{object.id}")
end
end
@ -178,7 +178,7 @@ class ImportedHtmlConverter
mig_id ||= context.attachment_path_id_lookup_lower[alt_rel_path.downcase]
end
mig_id && context.attachments.find_by_migration_id(mig_id)
mig_id && context.attachments.where(migration_id: mig_id).first
end
def self.replace_relative_file_url(rel_path, context)
@ -219,7 +219,7 @@ class ImportedHtmlConverter
if context.respond_to?(:attachment_path_id_lookup) &&
context.attachment_path_id_lookup &&
context.attachment_path_id_lookup[rel_path]
file = context.attachments.find_by_migration_id(context.attachment_path_id_lookup[rel_path])
file = context.attachments.where(migration_id: context.attachment_path_id_lookup[rel_path]).first
if file && file.media_object
media_id = file.media_object.media_id
node['id'] = "media_comment_#{media_id}"

View File

@ -20,13 +20,13 @@ module LearningOutcomeContext
# of the context's associated accounts.
def available_outcome(outcome_id, opts={})
if opts[:allow_global]
outcome = LearningOutcome.global.find_by_id(outcome_id)
outcome = LearningOutcome.global.where(id: outcome_id).first
return outcome if outcome
end
outcome =
linked_learning_outcomes.find_by_id(outcome_id) ||
created_learning_outcomes.find_by_id(outcome_id)
linked_learning_outcomes.where(id: outcome_id).first ||
created_learning_outcomes.where(id: outcome_id).first
return outcome if outcome
unless opts[:recurse] == false

View File

@ -286,7 +286,7 @@ class MessageableUser
# skipping messageability constraints, do I see the user in that specific
# course, and if so with which enrollment type(s)?
if include_course_id && course = Course.find_by_id(include_course_id)
if include_course_id && course = Course.where(id: include_course_id).first
missing_users = users.reject{ |user| user.global_common_courses.keys.include?(course.global_id) }
if missing_user.present?
course.shard.activate do
@ -339,7 +339,7 @@ class MessageableUser
# skipping messageability constraints, do I see the user in that specific
# group?
if include_group_id && group = Group.find_by_id(include_group_id)
if include_group_id && group = Group.where(id: include_group_id).first
missing_users = users.reject{ |user| user.global_common_groups.keys.include?(group.global_id) }
if missing_users.present?
group.shard.activate do
@ -356,7 +356,7 @@ class MessageableUser
# filters the provided list of users to just those that are participants in
# the conversation
def participants_in_conversation(users, conversation_id)
conversation = Conversation.find_by_id(conversation_id)
conversation = Conversation.where(id: conversation_id).first
return [] unless conversation
conversation.shard.activate do
@ -405,7 +405,7 @@ class MessageableUser
# populated only with the course given.
def messageable_users_in_course_scope(course_or_id, enrollment_types=nil, options={})
return unless course_or_id
course = course_or_id.is_a?(Course) ? course_or_id : Course.find_by_id(course_or_id)
course = course_or_id.is_a?(Course) ? course_or_id : Course.where(id: course_or_id).first
return unless course
course.shard.activate do
@ -435,7 +435,7 @@ class MessageableUser
# populated only with the course of the given section.
def messageable_users_in_section_scope(section_or_id, enrollment_types=nil, options={})
return unless section_or_id
section = section_or_id.is_a?(CourseSection) ? section_or_id : CourseSection.find_by_id(section_or_id)
section = section_or_id.is_a?(CourseSection) ? section_or_id : CourseSection.where(id: section_or_id).first
return unless section
section.shard.activate do
@ -465,7 +465,7 @@ class MessageableUser
# populated only with the group given.
def messageable_users_in_group_scope(group_or_id, options={})
return unless group_or_id
group = group_or_id.is_a?(Group) ? group_or_id : Group.find_by_id(group_or_id)
group = group_or_id.is_a?(Group) ? group_or_id : Group.where(id: group_or_id).first
return unless group
group.shard.activate do
@ -825,7 +825,7 @@ class MessageableUser
group_ids = [fully_visible_scope, section_visible_scope].map{ |scope| scope.map(&:group_id) }.flatten.uniq
if group_ids.present?
Group.find_all_by_id(group_ids)
Group.where(id: group_ids).to_a
else
[]
end

View File

@ -10,7 +10,7 @@ module RubricContext
# return the rubric but only if it's available in either the context or one
# of the context's associated accounts.
def available_rubric(rubric_id, opts={})
outcome = rubrics.find_by_id(rubric_id)
outcome = rubrics.where(id: rubric_id).first
return outcome if outcome
unless opts[:recurse] == false

View File

@ -76,7 +76,7 @@ module SimpleTags
if tag =~ /\A((course|group)_\d+).*/
ary << $1
elsif tag =~ /\Asection_(\d+).*/
section = CourseSection.find_by_id($1)
section = CourseSection.where(id: $1).first
ary << section.course.asset_string if section
# TODO: allow user-defined tags, e.g. #foo
end

View File

@ -52,16 +52,16 @@ module SIS
raise ImportError, "No long_name given for abstract course #{abstract_course_id}" if long_name.blank?
raise ImportError, "Improper status \"#{status}\" for abstract course #{abstract_course_id}" unless status =~ /\Aactive|\Adeleted/i
course = AbstractCourse.find_by_root_account_id_and_sis_source_id(@root_account.id, abstract_course_id)
course = AbstractCourse.where(root_account_id: @root_account, sis_source_id: abstract_course_id).first
course ||= AbstractCourse.new
if !course.stuck_sis_fields.include?(:enrollment_term_id)
course.enrollment_term = @root_account.enrollment_terms.find_by_sis_source_id(term_id) || @root_account.default_enrollment_term
course.enrollment_term = @root_account.enrollment_terms.where(sis_source_id: term_id).first || @root_account.default_enrollment_term
end
course.root_account = @root_account
account = nil
account = Account.find_by_root_account_id_and_sis_source_id(@root_account.id, account_id) if account_id.present?
account ||= Account.find_by_root_account_id_and_sis_source_id(@root_account.id, fallback_account_id) if fallback_account_id.present?
account = @root_account.all_accounts.where(sis_source_id: account_id).first if account_id.present?
account ||= @root_account.all_accounts.where(sis_source_id: fallback_account_id).first if fallback_account_id.present?
course.account = account if account
course.account ||= @root_account

View File

@ -52,12 +52,12 @@ module SIS
parent = nil
if !parent_account_id.blank?
parent = @accounts_cache[parent_account_id]
parent ||= Account.find_by_root_account_id_and_sis_source_id(@root_account.id, parent_account_id)
parent ||= @root_account.all_accounts.where(sis_source_id: parent_account_id).first
raise ImportError, "Parent account didn't exist for #{account_id}" unless parent
@accounts_cache[parent.sis_source_id] = parent
end
account = Account.find_by_root_account_id_and_sis_source_id(@root_account.id, account_id)
account = @root_account.all_accounts.where(sis_source_id: account_id).first
if account.nil?
raise ImportError, "No name given for account #{account_id}, skipping" if name.blank?
raise ImportError, "Improper status \"#{status}\" for account #{account_id}, skipping" unless status =~ /\A(active|deleted)/i

View File

@ -66,7 +66,7 @@ module SIS
raise ImportError, "No long_name given for course #{course_id}" if long_name.blank? && abstract_course_id.blank?
raise ImportError, "Improper status \"#{status}\" for course #{course_id}" unless status =~ /\A(active|deleted|completed)/i
course = Course.find_by_root_account_id_and_sis_source_id(@root_account.id, course_id)
course = @root_account.all_courses.where(sis_source_id: course_id).first
if course.nil?
course = Course.new
state_changes << :created
@ -75,14 +75,14 @@ module SIS
end
course_enrollment_term_id_stuck = course.stuck_sis_fields.include?(:enrollment_term_id)
if !course_enrollment_term_id_stuck && term_id
term = @root_account.enrollment_terms.active.find_by_sis_source_id(term_id)
term = @root_account.enrollment_terms.active.where(sis_source_id: term_id).first
end
course.enrollment_term = term if term
course.root_account = @root_account
account = nil
account = Account.find_by_root_account_id_and_sis_source_id(@root_account.id, account_id) if account_id.present?
account ||= Account.find_by_root_account_id_and_sis_source_id(@root_account.id, fallback_account_id) if fallback_account_id.present?
account = @root_account.all_accounts.where(sis_source_id: account_id).first if account_id.present?
account ||= @root_account.all_accounts.where(sis_source_id: fallback_account_id).first if fallback_account_id.present?
course.account = account if account
course.account ||= @root_account
@ -117,7 +117,7 @@ module SIS
abstract_course = nil
if abstract_course_id.present?
abstract_course = AbstractCourse.find_by_root_account_id_and_sis_source_id(@root_account.id, abstract_course_id)
abstract_course = @root_account.root_abstract_courses.where(sis_source_id: abstract_course_id).first
@messages << "unknown abstract course id #{abstract_course_id}, ignoring abstract course reference" unless abstract_course
end

View File

@ -127,7 +127,7 @@ module SIS
else
root_account = @root_account
end
pseudo = root_account.pseudonyms.find_by_sis_user_id(user_id)
pseudo = root_account.pseudonyms.where(sis_user_id: user_id).first
unless pseudo
@messages << "User #{user_id} didn't exist for user enrollment"
@ -142,8 +142,8 @@ module SIS
end
end
@course ||= Course.find_by_root_account_id_and_sis_source_id(@root_account.id, course_id) unless course_id.blank?
@section ||= CourseSection.find_by_root_account_id_and_sis_source_id(@root_account.id, section_id) unless section_id.blank?
@course ||= @root_account.all_courses.where(sis_source_id: course_id).first unless course_id.blank?
@section ||= @root_account.course_sections.where(sis_source_id: section_id).first unless section_id.blank?
unless (@course || @section)
@messages << "Neither course #{course_id} nor section #{section_id} existed for user enrollment"
next
@ -190,12 +190,12 @@ module SIS
'TaEnrollment'
elsif role =~ /\Aobserver\z/i
if associated_user_id
pseudo = root_account.pseudonyms.find_by_sis_user_id(associated_user_id)
pseudo = root_account.pseudonyms.where(sis_user_id: associated_user_id).first
if status =~ /\Aactive/i
associated_enrollment = pseudo && @course.student_enrollments.find_by_user_id(pseudo.user_id)
associated_enrollment = pseudo && @course.student_enrollments.where(user_id: pseudo.user_id).first
else
# the observed user may have already been concluded
associated_enrollment = pseudo && @course.all_student_enrollments.find_by_user_id(pseudo.user_id)
associated_enrollment = pseudo && @course.all_student_enrollments.where(user_id: pseudo.user_id).first
end
end
'ObserverEnrollment'

View File

@ -49,13 +49,13 @@ module SIS
account = nil
if account_id.present?
account = @accounts_cache[account_id]
account ||= Account.find_by_root_account_id_and_sis_source_id(@root_account.id, account_id)
account ||= @root_account.all_accounts.where(sis_source_id: account_id).first
raise ImportError, "Parent account didn't exist for #{account_id}" unless account
@accounts_cache[account.sis_source_id] = account
end
account ||= @root_account
group = Group.find_by_root_account_id_and_sis_source_id(@root_account.id, group_id)
group = @root_account.all_groups.where(sis_source_id: group_id).first
unless group
raise ImportError, "No name given for group #{group_id}, skipping" if name.blank?
raise ImportError, "Improper status \"#{status}\" for group #{group_id}, skipping" unless status =~ /\A(available|closed|completed|deleted)/i

View File

@ -47,18 +47,18 @@ module SIS
raise ImportError, "No user_id given for a group user" if user_id.blank?
raise ImportError, "Improper status \"#{status}\" for a group user" unless status =~ /\A(accepted|deleted)/i
pseudo = Pseudonym.find_by_account_id_and_sis_user_id(@root_account.id, user_id)
pseudo = @root_account.pseudonyms.where(sis_user_id: user_id).first
user = pseudo.try(:user)
group = @groups_cache[group_id]
group ||= Group.find_by_root_account_id_and_sis_source_id(@root_account.id, group_id)
group ||= @root_account.all_groups.where(sis_source_id: group_id).first
@groups_cache[group.sis_source_id] = group if group
raise ImportError, "User #{user_id} didn't exist for group user" unless user
raise ImportError, "Group #{group_id} didn't exist for group user" unless group
# can't query group.group_memberships, since that excludes deleted memberships
group_membership = GroupMembership.find_by_group_id_and_user_id(group.id, user.id)
group_membership = GroupMembership.where(group_id: group, user_id: user).first
group_membership ||= group.group_memberships.build(:user => user)
group_membership.sis_batch_id = @batch.id if @batch

View File

@ -56,12 +56,11 @@ module SIS
raise ImportError, "No name given for section #{section_id} in course #{course_id}" if name.blank?
raise ImportError, "Improper status \"#{status}\" for section #{section_id} in course #{course_id}" unless status =~ /\Aactive|\Adeleted/i
course = Course.find_by_root_account_id_and_sis_source_id(@root_account.id, course_id)
course = @root_account.all_courses.where(sis_source_id: course_id).first
raise ImportError, "Section #{section_id} references course #{course_id} which doesn't exist" unless course
section = CourseSection.find_by_root_account_id_and_sis_source_id(@root_account.id, section_id)
section ||= course.course_sections.find_by_sis_source_id(section_id)
section ||= course.course_sections.new
section = @root_account.course_sections.where(sis_source_id: section_id).first
section ||= course.course_sections.where(sis_source_id: section_id).first_or_initialize
section.root_account = @root_account
# this is an easy way to load up the cache with data we already have
section.course = course if course.id == section.course_id
@ -91,7 +90,6 @@ module SIS
end
section.integration_id = integration_id
section.sis_source_id = section_id
if status =~ /active/i
section.workflow_state = 'active'
elsif status =~ /deleted/i

View File

@ -47,8 +47,7 @@ module SIS
raise ImportError, "No name given for term #{term_id}" if name.blank?
raise ImportError, "Improper status \"#{status}\" for term #{term_id}" unless status =~ /\Aactive|\Adeleted/i
term = @root_account.enrollment_terms.find_by_sis_source_id(term_id)
term ||= @root_account.enrollment_terms.new
term = @root_account.enrollment_terms.where(sis_source_id: term_id).first_or_initialize
# only update the name on new records, and ones that haven't been
# changed since the last sis import
@ -57,7 +56,6 @@ module SIS
end
term.integration_id = integration_id
term.sis_source_id = term_id
term.sis_batch_id = @batch.id if @batch
if status =~ /active/i
term.workflow_state = 'active'

View File

@ -88,7 +88,7 @@ module SIS
@logger.debug("Processing User #{user_row.inspect}")
user_id, login_id, status, first_name, last_name, email, password, ssha_password, integration_id, short_name, full_name, sortable_name = user_row
pseudo = @root_account.pseudonyms.find_by_sis_user_id(user_id.to_s)
pseudo = @root_account.pseudonyms.where(sis_user_id: user_id.to_s).first
pseudo_by_login = @root_account.pseudonyms.active.by_unique_id(login_id).first
pseudo ||= pseudo_by_login
pseudo ||= @root_account.pseudonyms.active.by_unique_id(email).first if email.present?

View File

@ -57,11 +57,11 @@ module SIS
raise ImportError, "No section_id given for a cross-listing" if section_id.blank?
raise ImportError, "Improper status \"#{status}\" for a cross-listing" unless status =~ /\A(active|deleted)\z/i
section = CourseSection.find_by_root_account_id_and_sis_source_id(@root_account.id, section_id)
section = @root_account.course_sections.where(sis_source_id: section_id).first
raise ImportError, "A cross-listing referenced a non-existent section #{section_id}" unless section
unless @course && @course.sis_source_id == xlist_course_id
@course = Course.find_by_root_account_id_and_sis_source_id(@root_account.id, xlist_course_id)
@course = @root_account.all_courses.where(sis_source_id: xlist_course_id).first
if !@course && status =~ /\Aactive\z/i
# no course with this crosslist id found, make a new course,
# using the section's current course as a template

View File

@ -11,7 +11,7 @@ def create_notification(values = {})
end
def create_scribd_mime_type(ext, name)
ScribdMimeType.find_or_create_by_extension_and_name(ext, name)
ScribdMimeType.where(extension: ext, name: name).first_or_create
end
namespace :db do
@ -94,7 +94,7 @@ namespace :db do
name = filename.split(".")[0]
unless name[0,1] == "_"
titled = name.titleize.gsub(/Sms/, 'SMS')
puts "No notification found in db for #{name}" unless Notification.find_by_name(titled)
puts "No notification found in db for #{name}" unless Notification.where(name: titled).first
end
end
Notification.all.each do |n|
@ -121,8 +121,8 @@ namespace :db do
def create_admin(email, password)
begin
pseudonym = Account.site_admin.pseudonyms.active.custom_find_by_unique_id(email)
pseudonym ||= Account.default.pseudonyms.active.custom_find_by_unique_id(email)
pseudonym = Account.site_admin.pseudonyms.active.by_unique_id(email).first
pseudonym ||= Account.default.pseudonyms.active.by_unique_id(email).first
user = pseudonym ? pseudonym.user : User.create!
user.register! unless user.registered?
unless pseudonym

View File

@ -204,7 +204,7 @@ class UnzipAttachment
# For every directory in the path...
# (-2 means all entries but the last, which should be a filename)
list[0..-2].each do |dir|
if new_dir = current.sub_folders.find_by_name(dir)
if new_dir = current.sub_folders.where(name: dir).first
current = new_dir
else
current = assert_folder(current, dir)

View File

@ -70,7 +70,7 @@ class UserList
def users
existing = @addresses.select { |a| a[:user_id] }
existing_users = Shard.partition_by_shard(existing, lambda { |a| a[:shard] } ) do |shard_existing|
User.find_all_by_id(shard_existing.map { |a| a[:user_id] })
User.where(id: shard_existing.map { |a| a[:user_id] })
end
non_existing = @addresses.select { |a| !a[:user_id] }

View File

@ -5,7 +5,7 @@ module Canvas::Oauth
let(:provider) { Provider.new('123') }
def stub_dev_key(key)
DeveloperKey.stubs(:find_by_id).returns(key)
DeveloperKey.stubs(:where).returns(stub(first: key))
end
describe 'initialization' do