hpi-swt2/workshop-portal

View on GitHub
app/models/event.rb

Summary

Maintainability
A
2 hrs
Test Coverage

Class has too many lines. [106/100]
Open

class Event < ActiveRecord::Base
  UNREASONABLY_LONG_DATE_SPAN = 300

  has_many :application_letters
  has_many :agreement_letters
Severity: Minor
Found in app/models/event.rb by rubocop

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

Method has too many lines. [19/10]
Open

  def compare_participants_by_agreement(participant1, participant2)
    if participant1.requires_agreement_letter_for_event?(self)
      if participant2.requires_agreement_letter_for_event?(self)
        return participant1.name <=> participant2.name
      end
Severity: Minor
Found in app/models/event.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.

Assignment Branch Condition size for compare_participants_by_agreement is too high. [16.16/15]
Open

  def compare_participants_by_agreement(participant1, participant2)
    if participant1.requires_agreement_letter_for_event?(self)
      if participant2.requires_agreement_letter_for_event?(self)
        return participant1.name <=> participant2.name
      end
Severity: Minor
Found in app/models/event.rb by rubocop

This cop checks that the ABC size of methods is not higher than the configured maximum. The ABC size is based on assignments, branches (method calls), and conditions. See http://c2.com/cgi/wiki?AbcMetric

Cyclomatic complexity for compare_participants_by_agreement is too high. [7/6]
Open

  def compare_participants_by_agreement(participant1, participant2)
    if participant1.requires_agreement_letter_for_event?(self)
      if participant2.requires_agreement_letter_for_event?(self)
        return participant1.name <=> participant2.name
      end
Severity: Minor
Found in app/models/event.rb by rubocop

This cop checks that the cyclomatic complexity of methods is not higher than the configured maximum. The cyclomatic complexity is the number of linearly independent paths through a method. The algorithm counts decision points and adds one.

An if statement (or unless or ?:) increases the complexity by one. An else branch does not, since it doesn't add a decision point. The && operator (or keyword and) can be converted to a nested if statement, and ||/or is shorthand for a sequence of ifs, so they also add one. Loops can be said to have an exit condition, so they add one.

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

  def compare_participants_by_agreement(participant1, participant2)
    if participant1.requires_agreement_letter_for_event?(self)
      if participant2.requires_agreement_letter_for_event?(self)
        return participant1.name <=> participant2.name
      end
Severity: Minor
Found in app/models/event.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 1
Severity: Major
Found in app/models/event.rb - About 30 mins to fix

    Avoid too many return statements within this method.
    Open

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

      Avoid too many return statements within this method.
      Open

          return participant1.name <=> participant2.name
      Severity: Major
      Found in app/models/event.rb - About 30 mins to fix

        Use max_by(&:end_date) instead of max { |a, b| a.end_date <=> b.end_date }.
        Open

            (date_ranges.max { |a,b| a.end_date <=> b.end_date }).end_date
        Severity: Minor
        Found in app/models/event.rb by rubocop

        This cop identifies places where sort { |a, b| a.foo <=> b.foo } can be replaced by sort_by(&:foo). This cop also checks max and min methods.

        Example:

        # bad
        array.sort { |a, b| a.foo <=> b.foo }
        array.max { |a, b| a.foo <=> b.foo }
        array.min { |a, b| a.foo <=> b.foo }
        array.sort { |a, b| a[:foo] <=> b[:foo] }
        
        # good
        array.sort_by(&:foo)
        array.sort_by { |v| v.foo }
        array.sort_by do |var|
          var.foo
        end
        array.max_by(&:foo)
        array.min_by(&:foo)
        array.sort_by { |a| a[:foo] }

        Use min_by(&:start_date) instead of min { |a, b| a.start_date <=> b.start_date }.
        Open

            (date_ranges.min { |a,b| a.start_date <=> b.start_date }).start_date
        Severity: Minor
        Found in app/models/event.rb by rubocop

        This cop identifies places where sort { |a, b| a.foo <=> b.foo } can be replaced by sort_by(&:foo). This cop also checks max and min methods.

        Example:

        # bad
        array.sort { |a, b| a.foo <=> b.foo }
        array.max { |a, b| a.foo <=> b.foo }
        array.min { |a, b| a.foo <=> b.foo }
        array.sort { |a, b| a[:foo] <=> b[:foo] }
        
        # good
        array.sort_by(&:foo)
        array.sort_by { |v| v.foo }
        array.sort_by do |var|
          var.foo
        end
        array.max_by(&:foo)
        array.min_by(&:foo)
        array.sort_by { |a| a[:foo] }

        Line is too long. [100/80]
        Open

            accepted_applications = application_letters.where(status: ApplicationLetter.statuses[:accepted])
        Severity: Minor
        Found in app/models/event.rb by rubocop

        Favor modifier if usage when having a single-line body. Another good alternative is the usage of control flow &&/||.
        Open

            if participant2.requires_agreement_letter_for_event?(self)
        Severity: Minor
        Found in app/models/event.rb by rubocop

        Checks for if and unless statements that would fit on one line if written as a modifier if/unless. The maximum line length is configured in the Metrics/LineLength cop.

        Example:

        # bad
        if condition
          do_stuff(bar)
        end
        
        unless qux.empty?
          Foo.do_something
        end
        
        # good
        do_stuff(bar) if condition
        Foo.do_something unless qux.empty?

        Prefer single-quoted strings when you don't need string interpolation or special symbols.
        Open

          scope :draft_is, ->(draft) { where("draft = ?", draft) }
        Severity: Minor
        Found in app/models/event.rb by rubocop

        Checks if uses of quotes match the configured preference.

        Example: EnforcedStyle: single_quotes (default)

        # bad
        "No special symbols"
        "No string interpolation"
        "Just text"
        
        # good
        'No special symbols'
        'No string interpolation'
        'Just text'
        "Wait! What's #{this}!"

        Example: EnforcedStyle: double_quotes

        # bad
        'Just some text'
        'No special chars or interpolation'
        
        # good
        "Just some text"
        "No special chars or interpolation"
        "Every string in #{project} uses double_quotes"

        Line is too long. [98/80]
        Open

          # 2. All participants that have to submit an letter of agreement and did do so, ordered by name.
        Severity: Minor
        Found in app/models/event.rb by rubocop

        Line is too long. [98/80]
        Open

          # 2. All participants that have to submit an letter of agreement and did do so, ordered by name.
        Severity: Minor
        Found in app/models/event.rb by rubocop

        Extra empty line detected at class body end.
        Open

        
        end
        Severity: Minor
        Found in app/models/event.rb by rubocop

        This cops checks if empty lines around the bodies of classes match the configuration.

        Example: EnforcedStyle: empty_lines

        # good
        
        class Foo
        
          def bar
            # ...
          end
        
        end

        Example: EnforcedStyle: emptylinesexcept_namespace

        # good
        
        class Foo
          class Bar
        
            # ...
        
          end
        end

        Example: EnforcedStyle: emptylinesspecial

        # good
        class Foo
        
          def bar; end
        
        end

        Example: EnforcedStyle: noemptylines (default)

        # good
        
        class Foo
          def bar
            # ...
          end
        end

        Line is too long. [100/80]
        Open

            accepted_applications = application_letters.where(status: ApplicationLetter.statuses[:accepted])
        Severity: Minor
        Found in app/models/event.rb by rubocop

        Line is too long. [100/80]
        Open

            rejected_applications = application_letters.where(status: ApplicationLetter.statuses[:rejected])
        Severity: Minor
        Found in app/models/event.rb by rubocop

        Line is too long. [82/80]
        Open

            application_letters.where(status: ApplicationLetter.statuses[:accepted]).count
        Severity: Minor
        Found in app/models/event.rb by rubocop

        Inconsistent indentation detected.
        Open

            @participants.sort { |x, y| self.compare_participants_by_agreement(x,y) }
        Severity: Minor
        Found in app/models/event.rb by rubocop

        This cops checks for inconsistent indentation.

        Example:

        class A
          def test
            puts 'hello'
             puts 'world'
          end
        end

        Do not use space inside array brackets.
        Open

          enum kind: [ :workshop, :camp ]
        Severity: Minor
        Found in app/models/event.rb by rubocop

        Checks that brackets used for array literals have or don't have surrounding space depending on configuration.

        Example: EnforcedStyle: space

        # The `space` style enforces that array literals have
        # surrounding space.
        
        # bad
        array = [a, b, c, d]
        
        # good
        array = [ a, b, c, d ]

        Example: EnforcedStyle: no_space

        # The `no_space` style enforces that array literals have
        # no surrounding space.
        
        # bad
        array = [ a, b, c, d ]
        
        # good
        array = [a, b, c, d]

        Example: EnforcedStyle: compact

        # The `compact` style normally requires a space inside
        # array brackets, with the exception that successive left
        # or right brackets are collapsed together in nested arrays.
        
        # bad
        array = [ a, [ b, c ] ]
        
        # good
        array = [ a, [ b, c ]]

        Line is too long. [81/80]
        Open

            end_date - start_date > Rails.configuration.unreasonably_long_event_time_span
        Severity: Minor
        Found in app/models/event.rb by rubocop

        Rename has_date_ranges to date_ranges?.
        Open

          def has_date_ranges
        Severity: Minor
        Found in app/models/event.rb by rubocop

        This cop makes sure that predicates are named properly.

        Example:

        # bad
        def is_even?(value)
        end
        
        # good
        def even?(value)
        end
        
        # bad
        def has_value?
        end
        
        # good
        def value?
        end

        Redundant return detected.
        Open

            return email
        Severity: Minor
        Found in app/models/event.rb by rubocop

        This cop checks for redundant return expressions.

        Example:

        def test
          return something
        end
        
        def test
          one
          two
          three
          return something
        end

        It should be extended to handle methods whose body is if/else or a case expression with a default branch.

        Extra blank line detected.
        Open

        
          # @return the minimum start_date over all date ranges
        Severity: Minor
        Found in app/models/event.rb by rubocop

        This cops checks for two or more consecutive blank lines.

        Example:

        # bad - It has two empty lines.
        some_method
        # one empty line
        # two empty lines
        some_method
        
        # good
        some_method
        # one empty line
        some_method

        Space missing after comma.
        Open

            (date_ranges.max { |a,b| a.end_date <=> b.end_date }).end_date
        Severity: Minor
        Found in app/models/event.rb by rubocop

        Checks for comma (,) not followed by some kind of space.

        Example:

        # bad
        [1,2]
        { foo:bar,}
        
        # good
        [1, 2]
        { foo:bar, }

        Line is too long. [198/80]
        Open

            errors.add(:application_deadline, I18n.t('events.errors.application_deadline_before_start_of_event')) if application_deadline.present? && !date_ranges.blank? && application_deadline > start_date
        Severity: Minor
        Found in app/models/event.rb by rubocop

        Line is too long. [93/80]
        Open

            accepted_applications.map{ |application_letter| application_letter.user.email }.join(',')
        Severity: Minor
        Found in app/models/event.rb by rubocop

        Redundant return detected.
        Open

            return participant1.name <=> participant2.name
        Severity: Minor
        Found in app/models/event.rb by rubocop

        This cop checks for redundant return expressions.

        Example:

        def test
          return something
        end
        
        def test
          one
          two
          three
          return something
        end

        It should be extended to handle methods whose body is if/else or a case expression with a default branch.

        Redundant self detected.
        Open

            @participants = self.participants
        Severity: Minor
        Found in app/models/event.rb by rubocop

        This cop checks for redundant uses of self.

        The usage of self is only needed when:

        • Sending a message to same object with zero arguments in presence of a method name clash with an argument or a local variable.

        • Calling an attribute writer to prevent an local variable assignment.

        Note, with using explicit self you can only send messages with public or protected scope, you cannot send private messages this way.

        Note we allow uses of self with operators because it would be awkward otherwise.

        Example:

        # bad
        def foo(bar)
          self.baz
        end
        
        # good
        def foo(bar)
          self.bar  # Resolves name clash with the argument.
        end
        
        def foo
          bar = 1
          self.bar  # Resolves name clash with the local variable.
        end
        
        def foo
          %w[x y z].select do |bar|
            self.bar == bar  # Resolves name clash with argument of the block.
          end
        end

        Space missing after comma.
        Open

            @participants.sort { |x, y| self.compare_participants_by_agreement(x,y) }
        Severity: Minor
        Found in app/models/event.rb by rubocop

        Checks for comma (,) not followed by some kind of space.

        Example:

        # bad
        [1,2]
        { foo:bar,}
        
        # good
        [1, 2]
        { foo:bar, }

        Tab detected.
        Open

            @participants.sort { |x, y| self.compare_participants_by_agreement(x,y) }
        Severity: Minor
        Found in app/models/event.rb by rubocop

        Line is too long. [106/80]
        Open

          # 1. All participants that have to submit an letter of agreement but did not yet do so, ordered by name.
        Severity: Minor
        Found in app/models/event.rb by rubocop

        Missing top-level class documentation comment.
        Open

        class Event < ActiveRecord::Base
        Severity: Minor
        Found in app/models/event.rb by rubocop

        This cop checks for missing top-level documentation of classes and modules. Classes with no body are exempt from the check and so are namespace modules - modules that have nothing in their bodies except classes, other modules, or constant definitions.

        The documentation requirement is annulled if the class or module has a "#:nodoc:" comment next to it. Likewise, "#:nodoc: all" does the same for all its children.

        Example:

        # bad
        class Person
          # ...
        end
        
        # good
        # Description/Explanation of Person class
        class Person
          # ...
        end

        Favor modifier if usage when having a single-line body. Another good alternative is the usage of control flow &&/||.
        Open

            if participant2.agreement_letter_for_event?(self)
        Severity: Minor
        Found in app/models/event.rb by rubocop

        Checks for if and unless statements that would fit on one line if written as a modifier if/unless. The maximum line length is configured in the Metrics/LineLength cop.

        Example:

        # bad
        if condition
          do_stuff(bar)
        end
        
        unless qux.empty?
          Foo.do_something
        end
        
        # good
        do_stuff(bar) if condition
        Foo.do_something unless qux.empty?

        Space missing after comma.
        Open

            (date_ranges.min { |a,b| a.start_date <=> b.start_date }).start_date
        Severity: Minor
        Found in app/models/event.rb by rubocop

        Checks for comma (,) not followed by some kind of space.

        Example:

        # bad
        [1,2]
        { foo:bar,}
        
        # good
        [1, 2]
        { foo:bar, }

        Do not use space inside array brackets.
        Open

          enum kind: [ :workshop, :camp ]
        Severity: Minor
        Found in app/models/event.rb by rubocop

        Checks that brackets used for array literals have or don't have surrounding space depending on configuration.

        Example: EnforcedStyle: space

        # The `space` style enforces that array literals have
        # surrounding space.
        
        # bad
        array = [a, b, c, d]
        
        # good
        array = [ a, b, c, d ]

        Example: EnforcedStyle: no_space

        # The `no_space` style enforces that array literals have
        # no surrounding space.
        
        # bad
        array = [ a, b, c, d ]
        
        # good
        array = [a, b, c, d]

        Example: EnforcedStyle: compact

        # The `compact` style normally requires a space inside
        # array brackets, with the exception that successive left
        # or right brackets are collapsed together in nested arrays.
        
        # bad
        array = [ a, [ b, c ] ]
        
        # good
        array = [ a, [ b, c ]]

        Line is too long. [91/80]
        Open

          # 3. All participants that do not have to submit an letter of agreement, ordered by name.
        Severity: Minor
        Found in app/models/event.rb by rubocop

        Line is too long. [91/80]
        Open

            errors.add(:date_ranges, I18n.t('date_range.errors.no_timespan')) if date_ranges.blank?
        Severity: Minor
        Found in app/models/event.rb by rubocop

        Redundant return detected.
        Open

            return email
        Severity: Minor
        Found in app/models/event.rb by rubocop

        This cop checks for redundant return expressions.

        Example:

        def test
          return something
        end
        
        def test
          one
          two
          three
          return something
        end

        It should be extended to handle methods whose body is if/else or a case expression with a default branch.

        Keep a blank line before and after protected.
        Open

          protected
        Severity: Minor
        Found in app/models/event.rb by rubocop

        Access modifiers should be surrounded by blank lines.

        Example:

        # bad
        class Foo
          def bar; end
          private
          def baz; end
        end
        
        # good
        class Foo
          def bar; end
        
          private
        
          def baz; end
        end

        Space missing to the left of {.
        Open

            rejected_applications.map{ |applications_letter| applications_letter.user.email }.join(',')
        Severity: Minor
        Found in app/models/event.rb by rubocop

        Checks that block braces have or don't have a space before the opening brace depending on configuration.

        Example:

        # bad
        foo.map{ |a|
          a.bar.to_s
        }
        
        # good
        foo.map { |a|
          a.bar.to_s
        }

        Line is too long. [106/80]
        Open

          # 1. All participants that have to submit an letter of agreement but did not yet do so, ordered by name.
        Severity: Minor
        Found in app/models/event.rb by rubocop

        Line is too long. [95/80]
        Open

            rejected_applications.map{ |applications_letter| applications_letter.user.email }.join(',')
        Severity: Minor
        Found in app/models/event.rb by rubocop

        Redundant self detected.
        Open

            self.agreement_letters.where(user: user).take
        Severity: Minor
        Found in app/models/event.rb by rubocop

        This cop checks for redundant uses of self.

        The usage of self is only needed when:

        • Sending a message to same object with zero arguments in presence of a method name clash with an argument or a local variable.

        • Calling an attribute writer to prevent an local variable assignment.

        Note, with using explicit self you can only send messages with public or protected scope, you cannot send private messages this way.

        Note we allow uses of self with operators because it would be awkward otherwise.

        Example:

        # bad
        def foo(bar)
          self.baz
        end
        
        # good
        def foo(bar)
          self.bar  # Resolves name clash with the argument.
        end
        
        def foo
          bar = 1
          self.bar  # Resolves name clash with the local variable.
        end
        
        def foo
          %w[x y z].select do |bar|
            self.bar == bar  # Resolves name clash with argument of the block.
          end
        end

        Space missing to the left of {.
        Open

            accepted_applications.map{ |application_letter| application_letter.user.email }.join(',')
        Severity: Minor
        Found in app/models/event.rb by rubocop

        Checks that block braces have or don't have a space before the opening brace depending on configuration.

        Example:

        # bad
        foo.map{ |a|
          a.bar.to_s
        }
        
        # good
        foo.map { |a|
          a.bar.to_s
        }

        Line is too long. [100/80]
        Open

          # @return [String] Concatenation of all email addresses of accepted applications, seperated by ','
        Severity: Minor
        Found in app/models/event.rb by rubocop

        Use %i or %I for an array of symbols.
        Open

          enum kind: [ :workshop, :camp ]
        Severity: Minor
        Found in app/models/event.rb by rubocop

        This cop can check for array literals made up of symbols that are not using the %i() syntax.

        Alternatively, it checks for symbol arrays using the %i() syntax on projects which do not want to use that syntax.

        Configuration option: MinSize If set, arrays with fewer elements than this value will not trigger the cop. For example, a MinSize of3` will not enforce a style on an array of 2 or fewer elements.

        Example: EnforcedStyle: percent (default)

        # good
        %i[foo bar baz]
        
        # bad
        [:foo, :bar, :baz]

        Example: EnforcedStyle: brackets

        # good
        [:foo, :bar, :baz]
        
        # bad
        %i[foo bar baz]

        Line is too long. [92/80]
        Open

            application_letters.all? { |application_letter| application_letter.status != 'pending' }
        Severity: Minor
        Found in app/models/event.rb by rubocop

        Line is too long. [91/80]
        Open

          # 3. All participants that do not have to submit an letter of agreement, ordered by name.
        Severity: Minor
        Found in app/models/event.rb by rubocop

        Pass &:user as an argument to collect instead of a block.
        Open

            accepted_applications.collect { |a| a.user }
        Severity: Minor
        Found in app/models/event.rb by rubocop

        Use symbols as procs when possible.

        Example:

        # bad
        something.map { |s| s.upcase }
        
        # good
        something.map(&:upcase)

        Line is too long. [100/80]
        Open

          # @return [String] Concatenation of all email addresses of rejected applications, seperated by ','
        Severity: Minor
        Found in app/models/event.rb by rubocop

        Redundant self detected.
        Open

            self.date_ranges.clear
        Severity: Minor
        Found in app/models/event.rb by rubocop

        This cop checks for redundant uses of self.

        The usage of self is only needed when:

        • Sending a message to same object with zero arguments in presence of a method name clash with an argument or a local variable.

        • Calling an attribute writer to prevent an local variable assignment.

        Note, with using explicit self you can only send messages with public or protected scope, you cannot send private messages this way.

        Note we allow uses of self with operators because it would be awkward otherwise.

        Example:

        # bad
        def foo(bar)
          self.baz
        end
        
        # good
        def foo(bar)
          self.bar  # Resolves name clash with the argument.
        end
        
        def foo
          bar = 1
          self.bar  # Resolves name clash with the local variable.
        end
        
        def foo
          %w[x y z].select do |bar|
            self.bar == bar  # Resolves name clash with argument of the block.
          end
        end

        Missing space after #.
        Open

          #validate that application deadline is before the start of the event
        Severity: Minor
        Found in app/models/event.rb by rubocop

        This cop checks whether comments have a leading space after the # denoting the start of the comment. The leading space is not required for some RDoc special syntax, like #++, #--, #:nodoc, =begin- and =end comments, "shebang" directives, or rackup options.

        Example:

        # bad
        #Some comment
        
        # good
        # Some comment

        Line is too long. [84/80]
        Open

          validates :max_participants, numericality: { only_integer: true, greater_than: 0 }
        Severity: Minor
        Found in app/models/event.rb by rubocop

        Redundant self detected.
        Open

            @participants.sort { |x, y| self.compare_participants_by_agreement(x,y) }
        Severity: Minor
        Found in app/models/event.rb by rubocop

        This cop checks for redundant uses of self.

        The usage of self is only needed when:

        • Sending a message to same object with zero arguments in presence of a method name clash with an argument or a local variable.

        • Calling an attribute writer to prevent an local variable assignment.

        Note, with using explicit self you can only send messages with public or protected scope, you cannot send private messages this way.

        Note we allow uses of self with operators because it would be awkward otherwise.

        Example:

        # bad
        def foo(bar)
          self.baz
        end
        
        # good
        def foo(bar)
          self.bar  # Resolves name clash with the argument.
        end
        
        def foo
          bar = 1
          self.bar  # Resolves name clash with the local variable.
        end
        
        def foo
          %w[x y z].select do |bar|
            self.bar == bar  # Resolves name clash with argument of the block.
          end
        end

        There are no issues that match your filters.

        Category
        Status