AyuntamientoMadrid/participacion

View on GitHub
app/models/budget/investment.rb

Summary

Maintainability
D
1 day
Test Coverage

Class Investment has 51 methods (exceeds 20 allowed). Consider refactoring.
Open

  class Investment < ApplicationRecord
    SORTING_OPTIONS = { id: "id", supports: "cached_votes_up" }.freeze

    include Measurable
    include Sanitizable
Severity: Major
Found in app/models/budget/investment.rb - About 7 hrs to fix

    File investment.rb has 334 lines of code (exceeds 250 allowed). Consider refactoring.
    Open

    class Budget
      class Investment < ApplicationRecord
        SORTING_OPTIONS = { id: "id", supports: "cached_votes_up" }.freeze
    
        include Measurable
    Severity: Minor
    Found in app/models/budget/investment.rb - About 4 hrs to fix

      Method scoped_filter has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring.
      Open

          def self.scoped_filter(params, current_filter)
            budget  = Budget.find_by_slug_or_id params[:budget_id]
            results = Investment.by_budget(budget)
      
            if params[:min_total_supports].present?
      Severity: Minor
      Found in app/models/budget/investment.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

      Method advanced_filters has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
      Open

          def self.advanced_filters(params, results)
            results = results.without_admin      if params[:advanced_filters].include?("without_admin")
            results = results.without_valuator   if params[:advanced_filters].include?("without_valuator")
            results = results.under_valuation    if params[:advanced_filters].include?("under_valuation")
            results = results.valuation_finished if params[:advanced_filters].include?("valuation_finished")
      Severity: Minor
      Found in app/models/budget/investment.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

      Method reason_for_not_being_ballotable_by has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
      Open

          def reason_for_not_being_ballotable_by(user, ballot)
            return permission_problem(user)    if permission_problem?(user)
            return :not_selected               unless selected?
            return :no_ballots_allowed         unless budget.balloting?
            return :different_heading_assigned unless ballot.valid_heading?(heading)
      Severity: Minor
      Found in app/models/budget/investment.rb - About 45 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 order_filter has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
      Open

          def self.order_filter(params)
            sorting_key = params[:sort_by]&.downcase&.to_sym
            allowed_sort_option = SORTING_OPTIONS[sorting_key]
            direction = params[:direction] == "desc" ? "desc" : "asc"
      
      
      Severity: Minor
      Found in app/models/budget/investment.rb - About 45 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

      Avoid too many return statements within this method.
      Open

            return :casted_offline             if ballot.casted_offline?
      Severity: Major
      Found in app/models/budget/investment.rb - About 30 mins to fix

        Specify a :dependent option. (https://rails.rubystyle.guide#has_many-has_one-dependent-option)
        Open

            has_many :valuations, -> { where(valuation: true) },
        Severity: Minor
        Found in app/models/budget/investment.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

        Specify a :dependent option. (https://rails.rubystyle.guide#has_many-has_one-dependent-option)
        Open

            has_many :comments, -> { where(valuation: false) }, as: :commentable, inverse_of: :commentable
        Severity: Minor
        Found in app/models/budget/investment.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

        There are no issues that match your filters.

        Category
        Status