activerecord/lib/active_record/relation/query_methods.rb

Summary

Maintainability
F
6 days
Test Coverage

File query_methods.rb has 998 lines of code (exceeds 250 allowed). Consider refactoring.
Open

require "active_record/relation/from_clause"
require "active_record/relation/query_attribute"
require "active_record/relation/where_clause"
require "active_support/core_ext/array/wrap"

Severity: Major
Found in activerecord/lib/active_record/relation/query_methods.rb - About 2 days to fix

    Method build_join_buckets has a Cognitive Complexity of 26 (exceeds 5 allowed). Consider refactoring.
    Open

          def build_join_buckets
            buckets = Hash.new { |h, k| h[k] = [] }
    
            unless left_outer_joins_values.empty?
              stashed_left_joins = []
    Severity: Minor
    Found in activerecord/lib/active_record/relation/query_methods.rb - About 3 hrs 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 build_where_clause has a Cognitive Complexity of 20 (exceeds 5 allowed). Consider refactoring.
    Open

          def build_where_clause(opts, rest = []) # :nodoc:
            opts = sanitize_forbidden_attributes(opts)
    
            if opts.is_a?(Array)
              opts, *rest = opts
    Severity: Minor
    Found in activerecord/lib/active_record/relation/query_methods.rb - About 2 hrs 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 build_named_bound_sql_literal has a Cognitive Complexity of 18 (exceeds 5 allowed). Consider refactoring.
    Open

          def build_named_bound_sql_literal(statement, values)
            bound_values = values.transform_values do |value|
              if ActiveRecord::Relation === value
                Arel.sql(value.to_sql)
              elsif value.respond_to?(:map) && !value.acts_like?(:string)
    Severity: Minor
    Found in activerecord/lib/active_record/relation/query_methods.rb - About 2 hrs 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 build_bound_sql_literal has a Cognitive Complexity of 18 (exceeds 5 allowed). Consider refactoring.
    Open

          def build_bound_sql_literal(statement, values)
            bound_values = values.map do |value|
              if ActiveRecord::Relation === value
                Arel.sql(value.to_sql)
              elsif value.respond_to?(:map) && !value.acts_like?(:string)
    Severity: Minor
    Found in activerecord/lib/active_record/relation/query_methods.rb - About 2 hrs 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 build_arel has a Cognitive Complexity of 17 (exceeds 5 allowed). Consider refactoring.
    Open

          def build_arel(aliases = nil)
            arel = Arel::SelectManager.new(table)
    
            build_joins(arel.join_sources, aliases)
    
    
    Severity: Minor
    Found in activerecord/lib/active_record/relation/query_methods.rb - About 2 hrs 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 build_join_buckets has 45 lines of code (exceeds 25 allowed). Consider refactoring.
    Open

          def build_join_buckets
            buckets = Hash.new { |h, k| h[k] = [] }
    
            unless left_outer_joins_values.empty?
              stashed_left_joins = []
    Severity: Minor
    Found in activerecord/lib/active_record/relation/query_methods.rb - About 1 hr to fix

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

            def preprocess_order_args(order_args)
              @klass.disallow_raw_sql!(
                flattened_args(order_args),
                permit: model.adapter_class.column_name_with_order_matcher
              )
      Severity: Minor
      Found in activerecord/lib/active_record/relation/query_methods.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 unscope! has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring.
      Open

          def unscope!(*args) # :nodoc:
            self.unscope_values += args
      
            args.each do |scope|
              case scope
      Severity: Minor
      Found in activerecord/lib/active_record/relation/query_methods.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 transform_select_hash_values has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring.
      Open

            def transform_select_hash_values(fields)
              fields.flat_map do |key, columns_aliases|
                case columns_aliases
                when Hash
                  columns_aliases.map do |column, column_alias|
      Severity: Minor
      Found in activerecord/lib/active_record/relation/query_methods.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 build_where_clause has 35 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

            def build_where_clause(opts, rest = []) # :nodoc:
              opts = sanitize_forbidden_attributes(opts)
      
              if opts.is_a?(Array)
                opts, *rest = opts
      Severity: Minor
      Found in activerecord/lib/active_record/relation/query_methods.rb - About 1 hr to fix

        Method preprocess_order_args has 30 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

              def preprocess_order_args(order_args)
                @klass.disallow_raw_sql!(
                  flattened_args(order_args),
                  permit: model.adapter_class.column_name_with_order_matcher
                )
        Severity: Minor
        Found in activerecord/lib/active_record/relation/query_methods.rb - About 1 hr to fix

          Method reverse_sql_order has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
          Open

                def reverse_sql_order(order_query)
                  if order_query.empty?
                    return [table[primary_key].desc] if primary_key
                    raise IrreversibleOrderError,
                      "Relation has no current order and table has no primary key to be used as default order"
          Severity: Minor
          Found in activerecord/lib/active_record/relation/query_methods.rb - About 55 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 validate_order_args has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
          Open

                def validate_order_args(args)
                  args.each do |arg|
                    next unless arg.is_a?(Hash)
                    arg.each do |_key, value|
                      if value.is_a?(Hash)
          Severity: Minor
          Found in activerecord/lib/active_record/relation/query_methods.rb - About 55 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 build_joins has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
          Open

                def build_joins(join_sources, aliases = nil)
                  return join_sources if joins_values.empty? && left_outer_joins_values.empty?
          
                  buckets, join_type = build_join_buckets
          
          
          Severity: Minor
          Found in activerecord/lib/active_record/relation/query_methods.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 column_references has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
          Open

                def column_references(order_args)
                  order_args.flat_map do |arg|
                    case arg
                    when String, Symbol
                      arg
          Severity: Minor
          Found in activerecord/lib/active_record/relation/query_methods.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 associated has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
          Open

                def associated(*associations)
                  associations.each do |association|
                    reflection = scope_association_reflection(association)
                    unless @scope.joins_values.include?(reflection.name) || @scope.left_outer_joins_values.include?(reflection.name)
                      @scope.joins!(association)
          Severity: Minor
          Found in activerecord/lib/active_record/relation/query_methods.rb - About 35 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 in_order_of has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
          Open

              def in_order_of(column, values)
                klass.disallow_raw_sql!([column], permit: model.adapter_class.column_name_with_order_matcher)
                return spawn.none! if values.empty?
          
                references = column_references([column])
          Severity: Minor
          Found in activerecord/lib/active_record/relation/query_methods.rb - About 35 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 structurally_incompatible_values_for has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
          Open

                def structurally_incompatible_values_for(other)
                  values = other.values
                  STRUCTURAL_VALUE_METHODS.reject do |method|
                    v1, v2 = @values[method], values[method]
                    if v1.is_a?(Array)
          Severity: Minor
          Found in activerecord/lib/active_record/relation/query_methods.rb - About 35 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 resolve_arel_attributes has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
          Open

                def resolve_arel_attributes(attrs)
                  attrs.flat_map do |attr|
                    case attr
                    when Arel::Predications
                      attr
          Severity: Minor
          Found in activerecord/lib/active_record/relation/query_methods.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 arel_column has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
          Open

                def arel_column(field)
                  field = klass.attribute_aliases[field] || field
                  from = from_clause.name || from_clause.value
          
                  if klass.columns_hash.key?(field) && (!from || table_name_matches?(from))
          Severity: Minor
          Found in activerecord/lib/active_record/relation/query_methods.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

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

                  bound_values = values.transform_values do |value|
                    if ActiveRecord::Relation === value
                      Arel.sql(value.to_sql)
                    elsif value.respond_to?(:map) && !value.acts_like?(:string)
                      values = value.map { |v| v.respond_to?(:id_for_database) ? v.id_for_database : v }
          Severity: Major
          Found in activerecord/lib/active_record/relation/query_methods.rb and 1 other location - About 1 hr to fix
          activerecord/lib/active_record/relation/query_methods.rb on lines 1602..1611

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

          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

                  bound_values = values.map do |value|
                    if ActiveRecord::Relation === value
                      Arel.sql(value.to_sql)
                    elsif value.respond_to?(:map) && !value.acts_like?(:string)
                      values = value.map { |v| v.respond_to?(:id_for_database) ? v.id_for_database : v }
          Severity: Major
          Found in activerecord/lib/active_record/relation/query_methods.rb and 1 other location - About 1 hr to fix
          activerecord/lib/active_record/relation/query_methods.rb on lines 1582..1591

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

          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

          There are no issues that match your filters.

          Category
          Status