From e92d4d1b25934dc3693019225b0978bf3fb9c4b9 Mon Sep 17 00:00:00 2001 From: Pieter Verschaffelt Date: Tue, 14 Mar 2023 10:52:03 +0100 Subject: [PATCH 1/2] Add Rails config to Rubocop linter --- .rubocop.yml | 3 +++ Gemfile | 2 ++ Gemfile.lock | 5 +++++ app/controllers/api/api_controller.rb | 2 +- app/models/peptide.rb | 2 +- app/models/uniprot_entry.rb | 12 ++++++------ 6 files changed, 18 insertions(+), 8 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 5be2d38..1eea04c 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,3 +1,6 @@ +require: + - rubocop-rails + AllCops: Exclude: - "Gemfile" diff --git a/Gemfile b/Gemfile index 8e2f9fd..91164bd 100644 --- a/Gemfile +++ b/Gemfile @@ -65,3 +65,5 @@ gem "capistrano", "~> 3.17" gem "capistrano-bundler", "~> 2.1" gem "capistrano-rvm", "~> 0.1.2" + +gem 'rubocop-rails', '~> 2.18' diff --git a/Gemfile.lock b/Gemfile.lock index f3467f9..d10619c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -207,6 +207,10 @@ GEM unicode-display_width (>= 1.4.0, < 3.0) rubocop-ast (1.21.0) parser (>= 3.1.1.0) + rubocop-rails (2.18.0) + activesupport (>= 4.2.0) + rack (>= 1.1) + rubocop (>= 1.33.0, < 2.0) ruby-progressbar (1.11.0) ruby2_keywords (0.0.5) sawyer (0.9.2) @@ -247,6 +251,7 @@ DEPENDENCIES rails-controller-testing (~> 1.0) responders (~> 3.0) rubocop (~> 1.36) + rubocop-rails (~> 2.18) stathat (~> 0.1.7) tzinfo-data diff --git a/app/controllers/api/api_controller.rb b/app/controllers/api/api_controller.rb index c458e22..ff88bab 100644 --- a/app/controllers/api/api_controller.rb +++ b/app/controllers/api/api_controller.rb @@ -264,7 +264,7 @@ def taxa2tree # param[names]: "true" or "false", Include the lineage names def taxonomy @result = @query.where(id: @input) - @result = Hash[@result.map { |t| [t.id, t] }] + @result = @result.index_by(&:id) @input_order = @input.select { |i| @result.key? i.to_i } @result = @input_order.map { |i| @result[i.to_i] } respond_with(@result) diff --git a/app/models/peptide.rb b/app/models/peptide.rb index 5885d0a..0195c5b 100644 --- a/app/models/peptide.rb +++ b/app/models/peptide.rb @@ -13,7 +13,7 @@ class Peptide < ApplicationRecord belongs_to :uniprot_entry belongs_to :sequence - belongs_to :original_sequence, foreign_key: 'original_sequence_id', primary_key: 'id', class_name: 'Sequence' + belongs_to :original_sequence, primary_key: 'id', class_name: 'Sequence' # since this is a read-only model, these validations aren't used validates :sequence_id, presence: true diff --git a/app/models/uniprot_entry.rb b/app/models/uniprot_entry.rb index 580228d..78b92c0 100644 --- a/app/models/uniprot_entry.rb +++ b/app/models/uniprot_entry.rb @@ -26,9 +26,9 @@ class UniprotEntry < ApplicationRecord has_many :go_terms, through: :go_cross_references has_many :interpro_entries, through: :interpro_cross_references - belongs_to :taxon, foreign_key: 'taxon_id', - primary_key: 'id', - class_name: 'Taxon' + belongs_to :taxon, + primary_key: 'id', + class_name: 'Taxon' belongs_to :lineage, foreign_key: 'taxon_id', primary_key: 'taxon_id', @@ -63,7 +63,7 @@ def self.summarize_fa(entries) GoCrossReference .where(uniprot_entry_id: id_batch) .all - .each do |cr| + .find_each do |cr| ups_with_go.add(cr.uniprot_entry_id) # Also count in how many proteins this GO term occurs data[cr.go_term_code] += 1 @@ -77,7 +77,7 @@ def self.summarize_fa(entries) EcCrossReference .where(uniprot_entry_id: id_batch) .all - .each do |cr| + .find_each do |cr| ups_with_ec.add(cr.uniprot_entry_id) # Also count in how many proteins this EC number occurs data["EC:#{cr.ec_number_code}"] += 1 @@ -91,7 +91,7 @@ def self.summarize_fa(entries) InterproCrossReference .where(uniprot_entry_id: id_batch) .all - .each do |cr| + .find_each do |cr| ups_with_ipr.add(cr.uniprot_entry_id) data["IPR:#{cr.interpro_entry_code}"] += 1 end From 81dffab99cd29459dbe0df0c78d3322cacf845e2 Mon Sep 17 00:00:00 2001 From: Pieter Verschaffelt Date: Tue, 14 Mar 2023 14:09:30 +0100 Subject: [PATCH 2/2] Properly configure RuboCop (with Rails integration) --- .rubocop.yml | 7 +++++++ app/controllers/datasets_controller.rb | 1 - 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/.rubocop.yml b/.rubocop.yml index 1eea04c..b6a4609 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -22,6 +22,13 @@ Style/ClassAndModuleChildren: Style/FrozenStringLiteralComment: Enabled: false +Rails/InverseOf: + Enabled: false +Rails/HasManyOrHasOneDependent: + Enabled: false +Rails/HelperInstanceVariable: + Enabled: false + # disable for now Layout/LineLength: Enabled: false diff --git a/app/controllers/datasets_controller.rb b/app/controllers/datasets_controller.rb index 489f62c..4047a40 100644 --- a/app/controllers/datasets_controller.rb +++ b/app/controllers/datasets_controller.rb @@ -1,7 +1,6 @@ class DatasetsController < HandleOptionsController before_action :set_headers, only: %i[sampledata] before_action :default_format_json, only: %i[sampledata] - before_action :authorize, only: %i[new edit create update destroy] def sampledata @datasets = Dataset.includes(:dataset_items).all