Skip to content

Commit

Permalink
installed rubocop-performance gem and made necessary adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
briri committed Feb 1, 2023
1 parent 72e6f76 commit 813ee0d
Show file tree
Hide file tree
Showing 58 changed files with 135 additions and 123 deletions.
5 changes: 5 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
#
# Try to place any new Cops under their relevant section and in alphabetical order

require:
# - rubocop-rails
# - rubocop-rspec
- rubocop-performance

AllCops:
# Show the name of the cops being voilated in the feedback
DisplayCopNames: true
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## v4.0.3

### Maintenance
- Installed rubocop-performance gem and made suggested changes

### Added

- Added CHANGELOG.md and Danger Github Action [#3257](https://github.com/DMPRoadmap/roadmap/issues/3257)
Expand Down
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,9 @@ group :ci, :development do
# Thread-safety checks via static analysis. A plugin for the RuboCop code style
# enforcing & linting tool.
# gem 'rubocop-thread_safety'

# Performance checks by Rubocop
gem 'rubocop-performance', require: false
end

group :development do
Expand Down
7 changes: 6 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,9 @@ GEM
parser (>= 3.1.1.0)
rubocop-i18n (3.0.0)
rubocop (~> 1.0)
rubocop-performance (1.15.2)
rubocop (>= 1.7.0, < 2.0)
rubocop-ast (>= 0.4.0)
ruby-progressbar (1.11.0)
ruby2_keywords (0.0.5)
ruby_dig (0.0.2)
Expand Down Expand Up @@ -540,6 +543,7 @@ GEM
zeitwerk (2.6.6)

PLATFORMS
arm64-darwin-21
arm64-darwin-22
x86_64-linux

Expand Down Expand Up @@ -602,6 +606,7 @@ DEPENDENCIES
rspec-rails
rubocop
rubocop-i18n
rubocop-performance
sass-rails
sassc-rails
selenium-webdriver
Expand All @@ -625,4 +630,4 @@ RUBY VERSION
ruby 2.7.6p219

BUNDLED WITH
2.3.26
2.4.2
4 changes: 2 additions & 2 deletions app/controllers/answers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ def permitted_params
# rubocop:enable Metrics/AbcSize

def check_answered(section, q_array, all_answers)
n_qs = section.questions.select { |question| q_array.include?(question.id) }.length
n_ans = all_answers.select { |ans| q_array.include?(ans.question.id) and ans.answered? }.length
n_qs = section.questions.count { |question| q_array.include?(question.id) }
n_ans = all_answers.count { |ans| q_array.include?(ans.question.id) and ans.answered? }
[n_qs, n_ans]
end
end
2 changes: 1 addition & 1 deletion app/controllers/api/v0/plans_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def index
if params['updated_after'].present? || params['updated_before'].present?
@plans = @plans.where(updated_at: dates_to_range(params, 'updated_after', 'updated_before'))
end
if params['remove_tests'].present? && params['remove_tests'].downcase == 'true'
if params['remove_tests'].present? && params['remove_tests'].casecmp('true').zero?
@plans = @plans.where.not(visibility: Plan.visibilities[:is_test])
end
# filter on funder (dmptemplate_id)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v0/statistics_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def plans
raise Pundit::NotAuthorizedError unless Api::V0::StatisticsPolicy.new(@user, :statistics).plans?

@org_plans = @user.org.plans
if params['remove_tests'].present? && params['remove_tests'].downcase == 'true'
if params['remove_tests'].present? && params['remove_tests'].casecmp('true').zero?
@org_plans = @org_plans.where.not(visibility: Plan.visibilities[:is_test])
end
if params['start_date'].present? || params['end_date'].present?
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v1/plans_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def plan_exists?(json:)

# Get the Plan's owner
def determine_owner(client:, plan:)
contact = plan.contributors.select(&:data_curation?).first
contact = plan.contributors.find(&:data_curation?)
# Use the contact if it was sent in and has an affiliation defined
return contact if contact.present? && contact.org.present?

Expand Down
4 changes: 2 additions & 2 deletions app/controllers/concerns/conditional_user_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ module ConditionalUserMailer
#
# Returns Boolean
def deliver_if(key:, recipients: [], &block)
return false unless block_given?
return false unless block

Array(recipients).each do |recipient|
email_hash = recipient.get_preferences('email').with_indifferent_access
# Violation of rubocop's DoubleNegation check
# preference_value = !!email_hash.dig(*key.to_s.split("."))
preference_value = email_hash.dig(*key.to_s.split('.'))
block.call(recipient) if preference_value
yield(recipient) if preference_value
end

true
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/concerns/paginable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Paginable

##
# Regex to validate sort_field param is safe
SORT_COLUMN_FORMAT = /[\w_]+\.[\w_]+$/.freeze
SORT_COLUMN_FORMAT = /[\w_]+\.[\w_]+$/

PAGINATION_QUERY_PARAMS = %i[page sort_field sort_direction
search controller action].freeze
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/org_admin/plans_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def download_plans
raise Pundit::NotAuthorizedError unless current_user.present? && current_user.can_org_admin?

org = current_user.org
file_name = org.name.gsub(/ /, '_')
file_name = org.name.tr(' ', '_')
.gsub(/[.;,]/, '')
header_cols = [
_('Project title').to_s,
Expand Down
8 changes: 4 additions & 4 deletions app/controllers/org_admin/templates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class TemplatesController < ApplicationController
def index
authorize Template
templates = Template.latest_version.where(customization_of: nil)
published = templates.select { |t| t.published? || t.draft? }.length
published = templates.count { |t| t.published? || t.draft? }

@orgs = Org.includes(:identifiers).managed
@title = _('All Templates')
Expand All @@ -40,7 +40,7 @@ def organisational
authorize Template
templates = Template.latest_version_per_org(current_user.org.id)
.where(customization_of: nil, org_id: current_user.org.id)
published = templates.select { |t| t.published? || t.draft? }.length
published = templates.count { |t| t.published? || t.draft? }

@orgs = current_user.can_super_admin? ? Org.includes(:identifiers).all : nil
@title = if current_user.can_super_admin?
Expand Down Expand Up @@ -78,7 +78,7 @@ def customisable
customizations = customizations.select do |t|
funder_template_families.include?(t.customization_of)
end
published = customizations.select { |t| t.published? || t.draft? }.length
published = customizations.count { |t| t.published? || t.draft? }

@orgs = current_user.can_super_admin? ? Org.includes(:identifiers).all : []
@title = _('Customizable Templates')
Expand Down Expand Up @@ -328,7 +328,7 @@ def template_export
@formatting = Settings::Template::DEFAULT_SETTINGS[:formatting]

begin
safe_title = @template.title.gsub(/[^a-zA-Z\d\s]/, '').gsub(/ /, '_')
safe_title = @template.title.gsub(/[^a-zA-Z\d\s]/, '').tr(' ', '_')
file_name = "#{safe_title}_v#{@template.version}"
respond_to do |format|
format.docx do
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/plan_exports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def file_name
# Sanitize bad characters and replace spaces with underscores
ret = @plan.title
ret = ret.strip.gsub(/\s+/, '_')
ret = ret.gsub(/"/, '')
ret = ret.delete('"')
ret = ActiveStorage::Filename.new(ret).sanitized
# limit the filename length to 100 chars. Windows systems have a MAX_PATH allowance
# of 255 characters, so this should provide enough of the title to allow the user
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/plans_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def edit
.find(params[:id])
authorize plan
phase_id = params[:phase_id].to_i
phase = plan.template.phases.select { |p| p.id == phase_id }.first
phase = plan.template.phases.find { |p| p.id == phase_id }
raise ActiveRecord::RecordNotFound if phase.nil?

guidance_groups = GuidanceGroup.where(published: true, id: plan.guidance_group_ids)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/public_pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def template_export
@formatting = Settings::Template::DEFAULT_SETTINGS[:formatting]

begin
file_name = @template.title.gsub(/[^a-zA-Z\d\s]/, '').gsub(/ /, '_')
file_name = @template.title.gsub(/[^a-zA-Z\d\s]/, '').tr(' ', '_')
file_name = "#{file_name}_v#{@template.version}"
respond_to do |format|
format.docx do
Expand Down
6 changes: 2 additions & 4 deletions app/helpers/conditions_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,9 @@ def num_section_questions(plan, section, phase = nil)

phase = plan.template
.phases
.select { |ph| ph.number == phase[:number] }
.first
.find { |ph| ph.number == phase[:number] }
section = phase.sections
.select { |s| s.phase_id == phase.id && s.title == section[:title] }
.first
.find { |s| s.phase_id == phase.id && s.title == section[:title] }
end
count = 0
plan_remove_list = remove_list(plan)
Expand Down
2 changes: 1 addition & 1 deletion app/models/concerns/exportable_plan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def prepare_research_outputs_for_csv(csv, _headings, hash)

csv << [_('Research Outputs: ')]
# Convert the hash keys to column headers
csv << hash[:research_outputs].first.keys.map { |key| key.to_s.capitalize.gsub('_', ' ') }
csv << hash[:research_outputs].first.keys.map { |key| key.to_s.capitalize.tr('_', ' ') }
hash[:research_outputs].each do |research_output|
csv << research_output.values
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/concerns/identifiable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def self.from_identifiers(array:)
# gets the identifier for the scheme
def identifier_for_scheme(scheme:)
scheme = IdentifierScheme.by_name(scheme.downcase).first if scheme.is_a?(String)
identifiers.select { |id| id.identifier_scheme == scheme }.last
identifiers.reverse.find { |id| id.identifier_scheme == scheme }
end

# Combines the existing identifiers with the new ones
Expand Down
2 changes: 1 addition & 1 deletion app/models/exported_plan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def project_description
end

def owner
plan.roles.to_a.select(&:creator?).first.user
plan.roles.to_a.find(&:creator?)&.user
end

def funder
Expand Down
2 changes: 1 addition & 1 deletion app/models/identifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def identifier_format
return 'ark' if value.include?('ark:')

doi_regex = %r{(doi:)?[0-9]{2}\.[0-9]+/.}
return 'doi' if value =~ doi_regex
return 'doi' if value.match?(doi_regex)

return 'url' if value.starts_with?('http')

Expand Down
2 changes: 1 addition & 1 deletion app/models/language.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Language < ApplicationRecord

ABBREVIATION_MAXIMUM_LENGTH = 5

ABBREVIATION_FORMAT = /\A[a-z]{2}(-[A-Z]{2})?\Z/.freeze
ABBREVIATION_FORMAT = /\A[a-z]{2}(-[A-Z]{2})?\Z/

NAME_MAXIMUM_LENGTH = 20

Expand Down
10 changes: 6 additions & 4 deletions app/models/plan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class Plan < ApplicationRecord
# = Constants =
# =============

DMP_ID_TYPES = %w[ark doi].freeze

# Returns visibility message given a Symbol type visibility passed, otherwise
# nil
VISIBILITY_MESSAGE = {
Expand Down Expand Up @@ -290,7 +292,7 @@ def settings(key)
# rubocop:disable Metrics/AbcSize, Style/OptionalBooleanParameter
def answer(qid, create_if_missing = true)
answer = answers.select { |a| a.question_id == qid }
.max { |a, b| a.created_at <=> b.created_at }
.max_by(&:created_at)
if answer.nil? && create_if_missing
question = Question.find(qid)
answer = Answer.new
Expand Down Expand Up @@ -440,7 +442,7 @@ def latest_update
# Returns nil
def owner
r = roles.select { |rr| rr.active && rr.administrator }
.min { |a, b| a.created_at <=> b.created_at }
.min_by(&:created_at)
r.nil? ? nil : r.user
end

Expand Down Expand Up @@ -510,7 +512,7 @@ def authors
#
# Returns Integer
def num_answered_questions(phase = nil)
return answers.select(&:answered?).length unless phase.present?
return answers.count(&:answered?) unless phase.present?

answered = answers.select do |answer|
answer.answered? && phase.questions.include?(answer.question)
Expand Down Expand Up @@ -577,7 +579,7 @@ def deactivate!

# Returns the plan's identifier (either a DOI/ARK)
def landing_page
identifiers.select { |i| %w[doi ark].include?(i.identifier_format) }.first
identifiers.find { |i| DMP_ID_TYPES.include?(i.identifier_format) }
end

# Since the Grant is not a normal AR association, override the getter and setter
Expand Down
5 changes: 2 additions & 3 deletions app/models/question.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def example_answers(org_ids)
org_ids = Array(org_ids)
annotations.select { |a| org_ids.include?(a.org_id) }
.select(&:example_answer?)
.sort { |a, b| a.created_at <=> b.created_at }
.sort_by(&:created_at)
end

alias get_example_answers example_answers
Expand All @@ -183,8 +183,7 @@ def example_answers(org_ids)
# Returns Annotation
def guidance_annotation(org_id)
annotations.select { |a| a.org_id == org_id }
.select(&:guidance?)
.first
.find(&:guidance?)
end

alias get_guidance_annotation guidance_annotation
Expand Down
4 changes: 2 additions & 2 deletions app/models/settings/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class Template < RailsSettings::SettingObject
'Roboto, Arial, Sans-Serif'
].freeze

VALID_FONT_SIZE_RANGE = (8..14).freeze
VALID_MARGIN_RANGE = (5..25).freeze
VALID_FONT_SIZE_RANGE = (8..14)
VALID_MARGIN_RANGE = (5..25)

VALID_ADMIN_FIELDS = %w[project_name project_identifier grant_title
principal_investigator project_data_contact
Expand Down
6 changes: 3 additions & 3 deletions app/presenters/api/v1/contributor_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ class << self
# Convert the specified role into a CRediT Taxonomy URL
def role_as_uri(role:)
return nil unless role.present?
return 'other' if role.to_s.downcase == 'other'
return 'other' if role.to_s.casecmp('other').zero?

"#{Contributor::ONTOLOGY_BASE_URL}/#{role.to_s.downcase.gsub('_', '-')}"
"#{Contributor::ONTOLOGY_BASE_URL}/#{role.to_s.downcase.tr('_', '-')}"
end

def contributor_id(identifiers:)
identifiers.select { |id| id.identifier_scheme.name == 'orcid' }.first
identifiers.find { |id| id.identifier_scheme.name == 'orcid' }
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/presenters/api/v1/org_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ module V1
class OrgPresenter
class << self
def affiliation_id(identifiers:)
ident = identifiers.select { |id| id.identifier_scheme&.name == 'ror' }.first
ident = identifiers.find { |id| id.identifier_scheme&.name == 'ror' }
return ident if ident.present?

identifiers.select { |id| id.identifier_scheme&.name == 'fundref' }.first
identifiers.find { |id| id.identifier_scheme&.name == 'fundref' }
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/presenters/api/v1/plan_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def initialize(plan:)
# Extract the ARK or DOI for the DMP OR use its URL if none exists
def identifier
doi = @plan.identifiers.select do |id|
%w[ark doi].include?(id.identifier_format)
::Plan::DMP_ID_TYPES.include?(id.identifier_format)
end
return doi.first if doi.first.present?

Expand Down
2 changes: 1 addition & 1 deletion app/presenters/api/v1/research_output_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def fetch_q_and_a(themes:)
ret = themes.map do |theme|
qs = @plan.questions.select { |q| q.themes.collect(&:title).include?(theme) }
descr = qs.map do |q|
a = @plan.answers.select { |ans| ans.question_id = q.id }.first
a = @plan.answers.find { |ans| ans.question_id = q.id }
next unless a.present? && !a.blank?

"<strong>Question:</strong> #{q.text}<br><strong>Answer:</strong> #{a.text}"
Expand Down
Loading

0 comments on commit 813ee0d

Please sign in to comment.