Skip to content

Commit

Permalink
RuboCop: Performance/StartWith
Browse files Browse the repository at this point in the history
auto-corrected, with post-review checking for possible nilness

Change-Id: I89c30b92691a2a5f73d98d9c8ac721c50d3a4ba7
Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/278014
Tested-by: Service Cloud Jenkins <[email protected]>
Reviewed-by: Simon Williams <[email protected]>
QA-Review: Cody Cutrer <[email protected]>
Product-Review: Cody Cutrer <[email protected]>
  • Loading branch information
ccutrer committed Nov 11, 2021
1 parent 550699d commit 551e4d1
Show file tree
Hide file tree
Showing 15 changed files with 18 additions and 16 deletions.
2 changes: 2 additions & 0 deletions .rubocop.common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ Naming/FileName:

Performance/RegexpMatch:
Severity: error
Performance/StartWith:
Severity: error
Performance/StringInclude:
Severity: error
Performance/StringReplacement:
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1000,7 +1000,7 @@ def get_context(include_deleted: false)
params[:context_id] = params[:course_section_id]
params[:context_type] = "CourseSection"
@context = api_find(CourseSection, params[:course_section_id])
elsif request.path.match(/\A\/profile/) || request.path == '/' || request.path.match(/\A\/dashboard\/files/) || request.path.match(/\A\/calendar/) || request.path.match(/\A\/assignments/) || request.path.match(/\A\/files/) || request.path == '/api/v1/calendar_events/visible_contexts'
elsif request.path.start_with?('/profile') || request.path == '/' || request.path.start_with?('/dashboard/files') || request.path.start_with?('/calendar') || request.path.start_with?('/assignments') || request.path.start_with?('/files') || request.path == '/api/v1/calendar_events/visible_contexts'
# ^ this should be split out into things on the individual controllers
@context_is_current_user = true
@context = @current_user
Expand Down Expand Up @@ -1243,7 +1243,7 @@ def quota_exceeded(context = nil, redirect = nil)
# that we can offer the feeds without requiring password authentication.
def get_feed_context(opts = {})
pieces = params[:feed_code].split("_", 2)
if params[:feed_code].match?(/\Agroup_membership/)
if params[:feed_code].start_with?('group_membership')
pieces = ["group_membership", params[:feed_code].split("_", 3)[-1]]
end
@context = nil
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/calendar_events_api_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1431,7 +1431,7 @@ def require_authorization
@errors = {}
user = @observee || @current_user
# appointment groups show up here in find-appointment mode; give them a free ride
ag_count = (params[:context_codes] || []).count { |code| code =~ /\Aappointment_group_/ }
ag_count = (params[:context_codes] || []).count { |code| code.start_with?('appointment_group_') }
context_limit = @domain_root_account.settings[:calendar_contexts_limit] || 10
codes = (params[:context_codes] || [user.asset_string])[0, context_limit + ag_count]
# also accept a more compact comma-separated list of appointment group ids
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/external_content_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def success
@retrieved_data = {}
if params[:service] == 'equella'
params.each do |key, value|
if key.to_s.match?(/\Aeq_/)
if key.to_s.start_with?('eq_')
@retrieved_data[key.to_s.gsub(/\Aeq_/, "")] = value
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/file_previews_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def show
url = GoogleDocsPreview.url_for(@file)
redirect_to('//docs.google.com/viewer?' + { embedded: true, url: url }.to_query)
# images
elsif %r{\Aimage/}.match?(@file.content_type)
elsif @file.content_type&.start_with?('image/')
render template: 'file_previews/img_preview', layout: false
# media files
elsif %r{\A(audio|video)/}.match?(@file.content_type)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/files_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ def send_attachment(attachment)
# download param because the download param is used all over the place to mean stuff
# other than actually download the file. Long term we probably ought to audit the files
# controller, make download mean download, and remove download_frd.
if params[:inline] && !params[:download_frd] && attachment.content_type && (attachment.content_type.match(/\Atext/) || attachment.mime_class == 'text' || attachment.mime_class == 'html' || attachment.mime_class == 'code' || attachment.mime_class == 'image')
if params[:inline] && !params[:download_frd] && attachment.content_type && (attachment.content_type&.start_with?('text') || attachment.mime_class == 'text' || attachment.mime_class == 'html' || attachment.mime_class == 'code' || attachment.mime_class == 'image')
send_stored_file(attachment)
elsif attachment.inline_content? && !params[:download_frd] && !@context.is_a?(AssessmentQuestion)
if params[:file_path] || !params[:wrap]
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/attachment_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def set_cache_header(attachment, direct)
# investigate opportunities to reuse JWTs when the same user requests the
# same file within a reasonable window of time, so that the URL redirected
# too can still take advantage of browser caching.
unless (attachment.instfs_hosted? && !direct) || attachment.content_type.match(/\Atext/) || attachment.extension == '.html' || attachment.extension == '.htm'
unless (attachment.instfs_hosted? && !direct) || attachment.content_type&.start_with?('text') || attachment.extension == '.html' || attachment.extension == '.htm'
cancel_cache_buster
# set cache to expire whenever the s3 url does (or one day if local or inline proxy), max-age take seconds, and Expires takes a date
ttl = direct ? 1.day : attachment.url_ttl
Expand Down
2 changes: 1 addition & 1 deletion app/models/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ def reset_uuid!
end

def inline_content?
self.content_type.match(/\Atext/) || self.extension == '.html' || self.extension == '.htm' || self.extension == '.swf'
self.content_type.start_with?('text') || self.extension == '.html' || self.extension == '.htm' || self.extension == '.swf'
end

def self.shared_secret
Expand Down
2 changes: 1 addition & 1 deletion app/models/grading_standard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def ordered_scheme

def place_in_scheme(key_name)
# look for keys with only digits and a single '.'
if key_name.to_s&.match?((/\A(\d*[.])?\d+\Z/))
if key_name.to_s&.match?(/\A(\d*[.])?\d+\Z/)
# compare numbers
# second condition to filter letters so zeros work properly ("A".to_d == 0)
ordered_scheme.index { |g, _| g.to_d == key_name.to_d && g.to_s.match(/\A(\d*[.])?\d+\Z/) }
Expand Down
2 changes: 1 addition & 1 deletion app/models/kaltura_media_file_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def add_media_files(attachments, wait_for_completion)
files << {
:name => attachment.display_name,
:url => attachment.public_download_url,
:media_type => (attachment.content_type || "").match?(/\Avideo/) ? 'video' : 'audio',
:media_type => attachment.content_type&.start_with?('video') ? 'video' : 'audio',
:partner_data => build_partner_data(attachment)
}
end
Expand Down
4 changes: 2 additions & 2 deletions config/initializers/i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def infer_pluralization_hash(default, *args)
module I18nUtilities
def before_label(text_or_key, default_value = nil, *args)
if default_value
text_or_key = "labels.#{text_or_key}" unless /\A#/.match?(text_or_key.to_s)
text_or_key = "labels.#{text_or_key}" unless text_or_key.to_s.start_with?('#')
text_or_key = respond_to?(:t) ? t(text_or_key, default_value, *args) : I18n.t(text_or_key, default_value, *args)
end
I18n.t("#before_label_wrapper", "%{text}:", :text => text_or_key)
Expand All @@ -161,7 +161,7 @@ def _label_symbol_translation(method, text, options)
end
text = method if text.nil? && method.is_a?(Symbol)
if text.is_a?(Symbol)
text = "labels.#{text}" unless /\A#/.match?(text.to_s)
text = "labels.#{text}" unless text.to_s.start_with?('#')
text = t(text, options.delete(:en))
end
text = before_label(text) if options.delete(:before)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ def create_config_with_mock(seconds)
log_file_url = "https://www.instructuremedia.com/bulk_uploads/12345.log"
bulk_upload_add_stub = stub_request(:post, "https://www.instructuremedia.com/api_v3/")
.with(:query => hash_including(:service => 'bulkUpload', :action => 'add'))
.with { |request| request.headers['Content-Type'] =~ /\Amultipart\/form-data/ }
.with { |request| request.headers['Content-Type'].start_with?('multipart/form-data') }
.to_return(:body => <<-XML)
<result>
<id>batch_job_12345</id>
Expand Down
2 changes: 1 addition & 1 deletion lib/canvas/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def metadata(name)
end

def translate(key, default, options = {})
key = "canvas.plugins.#{@id}.#{key}" unless /\A#/.match?(key)
key = "canvas.plugins.#{@id}.#{key}" unless key.start_with?('#')
I18n.translate(key, default, options)
end
alias_method :t, :translate
Expand Down
2 changes: 1 addition & 1 deletion lib/sis/section_importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def add_section(section_id, course_id, name, status, start_date = nil, end_date
if section.course_id != course.id
if section.nonxlist_course_id
# this section is crosslisted
if (section.nonxlist_course_id != course.id && !section.stuck_sis_fields.include?(:course_id)) || (section.course.workflow_state == 'deleted' && !!(status =~ /\Aactive/))
if (section.nonxlist_course_id != course.id && !section.stuck_sis_fields.include?(:course_id)) || (section.course.workflow_state == 'deleted' && status.start_with?('active'))
# but the course id we were given didn't match the crosslist info
# we have, so, uncrosslist and move
@course_ids_to_update_associations.merge [course.id, section.course_id, section.nonxlist_course_id]
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/rspec.rake
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

# Don't load rspec if running "rake gems:*"
unless Rails.env.production? || ARGV.any? { |a| a =~ /\Agems/ }
unless Rails.env.production? || ARGV.any? { |a| a.start_with?('gems') }

begin
require 'rspec/core/rake_task'
Expand Down

0 comments on commit 551e4d1

Please sign in to comment.