SpeciesFileGroup/taxonworks

View on GitHub
app/models/taxon_name_classification.rb

Summary

Maintainability
C
1 day
Test Coverage

Method has too many lines. [67/25]
Open

  def set_cached_names_for_taxon_names
    begin
      TaxonName.transaction_with_retry do
        t = taxon_name

This cop checks if the length of a method exceeds some maximum value. Comment lines can optionally be ignored. The maximum allowed length is configurable.

Class TaxonNameClassification has 32 methods (exceeds 20 allowed). Consider refactoring.
Open

class TaxonNameClassification < ApplicationRecord
  include Housekeeping
  include Shared::Citations
  include Shared::Notes
  include Shared::IsData
Severity: Minor
Found in app/models/taxon_name_classification.rb - About 4 hrs to fix

    Method set_cached_names_for_taxon_names has 67 lines of code (exceeds 25 allowed). Consider refactoring.
    Open

      def set_cached_names_for_taxon_names
        begin
          TaxonName.transaction_with_retry do
            t = taxon_name
    
    
    Severity: Major
    Found in app/models/taxon_name_classification.rb - About 2 hrs to fix

      Method set_cached_names_for_taxon_names has a Cognitive Complexity of 14 (exceeds 5 allowed). Consider refactoring.
      Open

        def set_cached_names_for_taxon_names
          begin
            TaxonName.transaction_with_retry do
              t = taxon_name
      
      
      Severity: Minor
      Found in app/models/taxon_name_classification.rb - About 1 hr to fix

      Cognitive Complexity

      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

      A method's cognitive complexity is based on a few simple rules:

      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
      • Code is considered more complex for each "break in the linear flow of the code"
      • Code is considered more complex when "flow breaking structures are nested"

      Further reading

      Avoid too many return statements within this method.
      Open

          return nil
      Severity: Major
      Found in app/models/taxon_name_classification.rb - About 30 mins to fix

        Avoid using update_columns because it skips validations.
        Open

                    t1.update_columns(

        This cop checks for the use of methods which skip validations which are listed in https://guides.rubyonrails.org/active_record_validations.html#skipping-validations

        Methods may be ignored from this rule by configuring a Whitelist.

        Example:

        # bad
        Article.first.decrement!(:view_count)
        DiscussionBoard.decrement_counter(:post_count, 5)
        Article.first.increment!(:view_count)
        DiscussionBoard.increment_counter(:post_count, 5)
        person.toggle :active
        product.touch
        Billing.update_all("category = 'authorized', author = 'David'")
        user.update_attribute(:website, 'example.com')
        user.update_columns(last_request_at: Time.current)
        Post.update_counters 5, comment_count: -1, action_count: 1
        
        # good
        user.update(website: 'example.com')
        FileUtils.touch('file')

        Example: Whitelist: ["touch"]

        # bad
        DiscussionBoard.decrement_counter(:post_count, 5)
        DiscussionBoard.increment_counter(:post_count, 5)
        person.toggle :active
        
        # good
        user.touch

        Avoid using update_columns because it skips validations.
        Open

                    t1.update_columns(

        This cop checks for the use of methods which skip validations which are listed in https://guides.rubyonrails.org/active_record_validations.html#skipping-validations

        Methods may be ignored from this rule by configuring a Whitelist.

        Example:

        # bad
        Article.first.decrement!(:view_count)
        DiscussionBoard.decrement_counter(:post_count, 5)
        Article.first.increment!(:view_count)
        DiscussionBoard.increment_counter(:post_count, 5)
        person.toggle :active
        product.touch
        Billing.update_all("category = 'authorized', author = 'David'")
        user.update_attribute(:website, 'example.com')
        user.update_columns(last_request_at: Time.current)
        Post.update_counters 5, comment_count: -1, action_count: 1
        
        # good
        user.update(website: 'example.com')
        FileUtils.touch('file')

        Example: Whitelist: ["touch"]

        # bad
        DiscussionBoard.decrement_counter(:post_count, 5)
        DiscussionBoard.increment_counter(:post_count, 5)
        person.toggle :active
        
        # good
        user.touch

        Avoid using update_column because it skips validations.
        Open

                      c.update_column(:cached_valid_taxon_name_id, vn.id)

        This cop checks for the use of methods which skip validations which are listed in https://guides.rubyonrails.org/active_record_validations.html#skipping-validations

        Methods may be ignored from this rule by configuring a Whitelist.

        Example:

        # bad
        Article.first.decrement!(:view_count)
        DiscussionBoard.decrement_counter(:post_count, 5)
        Article.first.increment!(:view_count)
        DiscussionBoard.increment_counter(:post_count, 5)
        person.toggle :active
        product.touch
        Billing.update_all("category = 'authorized', author = 'David'")
        user.update_attribute(:website, 'example.com')
        user.update_columns(last_request_at: Time.current)
        Post.update_counters 5, comment_count: -1, action_count: 1
        
        # good
        user.update(website: 'example.com')
        FileUtils.touch('file')

        Example: Whitelist: ["touch"]

        # bad
        DiscussionBoard.decrement_counter(:post_count, 5)
        DiscussionBoard.increment_counter(:post_count, 5)
        person.toggle :active
        
        # good
        user.touch

        Prefer self[:attr] = val over write_attribute(:attr, val).
        Open

            write_attribute(:type, value.to_s)

        This cop checks for the use of the read_attribute or write_attribute methods and recommends square brackets instead.

        If an attribute is missing from the instance (for example, when initialized by a partial select) then read_attribute will return nil, but square brackets will raise an ActiveModel::MissingAttributeError.

        Explicitly raising an error in this situation is preferable, and that is why rubocop recommends using square brackets.

        Example:

        # bad
        x = read_attribute(:attr)
        write_attribute(:attr, val)
        
        # good
        x = self[:attr]
        self[:attr] = val

        Avoid using update_columns because it skips validations.
        Open

                  t.update_columns(

        This cop checks for the use of methods which skip validations which are listed in https://guides.rubyonrails.org/active_record_validations.html#skipping-validations

        Methods may be ignored from this rule by configuring a Whitelist.

        Example:

        # bad
        Article.first.decrement!(:view_count)
        DiscussionBoard.decrement_counter(:post_count, 5)
        Article.first.increment!(:view_count)
        DiscussionBoard.increment_counter(:post_count, 5)
        person.toggle :active
        product.touch
        Billing.update_all("category = 'authorized', author = 'David'")
        user.update_attribute(:website, 'example.com')
        user.update_columns(last_request_at: Time.current)
        Post.update_counters 5, comment_count: -1, action_count: 1
        
        # good
        user.update(website: 'example.com')
        FileUtils.touch('file')

        Example: Whitelist: ["touch"]

        # bad
        DiscussionBoard.decrement_counter(:post_count, 5)
        DiscussionBoard.increment_counter(:post_count, 5)
        person.toggle :active
        
        # good
        user.touch

        Avoid using update_column because it skips validations.
        Open

                    t1.update_column(:verbatim_name, t1.cached) if t1.verbatim_name.nil?

        This cop checks for the use of methods which skip validations which are listed in https://guides.rubyonrails.org/active_record_validations.html#skipping-validations

        Methods may be ignored from this rule by configuring a Whitelist.

        Example:

        # bad
        Article.first.decrement!(:view_count)
        DiscussionBoard.decrement_counter(:post_count, 5)
        Article.first.increment!(:view_count)
        DiscussionBoard.increment_counter(:post_count, 5)
        person.toggle :active
        product.touch
        Billing.update_all("category = 'authorized', author = 'David'")
        user.update_attribute(:website, 'example.com')
        user.update_columns(last_request_at: Time.current)
        Post.update_counters 5, comment_count: -1, action_count: 1
        
        # good
        user.update(website: 'example.com')
        FileUtils.touch('file')

        Example: Whitelist: ["touch"]

        # bad
        DiscussionBoard.decrement_counter(:post_count, 5)
        DiscussionBoard.increment_counter(:post_count, 5)
        person.toggle :active
        
        # good
        user.touch

        Avoid using update_column because it skips validations.
        Open

                  vn.update_column(:cached_valid_taxon_name_id, vn.id)  # update self too!

        This cop checks for the use of methods which skip validations which are listed in https://guides.rubyonrails.org/active_record_validations.html#skipping-validations

        Methods may be ignored from this rule by configuring a Whitelist.

        Example:

        # bad
        Article.first.decrement!(:view_count)
        DiscussionBoard.decrement_counter(:post_count, 5)
        Article.first.increment!(:view_count)
        DiscussionBoard.increment_counter(:post_count, 5)
        person.toggle :active
        product.touch
        Billing.update_all("category = 'authorized', author = 'David'")
        user.update_attribute(:website, 'example.com')
        user.update_columns(last_request_at: Time.current)
        Post.update_counters 5, comment_count: -1, action_count: 1
        
        # good
        user.update(website: 'example.com')
        FileUtils.touch('file')

        Example: Whitelist: ["touch"]

        # bad
        DiscussionBoard.decrement_counter(:post_count, 5)
        DiscussionBoard.increment_counter(:post_count, 5)
        person.toggle :active
        
        # good
        user.touch

        Avoid using update_columns because it skips validations.
        Open

                    t1.update_columns(

        This cop checks for the use of methods which skip validations which are listed in https://guides.rubyonrails.org/active_record_validations.html#skipping-validations

        Methods may be ignored from this rule by configuring a Whitelist.

        Example:

        # bad
        Article.first.decrement!(:view_count)
        DiscussionBoard.decrement_counter(:post_count, 5)
        Article.first.increment!(:view_count)
        DiscussionBoard.increment_counter(:post_count, 5)
        person.toggle :active
        product.touch
        Billing.update_all("category = 'authorized', author = 'David'")
        user.update_attribute(:website, 'example.com')
        user.update_columns(last_request_at: Time.current)
        Post.update_counters 5, comment_count: -1, action_count: 1
        
        # good
        user.update(website: 'example.com')
        FileUtils.touch('file')

        Example: Whitelist: ["touch"]

        # bad
        DiscussionBoard.decrement_counter(:post_count, 5)
        DiscussionBoard.increment_counter(:post_count, 5)
        person.toggle :active
        
        # good
        user.touch

        Avoid using update_column because it skips validations.
        Open

                    s.update_column(:cached_valid_taxon_name_id, vn.id)

        This cop checks for the use of methods which skip validations which are listed in https://guides.rubyonrails.org/active_record_validations.html#skipping-validations

        Methods may be ignored from this rule by configuring a Whitelist.

        Example:

        # bad
        Article.first.decrement!(:view_count)
        DiscussionBoard.decrement_counter(:post_count, 5)
        Article.first.increment!(:view_count)
        DiscussionBoard.increment_counter(:post_count, 5)
        person.toggle :active
        product.touch
        Billing.update_all("category = 'authorized', author = 'David'")
        user.update_attribute(:website, 'example.com')
        user.update_columns(last_request_at: Time.current)
        Post.update_counters 5, comment_count: -1, action_count: 1
        
        # good
        user.update(website: 'example.com')
        FileUtils.touch('file')

        Example: Whitelist: ["touch"]

        # bad
        DiscussionBoard.decrement_counter(:post_count, 5)
        DiscussionBoard.increment_counter(:post_count, 5)
        person.toggle :active
        
        # good
        user.touch

        Prefer self[:attr] over read_attribute(:attr).
        Open

            r = read_attribute(:type).to_s

        This cop checks for the use of the read_attribute or write_attribute methods and recommends square brackets instead.

        If an attribute is missing from the instance (for example, when initialized by a partial select) then read_attribute will return nil, but square brackets will raise an ActiveModel::MissingAttributeError.

        Explicitly raising an error in this situation is preferable, and that is why rubocop recommends using square brackets.

        Example:

        # bad
        x = read_attribute(:attr)
        write_attribute(:attr, val)
        
        # good
        x = self[:attr]
        self[:attr] = val

        Avoid using update_column because it skips validations.
        Open

                    c.update_column(:cached_valid_taxon_name_id, vn.id)

        This cop checks for the use of methods which skip validations which are listed in https://guides.rubyonrails.org/active_record_validations.html#skipping-validations

        Methods may be ignored from this rule by configuring a Whitelist.

        Example:

        # bad
        Article.first.decrement!(:view_count)
        DiscussionBoard.decrement_counter(:post_count, 5)
        Article.first.increment!(:view_count)
        DiscussionBoard.increment_counter(:post_count, 5)
        person.toggle :active
        product.touch
        Billing.update_all("category = 'authorized', author = 'David'")
        user.update_attribute(:website, 'example.com')
        user.update_columns(last_request_at: Time.current)
        Post.update_counters 5, comment_count: -1, action_count: 1
        
        # good
        user.update(website: 'example.com')
        FileUtils.touch('file')

        Example: Whitelist: ["touch"]

        # bad
        DiscussionBoard.decrement_counter(:post_count, 5)
        DiscussionBoard.increment_counter(:post_count, 5)
        person.toggle :active
        
        # good
        user.touch

        Avoid using update_column because it skips validations.
        Open

                    t1.update_column(:verbatim_name, t1.cached) if t1.verbatim_name.nil?

        This cop checks for the use of methods which skip validations which are listed in https://guides.rubyonrails.org/active_record_validations.html#skipping-validations

        Methods may be ignored from this rule by configuring a Whitelist.

        Example:

        # bad
        Article.first.decrement!(:view_count)
        DiscussionBoard.decrement_counter(:post_count, 5)
        Article.first.increment!(:view_count)
        DiscussionBoard.increment_counter(:post_count, 5)
        person.toggle :active
        product.touch
        Billing.update_all("category = 'authorized', author = 'David'")
        user.update_attribute(:website, 'example.com')
        user.update_columns(last_request_at: Time.current)
        Post.update_counters 5, comment_count: -1, action_count: 1
        
        # good
        user.update(website: 'example.com')
        FileUtils.touch('file')

        Example: Whitelist: ["touch"]

        # bad
        DiscussionBoard.decrement_counter(:post_count, 5)
        DiscussionBoard.increment_counter(:post_count, 5)
        person.toggle :active
        
        # good
        user.touch

        Avoid using update_columns because it skips validations.
        Open

                  t.update_columns(

        This cop checks for the use of methods which skip validations which are listed in https://guides.rubyonrails.org/active_record_validations.html#skipping-validations

        Methods may be ignored from this rule by configuring a Whitelist.

        Example:

        # bad
        Article.first.decrement!(:view_count)
        DiscussionBoard.decrement_counter(:post_count, 5)
        Article.first.increment!(:view_count)
        DiscussionBoard.increment_counter(:post_count, 5)
        person.toggle :active
        product.touch
        Billing.update_all("category = 'authorized', author = 'David'")
        user.update_attribute(:website, 'example.com')
        user.update_columns(last_request_at: Time.current)
        Post.update_counters 5, comment_count: -1, action_count: 1
        
        # good
        user.update(website: 'example.com')
        FileUtils.touch('file')

        Example: Whitelist: ["touch"]

        # bad
        DiscussionBoard.decrement_counter(:post_count, 5)
        DiscussionBoard.increment_counter(:post_count, 5)
        person.toggle :active
        
        # good
        user.touch

        TODO found
        Open

          # TODO: helper method

        Identical blocks of code found in 2 locations. Consider refactoring.
        Open

                  TaxonNameRelationship::Combination.where(subject_taxon_name: t).collect{|i| i.object_taxon_name}.uniq.each do |t1|
                    t1.update_column(:verbatim_name, t1.cached) if t1.verbatim_name.nil?
                    n = t1.get_full_name
                    t1.update_columns(
                        cached: n,
        Severity: Minor
        Found in app/models/taxon_name_classification.rb and 1 other location - About 40 mins to fix
        app/models/taxon_name_classification.rb on lines 229..235

        Duplicated Code

        Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

        Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

        When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

        Tuning

        This issue has a mass of 37.

        We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

        The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

        If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

        See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

        Refactorings

        Further Reading

        Identical blocks of code found in 2 locations. Consider refactoring.
        Open

                  TaxonNameRelationship::Combination.where(subject_taxon_name: t).collect{|i| i.object_taxon_name}.uniq.each do |t1|
                    t1.update_column(:verbatim_name, t1.cached) if t1.verbatim_name.nil?
                    n = t1.get_full_name
                    t1.update_columns(
                        cached: n,
        Severity: Minor
        Found in app/models/taxon_name_classification.rb and 1 other location - About 40 mins to fix
        app/models/taxon_name_classification.rb on lines 210..216

        Duplicated Code

        Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

        Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

        When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

        Tuning

        This issue has a mass of 37.

        We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

        The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

        If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

        See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

        Refactorings

        Further Reading

        Prefer the new style validations validates :column, presence: value over validates_presence_of.
        Open

          validates_presence_of :type

        This cop checks for the use of old-style attribute validation macros.

        Example:

        # bad
        validates_acceptance_of :foo
        validates_confirmation_of :foo
        validates_exclusion_of :foo
        validates_format_of :foo
        validates_inclusion_of :foo
        validates_length_of :foo
        validates_numericality_of :foo
        validates_presence_of :foo
        validates_absence_of :foo
        validates_size_of :foo
        validates_uniqueness_of :foo
        
        # good
        validates :foo, acceptance: true
        validates :foo, confirmation: true
        validates :foo, exclusion: true
        validates :foo, format: true
        validates :foo, inclusion: true
        validates :foo, length: true
        validates :foo, numericality: true
        validates :foo, presence: true
        validates :foo, absence: true
        validates :foo, size: true
        validates :foo, uniqueness: true

        Prefer the new style validations validates :column, presence: value over validates_presence_of.
        Open

          validates_presence_of :taxon_name

        This cop checks for the use of old-style attribute validation macros.

        Example:

        # bad
        validates_acceptance_of :foo
        validates_confirmation_of :foo
        validates_exclusion_of :foo
        validates_format_of :foo
        validates_inclusion_of :foo
        validates_length_of :foo
        validates_numericality_of :foo
        validates_presence_of :foo
        validates_absence_of :foo
        validates_size_of :foo
        validates_uniqueness_of :foo
        
        # good
        validates :foo, acceptance: true
        validates :foo, confirmation: true
        validates :foo, exclusion: true
        validates :foo, format: true
        validates :foo, inclusion: true
        validates :foo, length: true
        validates :foo, numericality: true
        validates :foo, presence: true
        validates :foo, absence: true
        validates :foo, size: true
        validates :foo, uniqueness: true

        Prefer the new style validations validates :column, uniqueness: value over validates_uniqueness_of.
        Open

          validates_uniqueness_of :taxon_name_id, scope: :type

        This cop checks for the use of old-style attribute validation macros.

        Example:

        # bad
        validates_acceptance_of :foo
        validates_confirmation_of :foo
        validates_exclusion_of :foo
        validates_format_of :foo
        validates_inclusion_of :foo
        validates_length_of :foo
        validates_numericality_of :foo
        validates_presence_of :foo
        validates_absence_of :foo
        validates_size_of :foo
        validates_uniqueness_of :foo
        
        # good
        validates :foo, acceptance: true
        validates :foo, confirmation: true
        validates :foo, exclusion: true
        validates :foo, format: true
        validates :foo, inclusion: true
        validates :foo, length: true
        validates :foo, numericality: true
        validates :foo, presence: true
        validates :foo, absence: true
        validates :foo, size: true
        validates :foo, uniqueness: true

        There are no issues that match your filters.

        Category
        Status