SpeciesFileGroup/taxonworks

View on GitHub
app/models/geographic_area.rb

Summary

Maintainability
C
1 day
Test Coverage

Class GeographicArea has 29 methods (exceeds 20 allowed). Consider refactoring.
Open

class GeographicArea < ApplicationRecord
  include Housekeeping::Users
  include Housekeeping::Timestamps
  include Shared::IsData
  include Shared::IsApplicationData
Severity: Minor
Found in app/models/geographic_area.rb - About 3 hrs to fix

    File geographic_area.rb has 288 lines of code (exceeds 250 allowed). Consider refactoring.
    Open

    class GeographicArea < ApplicationRecord
      include Housekeeping::Users
      include Housekeeping::Timestamps
      include Shared::IsData
      include Shared::IsApplicationData
    Severity: Minor
    Found in app/models/geographic_area.rb - About 2 hrs to fix

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

        def self.used_recently(user_id, project_id, used_on = 'CollectingEvent')
      
          case used_on
              when 'CollectingEvent'
                t = CollectingEvent.arel_table
      Severity: Minor
      Found in app/models/geographic_area.rb by rubocop

      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.

      Method used_recently has 26 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

        def self.used_recently(user_id, project_id, used_on = 'CollectingEvent')
      
          case used_on
              when 'CollectingEvent'
                t = CollectingEvent.arel_table
      Severity: Minor
      Found in app/models/geographic_area.rb - About 1 hr to fix

        Method matching has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
        Open

          def self.matching(text, has_shape = false, invert = false)
            if text.nil? || text.length == 0
              return Hash.new('No query provided!' => [])
            end
        
        
        Severity: Minor
        Found in app/models/geographic_area.rb - About 25 mins 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

        Method categorize has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
        Open

          def categorize
            n = geographic_area_type.name
            return {country: name} if GeographicAreaType::COUNTRY_LEVEL_TYPES.include?(n) || (id == level0_id)
            return {state: name} if GeographicAreaType::STATE_LEVEL_TYPES.include?(n) || (data_origin == 'ne_states') || (id == level1_id) || (!parent.nil? && (parent.try(:id) == parent.try(:level0_id)))
            return {county: name} if GeographicAreaType::COUNTY_LEVEL_TYPES.include?(n)
        Severity: Minor
        Found in app/models/geographic_area.rb - About 25 mins 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

        Specify a :dependent option.
        Open

          has_many :collecting_events, inverse_of: :geographic_area
        Severity: Minor
        Found in app/models/geographic_area.rb by rubocop

        This cop looks for has_many or has_one associations that don't specify a :dependent option. It doesn't register an offense if :through option was specified.

        Example:

        # bad
        class User < ActiveRecord::Base
          has_many :comments
          has_one :avatar
        end
        
        # good
        class User < ActiveRecord::Base
          has_many :comments, dependent: :restrict_with_exception
          has_one :avatar, dependent: :destroy
          has_many :patients, through: :appointments
        end

        Prefer 1.week.
        Open

                          .where(t['created_at'].gt(1.weeks.ago))
        Severity: Minor
        Found in app/models/geographic_area.rb by rubocop

        This cop checks for correct grammar when using ActiveSupport's core extensions to the numeric classes.

        Example:

        # bad
        3.day.ago
        1.months.ago
        
        # good
        3.days.ago
        1.month.ago

        Specify an :inverse_of option.
        Open

          belongs_to :level0, class_name: 'GeographicArea', foreign_key: :level0_id
        Severity: Minor
        Found in app/models/geographic_area.rb by rubocop

        This cop looks for has(one|many) and belongsto associations where Active Record can't automatically determine the inverse association because of a scope or the options used. Using the blog with order scope example below, traversing the a Blog's association in both directions with blog.posts.first.blog would cause the blog to be loaded from the database twice.

        :inverse_of must be manually specified for Active Record to use the associated object in memory, or set to false to opt-out. Note that setting nil does not stop Active Record from trying to determine the inverse automatically, and is not considered a valid value for this.

        Example:

        # good
        class Blog < ApplicationRecord
          has_many :posts
        end
        
        class Post < ApplicationRecord
          belongs_to :blog
        end

        Example:

        # bad
        class Blog < ApplicationRecord
          has_many :posts, -> { order(published_at: :desc) }
        end
        
        class Post < ApplicationRecord
          belongs_to :blog
        end
        
        # good
        class Blog < ApplicationRecord
          has_many(:posts,
                   -> { order(published_at: :desc) },
                   inverse_of: :blog)
        end
        
        class Post < ApplicationRecord
          belongs_to :blog
        end
        
        # good
        class Blog < ApplicationRecord
          with_options inverse_of: :blog do
            has_many :posts, -> { order(published_at: :desc) }
          end
        end
        
        class Post < ApplicationRecord
          belongs_to :blog
        end
        
        # good
        # When you don't want to use the inverse association.
        class Blog < ApplicationRecord
          has_many(:posts,
                   -> { order(published_at: :desc) },
                   inverse_of: false)
        end

        Example:

        # bad
        class Picture < ApplicationRecord
          belongs_to :imageable, polymorphic: true
        end
        
        class Employee < ApplicationRecord
          has_many :pictures, as: :imageable
        end
        
        class Product < ApplicationRecord
          has_many :pictures, as: :imageable
        end
        
        # good
        class Picture < ApplicationRecord
          belongs_to :imageable, polymorphic: true
        end
        
        class Employee < ApplicationRecord
          has_many :pictures, as: :imageable, inverse_of: :imageable
        end
        
        class Product < ApplicationRecord
          has_many :pictures, as: :imageable, inverse_of: :imageable
        end

        Example:

        # bad
        # However, RuboCop can not detect this pattern...
        class Physician < ApplicationRecord
          has_many :appointments
          has_many :patients, through: :appointments
        end
        
        class Appointment < ApplicationRecord
          belongs_to :physician
          belongs_to :patient
        end
        
        class Patient < ApplicationRecord
          has_many :appointments
          has_many :physicians, through: :appointments
        end
        
        # good
        class Physician < ApplicationRecord
          has_many :appointments
          has_many :patients, through: :appointments
        end
        
        class Appointment < ApplicationRecord
          belongs_to :physician, inverse_of: :appointments
          belongs_to :patient, inverse_of: :appointments
        end
        
        class Patient < ApplicationRecord
          has_many :appointments
          has_many :physicians, through: :appointments
        end

        @see https://guides.rubyonrails.org/association_basics.html#bi-directional-associations @see https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#module-ActiveRecord::Associations::ClassMethods-label-Setting+Inverses

        Specify an :inverse_of option.
        Open

          belongs_to :level1, class_name: 'GeographicArea', foreign_key: :level1_id
        Severity: Minor
        Found in app/models/geographic_area.rb by rubocop

        This cop looks for has(one|many) and belongsto associations where Active Record can't automatically determine the inverse association because of a scope or the options used. Using the blog with order scope example below, traversing the a Blog's association in both directions with blog.posts.first.blog would cause the blog to be loaded from the database twice.

        :inverse_of must be manually specified for Active Record to use the associated object in memory, or set to false to opt-out. Note that setting nil does not stop Active Record from trying to determine the inverse automatically, and is not considered a valid value for this.

        Example:

        # good
        class Blog < ApplicationRecord
          has_many :posts
        end
        
        class Post < ApplicationRecord
          belongs_to :blog
        end

        Example:

        # bad
        class Blog < ApplicationRecord
          has_many :posts, -> { order(published_at: :desc) }
        end
        
        class Post < ApplicationRecord
          belongs_to :blog
        end
        
        # good
        class Blog < ApplicationRecord
          has_many(:posts,
                   -> { order(published_at: :desc) },
                   inverse_of: :blog)
        end
        
        class Post < ApplicationRecord
          belongs_to :blog
        end
        
        # good
        class Blog < ApplicationRecord
          with_options inverse_of: :blog do
            has_many :posts, -> { order(published_at: :desc) }
          end
        end
        
        class Post < ApplicationRecord
          belongs_to :blog
        end
        
        # good
        # When you don't want to use the inverse association.
        class Blog < ApplicationRecord
          has_many(:posts,
                   -> { order(published_at: :desc) },
                   inverse_of: false)
        end

        Example:

        # bad
        class Picture < ApplicationRecord
          belongs_to :imageable, polymorphic: true
        end
        
        class Employee < ApplicationRecord
          has_many :pictures, as: :imageable
        end
        
        class Product < ApplicationRecord
          has_many :pictures, as: :imageable
        end
        
        # good
        class Picture < ApplicationRecord
          belongs_to :imageable, polymorphic: true
        end
        
        class Employee < ApplicationRecord
          has_many :pictures, as: :imageable, inverse_of: :imageable
        end
        
        class Product < ApplicationRecord
          has_many :pictures, as: :imageable, inverse_of: :imageable
        end

        Example:

        # bad
        # However, RuboCop can not detect this pattern...
        class Physician < ApplicationRecord
          has_many :appointments
          has_many :patients, through: :appointments
        end
        
        class Appointment < ApplicationRecord
          belongs_to :physician
          belongs_to :patient
        end
        
        class Patient < ApplicationRecord
          has_many :appointments
          has_many :physicians, through: :appointments
        end
        
        # good
        class Physician < ApplicationRecord
          has_many :appointments
          has_many :patients, through: :appointments
        end
        
        class Appointment < ApplicationRecord
          belongs_to :physician, inverse_of: :appointments
          belongs_to :patient, inverse_of: :appointments
        end
        
        class Patient < ApplicationRecord
          has_many :appointments
          has_many :physicians, through: :appointments
        end

        @see https://guides.rubyonrails.org/association_basics.html#bi-directional-associations @see https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#module-ActiveRecord::Associations::ClassMethods-label-Setting+Inverses

        Specify a :dependent option.
        Open

          has_many :asserted_distributions, inverse_of: :geographic_area
        Severity: Minor
        Found in app/models/geographic_area.rb by rubocop

        This cop looks for has_many or has_one associations that don't specify a :dependent option. It doesn't register an offense if :through option was specified.

        Example:

        # bad
        class User < ActiveRecord::Base
          has_many :comments
          has_one :avatar
        end
        
        # good
        class User < ActiveRecord::Base
          has_many :comments, dependent: :restrict_with_exception
          has_one :avatar, dependent: :destroy
          has_many :patients, through: :appointments
        end

        Use find_by instead of dynamic find_by_lat_long.
        Open

              .merge(GeographicArea.find_by_lat_long(latitude, longitude))
        Severity: Minor
        Found in app/models/geographic_area.rb by rubocop

        This cop checks dynamic find_by_* methods. Use find_by instead of dynamic method. See. https://github.com/rubocop-hq/rails-style-guide#find_by

        Example:

        # bad
        User.find_by_name(name)
        
        # bad
        User.find_by_name_and_email(name)
        
        # bad
        User.find_by_email!(name)
        
        # good
        User.find_by(name: name)
        
        # good
        User.find_by(name: name, email: email)
        
        # good
        User.find_by!(email: email)

        Prefer 1.week.
        Open

                          .where(t['created_at'].gt(1.weeks.ago))
        Severity: Minor
        Found in app/models/geographic_area.rb by rubocop

        This cop checks for correct grammar when using ActiveSupport's core extensions to the numeric classes.

        Example:

        # bad
        3.day.ago
        1.months.ago
        
        # good
        3.days.ago
        1.month.ago

        Specify an :inverse_of option.
        Open

          belongs_to :level2, class_name: 'GeographicArea', foreign_key: :level2_id
        Severity: Minor
        Found in app/models/geographic_area.rb by rubocop

        This cop looks for has(one|many) and belongsto associations where Active Record can't automatically determine the inverse association because of a scope or the options used. Using the blog with order scope example below, traversing the a Blog's association in both directions with blog.posts.first.blog would cause the blog to be loaded from the database twice.

        :inverse_of must be manually specified for Active Record to use the associated object in memory, or set to false to opt-out. Note that setting nil does not stop Active Record from trying to determine the inverse automatically, and is not considered a valid value for this.

        Example:

        # good
        class Blog < ApplicationRecord
          has_many :posts
        end
        
        class Post < ApplicationRecord
          belongs_to :blog
        end

        Example:

        # bad
        class Blog < ApplicationRecord
          has_many :posts, -> { order(published_at: :desc) }
        end
        
        class Post < ApplicationRecord
          belongs_to :blog
        end
        
        # good
        class Blog < ApplicationRecord
          has_many(:posts,
                   -> { order(published_at: :desc) },
                   inverse_of: :blog)
        end
        
        class Post < ApplicationRecord
          belongs_to :blog
        end
        
        # good
        class Blog < ApplicationRecord
          with_options inverse_of: :blog do
            has_many :posts, -> { order(published_at: :desc) }
          end
        end
        
        class Post < ApplicationRecord
          belongs_to :blog
        end
        
        # good
        # When you don't want to use the inverse association.
        class Blog < ApplicationRecord
          has_many(:posts,
                   -> { order(published_at: :desc) },
                   inverse_of: false)
        end

        Example:

        # bad
        class Picture < ApplicationRecord
          belongs_to :imageable, polymorphic: true
        end
        
        class Employee < ApplicationRecord
          has_many :pictures, as: :imageable
        end
        
        class Product < ApplicationRecord
          has_many :pictures, as: :imageable
        end
        
        # good
        class Picture < ApplicationRecord
          belongs_to :imageable, polymorphic: true
        end
        
        class Employee < ApplicationRecord
          has_many :pictures, as: :imageable, inverse_of: :imageable
        end
        
        class Product < ApplicationRecord
          has_many :pictures, as: :imageable, inverse_of: :imageable
        end

        Example:

        # bad
        # However, RuboCop can not detect this pattern...
        class Physician < ApplicationRecord
          has_many :appointments
          has_many :patients, through: :appointments
        end
        
        class Appointment < ApplicationRecord
          belongs_to :physician
          belongs_to :patient
        end
        
        class Patient < ApplicationRecord
          has_many :appointments
          has_many :physicians, through: :appointments
        end
        
        # good
        class Physician < ApplicationRecord
          has_many :appointments
          has_many :patients, through: :appointments
        end
        
        class Appointment < ApplicationRecord
          belongs_to :physician, inverse_of: :appointments
          belongs_to :patient, inverse_of: :appointments
        end
        
        class Patient < ApplicationRecord
          has_many :appointments
          has_many :physicians, through: :appointments
        end

        @see https://guides.rubyonrails.org/association_basics.html#bi-directional-associations @see https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#module-ActiveRecord::Associations::ClassMethods-label-Setting+Inverses

        TODO found
        Open

          # TODO: This method can be placed elsewhere inside this class (or even removed if not used)
        Severity: Minor
        Found in app/models/geographic_area.rb by fixme

        TODO found
        Open

          # TODO: parametrize to include gazeteer
        Severity: Minor
        Found in app/models/geographic_area.rb by fixme

        TODO found
        Open

          # TODO: Test, or extend a general method
        Severity: Minor
        Found in app/models/geographic_area.rb by fixme

        Similar blocks of code found in 3 locations. Consider refactoring.
        Open

                  i = t.project(t['citation_object_id'], t['citation_object_type'], t['created_at']).from(t)
                          .where(t['created_at'].gt(1.weeks.ago))
                          .where(t['created_by_id'].eq(user_id))
                          .where(t['project_id'].eq(project_id))
                          .order(t['created_at'].desc)
        Severity: Major
        Found in app/models/geographic_area.rb and 2 other locations - About 45 mins to fix
        app/models/image.rb on lines 320..324
        app/models/person.rb on lines 448..452

        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 39.

        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

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

          def self.are_contained_in(geographic_area)
            pieces = nil
            if geographic_area.geographic_items.any?
              pieces = GeographicItem.are_contained_in_item('any_poly', geographic_area.geo_object)
              others = []
        Severity: Minor
        Found in app/models/geographic_area.rb and 1 other location - About 40 mins to fix
        app/models/geographic_area.rb on lines 174..184

        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 38.

        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

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

          def self.is_contained_by(geographic_area)
            pieces = nil
            if geographic_area.geographic_items.any?
              pieces = GeographicItem.is_contained_by('any_poly', geographic_area.geo_object)
              others = []
        Severity: Minor
        Found in app/models/geographic_area.rb and 1 other location - About 40 mins to fix
        app/models/geographic_area.rb on lines 189..199

        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 38.

        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

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

          scope :ancestors_and_descendants_of, -> (geographic_area) do
            scoping do
              a = GeographicArea.self_and_ancestors_of(geographic_area)
              b = GeographicArea.descendants_of(geographic_area)
              GeographicArea.from("((#{a.to_sql}) UNION (#{b.to_sql})) as geographic_areas")
        Severity: Minor
        Found in app/models/geographic_area.rb and 1 other location - About 20 mins to fix
        app/models/taxon_name.rb on lines 266..272

        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 27.

        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 :geographic_area_type_id
        Severity: Minor
        Found in app/models/geographic_area.rb by rubocop

        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